The code I wrote last night to build a Map of Maps shows one reason why I like Python so much - it is so easy to work with collections!
I wrote a sample app that shows the same thing in Java and Python. The requirement is to take a list of triples of strings in the form
[ language code, item id, localized string ]
and build a two level Map from language code => item id => data triple. Both examples include a simple test driver which prints:
This is a test
C'est un essai
no data
Another test
no data
Here is the Java version abstracted from the code I wrote last night. It is 56 lines and 1797 characters. The functional part of the code (excluding main()) is 38 lines.
import java.util.*;
public class Test {
private Map _map = new HashMap();
public Test(String[][] data) {
// Convert the input data to a two-level Map from language code => course ID => locale data
for (int i = 0; i < data.length; i++) {
String[] itemData = data[i];
String lang = itemData[0];
Map langMap = (Map)_map.get(lang);
if (langMap == null) {
langMap = new HashMap();
_map.put(lang, langMap);
}
String id = itemData[1];
langMap.put(id, itemData);
}
}
public String lookup(String lang, String id, String defaultData) {
Map langMap = (Map)_map.get(lang);
if (langMap == null) return defaultData;
String[] itemData = (String[])langMap.get(id);
if (itemData == null) return defaultData;
String title = itemData[2];
if (title == null || title.length() == 0)
return defaultData;
return title;
}
public static void main(String[] args) {
String[][] data = {
{ "en", "123", "This is a test" },
{ "fr", "123", "C'est un essai" },
{ "es", "123", "" },
{ "en", "345", "Another test" }
};
Test test = new Test(data);
System.out.println(test.lookup("en", "123", "no data"));
System.out.println(test.lookup("fr", "123", "no data"));
System.out.println(test.lookup("es", "123", "no data"));
System.out.println(test.lookup("en", "345", "no data"));
System.out.println(test.lookup("fr", "345", "no data"));
}
}
And here is the Python version. It is 34 lines and 1036 characters. The functional part of the code (excluding main) is 17 lines. That is roughly 40% shorter than the Java version.
class Test:
def __init__(self, data):
# Convert the input data to a two-level Map from language code => course ID => locale data
self._map = {}
for itemData in data:
lang, id = itemData[:2]
self._map.setdefault(lang, {})[id] = itemData
def lookup(self, lang, id, defaultData):
itemData = self._map.get(lang, {}).get(id)
if not itemData:
return defaultData
return itemData[2] or defaultData
if __name__ == '__main__':
data = [
[ "en", "123", "This is a test" ],
[ "fr", "123", "C'est un essai" ],
[ "es", "123", "" ],
[ "en", "345", "Another test" ]
]
test = Test(data);
print test.lookup("en", "123", "no data")
print test.lookup("fr", "123", "no data")
print test.lookup("es", "123", "no data")
print test.lookup("en", "345", "no data")
print test.lookup("fr", "345", "no data")
I know which version I prefer!
Sigh. I chickened out.
I work in a predominantly Java shop. The project I am working on could grow to a GUI app or a web app or both. I'm familiar with Java Swing, Jetty web server and servlets. I know that there is a great variety of mature tools available for writing Java web apps. I have support available at work if I need help.
On the Python side, I would have to learn wxPython and/or one of the Python web app frameworks like WebWork or Quixote. I don't get such warm fuzzy feelings about the completeness of these frameworks, both in features and release quality. I would be working on my own and out on a limb if I had any problems.
In the end, I decided it was too great a risk so I went with the safer solution.
Sigh.
|