Skip to content

Instantly share code, notes, and snippets.

View ra0x3's full-sized avatar
🔥
fuego

rashad ra0x3

🔥
fuego
View GitHub Profile
@ra0x3
ra0x3 / prime-factorization.py
Last active November 9, 2019 20:53
Prime number factorization function
import os
import sys
from typing import *
def is_prime(n: int) -> bool:
if n < 2:
return False
return all(n % i for i in range(2, int(math.sqrt(n)) + 1))
def get_prime_factors(n: int) -> List[int]:
@ra0x3
ra0x3 / task.py
Created November 5, 2019 19:40
A concurrent task object
import os
import sys
from typing import *
TaskReturn = NewType("TaskReturn", Union[Any, TaskException])
class TaskException(Exception):
pass
@ra0x3
ra0x3 / process.py
Created November 5, 2019 19:43
A task processor abstraction wrapping multiprocessing.Process
import os
import sys
from typing import *
import multiprocessing
class Process(multiprocessing.Process):
def __init__(self, id: int, task_queue: Iterable, results: Iterable):
super(Process, self).__init__()
self.id = id
self.task_queue = task_queue
@ra0x3
ra0x3 / manager.py
Last active November 9, 2019 21:00
A process manager used to manage individual processes
import os
import sys
from typing import *
import multiprocessing
class ProcessManager:
def __init__(self):
self.task_queue = multiprocessing.JoinableQueue()
self.results = multiprocessing.Queue()
self.processes = [Process(i, self.task_queue, self.results) for i in range(self.num_cpus())]
@ra0x3
ra0x3 / main.py
Created November 5, 2019 19:45
Compare factorization sequentially vs concurrently
import os
import sys
import random
from typing import *
N_TASKS = 20
UPPER_BOUND = 10e12
LOWER_BOUND = 10e10
def sequential_factorization():
@ra0x3
ra0x3 / results.csv
Created November 5, 2019 20:18
Results of sequential vs concurrent compute tasks
Type Timeit Iters Tasks Factor Range Threads Seconds CPU Usage
sequential 1000 10 10e5 - 10e8 1 46 100%
concurrent 1000 10 10e5 - 10e8 2 57 50%
sequential 1000 20 10e10 - 10e12 1 816 100%
concurrent 1000 20 10e10 - 10e12 2 391 7%
@ra0x3
ra0x3 / attrs-demo.js
Last active November 11, 2019 23:50
model attributes rfc
// app/core/model-builder.js
class ModelBuilder {
// still using a wrapper function over `defineAttribute`, that
// creates default options via instance chanining. this will
// reduce boiler plate in the model manager files
formAttribute(attr, options) {
return this.defineAttribute(attr, options)
.certifiable()
@ra0x3
ra0x3 / peewee_extensions.py
Created March 4, 2020 05:01
Helper classes to allow recursive automatic joins in peewee
class Direct:
def __init__(self, native_key, foreign_key):
"""
Represents a direct relationship from one 'native'
model, to one 'foreign' model
"""
self.native_key = native_key
self.foreign_key = foreign_key
@ra0x3
ra0x3 / models.py
Created March 4, 2020 05:08
A sample messages model in peewee
from peewee import (
BaseModel,
ForeignKeyField,
TextField,
IntegerField,
DateTimeField,
CharField
)
from peewee_extensions import (
@ra0x3
ra0x3 / managers.py
Last active March 6, 2020 17:21
A sample model manager demonstrating how joins work
from peewee_extensions import PeeweeExtensions
class BaseManager:
def __init__(self, name):
self.model_name = name
self.exts = PeeweeExtensions(self)
self.model = BaseManager.model_for(self.model_name)
self.relations = self.model._meta.relations
@staticmethod