Python源码示例:locale.strxfrm()

示例1
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
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
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
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) 
示例5
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) 
示例6
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) 
示例7
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) 
示例8
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 
示例9
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)) 
示例10
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 
示例11
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) 
示例12
def test_strxfrm(self):
        self.assertLess(locale.strxfrm('a'), locale.strxfrm('b')) 
示例13
def test_strxfrm_with_diacritic(self):
        self.assertLess(locale.strxfrm('à'), locale.strxfrm('b')) 
示例14
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 
示例15
def test_strxfrm(self):
        self.assertLess(locale.strxfrm('a'), locale.strxfrm('b')) 
示例16
def test_strxfrm_with_diacritic(self):
        self.assertLess(locale.strxfrm('à'), locale.strxfrm('b')) 
示例17
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)) 
示例18
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"]) 
示例19
def sorted_langs(langs):
    return sorted(
        set(langs),
        key=lambda code: locale.strxfrm(
            get_english_language_name(code).encode("UTF-8")
        ),
    ) 
示例20
def dumb_sort():
        return strxfrm("A") < strxfrm("a") 
示例21
def test_strxfrm(self):
        self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
        # embedded null character
        self.assertRaises(ValueError, locale.strxfrm, 'a\0') 
示例22
def test_strxfrm_with_diacritic(self):
        self.assertLess(locale.strxfrm('à'), locale.strxfrm('b')) 
示例23
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) 
示例24
def sort_strings(strings, loc=None):
    """ Sort a list of strings using the specified locale's collation order """
    # Change locale temporarily for the sort
    with changedlocale(loc) as strxfrm:
        return sorted(strings, key=strxfrm) 
示例25
def __lt__(self, other) -> bool:
        if isinstance(other, FolderListItem):
            return False
        if isinstance(other, ScriptListItem):
            return locale.strxfrm(self.basename) < locale.strxfrm(other.basename)
        return NotImplemented 
示例26
def __lt__(self, other) -> bool:
        if isinstance(other, FolderListItem):
            return locale.strxfrm(self.basename) < locale.strxfrm(other.basename)
        if isinstance(other, ScriptListItem):
            return True
        return NotImplemented 
示例27
def _strxfrm(s):
    """Wrapper around locale.strxfrm that accepts unicode strings on Python 2.
    
    See Python bug #2481.
    """
    if (not PY3) and isinstance(s, unicode):
        s = s.encode('utf-8')
    return locale.strxfrm(s) 
示例28
def changedlocale(new_locale=None, category="LC_COLLATE"):
    """ Change locale temporarily within a context (with-statement) """
    # The new locale parameter should be a tuple, e.g. ('is_IS', 'UTF-8')
    # The category should be a string such as 'LC_TIME', 'LC_NUMERIC' etc.
    cat = getattr(locale, category)
    old_locale = locale.getlocale(cat)
    try:
        locale.setlocale(cat, new_locale or _DEFAULT_LOCALE)
        yield locale.strxfrm  # Function to transform string for sorting
    finally:
        locale.setlocale(cat, old_locale) 
示例29
def sort_strings(strings, loc=None):
    """ Sort a list of strings using the specified locale's collation order """
    # Change locale temporarily for the sort
    with changedlocale(loc) as strxfrm:
        return sorted(strings, key=strxfrm) 
示例30
def get_local_language_names():
    locale.setlocale(locale.LC_ALL, "C.UTF-8")
    languages = []
    for lang in settings.LANGUAGES:
        languages.append([lang[0], get_language_info(lang[0])['name_local']])
    return sorted(languages, key=lambda x: locale.strxfrm(unicodedata.normalize('NFD', x[1])).casefold())