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?

1
2
3
4
<NAME OF THE ENTRY> something <NUMBER 1> something <NUMBER 2> something
<NAME OF THE ENTRY> something <NUMBER 3> something <NUMBER 4> something
<NAME OF THE ENTRY> something <NUMBER 5> something <NUMBER 6> something
...

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
name="foo"
a=424242
b=12356612
print(name,a/b)

name="bar"
a=42412122
b=12351209806612
print(name,a/b)

name="boo"
a=4242019242
b=1235600612
print(name,a/b)

# ...

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.


  1. 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) ↩︎

edited 06.10.2023
created 05.10.2023
EOF
[+] click to leave a comment [+]
the comment system on this blog works via email. The button
below will generate a mailto: link based on this page's url 
and invoke your email client - please edit the comment there!

[optional] even better, encrypt the email with my public key

- don't modify the subject field
- specify a nickname, otherwise your comment will be shown as   
  anonymous
- your email address will not be disclosed
- you agree that the comment is to be made public.
- to take down a comment, send the request via email.

>> SEND COMMENT <<