本文整理匯總了Python中locale.strxfrm方法的典型用法代碼示例。如果您正苦於以下問題:Python locale.strxfrm方法的具體用法?Python locale.strxfrm怎麽用?Python locale.strxfrm使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類locale
的用法示例。
在下文中一共展示了locale.strxfrm方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sort_key
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def sort_key(self, string):
"""
Return a value suitable to pass to the "key" parameter of sorted()
"""
if HAVE_ICU and self.collator:
#ICU can digest strings and unicode
return self.collator.getCollationKey(string).getByteArray()
else:
if isinstance(string, bytes):
string = string.decode("utf-8", "replace")
try:
key = locale.strxfrm(string)
except Exception as err:
LOG.warning("Failed to obtain key for %s because %s",
self.collation, str(err))
return string
return key
示例2: get_sortable
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def get_sortable(self, key):
'''Get sortable of the key.'''
if key in ["album", "genre", "artist", "title"]:
value = self.get("sort_%s" % key)
elif key == "date":
value = self.get("#date")
if not value: value = None
elif key == "file":
try:
value = locale.strxfrm(self.get_filename())
except Exception:
value = self.get_filename()
else:
value = self.get(key, None)
if not value and key[0] == "#": value = 0
return value
示例3: get_country_names
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def get_country_names():
"""
Returns a list of (English) country names from the OKFN/Core Datasets "List of all countries with their
2 digit codes" list, which has to be available as a file called "countries.csv" in the same directory as
this source file.
"""
csv_file_name = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'countries.csv')
with open(csv_file_name, encoding='utf8') as csv_file:
csv_reader = csv.reader(csv_file)
# Skip header line
next(csv_reader)
countries = [row[0] for row in csv_reader]
# Some teams have members in multiple countries
countries.append('International')
return sorted(countries, key=locale.strxfrm)
示例4: select_most_frequent_words
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def select_most_frequent_words(words_and_frequencies, count):
if count == 0:
return []
def get_collated_word(word_and_freq):
word, freq = word_and_freq
return locale.strxfrm(word)
def get_frequency(word_and_freq):
word, freq = word_and_freq
return freq
words_and_frequencies.sort(key=get_frequency, reverse=True)
words_and_frequencies = words_and_frequencies[:count]
words_and_frequencies.sort(key=get_collated_word)
return words_and_frequencies
示例5: test_cp34188
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def test_cp34188(self):
import locale
locale.setlocale(locale.LC_COLLATE,"de_CH")
self.assertTrue(sorted([u'a', u'z', u'�'], cmp=locale.strcoll) == sorted([u'a', u'z', u'�'], key=locale.strxfrm))
示例6: sort_by
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def sort_by(paths, iterable):
"""
Sorts by a translatable name, using system locale for a better result.
"""
locale.setlocale(locale.LC_ALL, settings.SYSTEM_LOCALE)
for path in paths:
iterable = sorted(iterable, key=lambda obj: locale.strxfrm(str(getattr_(obj, path))))
return iterable
示例7: tag_sorting
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def tag_sorting(self, t1, t2, order):
t1_sp = t1.get_attribute("special")
t2_sp = t2.get_attribute("special")
t1_name = locale.strxfrm(t1.get_name())
t2_name = locale.strxfrm(t2.get_name())
if not t1_sp and not t2_sp:
return (t1_name > t2_name) - (t1_name < t2_name)
elif not t1_sp and t2_sp:
return 1
elif t1_sp and not t2_sp:
return -1
else:
t1_order = t1.get_attribute("order")
t2_order = t2.get_attribute("order")
return (t1_order > t2_order) - (t1_order < t2_order)
示例8: test_strxfrm
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def test_strxfrm(self):
self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
示例9: test_strxfrm_with_diacritic
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def test_strxfrm_with_diacritic(self):
self.assertLess(locale.strxfrm('à'), locale.strxfrm('b'))
示例10: cached_dict
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def cached_dict(self, locale_code="en-us", show_all=False):
"""Retrieves a sorted list of live language codes and names.
By default only returns live languages for enabled projects, but it can
also return live languages for disabled projects if specified.
:param locale_code: the UI locale for which language full names need to
be localized.
:param show_all: tells whether to return all live languages (both for
disabled and enabled projects) or only live languages for enabled
projects.
:return: an `OrderedDict`
"""
key_prefix = "all_cached_dict" if show_all else "cached_dict"
key = make_method_key(self, key_prefix, locale_code)
languages = cache.get(key, None)
if languages is None:
qs = self.get_all_queryset() if show_all else self.get_queryset()
languages = OrderedDict(
sorted(
[
(locale.strxfrm(lang[0]), tr_lang(lang[1]))
for lang in qs.values_list("code", "fullname")
],
key=itemgetter(0),
)
)
cache.set(key, languages, CACHE_TIMEOUT)
return languages
示例11: key_name_sort
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def key_name_sort(value):
# folders are always "in front" of apps and the "up" folder is
# always first
if "exec" in value:
c = "E"
elif not "up_folder" in value:
c = "D"
else:
c = "C"
return c + locale.strxfrm(value["name"])
示例12: sorted_langs
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def sorted_langs(langs):
return sorted(
set(langs),
key=lambda code: locale.strxfrm(
get_english_language_name(code).encode("UTF-8")
),
)
示例13: dumb_sort
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def dumb_sort():
return strxfrm("A") < strxfrm("a")
示例14: test_strxfrm
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def test_strxfrm(self):
self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
# embedded null character
self.assertRaises(ValueError, locale.strxfrm, 'a\0')
示例15: changedlocale
# 需要導入模塊: import locale [as 別名]
# 或者: from locale import strxfrm [as 別名]
def changedlocale(new_locale=None):
""" Change locale for collation temporarily within a context (with-statement) """
# The newone locale parameter should be a tuple: ('is_IS', 'UTF-8')
old_locale = locale.getlocale(locale.LC_COLLATE)
try:
locale.setlocale(locale.LC_COLLATE, new_locale or _DEFAULT_SORT_LOCALE)
yield locale.strxfrm # Function to transform string for sorting
finally:
locale.setlocale(locale.LC_COLLATE, old_locale)