Python locale.strxfrm() Examples

The following are 30 code examples of locale.strxfrm(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module locale , or try the search function .
Example #1
Source File: locale.py    From gprime with GNU General Public License v2.0 6 votes vote down vote up
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 
Example #2
Source File: clouds.py    From rednotebook with GNU General Public License v2.0 6 votes vote down vote up
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 
Example #3
Source File: util.py    From ctf-gameserver with ISC License 6 votes vote down vote up
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) 
Example #4
Source File: util.py    From ctf-gameserver with ISC License 6 votes vote down vote up
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) 
Example #5
Source File: util.py    From ctf-gameserver with ISC License 6 votes vote down vote up
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) 
Example #6
Source File: util.py    From ctf-gameserver with ISC License 6 votes vote down vote up
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) 
Example #7
Source File: util.py    From ctf-gameserver with ISC License 6 votes vote down vote up
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) 
Example #8
Source File: song.py    From QMusic with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 
Example #9
Source File: utils.py    From pasportaservo with GNU Affero General Public License v3.0 5 votes vote down vote up
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 
Example #10
Source File: journal.py    From rednotebook with GNU General Public License v2.0 5 votes vote down vote up
def categories(self):
        return sorted(
            set(itertools.chain.from_iterable(day.categories for day in self.days)),
            key=locale.strxfrm,
        ) 
Example #11
Source File: test_stdmodules.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
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)) 
Example #12
Source File: utils.py    From zim-desktop-wiki with GNU General Public License v2.0 5 votes vote down vote up
def natural_sort_key(string, numeric_padding=5):
	'''Format string such that it gives 'natural' sorting on string
	compare. Will pad any numbers in the string with "0" such that "10"
	sorts after "9". Also includes C{locale.strxfrm()}.

	@note: sorting not 100% stable for case, so order between "foo" and
	"Foo" is not defined. For this reason when sort needs to be absolutely
	stable it is advised to sort based on tuples of
	C{(sort_key, original_string)}. Or use either L{natural_sort()} or
	L{natural_sorted()} instead.

	@param string: the string to format
	@param numeric_padding: number of digits to use for padding
	@returns: string transformed to sorting key
	'''
	templ = '%0' + str(numeric_padding) + 'i'
	string.strip()
	string = _num_re.sub(lambda m: templ % int(m.group()), string)
	string = string.lower() # sort case insensitive

	try:
		bytestring = locale.strxfrm(string)
			# 8-bit byte string - enode to hex -- in pyton3 check if byte data type is handled better by sqlite3 and others
	except MemoryError:
		# Known python issue :(
		bytestring = string

	key = ''.join(["%02x" % ord(c) for c in bytestring])
	return key


####

# Python 2.7 has a weakref.WeakSet, but using this one for compatibility with 2.6 ..
# Did not switch implementations per version to make sure we test
# all modules with this implementation 
Example #13
Source File: util_tags.py    From donate-wagtail with Mozilla Public License 2.0 5 votes vote down vote up
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()) 
Example #14
Source File: settings.py    From Greynir with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #15
Source File: settings.py    From Greynir with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #16
Source File: Menu.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #17
Source File: ScriptsDialog.py    From nionswift with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #18
Source File: ScriptsDialog.py    From nionswift with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #19
Source File: settings.py    From ReynirPackage with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #20
Source File: settings.py    From ReynirPackage with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #21
Source File: models.py    From zing with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #22
Source File: treeview_factory.py    From gtg with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #23
Source File: test_locale.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_strxfrm(self):
        self.assertLess(locale.strxfrm('a'), locale.strxfrm('b')) 
Example #24
Source File: test_locale.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_strxfrm_with_diacritic(self):
        self.assertLess(locale.strxfrm('à'), locale.strxfrm('b')) 
Example #25
Source File: test_locale.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_strxfrm_with_diacritic(self):
        self.assertLess(locale.strxfrm('à'), locale.strxfrm('b')) 
Example #26
Source File: test_locale.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_strxfrm(self):
        self.assertLess(locale.strxfrm('a'), locale.strxfrm('b'))
        # embedded null character
        self.assertRaises(ValueError, locale.strxfrm, 'a\0') 
Example #27
Source File: locale.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def dumb_sort():
        return strxfrm("A") < strxfrm("a") 
Example #28
Source File: generate_website_data.py    From nototools with Apache License 2.0 5 votes vote down vote up
def sorted_langs(langs):
    return sorted(
        set(langs),
        key=lambda code: locale.strxfrm(
            get_english_language_name(code).encode("UTF-8")
        ),
    ) 
Example #29
Source File: launcher.py    From ftcommunity-TXT with GNU General Public License v3.0 5 votes vote down vote up
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"]) 
Example #30
Source File: test_stdmodules.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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))