This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
# Linking iterators together | |
# chain: Combines multiple iterators into a single sequential iterator. | |
# cycle: Repeats an iterator’s items forever. | |
# tee: Splits a single iterator into multiple parallel iterators. | |
# zip_longest: A variant of the zip built-in function that works well with iterators of different lengths. | |
# Filtering items from an iterator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# heaps in standard list types with functions like heappush, heappop, and nsmallest | |
# Items are always removed by highest priority (lowest number) first. | |
a = [] | |
heappush(a, 5) | |
heappush(a, 3) | |
heappush(a, 7) | |
heappush(a, 4) | |
# print(heappop(a), heappop(a), heappop(a), heappop(a)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Double-ended Queue | |
fifo = collections.deque() | |
fifo.append(1) # Producer | |
x = fifo.popleft() # Consumer | |
# OrderedDict | |
a = OrderedDict() | |
a[‘foo’] = 1 | |
a[‘bar’] = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timezone | |
# Conversion to computer local time | |
now = datetime(2014, 8, 10, 18, 18, 30) | |
now_utc = now.replace(tzinfo=timezone.utc) | |
now_local = now_utc.astimezone() | |
print(now_local) | |
# Conversion local to Unix timestamp in UTC | |
time_str = '2014-08-10 11:18:30' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@contextmanager | |
def debug_logging(level): | |
""" Change the logging level | |
""" | |
logger = logging.getLogger() | |
old_level = logger.getEffectiveLevel() | |
logger.setLevel(level) | |
try: | |
yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def trace_decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
# Decorate before | |
print('Hello') | |
# Call function | |
result = func(*args, **kwargs) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gcd(pair): | |
a, b = pair | |
low = min(a, b) | |
for i in range(low, 0, -1): | |
if a % i == 0 and b % i == 0: | |
return i | |
# No parallel | |
numbers = [(1963309, 2265973), (2030677, 3814172), | |
(1551645, 2229620), (2039045, 2020802)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Corutines | |
# Extension of generators | |
# They serve like "microservices" | |
def my_corutine(): | |
while True: # Alive microservice | |
received_value = yield # This will freeze the corutine until a value is "send" to it | |
# Do amazing work | |
print("Job done with input", received_value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LockingCounter(object): | |
def __init__(self): | |
self.lock = Lock() | |
self.count = 0 | |
def increment(self, offset): | |
with self.lock: | |
self.count += offset | |
def worker(sensor_index, how_many, counter): | |
for _ in range(how_many): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run_openssl(data): | |
env = os.environ.copy() | |
env[‘password’] = b’\xe24U\n\xd0Ql3S\x11’ | |
proc = subprocess.Popen( | |
[‘openssl’, ‘enc’, ‘-des3’, ‘-pass’, ‘env:password’], | |
env=env, | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE) | |
proc.stdin.write(data) | |
proc.stdin.flush() # Ensure the child gets input |
NewerOlder