Python源码示例:locale.strcoll()
示例1
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
示例2
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)
示例3
def test_strcoll_3303(self):
# test crasher from bug #3303
self.assertRaises(TypeError, locale.strcoll, u"a", None)
示例4
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))
示例5
def test_strcoll_3303(self):
# test crasher from bug #3303
self.assertRaises(TypeError, locale.strcoll, u"a", None)
示例6
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))
示例7
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))
示例8
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))
示例9
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))
示例10
def test_strcoll_3303(self):
# test crasher from bug #3303
self.assertRaises(TypeError, locale.strcoll, u"a", None)
示例11
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)
示例12
def test_strcoll_with_diacritic(self):
self.assertLess(locale.strcoll('à', 'b'), 0)
示例13
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)
示例14
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)
示例15
def test_strcoll_with_diacritic(self):
self.assertLess(locale.strcoll('à', 'b'), 0)
示例16
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)
示例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 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)
示例19
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)
示例20
def test_strcoll_3303(self):
# test crasher from bug #3303
self.assertRaises(TypeError, locale.strcoll, u"a", None)
示例21
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')
示例22
def test_strcoll_with_diacritic(self):
self.assertLess(locale.strcoll('à', 'b'), 0)
示例23
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)
示例24
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
示例25
def test_strcoll_3303(self):
# test crasher from bug #3303
self.assertRaises(TypeError, locale.strcoll, u"a", None)
示例26
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
)
示例27
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)
示例28
def __cmp__(self, other):
return locale.strcoll(self.getName(), other.getName())
示例29
def __cmp__(self, other):
return locale.strcoll(self.DesktopEntry.getName(), other.DesktopEntry.getName())
示例30
def test_strcoll_3303(self):
# test crasher from bug #3303
self.assertRaises(TypeError, locale.strcoll, u"a", None)