Skip to content

Instantly share code, notes, and snippets.

@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_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_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_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 / slicing.py
Created August 31, 2020 11:10
The length of a string.
hello: str = 'Hello World!'
assert len(hello) == 12
@raeq
raeq / slicing.py
Created August 31, 2020 11:11
Length of a string
hello: str = 'Hello World!'
assert len(hello) == 12
@raeq
raeq / slicing.py
Created August 31, 2020 11:13
Accessing an index.
hello: str = 'Hello World!'
assert hello[6] == 'W'
hello: str = 'Hello World!'
assert hello[-6] == 'W'
@raeq
raeq / slicing.py
Created August 31, 2020 11:20
Enumeration of a string.
hello: str = 'Hello World!'
print (list(enumerate(hello)))
@raeq
raeq / slicing.py
Created August 31, 2020 11:44
A slice
hello: str = 'Hello World!'
print(hello[3:8])
assert hello[3:8] == 'lo Wo'