Skip to content

Instantly share code, notes, and snippets.

import asyncio
from typing import Any, List, Callable, Coroutine
async def async_io_worker(time_sec: int) -> str:
await asyncio.sleep(time_sec)
return f"Waited for {time_sec}s..."
async def async_scheduler(instances: int, worker: Coroutine, args: List[Any]) -> None:
coroutines = [worker(*args) for _ in range(instances)]
await asyncio.gather(*coroutines)
from multiprocessing import Process
def multithread_scheduler(instances: int, worker: Callable, args: List[Any]) -> None:
threads = [
Thread(target=worker, args=args)
for _ in range(instances)
]
for thread in threads:
thread.start()
from multiprocessing import Process
def multithread_scheduler(instances: int, worker: Callable, args: List[Any]) -> None:
threads = [
Thread(target=worker, args=args)
for _ in range(instances)
]
for thread in threads:
thread.start()
from multiprocessing import Process
def multiprocess_scheduler(instances: int, worker: Callable, args):
processes = [
Process(target=worker, args=args)
for _ in range(instances)
]
for process in processes:
process.start()
import time
from contextlib import contextmanager
from typing import Any, List, Callable
@contextmanager
def timer():
start_time = time.time()
yield
elapsed_time = time.time() - start_time
string1 = "Hello World!"
string2 = "I'm too odd for this s#!t"
def reverse_string(string):
string = list(string)
for i in range(0, len(string)//2):
string[i], string[-(i+1)] = string[-(i+1)], string[i]
return "".join(string)
@amaciel81
amaciel81 / ioctl_example.py
Created August 27, 2018 01:05
ioctl example
import fcntl
import os
CD_DEVICE = '/dev/cdrom'
CD_EJECT = 0x5309 # From https://github.com/torvalds/linux/blob/master/include/uapi/linux/cdrom.h
# You need to open the device, not the link
if os.path.islink(CD_DEVICE):
device_path = os.readlink(CD_DEVICE)
if not device_path.startswith('/'):
@amaciel81
amaciel81 / ls.strace
Last active August 23, 2018 01:18
strace output of 'ls -l ~/test/*.txt'
execve("/bin/ls", ["ls", "-l", "/home/cloudchef/test/a.txt", "/home/cloudchef/test/b.txt", "/home/cloudchef/test/c.txt"], 0x7ffcc2e83e30 /* 63 vars */) = 0
brk(NULL) = 0x557de4912000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=98921, ...}) = 0
mmap(NULL, 98921, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f91796de000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libselinux.so.1", O_RDONLY|O_CLOEXEC) = 3
@amaciel81
amaciel81 / parse_csv.py
Created August 7, 2018 03:05
Import a CSV file. If condition is met, print two attributes for that condition
from csv import reader as csv_reader
with open("sample_input.csv") as input_fh:
people = csv_reader(input_fh)
headers = next(people)
for row in people:
person = (dict(zip(headers, row)))
if int(person["age"]) >= 30:
print("Name: {name}, City: {city}".format(name=person["name"], city=person["city"]))
@amaciel81
amaciel81 / parse_json.py
Last active November 21, 2020 01:02
Import a JSON file. If condition is met, print two attributes.
from json import load as json_load
with open("sample_input.json") as input_fh:
people = json_load(input_fh)
for person in people["people"]:
if person["age"] >= 30:
print("Name: {name}, City: {city}".format(name=person["name"], city=person["city"]))