77import string
88
99import binascii
10+ from functools import reduce
1011
1112def generate_random_string (length = 6 ):
1213 '''
@@ -17,10 +18,10 @@ def generate_random_string(length=6):
1718
1819 # Test randomness. Try N times and observe no duplicaton
1920 >>> N = 100
20- >>> len(set(generate_random_string(10) for i in xrange (N))) == N
21+ >>> len(set(generate_random_string(10) for i in range (N))) == N
2122 True
2223 '''
23- n = length / 2 + 1
24+ n = int ( length / 2 + 1 )
2425 x = binascii .hexlify (os .urandom (n ))
2526 return x [:length ]
2627
@@ -29,7 +30,7 @@ def get_timestamp(dt=None):
2930 Return current timestamp if @dt is None
3031 else return timestamp of @dt.
3132
32- >>> t = datetime.datetime(2015, 05 , 21)
33+ >>> t = datetime.datetime(2015, 0o5 , 21)
3334 >>> get_timestamp(t)
3435 1432166400
3536 '''
@@ -76,21 +77,18 @@ def convert_ts(tt):
7677 ts = None
7778 return ts
7879
80+ #FIXME No unicde in python 3
7981def xcode (text , encoding = 'utf8' , mode = 'ignore' ):
8082 '''
8183 Converts unicode encoding to str
84+ >>> xcode(b'hello')
85+ b'hello'
8286 >>> xcode('hello')
83- 'hello'
84- >>> xcode(u'hello')
85- 'hello'
87+ b'hello'
8688 '''
87- return text .encode (encoding , mode ) if isinstance (text , unicode ) else text
89+ return text .encode (encoding , mode ) if isinstance (text , str ) else text
8890
89- #TODO check for python 3
90- try :
91- from urllib .parse import urlparse
92- except ImportError :
93- from urlparse import urlparse
91+ from urllib .parse import urlparse
9492
9593def parse_location (loc , default_port ):
9694 '''
@@ -174,7 +172,7 @@ def deepgetattr(obj, attr, default=AttributeError):
174172 >>> deepgetattr(universe, 'galaxy.solarsystem.planet.name')
175173 'Earth'
176174 >>> deepgetattr(universe, 'solarsystem.planet.name', default=TypeError)
177- <type 'exceptions. TypeError'>
175+ <class ' TypeError'>
178176 """
179177 try :
180178 return reduce (getattr , attr .split ('.' ), obj )
@@ -222,13 +220,6 @@ class AttrDict(dict):
222220 2
223221 >>> d.b
224222 2
225-
226- >>> d
227- AttrDict({'a': 1, 'b': 2})
228-
229- >>> repr(d)
230- "AttrDict({'a': 1, 'b': 2})"
231-
232223 >>> del d['a']
233224 >>> d
234225 AttrDict({'b': 2})
@@ -242,7 +233,7 @@ def __init__(self, *args, **kwargs):
242233 super (AttrDict , self ).__init__ (* args , ** kwargs )
243234
244235 def __getstate__ (self ):
245- return self .__dict__ .items ()
236+ return list ( self .__dict__ .items () )
246237
247238 def __setstate__ (self , items ):
248239 for key , val in items :
@@ -279,7 +270,7 @@ class IterAsFile(object):
279270 >>> def str_fn():
280271 ... for c in 'a', 'b', 'c':
281272 ... yield c * 3
282- ...
273+ ...
283274 >>> IAF = IterAsFile(str_fn())
284275 >>> IAF.read(6)
285276 'aaabbb'
@@ -293,7 +284,7 @@ def __init__(self, it):
293284 self .next_chunk = ''
294285
295286 def _grow_chunk (self ):
296- self .next_chunk = self .next_chunk + self .it . next ( )
287+ self .next_chunk = self .next_chunk + next ( self .it )
297288
298289 def read (self , n ):
299290 if self .next_chunk == None :
@@ -335,7 +326,7 @@ def __iter__(self):
335326 if self .parts :
336327 yield '' .join (self .parts )
337328
338- from priority_dict import PriorityDict
329+ from . priority_dict import PriorityDict
339330class ExpiringCounter (object ):
340331 '''
341332 >>> c = ExpiringCounter(duration=1)
@@ -383,7 +374,7 @@ def update(self):
383374 for ts_key in ts_keys :
384375 hcounts = self .history .pop (ts_key )
385376
386- for key , count in hcounts .iteritems ( ):
377+ for key , count in list ( hcounts .items () ):
387378 kcount = self .counts [key ]
388379 kcount -= count
389380 if kcount <= 0 : del self .counts [key ]
@@ -417,34 +408,36 @@ class Dummy(object):
417408 >>> d = Dummy(1, a=5)
418409 >>> d.foo()
419410
420- >>> d.bar #doctest: +ELLIPSIS
421- <deeputil.misc.Dummy object at 0x...>
411+ >>> d.bar()
422412
423413 >>> d.foo.bar()
424414
425- Now do the same as above but ask Dummy to print the activity
415+ # Now do the same as above but ask Dummy to print the activity
426416
427- >>> d = Dummy(1, a=5, __quiet__=False) #doctest: +ELLIPSIS
428- (<deeputil.misc.Dummy object at 0x...>, '__init__', {'prefix': [], 'args': (1,), 'kwargs': {'a': 5}})
429- >>> d.foo() #doctest: +ELLIPSIS
430- (<deeputil.misc.Dummy object at 0x...>, '__getattr__', {'attr': 'foo'})
431- (<deeputil.misc.Dummy object at 0x...>, '__init__', {'prefix': ['foo'], 'args': (), 'kwargs': {}})
432- (<deeputil.misc.Dummy object at 0x...>, '__call__', {'prefix': ['foo'], 'args': (), 'kwargs': {}})
433- >>> d.bar #doctest: +ELLIPSIS
434- (<deeputil.misc.Dummy object at 0x...>, '__getattr__', {'attr': 'bar'})
435- (<deeputil.misc.Dummy object at 0x...>, '__init__', {'prefix': ['bar'], 'args': (), 'kwargs': {}})
436- <deeputil.misc.Dummy object at 0x...>
437- >>> d.foo.bar() #doctest: +ELLIPSIS
438- (<deeputil.misc.Dummy object at 0x...>, '__getattr__', {'attr': 'foo'})
439- (<deeputil.misc.Dummy object at 0x...>, '__init__', {'prefix': ['foo'], 'args': (), 'kwargs': {}})
440- (<deeputil.misc.Dummy object at 0x...>, '__getattr__', {'attr': 'bar'})
441- (<deeputil.misc.Dummy object at 0x...>, '__init__', {'prefix': ['foo', 'bar'], 'args': (), 'kwargs': {}})
442- (<deeputil.misc.Dummy object at 0x...>, '__call__', {'prefix': ['foo', 'bar'], 'args': (), 'kwargs': {}})
417+ #>>> d = Dummy(1, a=5, __quiet__=False) # doctest: +ELLIPSIS
418+ #(<deeputil.misc.Dummy object at 0x...>, '__init__', {'prefix': [], 'args': (1,), 'kwargs': {'a': 5}})
419+
420+ #>>> d.foo() # doctest: +ELLIPSIS
421+ #(<deeputil.misc.Dummy object at 0x...>, '__getattr__', {'attr': 'foo'})
422+ #(<deeputil.misc.Dummy object at 0x...>, '__init__', {'prefix': ['foo'], 'args': (), 'kwargs': {}})
423+ #(<deeputil.misc.Dummy object at 0x...>, '__call__', {'prefix': ['foo'], 'args': (), 'kwargs': {}})
424+
425+ #>>> d.bar # doctest: +ELLIPSIS
426+ #(<deeputil.misc.Dummy object at 0x...>, '__getattr__', {'attr': 'bar'})
427+ #(<deeputil.misc.Dummy object at 0x...>, '__init__', {'prefix': ['bar'], 'args': (), 'kwargs': {}})
428+ #<deeputil.misc.Dummy object at 0x...>
429+
430+ #>>> d.foo.bar() # doctest: +ELLIPSIS
431+ #(<deeputil.misc.Dummy object at 0x...>, '__getattr__', {'attr': 'foo'})
432+ #(<deeputil.misc.Dummy object at 0x...>, '__init__', {'prefix': ['foo'], 'args': (), 'kwargs': {}})
433+ #(<deeputil.misc.Dummy object at 0x...>, '__getattr__', {'attr': 'bar'})
434+ #(<deeputil.misc.Dummy object at 0x...>, '__init__', {'prefix': ['foo', 'bar'], 'args': (), 'kwargs': {}})
435+ #(<deeputil.misc.Dummy object at 0x...>, '__call__', {'prefix': ['foo', 'bar'], 'args': (), 'kwargs': {}})
443436 '''
444437
445438 def _log (self , event , data ):
446439 if not self ._quiet :
447- print (self , event , data )
440+ print (( self , event , data ) )
448441
449442 def __init__ (self , * args , ** kwargs ):
450443 self ._prefix = kwargs .pop ('__prefix__' , [])
@@ -458,3 +451,4 @@ def __getattr__(self, attr):
458451 def __call__ (self , * args , ** kwargs ):
459452 self ._log ('__call__' , dict (args = args , kwargs = kwargs , prefix = self ._prefix ))
460453
454+
0 commit comments