Skip to content

Commit

Permalink
Replace custom set implementation with dictionary from standard library
Browse files Browse the repository at this point in the history
This gets rid of the deprecated reference to `collections.MutableSet`.

Starting from Python 3.7 the built-in `dict` class is also guaranteed to
preserve order, which can be used after dropping support for Python 3.6.
  • Loading branch information
pekkarr committed Dec 16, 2021
1 parent 4a1f4a2 commit 158dd23
Showing 1 changed file with 4 additions and 69 deletions.
73 changes: 4 additions & 69 deletions makehuman/lib/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
import json
from getpath import getPath, getSysDataPath, getDataPath

import collections
from collections import OrderedDict

class Language(object):
def __init__(self):
self.language = None
self.languageStrings = None
self.missingStrings = OrderedSet()
self.missingStrings = OrderedDict()
self.rtl = False

def setLanguage(self, lang):
Expand Down Expand Up @@ -95,7 +95,7 @@ def getLanguageString(self, string, appendData=None, appendFormat=None):
else:
result = result + appendData
return result
self.missingStrings.add(string)
self.missingStrings[string] = None
return string

def dumpMissingStrings(self):
Expand All @@ -106,78 +106,13 @@ def dumpMissingStrings(self):
if not os.path.isdir(pathdir):
os.makedirs(pathdir)
with open(path, 'w', encoding='utf-8') as f:
for string in self.missingStrings:
for string in self.missingStrings.keys():
if self.language == "master":
f.write('"%s": "%s",\n' % (string.replace('\n', '\\n').encode('utf8'), string.replace('\n', '\\n').encode('utf8')))
else:
f.write('"%s": "",\n' % string.replace('\n', '\\n').encode('utf8'))


class OrderedSet(collections.MutableSet):
"""
Set that maintains insertion order.
This is a python recipe, as referenced in the official documentation
(http://docs.python.org/2/library/collections.html)
Source: http://code.activestate.com/recipes/576694/
"""

def __init__(self, iterable=None):
self.end = end = []
end += [None, end, end] # sentinel node for doubly linked list
self.map = {} # key --> [key, prev, next]
if iterable is not None:
self |= iterable

def __len__(self):
return len(self.map)

def __contains__(self, key):
return key in self.map

def add(self, key):
if key not in self.map:
end = self.end
curr = end[1]
curr[2] = end[1] = self.map[key] = [key, curr, end]

def discard(self, key):
if key in self.map:
key, prev, next = self.map.pop(key)
prev[2] = next
next[1] = prev

def __iter__(self):
end = self.end
curr = end[2]
while curr is not end:
yield curr[0]
curr = curr[2]

def __reversed__(self):
end = self.end
curr = end[1]
while curr is not end:
yield curr[0]
curr = curr[1]

def pop(self, last=True):
if not self:
raise KeyError('set is empty')
key = self.end[1][0] if last else self.end[2][0]
self.discard(key)
return key

def __repr__(self):
if not self:
return '%s()' % (self.__class__.__name__,)
return '%s(%r)' % (self.__class__.__name__, list(self))

def __eq__(self, other):
if isinstance(other, OrderedSet):
return len(self) == len(other) and list(self) == list(other)
return set(self) == set(other)

def getLanguages():
"""
The languages available on this MH installation, by listing all .json
Expand Down

0 comments on commit 158dd23

Please sign in to comment.