Skip to content

Commit f098bf1

Browse files
fujicoinmatejcik
authored andcommitted
Eliminate unnecessary processing of language detection
1 parent e63ced2 commit f098bf1

File tree

1 file changed

+3
-23
lines changed

1 file changed

+3
-23
lines changed

mnemonic/mnemonic.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def b58encode(v: bytes) -> str:
6666

6767
class Mnemonic(object):
6868
def __init__(self, language: str):
69+
self.language = language
6970
self.radix = 2048
7071
with open(
7172
"%s/%s.txt" % (self._get_directory(), language), "r", encoding="utf-8"
@@ -81,14 +82,6 @@ def __init__(self, language: str):
8182
def _get_directory() -> str:
8283
return os.path.join(os.path.dirname(__file__), "wordlist")
8384

84-
@classmethod
85-
def list_languages(cls) -> List[str]:
86-
return [
87-
f.split(".")[0]
88-
for f in os.listdir(cls._get_directory())
89-
if f.endswith(".txt")
90-
]
91-
9285
@staticmethod
9386
def normalize_string(txt: AnyStr) -> str:
9487
if isinstance(txt, bytes):
@@ -100,19 +93,6 @@ def normalize_string(txt: AnyStr) -> str:
10093

10194
return unicodedata.normalize("NFKD", utxt)
10295

103-
@classmethod
104-
def detect_language(cls, code: str) -> str:
105-
code = cls.normalize_string(code)
106-
first = code.split(" ")[0]
107-
languages = cls.list_languages()
108-
109-
for lang in languages:
110-
mnemo = cls(lang)
111-
if first in mnemo.wordlist:
112-
return lang
113-
114-
raise ConfigurationError("Language not detected")
115-
11696
def generate(self, strength: int = 128) -> str:
11797
if strength not in [128, 160, 192, 224, 256]:
11898
raise ValueError(
@@ -135,7 +115,7 @@ def to_entropy(self, words: Union[List[str], str]) -> bytearray:
135115
concatLenBits = len(words) * 11
136116
concatBits = [False] * concatLenBits
137117
wordindex = 0
138-
if self.detect_language(" ".join(words)) == "english":
118+
if self.language == "english":
139119
use_binary_search = True
140120
else:
141121
use_binary_search = False
@@ -189,7 +169,7 @@ def to_mnemonic(self, data: bytes) -> str:
189169
idx = int(b[i * 11 : (i + 1) * 11], 2)
190170
result.append(self.wordlist[idx])
191171
if (
192-
self.detect_language(" ".join(result)) == "japanese"
172+
self.language == "japanese"
193173
): # Japanese must be joined by ideographic space.
194174
result_phrase = u"\u3000".join(result)
195175
else:

0 commit comments

Comments
 (0)