Skip to content

Commit

Permalink
argparse_utils objects strings
Browse files Browse the repository at this point in the history
  • Loading branch information
chapmanjacobd committed Dec 13, 2024
1 parent 9d6b6ee commit a0f0f26
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions xklb/utils/argparse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from xklb.utils import nums
from xklb.utils.iterables import flatten
from xklb.utils.strings import format_two_columns
from xklb.utils.strings import format_two_columns, load_string

STDIN_DASH = ["-"]

Expand Down Expand Up @@ -56,7 +56,7 @@ def __call__(self, parser, args, values, option_string=None):
k, v = s.split("=", 1)
v_strip = v.strip()
if any(sym in v for sym in (" [", " {")):
d[k] = literal_eval(v)
d[k] = load_string(v)
elif v_strip in ("True", "False"):
d[k] = bool(v_strip)
elif v_strip.isnumeric():
Expand Down
30 changes: 30 additions & 0 deletions xklb/utils/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from contextlib import contextmanager
from functools import wraps

import difflib

class NoneSpace(types.SimpleNamespace):
def __getattr__(self, name):
Expand Down Expand Up @@ -203,6 +204,20 @@ def merge_dict_values_str(dict1, dict2):
return merged_dict


def upsert(list_of_dicts, primary_keys):
merged_dict = {}

for d in list_of_dicts:
key = tuple(d[pk] for pk in primary_keys)

if key in merged_dict:
merged_dict[key].update(d)
else:
merged_dict[key] = d.copy()

return list(merged_dict.values())


class Reverser:
def __init__(self, obj):
self.obj = obj
Expand Down Expand Up @@ -234,3 +249,18 @@ def replace_keys_in_dict(d, replacements):
for k, v in replacements.items():
d = replace_key_in_dict(d, k, v)
return d


def dict_filter_similar_key(input_dict, input_string, threshold=0.7):
similar_keys = {
key: difflib.SequenceMatcher(None, input_string.lower(), key.lower()).ratio()
for key in input_dict
}

filtered_dict = {
key: value
for key, value in input_dict.items()
if similar_keys[key] >= threshold
}

return filtered_dict
11 changes: 11 additions & 0 deletions xklb/utils/strings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ast import literal_eval
import functools, html, json, math, operator, re, sys, textwrap
from copy import deepcopy
from datetime import datetime, timedelta
Expand Down Expand Up @@ -280,6 +281,16 @@ def safe_percent(v) -> str | None:
return None


def load_string(s):
try:
return json.loads(s)
except Exception:
try:
return literal_eval(s)
except Exception:
return s


def format_two_columns(text1, text2, width1=25, width2=75, left_gutter=2, middle_gutter=2, right_gutter=3):
terminal_width = min(consts.TERMINAL_SIZE.columns, 120) - (left_gutter + middle_gutter + right_gutter)
if text2:
Expand Down

0 comments on commit a0f0f26

Please sign in to comment.