Skip to content

Instantly share code, notes, and snippets.

@raeq
raeq / slicing.py
Created August 31, 2020 11:10
The length of a string.
hello: str = 'Hello World!'
assert len(hello) == 12
@raeq
raeq / list_01_defaultlist.py
Created August 13, 2020 10:40
A list of unique elements.
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 12, 2020 19:41
Iterators and large lists.
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_07_map.py
Created August 12, 2020 19:20
Obfuscated Python lists using map and reduce.
import functools
a = [5, 7, 9, 11]
b = [1, 20, 30, 40]
c = [3, 3, 3, 6]
answers = list(map(lambda x, y, z: (x + y) / z, a, b, c))
assert answers == [2.0, 9.0, 13.0, 8.5]
summation = functools.reduce(lambda x, next_reduce: x + next_reduce, [2.0, 9.0, 13.0, 8.5])
@raeq
raeq / list_06_urlfilter.py
Created August 12, 2020 18:54
A list which only accepts URLs.
from collections import UserList
from urllib.parse import urlparse
import json
import requests
def recursive_key_values(dictionary):
for key, value in dictionary.items():
i = 0
if type(value) is str:
@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)
@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_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_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_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))