Skip to content

Instantly share code, notes, and snippets.

@tripulse
tripulse / bfgen.py
Created September 7, 2020 17:16
Brainfuck code generator from bytestrings.
# (c) Copyright 2020 tripulse.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
from bitstring import Bits
class SIGIO:
"""SIGIO is a binary-like format used by a person named 5IGI0 to interchange feelings.
The format is really limited to them and has no specification, with efforts of digging
it made this implementation possible."""
@staticmethod
def encode(s):
return ''.join("'" if b else '-' for b in Bits(s.encode('ascii')))
@tripulse
tripulse / morse.py
Created June 22, 2020 16:09
Morsecode generator implementation, converts unicode strings from/to morsecode.
from itertools import zip_longest
from functools import partial
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
def morsedec(s, bits=8, chars='.-') -> str:
return ''.join(chr(int(''.join(b), base=2)) for b in
grouper(map(lambda b: str(chars.index(b)), s), bits, '0'))
@tripulse
tripulse / kvpio.py
Created June 5, 2020 14:34
KVP — key value pair is a similar format to SQLite/INI but more abstract.
import io
from functools import partial
def isbinaryfile(f, mode) -> bool:
"""Is a given file-like object `f` capable of handling
binary operations a given mode (read='r' or write='w')."""
if mode == 'r':
try:
return f.read(0) == b''
@tripulse
tripulse / rkpio.py
Created May 24, 2020 02:02
RKPI2 implementation in pure Python.
"""
A reference implementation of the RKPI2 format based on the specification,
this module operates on NumPy arrays to input/output of samples.
This is a trivial example would be like this:
```
>>> import rkpio
>>> import numpy as np
>>> out = rkpio.File('sine.rkp', 'w',
rkpio.Header(rkpio.Format.F32,
@tripulse
tripulse / lameenc.py
Last active May 5, 2020 04:12
A high-level pythonic wrapper over the libmp3lame C API.
"""
A high-level wrapper over libmp3lame C API (a high-quality
production grade MPEG Layer-III encoder). This excludes some
useless and undocumented routines which don't affect encoding
at all or not used often.
"""
from enum import IntEnum
from array import array
from os import name as _os_name
@tripulse
tripulse / sc_client_id.py
Last active April 29, 2020 03:03
Extracts the available Client ID generated for user if visited https://soundcloud.com. The original extraction method is from YouTube-DL's Soundcloud extractor.
import requests
import re
def _extract_client_id():
for scrsrc in reversed(re.findall(r'<script[^>]+src="([^"]+)"',
requests.get('https://soundcloud.com').text)):
if (clid := re.search(r'client_id\s*:\s*"(?P<id>[0-9a-zA-Z]{32})"',
requests.get(scrsrc).text)):
return clid.groupdict()['id']
# DCT is required for quantising coefficients, this always
# performs a orthonormal DCT and IDCT.
from scipy.fftpack import dctn,idctn
from numpy import any, clip, floor, array, asarray, zeros, round, resize
from functools import partial
# Explicitly specify that Type-II DCT is required.
dctn = partial(dctn, type=2, norm='ortho')
idctn = partial(idctn, type=2, norm='ortho')
"""
Plugin for both encoding and decoding Suckless Farbfeld files
for the PIL library (compression not supported).
"""
from PIL import Image
from PIL.ImageFile import ImageFile
from struct import unpack, pack
class FarbfeldImageFile(ImageFile):
@tripulse
tripulse / requirements.txt
Last active April 22, 2020 03:42
Reekpie v2 implementation in pure Python 3x.
numpy==1.18.2
zstandard==0.13.0