Skip to content

Commit afcee10

Browse files
committed
optimize
1 parent d119157 commit afcee10

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

pymt5adapter/calendar.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,21 @@ def _split_pairs(p: Iterable[str]):
142142

143143

144144
def _camel_to_snake(w):
145-
if (c := _camel_to_snake.pattern.findall(w)):
146-
return '_'.join(map(str.lower, c))
147-
return w
148-
149-
150-
_camel_to_snake.pattern = re.compile(r'[A-Z][a-z]*')
145+
try:
146+
cache = _camel_to_snake.cache
147+
if w in cache:
148+
return cache[w]
149+
p = _camel_to_snake.pattern
150+
except AttributeError:
151+
p = _camel_to_snake.pattern = re.compile(r'[A-Z][a-z0-9]*')
152+
cache = _camel_to_snake.cache = {}
153+
if (words := p.findall(w)):
154+
res = '_'.join(map(str.lower, words))
155+
cache[w] = res
156+
return res
157+
else:
158+
cache[w] = w
159+
return w
151160

152161

153162
@functools.lru_cache(maxsize=128, typed=True)

0 commit comments

Comments
 (0)