Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_keys_of_no_dict_values(*dicts):
- keys = []
- for d in dicts:
- keys.extend([k
- for k
- in d.keys()
- if ((k not in keys)
- and (not isinstance(d[k], dict)))]
- )
- return keys
- def get_keys_of_dict_values(*dicts):
- keys = []
- for d in dicts:
- keys.extend([k
- for k
- in d.keys()
- if ((k not in keys) and isinstance(d[k], dict))])
- return keys
- def get_split_indices(text):
- indices = []
- import string
- is_lower = lambda c: c in string.ascii_lowercase
- is_upper = lambda c: c in string.ascii_uppercase
- last_lower = is_lower(text[0])
- for i, ch in enumerate(text[1:], start=1):
- current_lower = is_lower(ch)
- if last_lower != current_lower:
- indices.append(i)
- last_lower = current_lower
- return indices
- INDENT = " "
- def key2attr(key):
- return key.strip().replace("-","_").lower()
- def convert2class(data, cls_name, indent=0):
- offset = indent * INDENT
- spacing = offset + INDENT
- lines = []
- for key in get_keys_of_dict_values(data):
- lines.append(convert2class(data[key], key.capitalize()))
- lines.append("")
- lines.extend([
- f"{offset}class {cls_name}:",
- f"{spacing}def __init__(self, data):",
- f"{spacing + INDENT}self._data = data"])
- for key in get_keys_of_no_dict_values(data):
- lines.append("")
- lines.append(f"{spacing}@property")
- lines.append(f"{spacing}def {key2attr(key)}(self):")
- lines.append(f"{spacing + INDENT}return self._data[\"{key}\"]")
- for key in get_keys_of_dict_values(data):
- lines.append("")
- lines.append(f"{spacing}@property")
- lines.append(f"{spacing}def {key2attr(key)}(self) -> {key.capitalize()}:")
- lines.append(f"{spacing + INDENT}return {key.capitalize()}(self._data[\"{key}\"])")
- return "\n".join(lines)
- DICT = dict(name="ico", address=dict(street="hemus", city="sofia"))
- def main():
- import string
- print(convert2class(DICT, "Person"))
- [print(_) for _ in dir(string)]
- word="myLocalValue"
- print(word)
- print(get_split_indices(word))
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement