本文整理汇总了Python中babel.dates.parse_pattern函数的典型用法代码示例。如果您正苦于以下问题:Python parse_pattern函数的具体用法?Python parse_pattern怎么用?Python parse_pattern使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_pattern函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parse_pattern
def test_parse_pattern():
assert dates.parse_pattern("MMMMd").format == u'%(MMMM)s%(d)s'
assert (dates.parse_pattern("MMM d, yyyy").format ==
u'%(MMM)s %(d)s, %(yyyy)s')
assert (dates.parse_pattern("H:mm' Uhr 'z").format ==
u'%(H)s:%(mm)s Uhr %(z)s')
assert dates.parse_pattern("hh' o''clock'").format == u"%(hh)s o'clock"
示例2: processFormats
def processFormats(self, locale, formats):
'''
Process the formats to a complete list of formats that will be used by conversion.
'''
assert isinstance(formats, dict), 'Invalid formats %s' % formats
assert isinstance(locale, Locale), 'Invalid locale %s' % locale
for clsTyp, format in formats.items():
# In here we just check that the format is valid.
try:
if clsTyp in (Number, Percentage): bn.parse_pattern(format)
elif format not in self.formats[clsTyp]: bd.parse_pattern(format)
except Exception as e:
raise FormatError('invalid %s format \'%s\' because: %s' % (clsTyp.__name__, format, str(e)))
if Number not in formats: formats[Number] = locale.decimal_formats.get(None).pattern
if Percentage not in formats: formats[Percentage] = locale.percent_formats.get(None).pattern
for clsTyp, default in self.defaults.items():
if clsTyp not in formats: formats[clsTyp] = default
return formats
示例3: babel2datepicker
def babel2datepicker(pattern):
"""
Converts date format from babel
(http://babel.pocoo.org/docs/dates/#date-fields)) to a format understood by
bootstrap-datepicker.
"""
if not isinstance(pattern, DateTimePattern):
pattern = parse_pattern(pattern)
map_fmt = {
# days
'd' : 'dd',
'dd' : 'dd',
'EEE' : 'D',
'EEEE' : 'DD',
'EEEEE': 'D', # narrow name => short name
# months
'M' : 'mm',
'MM' : 'mm',
'MMM' : 'M',
'MMMM': 'MM',
# years
'y' : 'yyyy',
'yy' : 'yyyy',
'yyy' : 'yyyy',
'yyyy': 'yyyy',
# time picker format
# hours
'h' : '%I',
'hh': '%I',
'H' : '%H',
'HH': '%H',
# minutes,
'm' : '%M',
'mm': '%M',
# seconds
's' : '%S',
'ss': '%S',
# am/pm
'a': '%p',
}
return pattern.format % map_fmt
示例4: parse_calendar_time_formats
def parse_calendar_time_formats(data, calendar):
time_formats = data.setdefault('time_formats', {})
for format in calendar.findall('timeFormats'):
for elem in format.getiterator():
if elem.tag == 'timeFormatLength':
type = elem.attrib.get('type')
if _should_skip_elem(elem, type, time_formats):
continue
try:
time_formats[type] = dates.parse_pattern(
text_type(elem.findtext('timeFormat/pattern'))
)
except ValueError as e:
error(e)
elif elem.tag == 'alias':
time_formats = Alias(_translate_alias(
['time_formats'], elem.attrib['path'])
)
示例5: babel2datepicker
def babel2datepicker(pattern):
"""Convert date format from babel (http://babel.pocoo.org/docs/dates/#date-
fields)) to a format understood by bootstrap-datepicker."""
if not isinstance(pattern, DateTimePattern):
pattern = parse_pattern(pattern)
map_fmt = {
# days
"d": "dd",
"dd": "dd",
"EEE": "D",
"EEEE": "DD",
"EEEEE": "D", # narrow name => short name
# months
"M": "mm",
"MM": "mm",
"MMM": "M",
"MMMM": "MM",
# years
"y": "yyyy",
"yy": "yyyy",
"yyy": "yyyy",
"yyyy": "yyyy",
# time picker format
# hours
"h": "%I",
"hh": "%I",
"H": "%H",
"HH": "%H",
# minutes,
"m": "%M",
"mm": "%M",
# seconds
"s": "%S",
"ss": "%S",
# am/pm
"a": "%p",
}
return pattern.format % map_fmt
示例6: parse_calendar_datetime_skeletons
def parse_calendar_datetime_skeletons(data, calendar):
datetime_formats = data.setdefault('datetime_formats', {})
datetime_skeletons = data.setdefault('datetime_skeletons', {})
for format in calendar.findall('dateTimeFormats'):
for elem in format.getiterator():
if elem.tag == 'dateTimeFormatLength':
type = elem.attrib.get('type')
if _should_skip_elem(elem, type, datetime_formats):
continue
try:
datetime_formats[type] = text_type(elem.findtext('dateTimeFormat/pattern'))
except ValueError as e:
error(e)
elif elem.tag == 'alias':
datetime_formats = Alias(_translate_alias(
['datetime_formats'], elem.attrib['path'])
)
elif elem.tag == 'availableFormats':
for datetime_skeleton in elem.findall('dateFormatItem'):
datetime_skeletons[datetime_skeleton.attrib['id']] = (
dates.parse_pattern(text_type(datetime_skeleton.text))
)
示例7: main
#.........这里部分代码省略.........
width_type = NAME_MAP[width.tag]
widths = eras.setdefault(width_type, {})
for elem in width.getiterator():
if elem.tag == 'era':
if ('draft' in elem.attrib or 'alt' in elem.attrib) \
and int(elem.attrib['type']) in widths:
continue
widths[int(elem.attrib.get('type'))] = text_type(elem.text)
elif elem.tag == 'alias':
eras[width_type] = Alias(
_translate_alias(['eras', width_type],
elem.attrib['path'])
)
# AM/PM
periods = data.setdefault('periods', {})
for day_period_width in calendar.findall(
'dayPeriods/dayPeriodContext/dayPeriodWidth'):
if day_period_width.attrib['type'] == 'wide':
for day_period in day_period_width.findall('dayPeriod'):
if 'alt' not in day_period.attrib:
periods[day_period.attrib['type']] = text_type(
day_period.text)
date_formats = data.setdefault('date_formats', {})
for format in calendar.findall('dateFormats'):
for elem in format.getiterator():
if elem.tag == 'dateFormatLength':
if 'draft' in elem.attrib and \
elem.attrib.get('type') in date_formats:
continue
try:
date_formats[elem.attrib.get('type')] = \
dates.parse_pattern(text_type(
elem.findtext('dateFormat/pattern')))
except ValueError as e:
error(e)
elif elem.tag == 'alias':
date_formats = Alias(_translate_alias(
['date_formats'], elem.attrib['path'])
)
time_formats = data.setdefault('time_formats', {})
for format in calendar.findall('timeFormats'):
for elem in format.getiterator():
if elem.tag == 'timeFormatLength':
if ('draft' in elem.attrib or 'alt' in elem.attrib) \
and elem.attrib.get('type') in time_formats:
continue
try:
time_formats[elem.attrib.get('type')] = \
dates.parse_pattern(text_type(
elem.findtext('timeFormat/pattern')))
except ValueError as e:
error(e)
elif elem.tag == 'alias':
time_formats = Alias(_translate_alias(
['time_formats'], elem.attrib['path'])
)
datetime_formats = data.setdefault('datetime_formats', {})
for format in calendar.findall('dateTimeFormats'):
for elem in format.getiterator():
if elem.tag == 'dateTimeFormatLength':
if ('draft' in elem.attrib or 'alt' in elem.attrib) \
and elem.attrib.get('type') in datetime_formats:
示例8: main
#.........这里部分代码省略.........
for ctxt in calendar.findall('days/dayContext'):
ctxt_type = ctxt.attrib['type']
ctxts = days.setdefault(ctxt_type, {})
for width in ctxt.findall('dayWidth'):
width_type = width.attrib['type']
widths = ctxts.setdefault(width_type, {})
for elem in width.getiterator():
if elem.tag == 'day':
dtype = weekdays[elem.attrib['type']]
if ('draft' in elem.attrib or 'alt' not in elem.attrib) \
and dtype in widths:
continue
widths[dtype] = unicode(elem.text)
elif elem.tag == 'alias':
ctxts[width_type] = Alias(
_translate_alias(['days', ctxt_type, width_type],
elem.attrib['path'])
)
quarters = data.setdefault('quarters', {})
for ctxt in calendar.findall('quarters/quarterContext'):
ctxt_type = ctxt.attrib['type']
ctxts = quarters.setdefault(ctxt.attrib['type'], {})
for width in ctxt.findall('quarterWidth'):
width_type = width.attrib['type']
widths = ctxts.setdefault(width_type, {})
for elem in width.getiterator():
if elem.tag == 'quarter':
if ('draft' in elem.attrib or 'alt' in elem.attrib) \
and int(elem.attrib['type']) in widths:
continue
widths[int(elem.attrib['type'])] = unicode(elem.text)
elif elem.tag == 'alias':
ctxts[width_type] = Alias(
_translate_alias(['quarters', ctxt_type, width_type],
elem.attrib['path'])
)
eras = data.setdefault('eras', {})
for width in calendar.findall('eras/*'):
width_type = NAME_MAP[width.tag]
widths = eras.setdefault(width_type, {})
for elem in width.getiterator():
if elem.tag == 'era':
if ('draft' in elem.attrib or 'alt' in elem.attrib) \
and int(elem.attrib['type']) in widths:
continue
widths[int(elem.attrib.get('type'))] = unicode(elem.text)
elif elem.tag == 'alias':
eras[width_type] = Alias(
_translate_alias(['eras', width_type],
elem.attrib['path'])
)
# AM/PM
periods = data.setdefault('periods', {})
for elem in calendar.findall('am'):
if ('draft' in elem.attrib or 'alt' in elem.attrib) \
and elem.tag in periods:
continue
periods[elem.tag] = unicode(elem.text)
for elem in calendar.findall('pm'):
if ('draft' in elem.attrib or 'alt' in elem.attrib) \
and elem.tag in periods:
continue
periods[elem.tag] = unicode(elem.text)
date_formats = data.setdefault('date_formats', {})
for format in calendar.findall('dateFormats'):
for elem in format.getiterator():
if elem.tag == 'dateFormatLength':
if 'draft' in elem.attrib and \
elem.attrib.get('type') in date_formats:
continue
try:
date_formats[elem.attrib.get('type')] = \
dates.parse_pattern(unicode(elem.findtext('dateFormat/pattern')))
except ValueError, e:
print>>sys.stderr, 'ERROR: %s' % e
elif elem.tag == 'alias':
date_formats = Alias(_translate_alias(
['date_formats'], elem.attrib['path'])
)
time_formats = data.setdefault('time_formats', {})
for format in calendar.findall('timeFormats'):
for elem in format.getiterator():
if elem.tag == 'timeFormatLength':
if ('draft' in elem.attrib or 'alt' in elem.attrib) \
and elem.attrib.get('type') in time_formats:
continue
try:
time_formats[elem.attrib.get('type')] = \
dates.parse_pattern(unicode(elem.findtext('timeFormat/pattern')))
except ValueError, e:
print>>sys.stderr, 'ERROR: %s' % e
elif elem.tag == 'alias':
time_formats = Alias(_translate_alias(
['time_formats'], elem.attrib['path'])
)
示例9: main
#.........这里部分代码省略.........
for width in ctxt.findall("monthWidth"):
width_type = width.attrib["type"]
widths = ctxts.setdefault(width_type, {})
for elem in width.getiterator():
if elem.tag == "month":
if ("draft" in elem.attrib or "alt" in elem.attrib) and int(elem.attrib["type"]) in widths:
continue
widths[int(elem.attrib.get("type"))] = unicode(elem.text)
elif elem.tag == "alias":
ctxts[width_type] = Alias(
_translate_alias(["months", ctxt_type, width_type], elem.attrib["path"])
)
days = data.setdefault("days", {})
for ctxt in calendar.findall("days/dayContext"):
ctxt_type = ctxt.attrib["type"]
ctxts = days.setdefault(ctxt_type, {})
for width in ctxt.findall("dayWidth"):
width_type = width.attrib["type"]
widths = ctxts.setdefault(width_type, {})
for elem in width.getiterator():
if elem.tag == "day":
dtype = weekdays[elem.attrib["type"]]
if ("draft" in elem.attrib or "alt" not in elem.attrib) and dtype in widths:
continue
widths[dtype] = unicode(elem.text)
elif elem.tag == "alias":
ctxts[width_type] = Alias(
_translate_alias(["days", ctxt_type, width_type], elem.attrib["path"])
)
quarters = data.setdefault("quarters", {})
for ctxt in calendar.findall("quarters/quarterContext"):
ctxt_type = ctxt.attrib["type"]
ctxts = quarters.setdefault(ctxt.attrib["type"], {})
for width in ctxt.findall("quarterWidth"):
width_type = width.attrib["type"]
widths = ctxts.setdefault(width_type, {})
for elem in width.getiterator():
if elem.tag == "quarter":
if ("draft" in elem.attrib or "alt" in elem.attrib) and int(elem.attrib["type"]) in widths:
continue
widths[int(elem.attrib["type"])] = unicode(elem.text)
elif elem.tag == "alias":
ctxts[width_type] = Alias(
_translate_alias(["quarters", ctxt_type, width_type], elem.attrib["path"])
)
eras = data.setdefault("eras", {})
for width in calendar.findall("eras/*"):
width_type = NAME_MAP[width.tag]
widths = eras.setdefault(width_type, {})
for elem in width.getiterator():
if elem.tag == "era":
if ("draft" in elem.attrib or "alt" in elem.attrib) and int(elem.attrib["type"]) in widths:
continue
widths[int(elem.attrib.get("type"))] = unicode(elem.text)
elif elem.tag == "alias":
eras[width_type] = Alias(_translate_alias(["eras", width_type], elem.attrib["path"]))
# AM/PM
periods = data.setdefault("periods", {})
for elem in calendar.findall("am"):
if ("draft" in elem.attrib or "alt" in elem.attrib) and elem.tag in periods:
continue
periods[elem.tag] = unicode(elem.text)
for elem in calendar.findall("pm"):
if ("draft" in elem.attrib or "alt" in elem.attrib) and elem.tag in periods:
continue
periods[elem.tag] = unicode(elem.text)
date_formats = data.setdefault("date_formats", {})
for format in calendar.findall("dateFormats"):
for elem in format.getiterator():
if elem.tag == "dateFormatLength":
if "draft" in elem.attrib and elem.attrib.get("type") in date_formats:
continue
try:
date_formats[elem.attrib.get("type")] = dates.parse_pattern(
unicode(elem.findtext("dateFormat/pattern"))
)
except ValueError, e:
print >>sys.stderr, "ERROR: %s" % e
elif elem.tag == "alias":
date_formats = Alias(_translate_alias(["date_formats"], elem.attrib["path"]))
time_formats = data.setdefault("time_formats", {})
for format in calendar.findall("timeFormats"):
for elem in format.getiterator():
if elem.tag == "timeFormatLength":
if ("draft" in elem.attrib or "alt" in elem.attrib) and elem.attrib.get("type") in time_formats:
continue
try:
time_formats[elem.attrib.get("type")] = dates.parse_pattern(
unicode(elem.findtext("timeFormat/pattern"))
)
except ValueError, e:
print >>sys.stderr, "ERROR: %s" % e
elif elem.tag == "alias":
time_formats = Alias(_translate_alias(["time_formats"], elem.attrib["path"]))
示例10: main
#.........这里部分代码省略.........
widths[int(elem.attrib["type"])] = text_type(elem.text)
elif elem.tag == "alias":
ctxts[width_type] = Alias(
_translate_alias(["quarters", ctxt_type, width_type], elem.attrib["path"])
)
eras = data.setdefault("eras", {})
for width in calendar.findall("eras/*"):
width_type = NAME_MAP[width.tag]
widths = eras.setdefault(width_type, {})
for elem in width.getiterator():
if elem.tag == "era":
if ("draft" in elem.attrib or "alt" in elem.attrib) and int(elem.attrib["type"]) in widths:
continue
widths[int(elem.attrib.get("type"))] = text_type(elem.text)
elif elem.tag == "alias":
eras[width_type] = Alias(_translate_alias(["eras", width_type], elem.attrib["path"]))
# AM/PM
periods = data.setdefault("periods", {})
for day_period_width in calendar.findall("dayPeriods/dayPeriodContext/dayPeriodWidth"):
if day_period_width.attrib["type"] == "wide":
for day_period in day_period_width.findall("dayPeriod"):
if "alt" not in day_period.attrib:
periods[day_period.attrib["type"]] = text_type(day_period.text)
date_formats = data.setdefault("date_formats", {})
for format in calendar.findall("dateFormats"):
for elem in format.getiterator():
if elem.tag == "dateFormatLength":
if "draft" in elem.attrib and elem.attrib.get("type") in date_formats:
continue
try:
date_formats[elem.attrib.get("type")] = dates.parse_pattern(
text_type(elem.findtext("dateFormat/pattern"))
)
except ValueError as e:
error(e)
elif elem.tag == "alias":
date_formats = Alias(_translate_alias(["date_formats"], elem.attrib["path"]))
time_formats = data.setdefault("time_formats", {})
for format in calendar.findall("timeFormats"):
for elem in format.getiterator():
if elem.tag == "timeFormatLength":
if ("draft" in elem.attrib or "alt" in elem.attrib) and elem.attrib.get("type") in time_formats:
continue
try:
time_formats[elem.attrib.get("type")] = dates.parse_pattern(
text_type(elem.findtext("timeFormat/pattern"))
)
except ValueError as e:
error(e)
elif elem.tag == "alias":
time_formats = Alias(_translate_alias(["time_formats"], elem.attrib["path"]))
datetime_formats = data.setdefault("datetime_formats", {})
for format in calendar.findall("dateTimeFormats"):
for elem in format.getiterator():
if elem.tag == "dateTimeFormatLength":
if ("draft" in elem.attrib or "alt" in elem.attrib) and elem.attrib.get(
"type"
) in datetime_formats:
continue
try:
datetime_formats[elem.attrib.get("type")] = text_type(