commit 1dce0b0c40dcaf6910b1c0a3aca8a90c11bb4029
parent 93c5197dbd6a941b3b095f243b7155ca8af366a4
Author: lash <dev@holbrook.no>
Date: Sun, 14 Apr 2024 14:34:17 +0100
Add ensure key presence functions
Diffstat:
4 files changed, 105 insertions(+), 11 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,5 @@
+__pycache__
+*.pyc
+dist/
+build/
+*.egg-info
diff --git a/potaahto/symbols.py b/potaahto/symbols.py
@@ -1,15 +1,11 @@
# standard imports
import re
-#import logging
-
-#logg = logging.getLogger().getChild(__name__)
-
re_camel = re.compile(r'([a-z0-9]+)([A-Z])')
re_snake = re.compile(r'([a-z0-9]+)_([A-Za-z])')
-def snake_and_camel_s(k):
- s_camel = ''
+
+def camel_to_snake(k):
s_snake = ''
right_pos = 0
for m in re_camel.finditer(k):
@@ -18,16 +14,24 @@ def snake_and_camel_s(k):
s_snake += '_' + g[len(g)-1].lower()
right_pos = m.span()[1]
s_snake += k[right_pos:]
+ return s_snake
+
+def snake_to_camel(k):
+ s_camel = ''
right_pos = 0
for m in re_snake.finditer(k):
g = m.group(0)
s_camel += g[:len(g)-2]
- #s += '_' + g[len(g)-1].lower()
s_camel += g[len(g)-1].upper()
right_pos = m.span()[1]
s_camel += k[right_pos:]
+ return s_camel
+
+def snake_and_camel_s(k):
+ s_camel = camel_to_snake(k)
+ s_snake = snake_to_camel(k)
return (s_snake, s_camel)
@@ -38,11 +42,38 @@ def snake_and_camel(src):
src_normal[k] = src[k]
#if s != k:
if k != s_snake:
- #logg.debug('adding snake {} for camel {}'.format(s_snake, k))
src_normal[s_snake] = src[k]
if k != s_camel:
- #logg.debug('adding camel {} for snake {}'.format(s_camel, k))
src_normal[s_camel] = src[k]
return src_normal
+
+
+def ensure_key(src, key, default_value):
+ (s_snake, s_camel) = snake_and_camel_s(key)
+ try:
+ v = src[s_snake]
+ except KeyError:
+ try:
+ v = src[s_camel]
+ except KeyError:
+ src[key] = default_value
+ return src
+
+
+def mimic_key(src, key_one, key_two, default_value=None):
+ v = None
+ try:
+ v = src[key_one]
+ except KeyError:
+ try:
+ v = src[key_two]
+ except KeyError:
+ if default_value != None:
+ v = default_value
+
+ if v != None:
+ src[key_one] = v
+ src[key_two] = v
+ return src
diff --git a/setup.cfg b/setup.cfg
@@ -1,6 +1,6 @@
[metadata]
name = potaahto
-version = 0.1.1
+version = 0.1.2
description = Conversions and redundancy for poor data formatting choices
author = Louis Holbrook
author_email = dev@holbrook.no
diff --git a/tests/test_basic.py b/tests/test_basic.py
@@ -1,13 +1,15 @@
# standard imports
+import copy
import unittest
# local imports
from potaahto.symbols import snake_and_camel
+from potaahto.symbols import ensure_key
+from potaahto.symbols import mimic_key
class TestFauna(unittest.TestCase):
-
def test_snake_and_camel(self):
k_stupid = {
'fooBarBaz': 1,
@@ -26,5 +28,61 @@ class TestFauna(unittest.TestCase):
self.assertEqual(k['baz_foo_bar'], 3)
+ def test_ensure(self):
+ k_stupid = {
+ 'fooBarBaz': 1,
+ 'barBazFoo': 2,
+ }
+
+ k = copy.copy(k_stupid)
+ k = ensure_key(k, 'barBazFoo', 42)
+ self.assertDictEqual(k_stupid, k)
+ self.assertEqual(k['barBazFoo'], 2)
+
+ k = copy.copy(k_stupid)
+ ensure_key(k, 'barBazFoo', 42)
+ self.assertEqual(k['barBazFoo'], 2)
+
+ k = copy.copy(k_stupid)
+ k = ensure_key(k, 'bazFooBar', 42)
+ try:
+ self.assertDictEqual(k_stupid, k)
+ except AssertionError:
+ self.assertEqual(k['bazFooBar'], 42)
+
+ k = copy.copy(k_stupid)
+ k = ensure_key(k, 'baz_foo_bar', 42)
+ self.assertEqual(k['baz_foo_bar'], 42)
+
+ k = copy.copy(k_stupid)
+ k = ensure_key(k, 'foo_bar_baz', 42)
+ self.assertEqual(k['fooBarBaz'], 1)
+
+ k = copy.copy(k_stupid)
+ ensure_key(k, 'baz_foo_bar', 42)
+ self.assertEqual(k['baz_foo_bar'], 42)
+
+
+ def test_mimic(self):
+ k_stupid = {
+ 'fooBarBaz': 1,
+ 'barBazFoo': 2,
+ }
+
+ k = copy.copy(k_stupid)
+ k = mimic_key(k, 'barBazFoo', 'xyzzy', 42)
+ self.assertEqual(k['xyzzy'], 2)
+
+ k = copy.copy(k_stupid)
+ k = mimic_key(k, 'baz_baz_baz', 'inkyPinky')
+ self.assertNotIn('baz_baz_baz', k.keys())
+ self.assertNotIn('inkyPinky', k.keys())
+
+ k = copy.copy(k_stupid)
+ k = mimic_key(k, 'baz_baz_baz', 'inkyPinky', default_value=42)
+ self.assertEqual(k['baz_baz_baz'], 42)
+ self.assertEqual(k['inkyPinky'], 42)
+
+
if __name__ == '__main__':
unittest.main()