Union of dict objects in Python [duplicate]

This question already has answers here: How do I merge two dictionaries in a single expression (take union of dictionaries)? (48 answers) Closed 9 years ago. How do you calculate the union of two dict objects in Python, where a (key, value) pair is present in the result iff key is in either dict (unless … Read more

Python: most idiomatic way to convert None to empty string?

What is the most idiomatic way to do the following? def xstr(s): if s is None: return ” else: return s s = xstr(a) + xstr(b) update: I’m incorporating Tryptich’s suggestion to use str(s), which makes this routine work for other types besides strings. I’m awfully impressed by Vinay Sajip’s lambda suggestion, but I want … Read more

How can I loop through a C++ map of maps?

How can I loop through a std::map in C++? My map is defined as: std::map< std::string, std::map<std::string, std::string> > For example, the above container holds data like this: m[“name1”][“value1”] = “data1”; m[“name1”][“value2”] = “data2”; m[“name2”][“value1”] = “data1”; m[“name2”][“value2”] = “data2”; m[“name3”][“value1”] = “data1”; m[“name3”][“value2”] = “data2”; How can I loop through this map and access … Read more

How to implement the factory method pattern in C++ correctly

There’s this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don’t know how to do it, even though it sounds simple: How do I implement Factory Method in C++ correctly? Goal: to make it possible to allow the client to instantiate some object using … Read more

How can I initialise a static Map?

How would you initialise a static Map in Java? Method one: static initialiser Method two: instance initialiser (anonymous subclass) or some other method? What are the pros and cons of each? Here is an example illustrating the two methods: import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new … Read more