Skip to content

Instantly share code, notes, and snippets.

Cathal Garvey cathalgarvey

Block or report user

Report or block cathalgarvey

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@cathalgarvey
cathalgarvey / jlgz_dump.go
Created Apr 5, 2017
How to Read Lines from GZIP-Compressed Files in Go
View jlgz_dump.go
package main
import (
"compress/gzip"
"os"
"bufio"
"fmt"
"log"
)
@cathalgarvey
cathalgarvey / strict.py
Created Apr 12, 2015
Strictness decorator for Python with optional group-disabling system for debug-only checks
View strict.py
import inspect
import collections
import functools
# NB: For all the "make_x_tester" functions the errors raised should use lowercase
# "argument" to describe the problem, which will be patched to "return value"
# where appropriate!
def make_simple_tester(valuetype):
def testerfunc(tested_val):
@cathalgarvey
cathalgarvey / patch_wgetted_4chan.py
Last active Aug 29, 2015
A CSS/JS-fetching and HTML-patching script for correctly archiving sites (well, 4chan at least) using wget, as requested on Reddit.
View patch_wgetted_4chan.py
#!/usr/bin/env python3
# by Cathal Garvey, copyright 2015, released under the AGPL: https://gnu.org/licenses/agpl.txt
# Commissioned by a 4chan user on reddit /r/linux who wanted backups but wget couldn't fetch most JS/CSS
# correctly. Only tested on 4chan in keeping with request.
# Usage e.g. (papercraft sub on 4chan):
# wget --recursive --no-clobber --page-requisites --html-extension --convert-links --no-parent http://boards.4chan.org/po/
# cd boards.4chan.org
# # (Directory contains subdirectory "po/" which contains all HTML)
# # (Provide root domain of crawled site to help resolve relative links, and target folder)
# python3 <this script> boards.4chan.org po
@cathalgarvey
cathalgarvey / biorad_dialogue_killer.txt
Last active Aug 29, 2015
Bookmarklet to remove the goddamed "pick your location" landing dialog from bio-rad.com
View biorad_dialogue_killer.txt
Bio-rad.com has one of those intensely aggravating landing dialogs that demands
to know where you are so it can forcibly remove you from what you're trying to
view, and perhaps gouge you for more money.
This is annoying, invasive and generally not-cool, so here's a little
bookmarklet to remove the dialog and get on with things.
To use, just copy the below code into a new bookmark using your browser's
bookmark manager. Don't omit the "javascript:" bit or the final empty
parentheses: "()".
@cathalgarvey
cathalgarvey / chacha20
Created Aug 15, 2014
ChaCha20 stream cipher in Python 3
View chacha20
# Pure Python ChaCha20
# Based on Numpy implementation: https://gist.github.com/chiiph/6855750
# Based on http://cr.yp.to/chacha.html
#
# I wanted an implementation of ChaCha in clean, understandable Python
# as a way to get a handle on the algorithm for porting to another language.
# There are plenty of bindings but few pure implementations, because
# Pure Python is too slow for normal practical use in Cryptography.
#
# The preceding implementation used NumPy, which avoided a lot of the
@cathalgarvey
cathalgarvey / mullis.py
Last active Aug 29, 2015
A micro-lisp-like for processing 'formulas' in and upon JSON data, in Python / Coffeescript
View mullis.py
# This is a micro-language designed to be embedded in JSON, allowing
# data in the JSON tree to specify a formula for how it should be derived
# based on the rest of the tree. Formulas are constructed of prefix-notation
# lists naming a function and passing arguments, which are recursively
# evaluated.
#
# Operations are mostly mathematical, with one ternary function that allows
# for simple conditional operations or code-branching.
#
# An example:
@cathalgarvey
cathalgarvey / 2048_arbgrid.py
Created Jun 16, 2014
An almost-2048 clone for CLI use, with optional arbitrary grid dimensions (Python 3.3+)
View 2048_arbgrid.py
import random
import shutil
import tty
import sys
import termios
def getchar():
'Linux-only: Could not be bothered making arrow-getting code WinMac-compatible.'
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
@cathalgarvey
cathalgarvey / strict_decorator.py
Created May 1, 2014
A decorator for when you want strictness in Python3.
View strict_decorator.py
def strict(func):
'''A decorator for methods or functions that requires annotations for all
arguments and the return value, throws typeerrors on deviations.
Remember that for more than one return value, the return type is "tuple".
Container-type arguments or return values are only inspected at top-level.
Note that as written, this does not handle catchall argument types "*args", or "**kwargs".
'''
import inspect, collections
NoneType = type(None)
def die_on_untyped_annotation(par, ann_type="argument annotation"):
@cathalgarvey
cathalgarvey / pruner.py
Created Apr 10, 2014
An object for recursively pruning empty containers and Nonetypes from containers or sequences.
View pruner.py
class Prune:
'Treat like a function; call Prune.prune on any datatype to prune NoneTypes and empty Tuples/Lists/Dicts.'
_t = tuple() # Empty tuple, as "(,)" literal doesn't work.
@classmethod
def prune(self, some_data):
if isinstance(some_data, (list, tuple)):
return self.prune_sequence(some_data)
elif isinstance(some_data, dict):
return self.prune_tree(some_data)
@cathalgarvey
cathalgarvey / dncode.py
Last active Aug 29, 2015
Python 8-line DNA compression tool. Usage: 'python3 dncode.py [e/d] <somefile>', output to stdout.
View dncode.py
import sys as s, itertools as i
t,u,i2b,o=b'T',b'U',lambda i:i.to_bytes(1,'big'),[s.stdout.buffer.write,lambda s:0]
A,e=s.argv,dict(zip((''.join(x).encode()for x in i.product(*('ACGT',)*4)),map(i2b,range(256))))
with open(A[2],'rb')as I:D,Rr=b''.join(I.read().strip().split()),lambda s:s.replace(t,u)
S,R,M,o,d=D[:-1],D[-1]&4,D[-1]&2,o[::-1]if A[1]=="d"else o,dict(zip(e.values(),e)).get
[o[0](e.get(q.replace(u,t)+(b'A'*(4-len(q)))))for q in(D[i: i+4] for i in range(0,len(D),4))]
o[0](i2b((len( [D[i: i+4] for i in range(0,len(D),4)][-1] )%4)|(4 if u in S else 0)))
o[1](b''.join(Rr(d(i2b(x)))if R else d(x)for x in S)[:-M if M else None]+b'\n')
You can’t perform that action at this time.