Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
nicker-bocker committed Apr 10, 2020
1 parent d119157 commit afcee10
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pymt5adapter/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,21 @@ def _split_pairs(p: Iterable[str]):


def _camel_to_snake(w):
if (c := _camel_to_snake.pattern.findall(w)):
return '_'.join(map(str.lower, c))
return w


_camel_to_snake.pattern = re.compile(r'[A-Z][a-z]*')
try:
cache = _camel_to_snake.cache
if w in cache:
return cache[w]
p = _camel_to_snake.pattern
except AttributeError:
p = _camel_to_snake.pattern = re.compile(r'[A-Z][a-z0-9]*')
cache = _camel_to_snake.cache = {}
if (words := p.findall(w)):
res = '_'.join(map(str.lower, words))
cache[w] = res
return res
else:
cache[w] = w
return w


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

0 comments on commit afcee10

Please sign in to comment.