2 from fcntl import flock, LOCK_EX, LOCK_UN
6 'A simple namespace object.'
7 def __init__(self, d = {}, **kwargs):
8 'd is the dictionary to update my __dict__ with.'
9 self.__dict__.update(d)
10 self.__dict__.update(kwargs)
14 Given a tree of lists/dicts, perform a deep traversal to transform all the
18 return struct((k, dicts2struct(v)) for k,v in x.iteritems())
20 return [dicts2struct(v) for v in x]
25 # Hacks to work around lack of Python 2.5's `with` statement.
28 def with_closing(rsrc):
29 "Utility to emulate Python 2.5's `with closing(rsrc)` context manager."
31 try: return func(rsrc)
35 def with_lock_file(path):
37 Context manager for lock files. Example:
39 @with_lock_file('/tmp/mylock')
43 # decorator is executed immediately
44 print input # prints the input text
47 @with_closing(file(path, 'w'))
51 finally: flock(f, LOCK_UN)
60 class common_tests(unittest.TestCase):
61 def test_dicts2structs(self):
64 'dict': { 'atom': 'atom', 'list': [1,2,3] },
65 'list': [ 'atom', {'key': 'value'} ]
67 structs = dicts2struct(dicts)
68 self.assertEqual(structs.atom, dicts['atom'])
69 self.assertEqual(structs.dict.atom, dicts['dict']['atom'])
70 self.assertEqual(structs.dict.list, dicts['dict']['list'])
71 self.assertEqual(structs.list[0], dicts['list'][0])
72 self.assertEqual(structs.list[1].key, dicts['list'][1]['key'])
74 if __name__ == '__main__':