def convert(self, obj): if obj is True: return (Atom("bert"), Atom("true")) elif obj is False: return (Atom("bert"), Atom("false")) elif obj is None: return (Atom("bert"), Atom("nil")) elif isinstance(obj, unicode): return (Atom("bert"), Atom("string"), Atom(self.encoding.upper()), obj.encode(self.encoding)) elif isinstance(obj, dict): return (Atom("bert"), Atom("dict"), [(self.convert(k), self.convert(v)) for k, v in obj.items()]) elif isinstance(obj, datetime.datetime): seconds, microseconds = datetime_to_utc(obj) megaseconds = seconds // 1000000 seconds = seconds % 1000000 return (Atom("bert"), Atom("time"), megaseconds, seconds, microseconds) elif isinstance(obj, list): return [self.convert(item) for item in obj] elif isinstance(obj, tuple): return tuple(self.convert(item) for item in obj) elif type(obj) == RE_TYPE: options = [] if obj.flags & re.VERBOSE: options.append(Atom('extended')) if obj.flags & re.IGNORECASE: options.append(Atom('caseless')) if obj.flags & re.MULTILINE: options.append(Atom('multiline')) if obj.flags & re.DOTALL: options.append(Atom('dotall')) return (Atom("bert"), Atom("regex"), obj.pattern, tuple(options)) return obj
def convert(self, obj): if obj is True: return (Atom("bert"), Atom("true")) elif obj is False: return (Atom("bert"), Atom("false")) elif obj is None: return (Atom("bert"), Atom("nil")) elif isinstance(obj, unicode): return (Atom("bert"), Atom("string"), Atom(self.encoding.upper()), obj.encode(self.encoding)) elif isinstance(obj, dict): return (Atom("bert"), Atom("dict"), [ (self.convert(k), self.convert(v)) for k, v in obj.items() ]) elif isinstance(obj, datetime.datetime): seconds, microseconds = datetime_to_utc(obj) megaseconds = seconds // 1000000 seconds = seconds % 1000000 return (Atom("bert"), Atom("time"), megaseconds, seconds, microseconds) elif isinstance(obj, list): return [self.convert(item) for item in obj] elif isinstance(obj, tuple): return tuple(self.convert(item) for item in obj) elif type(obj) == RE_TYPE: options = [] if obj.flags & re.VERBOSE: options.append(Atom('extended')) if obj.flags & re.IGNORECASE: options.append(Atom('caseless')) if obj.flags & re.MULTILINE: options.append(Atom('multiline')) if obj.flags & re.DOTALL: options.append(Atom('dotall')) return (Atom("bert"), Atom("regex"), obj.pattern, tuple(options)) return obj
def testFromDatetime(self): for dt, tstamp in self.test_dates: self.failUnlessEqual(tstamp, datetime_to_utc(dt))
def testFromDatetime(self): for dt, tstamp in self.test_dates: self.assertEqual(tstamp, datetime_to_utc(dt))