Human as runtime
Let’s bring “cursed” programming to a whole new level.
So I had a tedious workload : I had to extract some numbers from a webpage and do some basic math to get some statistics.
I call it tedious because the workload is too heavy to manually read the numbers and type them in a calculator and get the results, yet too little to worth a dedicated script (there were like 20 entries).
Here is a over-simplified1 example: if you copy a line from the webpage, it looks like this (note that “something” has a pattern but not necessarily all the same)
<NAME OF THE ENTRY> something <NUMBER 1> something <NUMBER 2> something
say you want to get the name, get the two numbers, calculate the ratio of the numbers and append it to the name.
Path not taken - a script (that I was too lazy to do):
- read each line
- extract the info from line with regex
- do the calculation and repeat.
But I don’t want to write a script that read file line by line, and I don’t want to tweak regex so that it works.
Meet the semi interactive
I opened wtf.py
and directly pasted the input into the editor, why do file io
when you can paste the input directly into the code?
Yeah, errors, errors. “Hey, wrap it in a string or an array of strings” – you are too naive!
Oh.I forgot to mention I’m using vim. Come on, everyone should use vim. So I proceed to record a macro, to turn one line from
<NAME OF THE ENTRY> something <NUMBER 1> something <NUMBER 2> something
to
name = <NAME OF ENTRY>
a = <NUMBER 1>
b = <NUMBER 2>
Trust me, this is trivial with vim macro. Also with macro, you can put a print
under each chunk.
So repeat the macro and the file becomes this:
Now, the input data turned into a script that handles it self! With this semi-script I got the data I want in the right format…
§ Moral???
Well, fuck.
-
Anyone who knows basic regex can sketch a script to extract the numbers, and you would disagree with my claim that “it doesn’t worth a dedicated script”. Well, this example is overly simplified here only to showcase what I’ve done. Don’t take it too seriously.. (actually you shouldn’t take this post seriously) ↩︎
/post/human_as_runtime