SHARE
TWEET

Untitled

a guest Aug 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def get_keys_of_no_dict_values(*dicts):
  2.     keys = []
  3.     for d in dicts:
  4.         keys.extend([k
  5.                     for k
  6.                     in d.keys()
  7.                     if ((k not in keys)
  8.                         and (not isinstance(d[k], dict)))]
  9.         )
  10.  
  11.     return keys
  12.  
  13.  
  14. def get_keys_of_dict_values(*dicts):
  15.     keys = []
  16.     for d in dicts:
  17.         keys.extend([k
  18.                     for k
  19.                     in d.keys()
  20.                     if ((k not in keys) and isinstance(d[k], dict))])
  21.  
  22.     return keys
  23.  
  24. def get_split_indices(text):
  25.     indices = []
  26.     import string
  27.     is_lower = lambda c: c in string.ascii_lowercase
  28.     is_upper = lambda c: c in string.ascii_uppercase
  29.    
  30.     last_lower = is_lower(text[0])
  31.     for i, ch in enumerate(text[1:], start=1):
  32.         current_lower = is_lower(ch)
  33.         if last_lower != current_lower:
  34.             indices.append(i)
  35.             last_lower = current_lower
  36.            
  37.     return indices
  38.  
  39. INDENT = "   "
  40.  
  41. def key2attr(key):
  42.     return key.strip().replace("-","_").lower()
  43.  
  44. def convert2class(data, cls_name, indent=0):
  45.     offset = indent * INDENT
  46.     spacing = offset + INDENT
  47.     lines = []
  48.    
  49.     for key in get_keys_of_dict_values(data):
  50.         lines.append(convert2class(data[key], key.capitalize()))
  51.         lines.append("")
  52.    
  53.     lines.extend([
  54.         f"{offset}class {cls_name}:",
  55.         f"{spacing}def __init__(self, data):",
  56.         f"{spacing + INDENT}self._data = data"])
  57.  
  58.     for key in get_keys_of_no_dict_values(data):
  59.         lines.append("")
  60.         lines.append(f"{spacing}@property")
  61.         lines.append(f"{spacing}def {key2attr(key)}(self):")
  62.         lines.append(f"{spacing + INDENT}return self._data[\"{key}\"]")
  63.    
  64.     for key in get_keys_of_dict_values(data):
  65.         lines.append("")
  66.         lines.append(f"{spacing}@property")
  67.         lines.append(f"{spacing}def {key2attr(key)}(self) -> {key.capitalize()}:")
  68.         lines.append(f"{spacing + INDENT}return {key.capitalize()}(self._data[\"{key}\"])")
  69.    
  70.        
  71.     return "\n".join(lines)
  72.  
  73. DICT = dict(name="ico", address=dict(street="hemus", city="sofia"))
  74.  
  75. def main():
  76.     import string
  77.     print(convert2class(DICT, "Person"))
  78.     [print(_) for _ in dir(string)]
  79.     word="myLocalValue"
  80.     print(word)
  81.     print(get_split_indices(word))
  82.    
  83. if __name__ == "__main__":
  84.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top