summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2022-10-25 19:06:19 -0500
committerGitHub <noreply@github.com>2022-10-25 19:06:19 -0500
commit2e3e25514004f11d2238793752536fbc43f4bbf4 (patch)
tree0b0b6237039add86523d801be6a0c191705954f6 /python
parentc0594071c6c1062cc0069377b6666de89a172d0a (diff)
parent29b656f14d8bb7622ff861208d26cf8eb018670d (diff)
downloadvyos-1x-2e3e25514004f11d2238793752536fbc43f4bbf4.tar.gz
vyos-1x-2e3e25514004f11d2238793752536fbc43f4bbf4.zip
Merge pull request #1617 from jestabro/camel_to_snake_case
vyos.util: T4773: add camel_to_snake_case conversion
Diffstat (limited to 'python')
-rw-r--r--python/vyos/util.py7
1 files changed, 7 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 461df9a6e..e4e2a44ec 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -1105,3 +1105,10 @@ def sysctl_write(name, value):
call(f'sysctl -wq {name}={value}')
return True
return False
+
+# approach follows a discussion in:
+# https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case
+def camel_to_snake_case(name: str) -> str:
+ pattern = r'\d+|[A-Z]?[a-z]+|\W|[A-Z]{2,}(?=[A-Z][a-z]|\d|\W|$)'
+ words = re.findall(pattern, name)
+ return '_'.join(map(str.lower, words))