Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
_strptime.TimeRE Class Reference

List of all members.

Public Member Functions

def __init__
def pattern
def compile

Public Attributes

 locale_time

Private Member Functions

def __seqToRE

Detailed Description

Handle conversion from format directives to regexes.

Definition at line 178 of file _strptime.py.


Constructor & Destructor Documentation

def _strptime.TimeRE.__init__ (   self,
  locale_time = None 
)
Create keys/values.

Order of execution is important for dependency reasons.

Definition at line 181 of file _strptime.py.

00181 
00182     def __init__(self, locale_time=None):
00183         """Create keys/values.
00184 
00185         Order of execution is important for dependency reasons.
00186 
00187         """
00188         if locale_time:
00189             self.locale_time = locale_time
00190         else:
00191             self.locale_time = LocaleTime()
00192         base = super()
00193         base.__init__({
00194             # The " \d" part of the regex is to make %c from ANSI C work
00195             'd': r"(?P<d>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
00196             'f': r"(?P<f>[0-9]{1,6})",
00197             'H': r"(?P<H>2[0-3]|[0-1]\d|\d)",
00198             'I': r"(?P<I>1[0-2]|0[1-9]|[1-9])",
00199             'j': r"(?P<j>36[0-6]|3[0-5]\d|[1-2]\d\d|0[1-9]\d|00[1-9]|[1-9]\d|0[1-9]|[1-9])",
00200             'm': r"(?P<m>1[0-2]|0[1-9]|[1-9])",
00201             'M': r"(?P<M>[0-5]\d|\d)",
00202             'S': r"(?P<S>6[0-1]|[0-5]\d|\d)",
00203             'U': r"(?P<U>5[0-3]|[0-4]\d|\d)",
00204             'w': r"(?P<w>[0-6])",
00205             # W is set below by using 'U'
00206             'y': r"(?P<y>\d\d)",
00207             #XXX: Does 'Y' need to worry about having less or more than
00208             #     4 digits?
00209             'Y': r"(?P<Y>\d\d\d\d)",
00210             'z': r"(?P<z>[+-]\d\d[0-5]\d)",
00211             'A': self.__seqToRE(self.locale_time.f_weekday, 'A'),
00212             'a': self.__seqToRE(self.locale_time.a_weekday, 'a'),
00213             'B': self.__seqToRE(self.locale_time.f_month[1:], 'B'),
00214             'b': self.__seqToRE(self.locale_time.a_month[1:], 'b'),
00215             'p': self.__seqToRE(self.locale_time.am_pm, 'p'),
00216             'Z': self.__seqToRE((tz for tz_names in self.locale_time.timezone
00217                                         for tz in tz_names),
00218                                 'Z'),
00219             '%': '%'})
00220         base.__setitem__('W', base.__getitem__('U').replace('U', 'W'))
00221         base.__setitem__('c', self.pattern(self.locale_time.LC_date_time))
00222         base.__setitem__('x', self.pattern(self.locale_time.LC_date))
00223         base.__setitem__('X', self.pattern(self.locale_time.LC_time))

Here is the caller graph for this function:


Member Function Documentation

def _strptime.TimeRE.__seqToRE (   self,
  to_convert,
  directive 
) [private]
Convert a list to a regex string for matching a directive.

Want possible matching values to be from longest to shortest.  This
prevents the possibility of a match occuring for a value that also
a substring of a larger value that should have matched (e.g., 'abc'
matching when 'abcdef' should have been the match).

Definition at line 224 of file _strptime.py.

00224 
00225     def __seqToRE(self, to_convert, directive):
00226         """Convert a list to a regex string for matching a directive.
00227 
00228         Want possible matching values to be from longest to shortest.  This
00229         prevents the possibility of a match occuring for a value that also
00230         a substring of a larger value that should have matched (e.g., 'abc'
00231         matching when 'abcdef' should have been the match).
00232 
00233         """
00234         to_convert = sorted(to_convert, key=len, reverse=True)
00235         for value in to_convert:
00236             if value != '':
00237                 break
00238         else:
00239             return ''
00240         regex = '|'.join(re_escape(stuff) for stuff in to_convert)
00241         regex = '(?P<%s>%s' % (directive, regex)
00242         return '%s)' % regex

Here is the call graph for this function:

def _strptime.TimeRE.compile (   self,
  format 
)
Return a compiled re object for the format string.

Definition at line 266 of file _strptime.py.

00266 
00267     def compile(self, format):
00268         """Return a compiled re object for the format string."""
00269         return re_compile(self.pattern(format), IGNORECASE)

Here is the call graph for this function:

Here is the caller graph for this function:

def _strptime.TimeRE.pattern (   self,
  format 
)
Return regex pattern for the format string.

Need to make sure that any characters that might be interpreted as
regex syntax are escaped.

Definition at line 243 of file _strptime.py.

00243 
00244     def pattern(self, format):
00245         """Return regex pattern for the format string.
00246 
00247         Need to make sure that any characters that might be interpreted as
00248         regex syntax are escaped.
00249 
00250         """
00251         processed_format = ''
00252         # The sub() call escapes all characters that might be misconstrued
00253         # as regex syntax.  Cannot use re.escape since we have to deal with
00254         # format directives (%m, etc.).
00255         regex_chars = re_compile(r"([\\.^$*+?\(\){}\[\]|])")
00256         format = regex_chars.sub(r"\\\1", format)
00257         whitespace_replacement = re_compile('\s+')
00258         format = whitespace_replacement.sub('\s+', format)
00259         while '%' in format:
00260             directive_index = format.index('%')+1
00261             processed_format = "%s%s%s" % (processed_format,
00262                                            format[:directive_index-1],
00263                                            self[format[directive_index]])
00264             format = format[directive_index+1:]
00265         return "%s%s" % (processed_format, format)

Here is the caller graph for this function:


Member Data Documentation

Definition at line 188 of file _strptime.py.


The documentation for this class was generated from the following file: