printDict[source]

printDict(d:dict, length:int=10, space=0)

print dictionary as first length value of values

d: dict: the dictionary to be printed

length: int: if the value is a string, then it will print the first length letter of the value, default = 10

space: int: the amount of additional space to be added for each nested dictionary, default = 0

printDict({'key':'sjfhdkljhafsdlkjhdfaslkjhkljfadshklhfa', 'nestedKey':{'nestedKey2':'938023840843', 'nested3':{'nested4':'hello'}}})
key : sjfhdkljha
nestedKey
 nestedKey2 : 9380238408
 nested3
  nested4 : hello

allKeysInDict[source]

allKeysInDict(inputDict:dict, keys:list)

checks whether all the keys given in the list are also keys in the inputDict dictionary

inputDict: dict: the dictionary that will be used to check whether all the keys are in this dictionary

keys: list: the list of keys

inputDict = {"user":"nic","phone":"+66816684442","pw":"12345678","name":"nic"}
passingKeys = ['user','phone','pw']
failedKeys = ['us', 'phone']
print(allKeysInDict(inputDict, passingKeys))
print(allKeysInDict(inputDict, failedKeys))
True
False

filterDt[source]

filterDt(dtDict:dict)

convert unjsonable datetime object to timestamp in the dictionary, this works for nested dictionary as well

dtDict: dict: the dictionary inputted to convert the datetime object of its value

from datetime import datetime
filterDt({'time': {'time2':datetime.now()}, 'hello': 'world'})
{'time': {'time2': 1618763407.18315}, 'hello': 'world'}

stripDict[source]

stripDict(data:dict)

if the value in the dictionary is a string, it will 'strip' the value to make it more clear

data: dict: the dictionary inputted to be 'stripped'

stripDict({'sdfd': 'dsf  ', 'gdsgsa  ':234})
{'sdfd': 'dsf', 'gdsgsa  ': 234}

hashDict[source]

hashDict(data:dict, hasher=<sha1 HASH object @ 0x7f0e2c91bfb0>, encoder=dumps)

hashes the dictionary inputted

data: dict: the dictionary inputted to be 'hashed'

%%time
hashDict({'hello':'world'})
CPU times: user 37 µs, sys: 6 µs, total: 43 µs
Wall time: 47.2 µs
'DJ0JD7FLbd/e7NfdvLcRXbJGa8w='

saveDictToFile[source]

saveDictToFile(data:dict, path:str)

saves the dictionary to the file directed by the path

data: dict: the dictionary to be saved

path: str: the file path to the file the dictionary is going to be saved

loadDictFromFile[source]

loadDictFromFile(path:str)

returns the dictionary that is saved in the file

path: str: the path taken to the file of the dictionary

saveDictToFile({'test':'test'},'/tmp/testdict')
loadDictFromFile('/tmp/testdict')
{'test': 'test'}

saveStringToFile[source]

saveStringToFile(data:str, path:str)

saves the string to the file directed by the path

data: str: the string to be saved

path: str: the file path to the file the string is going to be saved

loadStringFromFile[source]

loadStringFromFile(path:str)

returns the string that is saved in the file

path: str: the path taken to the file of the string

path = '/tmp/teststr'
saveStringToFile('hello', path)
loadStringFromFile(path)
'hello'

show tree

genSchema[source]

genSchema(inputDict:dict, format_='yaml')

generate a json schema from dict

format: return schema in json or yaml, 'both' can be inputted to return a tuple of (json, yaml), output = dict or str, default = 'yaml'

inputDict: dict: the dict inputted to be used to generate the schema

dict_ = {'test':1}
r = genSchema(dict_, format_='yaml')
print(r)
$schema: http://json-schema.org/schema#
properties:
  test:
    type: integer
required:
- test
type: object

printYaml

printYaml[source]

printYaml(input_:dict, returnYaml=False)

printYaml({'test':{'test1':123}})
test:
  test1: 123

load yaml

loadYaml[source]

loadYaml(path:str, loader=FullLoader)

load yaml string input: path:str: path of the data to load return: diet of the yaml

path = 'testData/test.yaml'
data = {'test':'test'}
with open(path, 'w') as f:
  f.write(yaml.dump(data))
loadYaml(path)
{'test': 'test'}

writeYaml

writeYaml[source]

writeYaml(path:str, data:dict)

path = 'testData/test.yaml'
data = {'test':'test'}
writeYaml(path,data)
loadYaml(path)
{'test': 'test'}

hashDict

hash_dict[source]

hash_dict(func)

Transform mutable dictionnary Into immutable Useful to be compatible with cache

import cachetools.func
from beartype import beartype
import time

@beartype
@hash_dict
@cachetools.func.ttl_cache(ttl=10)
def testFunc(input_:dict)->dict:
  time.sleep(2)
  return input_
testFunc({'test':{'test':'test'}})
{'test': {'test': 'test'}}
hash(ujson.dumps(({'test':{'test':'test'}}),sort_keys=True))
-2608427765084268190