[PATCH] tbot_contrib/utils.py: convert a string into a dictionary via a pattern

Heiko Schocher hs at denx.de
Mon Apr 20 15:51:13 CEST 2020


introduce function string_to_dict(), which converts
a string into dictionary via a pattern.

Signed-off-by: Heiko Schocher <hs at denx.de>
---

 tbot_contrib/utils.py | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/tbot_contrib/utils.py b/tbot_contrib/utils.py
index 8073f89..1afb65b 100644
--- a/tbot_contrib/utils.py
+++ b/tbot_contrib/utils.py
@@ -15,6 +15,7 @@
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 from tbot.machine import linux
+import re
 
 
 def check_systemd_services_running(lnx: linux.LinuxShell, services: list) -> None:
@@ -29,3 +30,27 @@ def check_systemd_services_running(lnx: linux.LinuxShell, services: list) -> Non
         ret = lnx.exec("systemctl", "status", s, "--no-pager")
         if ret[0] != 0:
             lnx.test("sudo", "systemctl", "start", s)
+
+
+def string_to_dict(string: str, pattern: str) -> dict:
+    """
+    convert a string into a dictionary via a pattern
+
+    example pattern:
+    'hello, my name is {name} and I am a {age} year old {what}'
+
+    string:
+    'hello, my name is dan and I am a 33 year old developer'
+
+    returned dict:
+    {'age': '33', 'name': 'dan', 'what': 'developer'}
+    from:
+    https://stackoverflow.com/questions/11844986/convert-or-unformat-a-string-to-variables-like-format-but-in-reverse-in-p
+    """
+    regex = re.sub(r"{(.+?)}", r"(?P<_\1>.+)", pattern)
+    match = re.search(regex, string)
+    assert match is not None, f"The pattern {regex!r} was not found!"
+    values = list(match.groups())
+    keys = re.findall(r"{(.+?)}", pattern)
+    _dict = dict(zip(keys, values))
+    return _dict
-- 
2.24.1



More information about the tbot mailing list