utils.py
branchtls-sprint
changeset 1024 bb96289257bf
parent 1016 26387b836099
child 1132 96752791c2b6
equal deleted inserted replaced
1016:26387b836099 1024:bb96289257bf
     6 """
     6 """
     7 __docformat__ = "restructuredtext en"
     7 __docformat__ = "restructuredtext en"
     8 
     8 
     9 import locale
     9 import locale
    10 from md5 import md5
    10 from md5 import md5
    11 from datetime import datetime, timedelta
    11 from datetime import datetime, timedelta, date
    12 from time import time
    12 from time import time
    13 from random import randint, seed
    13 from random import randint, seed
       
    14     
       
    15 # initialize random seed from current time
       
    16 seed()
    14 
    17 
    15 try:
    18 try:
    16     strptime = datetime.strptime
    19     strptime = datetime.strptime
    17 except AttributeError: # py < 2.5
    20 except AttributeError: # py < 2.5
    18     from time import strptime as time_strptime
    21     from time import strptime as time_strptime
    19     def strptime(value, format):
    22     def strptime(value, format):
    20         return datetime(*time_strptime(value, format)[:6])
    23         return datetime(*time_strptime(value, format)[:6])
    21     
    24 
    22 # initialize random seed from current time
    25 def todate(somedate):
    23 seed()
    26     """return a date from a date (leaving unchanged) or a datetime"""
    24 
    27     if isinstance(somedate, datetime):
    25 def make_uid(key):
    28         return date(somedate.year, somedate.month, somedate.day)
    26     """forge a unique identifier"""
    29     assert isinstance(somedate, date)
    27     msg = str(key) + "%.10f"%time() + str(randint(0, 1000000))
    30     return date
    28     return md5(msg).hexdigest()
    31 
    29 
       
    30 def working_hours(mxdate):
       
    31     """
       
    32     Predicate returning True is the date's hour is in working hours (8h->20h)
       
    33     """
       
    34     if mxdate.hour > 7 and mxdate.hour < 21:
       
    35         return True
       
    36     return False
       
    37     
       
    38 def date_range(begin, end, incr=1, include=None):
    32 def date_range(begin, end, incr=1, include=None):
    39     """yields each date between begin and end
    33     """yields each date between begin and end
    40     :param begin: the start date
    34     :param begin: the start date
    41     :param end: the end date
    35     :param end: the end date
    42     :param incr: the step to use to iterate over dates. Default is
    36     :param incr: the step to use to iterate over dates. Default is
    59     encoding is guessed by locale.getpreferredencoding()
    53     encoding is guessed by locale.getpreferredencoding()
    60     """
    54     """
    61     # date format may depend on the locale
    55     # date format may depend on the locale
    62     encoding = locale.getpreferredencoding(do_setlocale=False) or 'UTF-8'
    56     encoding = locale.getpreferredencoding(do_setlocale=False) or 'UTF-8'
    63     return unicode(date.strftime(fmt), encoding)
    57     return unicode(date.strftime(fmt), encoding)
       
    58 
       
    59 def make_uid(key):
       
    60     """forge a unique identifier"""
       
    61     msg = str(key) + "%.10f"%time() + str(randint(0, 1000000))
       
    62     return md5(msg).hexdigest()
    64 
    63 
    65 
    64 
    66 def dump_class(cls, clsname):
    65 def dump_class(cls, clsname):
    67     """create copy of a class by creating an empty class inheriting
    66     """create copy of a class by creating an empty class inheriting
    68     from the given cls.
    67     from the given cls.