Python locale.strcoll() Examples

The following are 30 code examples of locale.strcoll(). 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: ObjectListView.py    From bookhub with MIT License 6 votes vote down vote up
def _SortItemsNow(self):
        """
        Sort the actual items in the list now, according to the current column and order
        """
        sortColumn = self.GetSortColumn()
        if not sortColumn:
            return

        secondarySortColumn = None # self.GetSecondarySortColumn()

        def _singleObjectComparer(col, object1, object2):
            value1 = col.GetValue(object1)
            value2 = col.GetValue(object2)
            try:
                return locale.strcoll(value1.lower(), value2.lower())
            except:
                return cmp(value1, value2)

        def _objectComparer(object1, object2):
            result = _singleObjectComparer(sortColumn, object1, object2)
            if secondarySortColumn and result == 0:
                result = _singleObjectComparer(secondarySortColumn, object1, object2)
            return result

        self.SortListItemsBy(_objectComparer) 
Example #2
Source File: listmixin.py    From PandasDataFrameGUI with MIT License 6 votes vote down vote up
def __ColumnSorter(self, key1, key2):
        col = self._col
        ascending = self._colSortFlag[col]
        item1 = self.itemDataMap[key1][col]
        item2 = self.itemDataMap[key2][col]

        #--- Internationalization of string sorting with locale module
        if type(item1) == unicode and type(item2) == unicode:
            cmpVal = locale.strcoll(item1, item2)
        elif type(item1) == str or type(item2) == str:
            cmpVal = locale.strcoll(str(item1), str(item2))
        else:
            cmpVal = cmp(item1, item2)
        #---

        # If the items are equal then pick something else to make the sort value unique
        if cmpVal == 0:
            cmpVal = apply(cmp, self.GetSecondarySortValues(col, key1, key2))

        if ascending:
            return cmpVal
        else:
            return -cmpVal 
Example #3
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)) 
Example #4
Source File: test_locale.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_strcoll_3303(self):
            # test crasher from bug #3303
            self.assertRaises(TypeError, locale.strcoll, u"a", None) 
Example #5
Source File: Menu.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def __cmp__(self, other):
        return locale.strcoll(self.DesktopEntry.getName(), other.DesktopEntry.getName()) 
Example #6
Source File: Menu.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def __cmp__(self, other):
        return locale.strcoll(self.getName(), other.getName()) 
Example #7
Source File: command_base.py    From servoshell with Mozilla Public License 2.0 5 votes vote down vote up
def archive_deterministically(dir_to_archive, dest_archive, prepend_path=None):
    """Create a .tar.gz archive in a deterministic (reproducible) manner.

    See https://reproducible-builds.org/docs/archives/ for more details."""

    def reset(tarinfo):
        """Helper to reset owner/group and modification time for tar entries"""
        tarinfo.uid = tarinfo.gid = 0
        tarinfo.uname = tarinfo.gname = "root"
        tarinfo.mtime = 0
        return tarinfo

    dest_archive = os.path.abspath(dest_archive)
    with cd(dir_to_archive):
        current_dir = "."
        file_list = [current_dir]
        for root, dirs, files in os.walk(current_dir):
            for name in itertools.chain(dirs, files):
                file_list.append(os.path.join(root, name))

        # Sort file entries with the fixed locale
        with setlocale('C'):
            file_list.sort(cmp=locale.strcoll)

        # Use a temporary file and atomic rename to avoid partially-formed
        # packaging (in case of exceptional situations like running out of disk space).
        # TODO do this in a temporary folder after #11983 is fixed
        temp_file = '{}.temp~'.format(dest_archive)
        with os.fdopen(os.open(temp_file, os.O_WRONLY | os.O_CREAT, 0644), 'w') as out_file:
            with gzip.GzipFile('wb', fileobj=out_file, mtime=0) as gzip_file:
                with tarfile.open(fileobj=gzip_file, mode='w:') as tar_file:
                    for entry in file_list:
                        arcname = entry
                        if prepend_path is not None:
                            arcname = os.path.normpath(os.path.join(prepend_path, arcname))
                        tar_file.add(entry, filter=reset, recursive=False, arcname=arcname)
        os.rename(temp_file, dest_archive) 
Example #8
Source File: web.py    From psdash with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def view_logs():
    available_logs = current_service.get_logs()
    available_logs.sort(cmp=lambda x1, x2: locale.strcoll(x1['path'], x2['path']))

    return render_template(
        'logs.html',
        page='logs',
        logs=available_logs,
        is_xhr=request.is_xhr
    ) 
Example #9
Source File: test_locale.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_strcoll_3303(self):
            # test crasher from bug #3303
            self.assertRaises(TypeError, locale.strcoll, u"a", None) 
Example #10
Source File: ListCtrl.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def __ColumnSorter(self, itemData1, itemData2):
        """Allows custom compare functions, in self.colcmps."""
        col = self._col
        ascending = self._colSortFlag[col]

        if col < len(self.enabled_columns):
            name = self.enabled_columns[col]
        else:
            name = self.column_order[0]

        itemData1 = self.TranslateItemData(itemData1)
        itemData2 = self.TranslateItemData(itemData2)
        item1 = self.itemData_to_row[itemData1][name]
        item2 = self.itemData_to_row[itemData2][name]

        column = self.columns[name]

        if column.comparator != None:
            # use custom cmp method
            cmpVal = column.comparator(item1, item2)
        elif isinstance(item1, str) or isinstance(item2, str):
            # Internationalization of string sorting with locale module
            cmpVal = locale.strcoll(unicode(item1), unicode(item2))
        else:
            cmpVal = cmp(item1, item2)

        # If the items are equal then pick something else to make the sort value unique
        if cmpVal == 0:
            cmpVal = apply(cmp, self.GetSecondarySortValues(col, itemData1, itemData2))

        if ascending:
            return cmpVal
        else:
            return -cmpVal 
Example #11
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_strcoll_3303(self):
        # test crasher from bug #3303
        self.assertRaises(TypeError, locale.strcoll, "a", None)
        self.assertRaises(TypeError, locale.strcoll, b"a", None) 
Example #12
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_strcoll_with_diacritic(self):
        self.assertLess(locale.strcoll('à', 'b'), 0) 
Example #13
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_strcoll(self):
        self.assertLess(locale.strcoll('a', 'b'), 0)
        self.assertEqual(locale.strcoll('a', 'a'), 0)
        self.assertGreater(locale.strcoll('b', 'a'), 0)
        # embedded null character
        self.assertRaises(ValueError, locale.strcoll, 'a\0', 'a')
        self.assertRaises(ValueError, locale.strcoll, 'a', 'a\0') 
Example #14
Source File: test_locale.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_strcoll_3303(self):
            # test crasher from bug #3303
            self.assertRaises(TypeError, locale.strcoll, u"a", None) 
Example #15
Source File: locale.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def custom_strcoll(a, b, last=sentinel):
            """strcoll that can handle a sentinel that is always last."""
            if a is last:
                return 0 if a is b else 1
            elif b is last:  # a cannot also be sentinel b/c above logic
                return -1
            else:  # neither are sentinel
                return strcoll(a, b) 
Example #16
Source File: generate_sample_from_exemplar.py    From nototools with Apache License 2.0 5 votes vote down vote up
def sort_for_script(cp_list, script):
    lang = lang_for_script(script)
    if not lang:
        print("cannot sort for script, no lang for %s" % script)
        return cp_list
    if _HAVE_ICU:
        from icu import Locale, Collator

        loc = Locale(lang + "_" + script)
        col = Collator.createInstance(loc)
        return sorted(cp_list, cmp=col.compare)
    else:
        import locale

        return sorted(cp_list, cmp=locale.strcoll) 
Example #17
Source File: test_locale.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_strcoll_3303(self):
            # test crasher from bug #3303
            self.assertRaises(TypeError, locale.strcoll, u"a", None) 
Example #18
Source File: test_locale.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_strcoll_3303(self):
        # test crasher from bug #3303
        self.assertRaises(TypeError, locale.strcoll, "a", None)
        self.assertRaises(TypeError, locale.strcoll, b"a", None) 
Example #19
Source File: test_locale.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_strcoll_with_diacritic(self):
        self.assertLess(locale.strcoll('à', 'b'), 0) 
Example #20
Source File: test_locale.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_strcoll(self):
        self.assertLess(locale.strcoll('a', 'b'), 0)
        self.assertEqual(locale.strcoll('a', 'a'), 0)
        self.assertGreater(locale.strcoll('b', 'a'), 0) 
Example #21
Source File: test_locale.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_strcoll_3303(self):
        # test crasher from bug #3303
        self.assertRaises(TypeError, locale.strcoll, "a", None)
        self.assertRaises(TypeError, locale.strcoll, b"a", None) 
Example #22
Source File: test_locale.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_strcoll_with_diacritic(self):
        self.assertLess(locale.strcoll('à', 'b'), 0) 
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_strcoll(self):
        self.assertLess(locale.strcoll('a', 'b'), 0)
        self.assertEqual(locale.strcoll('a', 'a'), 0)
        self.assertGreater(locale.strcoll('b', 'a'), 0) 
Example #24
Source File: test_locale.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_strcoll_3303(self):
            # test crasher from bug #3303
            self.assertRaises(TypeError, locale.strcoll, u"a", None) 
Example #25
Source File: geoprocessor.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def _ParseUSPostal(self, datafile):
    """Loads and parses the provided datafile into an array of placename
    information.
    """
    self._us_postal = []
    with open(datafile, 'r') as f:
      for line in f.readlines():
        fields = line.split('\t')
        datum = GeoDatum(None, self._CleanName(fields[2].decode('utf-8')),
                         float(fields[9]), float(fields[10]), fields[0], fields[4], fields[5], None)
        self._us_postal.append(datum)
    self._us_postal = sorted(self._us_postal, key=attrgetter('name'), cmp=locale.strcoll)
    logging.info('parsed %d places from US postal database' % len(self._us_postal)) 
Example #26
Source File: geoprocessor.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def FilterTopCities(self):
    """Applies the boost to US cities, sorts by population and outputs the
    list of files to output directory.
    """
    filtered_cities = []
    for city in self._data:
      filtered_cities.append(city)
    cities_sorted = sorted(filtered_cities, key=attrgetter('name'), cmp=locale.strcoll)
    with codecs.open(os.path.join(options.options.output_dir, 'top_cities.txt'), 'w', 'utf-8') as f:
      for c in cities_sorted:
        f.write('%s,%s,%s,%f,%f\n' % (c.name, c.state or '', c.cc, c.lat, c.lon)) 
Example #27
Source File: geoprocessor.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def _ParseUSPostal(self, datafile):
    """Loads and parses the provided datafile into an array of placename
    information.
    """
    self._us_postal = []
    with open(datafile, 'r') as f:
      for line in f.readlines():
        fields = line.split('\t')
        datum = GeoDatum(None, self._CleanName(fields[2].decode('utf-8')),
                         float(fields[9]), float(fields[10]), fields[0], fields[4], fields[5], None)
        self._us_postal.append(datum)
    self._us_postal = sorted(self._us_postal, key=attrgetter('name'), cmp=locale.strcoll)
    logging.info('parsed %d places from US postal database' % len(self._us_postal)) 
Example #28
Source File: geoprocessor.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def FilterTopCities(self):
    """Applies the boost to US cities, sorts by population and outputs the
    list of files to output directory.
    """
    filtered_cities = []
    for city in self._data:
      filtered_cities.append(city)
    cities_sorted = sorted(filtered_cities, key=attrgetter('name'), cmp=locale.strcoll)
    with codecs.open(os.path.join(options.options.output_dir, 'top_cities.txt'), 'w', 'utf-8') as f:
      for c in cities_sorted:
        f.write('%s,%s,%s,%f,%f\n' % (c.name, c.state or '', c.cc, c.lat, c.lon)) 
Example #29
Source File: test_locale.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_strcoll_3303(self):
            # test crasher from bug #3303
            self.assertRaises(TypeError, locale.strcoll, u"a", None) 
Example #30
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))