Skip to content

Instantly share code, notes, and snippets.

View UNIcodeX's full-sized avatar

Jared Fields UNIcodeX

View GitHub Profile
@UNIcodeX
UNIcodeX / async_executor.py
Created March 19, 2018 14:28
An example of how to use run_in_executor() to call SYNCHRONOUS code from ASYNCHRONOUS code, to avoid blocking execution of other tasks.
import asyncio
import time
def sync_func():
time.sleep(10)
return 'sync_func() - Synchronous function data'
async def async_func():
while True:
import asyncio # to get access to the asyncio.sleep function. Not sure if or where this is available in Sanic.
import random
import aioredis
import redis
from sanic import Sanic, request, response
app = Sanic(__name__)
sr = redis.StrictRedis(host='localhost', port=6379)
sr.execute_command('FLUSHDB')
import asyncio
import random
import aioredis
import redis
from quart import Quart, request, url_for, jsonify
app = Quart(__name__)
sr = redis.StrictRedis(host='localhost', port=6379)
sr.execute_command('FLUSHDB')
@UNIcodeX
UNIcodeX / FreeNAS Jailed DNS Service
Last active May 9, 2018 21:31
Instructions to configure a FreeNAS Jailed DNS Service and Also Black Hole DNS Names
1) create a jail
2) chroot to the jail
3) pkg update
4) pkg install python36-3.6.5 (or latest)
5) pkg install python py36-pip-9.0.3 (or latest)
6) pkg install bash
7) pip install --upgrade pip
8) pip install twisted
9) pkg install py27-supervisor-3.3.3,1 (or latest)
10) pkg install wget
@UNIcodeX
UNIcodeX / example_multiprocessing.py
Created October 20, 2018 04:06
Example of using multiprocessing while also drawing a progress bar to show the status.
import multiprocessing
from tqdm import tqdm
import time
import sys
def worker(i):
time.sleep(.1)
return i*8
import times
let time_begin = times.cpuTime()
proc primes(n: int): seq[int] =
result = seq[int]
if n == 2:
result.add(2)
elif n < 2:
result.add(0)
@UNIcodeX
UNIcodeX / micropython_opt_tips.py
Last active November 9, 2018 20:21
Micropython Optimization Tips from Damien George
"""
1) Dont use * or ** args
2) Short variable names
3) Functions to avoid global variables (local over global)
4) "mpy-cross" to pre-compile code so that the microcontroller doesnt have to use as many resources to compile and load / import
5) freeze modules
"""
# HARDWARE CONTROL EXAMPLES (BLINK LED)
# =====================================
# Python 2x only. Python 3x does not work with this.
import time
def primes(n):
if n == 2: return [2]
elif n < 2: return []
s = range(3, n + 1, 2)
mroot = n ** 0.5
half = (n + 1) / 2 - 1
i = 0
@UNIcodeX
UNIcodeX / Nim threading example.nim
Created October 19, 2019 16:25
Rahmatullah's threading example
import os, threadpool, strformat, strutils, sequtils
const
mkvmerge = r"d:\installer\mkvtoolnix\mkvmerge.exe"
proc remux(idx: int, filename: string) =
let
newcount = &"{idx:02}"
newname = newcount & ".mkv"
fonts = toseq(walkFiles("./" & newcount & "/*"))
@UNIcodeX
UNIcodeX / threaded_pi.nim
Created October 20, 2019 05:15
Nim Threaded Pi
# Compute PI in an inefficient way
import strutils, math, threadpool
{.experimental: "parallel".}
proc term(k: float): float = 4 * math.pow(-1, k) / (2*k + 1)
proc pi(n: int): float =
var ch = newSeq[float](n+1)
parallel:
for k in 0..ch.high: