Skip to content

Instantly share code, notes, and snippets.

@raeq
raeq / dict_12_deepcopy.py
Created August 5, 2020 13:15
Dictionary deepcopy
import copy
original = {}
original["object1"] = dict({"a": {"b": [1, 2, 3]}})
dict_shallow = original.copy()
dict_deep = copy.deepcopy(original)
# change the mutable object in original and dict_shallow
original["object1"]["a"]["b"] = [3, 4, 5]
@raeq
raeq / dict_13_invert.py
Created August 5, 2020 17:22
Invert dictionary
def invert_dictionary(input:dict)->dict:
return {v: k for k, v in dict1.items()}
dict1 = {"a": 1, "b": 2, "c": 3}
assert invert_dictionary(dict1) == {1: "a", 2: "b", 3: "c"}
@raeq
raeq / dict_15_json.py
Created August 6, 2020 05:07
Complex dictionaries to JSON
from datetime import datetime
import json
class PythonObjectEncoder(json.JSONEncoder):
"""PythonObjectEncoder.
"""
def default(self, an_object_value):
"""default.
@raeq
raeq / dict_15_json.py
Created August 6, 2020 05:23
Complex dictionary to JSON
import json
from datetime import datetime
class PythonObjectEncoder(json.JSONEncoder):
def default(self, an_object_value):
if isinstance(an_object_value, str):
return an_object_value
@raeq
raeq / dict_16_dynamic.py
Created August 6, 2020 06:09
Remove items from a dictionary with matching value
def remove_even_items(the_dict: dict):
# collect the keys of all the items to remove
delete_these = set(k for k, v in the_dict.items() if v % 2 == 0)
for delete_this in delete_these:
del the_dict[delete_this]
return the_dict
@raeq
raeq / list_01_defaultlist.py
Last active August 9, 2020 20:20
A UserList subclass implementing unique list values.
from collections import UserList
class UniquesList(UserList):
"""
A List Class which works just like a list, except
that it only holds unique values - similar to a set.
>>> ul = UniquesList("The Jolly Green Giant")
>>> print("".join(ul))
@raeq
raeq / list_02_indices.py
Created August 9, 2020 20:46
Finds the indices of matching elements in a list.
import typing
def get_indices(the_list: list, test_value: object) -> typing.Iterable[int]:
"""
Returns the indices of matching list items.
Uses a generator to create an iterator.
Args:
the_list: the list containing search elements
test_value: what we want to find
@raeq
raeq / list_03_flatten.py
Created August 9, 2020 21:46
Flatten a list of lists to a list.
from itertools import chain
def flatten_nested_lists(*input_list: list) -> list:
"""flatten_nested_lists.
Args:
input_list:
Returns:
@raeq
raeq / list_04_frozen.py
Created August 12, 2020 18:45
A frozen list implementation.
from collections.abc import Iterable
from collections import UserList
def immutable_decorator(f):
def wrapper(self, *args, **kwargs):
raise TypeError("Object is frozen")
return wrapper
@raeq
raeq / list_05_autoappend.py
Created August 12, 2020 18:47
A simple auto append list.
from collections import UserList
class AutoAppendList(UserList):
"""
AutoAppendList. Will append an item if you are off by one index assignment.
>>> aal: AutoAppendList = AutoAppendList()
>>> for i in range(10):
... aal[i] = i
>>> print(aal)