Skip to content

Instantly share code, notes, and snippets.

View ttsiodras's full-sized avatar
💭
while True: hack()

Thanassis Tsiodras ttsiodras

💭
while True: hack()
View GitHub Profile
@ttsiodras
ttsiodras / hack.py
Last active August 21, 2017 09:48
I just did something that depending on your viewpoint on coding, is either an insult to God and humanity, or absolutely brilliant.
#
# I inherited a large code base, where hundreds of code paths end up
# calling "common_function_called_in_gazillion_places".
#
# And the need arose for this function to access the HTTP request's
# headers...
#
# What to do? Refactor all the places leading up to here? In a dynamically
# typed language, with no compiler to tell us the places to refactor?
#
@ttsiodras
ttsiodras / mp_import_evts.py
Created September 1, 2015 09:31
MixPanel importer
#!/usr/bin/env python
"""Mixpanel event importer
Usage:
mp_import_evts.py -c configFile -t token -a api_key -i fname -d delta
mp_import_evts.py (-h | --help)
mp_import_evts.py --version
Options:
-c configFile config file with settings

I code for a living - for two decades, now. And UNIX is in my blood.

I have therefore (naturally) installed Termux on my Android phone, and use it to spawn sshd - a very useful process when your ISP connection fails you, since you get to use the phone's internet connection (via ssh -D 1080 ... and configuring Firefox to use localhost:1080 as SOCKS proxy). This does not depend on your contract allowing hotspot-ing or not - it's close to bullet-proof.

Yesterday I ssh-ed in the phone, looking around and checking out the Android environment - and I see Termux's ps aux return among others .... /system/xbin/dropboxd.

What the heck - I never installed Dropbox on my phone!

It does not appear in the list of installed apps, either - and is naturally not removable, since I am not root.

@ttsiodras
ttsiodras / RustyThoughts.md
Last active September 8, 2020 08:17
Rusty thoughts on affine types

Below is my understanding of affine types and how they help us in Rust (unsure if I got this right - please correct if I am talking nonsense).

The issue is... How can we use the power of a type system so a compiler will block us from doing this wrong sequence of calls?

FILE *fp = NULL;
fclose(fp);
fp = fopen("...");

An idea is to "mirror" the states that the file variable goes through (unused/closed, opened) in the type system:

@ttsiodras
ttsiodras / TypePatcher.md
Created July 9, 2016 15:57
Annotating Python code with types for mypy

Annotating my python code with type annotations (for the magnificent mypy), I had to do stuff like this:

find . -type f -iname \*A_mapper.py | while read ANS ; do
    cat "$ANS" | \
        sed 's/def OnBasic(\([^,:]*\), \([^,:]*\), \([^,:]*\)):$/def OnBasic(\1: str, \2: AsnBasicNode, \3: AST_Leaftypes) -> None:/' | \
        sed 's/def OnChoice(\([^,:]*\), \([^,:]*\), \([^,:]*\)):$/def OnChoice(\1: str, \2: AsnChoice, \3: AST_Leaftypes) -> None:/' | \
        sed 's/def OnSequence(\([^,:]*\), \([^,:]*\), \([^,:]*\)):$/def OnSequence(\1: str, \2: AsnSequenceOrSet, \3: AST_Leaftypes) -> None:/' | \
        sed 's/def OnSet(\([^,:]*\), \([^,:]*\), \([^,:]*\)):$/def OnSet(\1: str, \2: AsnSequenceOrSet, \3: AST_Leaftypes) -> None:/' | \
        sed 's/def OnSequenceOf(\([^,:]*\), \([^,:]*\), \([^,:]*\)):$/def OnSequenceOf(\1: str, \2: AsnSequenceOrSetOf, \3: AST_Leaftypes) -> None:/' | \

sed 's/def OnSetOf(([^,:]), ([^,:]), ([^,:]*)):$/def OnSetOf(\1: str, \2: AsnS

@ttsiodras
ttsiodras / TypePatcher.md
Last active July 20, 2017 03:12
Annotating Python code with types (for mypy-checking)

I just finished adding type annotations to a 10-year old python codebase (for type-checking with the magnificent mypy).

I had to do stuff like this:

find . -type f -iname \*A_mapper.py | while read ANS ; do
    cat "$ANS" | \
        sed 's/def OnBasic(\([^,:]*\), \([^,:]*\), \([^,:]*\)):$/def OnBasic(\1: str, \2: AsnBasicNode, \3: AST_Leaftypes) -> None:/' | \
        sed 's/def OnChoice(\([^,:]*\), \([^,:]*\), \([^,:]*\)):$/def OnChoice(\1: str, \2: AsnChoice, \3: AST_Leaftypes) -> None:/' | \

sed 's/def OnSequence(([^,:]), ([^,:]), ([^,:]*)):$/def OnSequence(\1: str, \2: AsnSequenceOrSet, \3: AST_Leaftypes) -> None:/' | \

@ttsiodras
ttsiodras / protocol_via_coroutines.py
Last active January 1, 2024 06:03
The cleanest way (I've ever seen) to implement state machines in Python
#!/usr/bin/env python3
# Based on a great article from Eli Bendersky:
#
# http://eli.thegreenplace.net/2009/08/29/co-routines-as-an-alternative-to-state-machines/
#
# I just "ported" to Python3 (still using plain old yield) and added the proper type annotations,
# since Python 3.5 can now support them (and mypy can check them):
#
# $ mypy --disallow-untyped-defs protocol_via_coroutines.py
@ttsiodras
ttsiodras / gist:4c1a43c38d9b3cd9fc456da26d6f5b4d
Last active May 22, 2017 11:11
Makefile stdout redirection
# In relation to the discussion taking place at
# http://stackoverflow.com/questions/44097324/timing-of-makefile-include-statements-for-auto-generated-files/44103228
# OK, since I can't use PHONY to force the rebuild of piped.mk
# without losing the re-inclusion itself, I will just delete the file
# directly after using it (and thus force it to be re-created each
# time, as intended, without using .PHONY).
$ cat Makefile
all: piped.mk
@ttsiodras
ttsiodras / monkeyIslandData.h
Last active September 19, 2020 14:23
Monkey Island songs - frequencies and durations.
/////////////////////////////////////////////////////////////////////////////
//
// I designed a small circuit playing the Monkey Island songs via a buzzer
// and an Arduino pro mini:
//
// https://www.youtube.com/watch?v=9FKhpKlKFoc
//
// I first collected the song data by hacking the source code of DOSBOX,
// and playing the game a bit while gathering the frequencies...
//
@ttsiodras
ttsiodras / gist:c3e71b0db07ec2ca5a3d236fa6e98589
Last active March 4, 2024 01:56
List of RTEMS APIs used by uPython codebase
$ find py/ leon-common/ extmod/ -type f -iname '*.[ch]' -exec grep -ow 'rtems_[a-zA-Z0-9_-]*' '{}' ';' | sort -u | grep -v _obj
rtems_attribute
rtems_build_name
rtems_clock_get
rtems_clock_time_value
rtems_id
rtems_interval
rtems_message_queue_broadcast
rtems_message_queue_create