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 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) |
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 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() |
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 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() |
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 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() |
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 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 |
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
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) |
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 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('/'): |
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
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 |
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 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"])) |
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 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"])) |