Skip to content

Instantly share code, notes, and snippets.

View UNIcodeX's full-sized avatar

Jared Fields UNIcodeX

View GitHub Profile
# 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 / 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)
# =====================================
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 / 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
@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
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')
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')
@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:
"""Simple class to read IFF chunks.
An IFF chunk (used in formats such as AIFF, TIFF, RMFF (RealMedia File
Format)) has the following structure:
+----------------+
| ID (4 bytes) |
+----------------+
| size (4 bytes) |
+----------------+
"""Stuff to parse WAVE files.
Usage.
Reading WAVE files:
f = wave.open(file, 'r')
where file is either the name of a file or an open file pointer.
The open file pointer must have methods read(), seek(), and close().
When the setpos() and rewind() methods are not used, the seek()
method is not necessary.