Treating JSON as Python

It just occured to me that JSON (with some exceptions) can be a subset of Python, and parsed as such without the help of json module.

Say, if we have a sample JSON, which includes all of the value types (object/array/string/numbers/true/false/null):

  "numbers": [123, 123.456],
  "booleans": [true, false],
  "null": null,
  "string": "string"

we can parse it using eval, by complementing null/true/false with corresponding variables:

# `sample` is a string representation of the above example
eval(sample, {}, {'null': None, 'true': True, 'false': False})

which results in a proper Python object:

{'booleans': [True, False],
 'null': None,
 'numbers': [123, 123.456],
 'string': 'string'}

This, of course, doesn't handle certain cases, like string escape sequences. And the solution overall isn't safe, and shouldn't be used in a production code. Nevertheless, I find the trick interesting.

December 15, 2020

Writing an eye-catching quine in Go

I wrote a code in Go. It's fixed-width, nicely commented and when run, outputs it's own source code:

package main;import("fmt";"strings";"regexp");func main(){ x:=(
`/* ---------------------------------------------------------`+
`-*/No:=""; P:=func(x interface{}) {o += fmt.Sprint(x)};P(Qpa`+
`ckageQ+N  Q main;import("fmt";"strings";"regexp");func main(`+
`){ x:=(Q)N  for i,ch:=range x {; if i%60==0 { P("\nQ") }; P(`+
`string(ch))N if i==len(x)-1 {P("Q)")}else if i%60==59 {P("Q+`+
`")} }; P("\n")Na:=strings.Replace(x,string(78),string(10),-1`+
`) /* newline \n */Nb:=strings.Replace(a,string(84),string(9)`+
`,-1)  /* tab     \t */Nc:=strings.Replace(b,string(81),strin`+
`g(96),-1) /* quote   \Q */NP(c); r:=regexp.MustCompile(Q(\x6`+
`b)Q); Replace :=r.ReplaceAllStringFuncNfmt.Print (Replace (o`+
`, func (x string) string {  switch x[0]  {Ncase 0x22: return`+
` "\x1b[36m"+x+"\x1b[0m"; /* "double-quoted" */Ncase 0x60: re`+
`turn "\x1b[36m"+x+"\x1b[0m"; /*  Qback-quotedQ  */Ncase 0x2f`+
`: return "\x1b[90m"+x+"\x1b[0m"; /* general comment */Ndefau`+
`lt: return "\x1b[0;1m"+x+"\x1b[0m"} })); /* keyword    */}N`)
/* ----------------------------------------------------------*/
o:=""; P:=func(x interface{}) {o += fmt.Sprint(x)};P(`package`+
  ` main;import("fmt";"strings";"regexp");func main(){ x:=(`)
  for i,ch:=range x {; if i%60==0 { P("\n`") }; P(string(ch))
 if i==len(x)-1 {P("`)")}else if i%60==59 {P("`+")} }; P("\n")
a:=strings.Replace(x,string(78),string(10),-1) /* newline \n */
b:=strings.Replace(a,string(84),string(9),-1)  /* tab     \t */
c:=strings.Replace(b,string(81),string(96),-1) /* quote   \` */
P(c); r:=regexp.MustCompile(`(\x60.*?\x60|\x22[^\n"]*\x22|/\*`+
`tch|case|return|default\b)`); Replace :=r.ReplaceAllStringFunc
fmt.Print (Replace (o, func (x string) string {  switch x[0]  {
case 0x22: return "\x1b[36m"+x+"\x1b[0m"; /* "double-quoted" */
case 0x60: return "\x1b[36m"+x+"\x1b[0m"; /*  `back-quoted`  */
case 0x2f: return "\x1b[90m"+x+"\x1b[0m"; /* general comment */
default: return "\x1b[0;1m"+x+"\x1b[0m"} })); /* keyword    */}

Right, forgot to mention - the output is syntax highlighted. Here's how it actually looks like:



It's quite simple. First you write a quine. Then you add syntax highlighting feature to it.

OK, a bit more verbose explanation:

  1. Write a quine of the form:
p1: initial boilerplate: package name, imports etc.
p2: declare a variable `x` that contains the code after itself
p3: print statement that outputs the initial boilerplate
p4: code that outputs `x`'s value representation
p5: print statement that outputs the value of `x`

The tricky part is updating the value of x in p2. Luckily, it can be automated by an external script that reads the code starting from p3 (/* --- in my case) and outputs the necessary value. The script's output is copy-pasted back into the quine once p3, p4 & p5 is polished.

I also encode certain characters that require character escaping, which will be decoded before p5. This way I don't have to deal with them in p4.

  1. Update the quine to store it's own value in a certain variable.

In the code above, it's o. This way the quine becomes "self-aware", knowing what it's source will look like prior to printing.

  1. Add ANSI escape sequences between certain terms.

I like to keep things simple, so my colorscheme only highlights keywords with bold, strings with cyan and comments with low-contrast colour.

For that, I used a hand-crafted regexp that matches certain code parts. Here's the breakdown:

\x60.*?\x60                        <- `back-quoted strings`
\x22[^\n"]*\x22                    <- "double-quoted string"
/\*.*?\*/                          <- /* general comments */
\bpackage|import|func|and|etc\b    <- reserved keywords

The matches are then wrapped in ANSI codes:

"\x1b[36m"  + x + "\x1b[0m" // cyan
"\x1b[90m"  + x + "\x1b[0m" // dark grey
"\x1b[0;1m" + x + "\x1b[0m" // bold
September 28, 2020 · code

Writing a triple polyglot script

In the previous post I've described how to write a script that's both valid Ruby and Python. Then I came up with the idea of adding Perl on top if it.

So, the basic form of the polyglot code (with slight modifications) is this:

(0 and eval("puts 1") or 1) or eval(compile("print(2)", "", "exec"))

Now, if I could find a value that sets Perl apart from Ruby & Python and add it to the expression above, I'd be able to run some code via eval. Luckily, quick google search revealed that "0" is falsey in Perl. After a bit of tinkering I've managed to construct something like this:

(0 and (eval("puts 1") or 1)) or ("0" and (eval(compile("print(2)", "", "exec")) or 1)) or eval("print 3;")

That's quite a lot of brackets to make non-lisper's eyes bleed, so here's the brief summary of what's going on:

(0 and (<ruby_expr> or 1))
  or ("0" and (<python_expr> or 1))
    or <perl_expr>

making code "readable"

Again, this is still not enough for me, since I wanted placeholders that would let me write more "readable" code inside eval calls (read: multiline strings).

And this is where I encountered a small problem. Python supports multiline strings only via triple-quotes. Perl (unlike Ruby) raises syntax error on that. So I needed some workaround.

The solution was to use q "function" in Perl:

"""), 3, -3)

Syntax is still valid in 3 languages. But there's a catch - q function does not exist in Ruby and Python, but defining it should be easy using the same techniques described earlier:

(0 and eval("def q(x) x end", binding)) or ("0" and (eval(compile("def q(x):\n return x", "", "exec")) or 1));

Now we can wrap the rest of the strings in q identity function, and we're done.

creating a "practical" application

I didn't spend so much time just for nothing, so here's a working fizzbuzz example:

(0 and eval("def q(x) x end", binding)) or ("0" and (eval(compile("def q(x):\n return x", "", "exec")) or 1));

(0 and (eval(q("""
1.upto(100){|i|puts ('fizzbuzz '[n=i**4%-15,n+13] || i.to_s).rstrip}
""")) or 1)) or ("0" and (eval(compile(q("""
for i in range(1,101): print('fizz'*(i%3==0) + 'buzz'*(i%5==0) or i)
"""), "", "exec")) or 1)) or eval(substr(q('''
print "fizz"x!($_ % 3) . "buzz"x!($_ % 5) || $_ , "\n" for 1 .. 100;
'''), 3, -3))


January 28, 2020 · code

Writing a polyglot script

So here's the thing - I'm relatively good at Python. I somewhat know Ruby. Not good enough to write something complex, but enough to understand that it's "a bit" different from Python. Yet both of the languages have similar syntaxes, which made me think whether I could come up with a script that's valid in both languages.

The shortest valid script could be:

print("hello world")

I thought that was too easy, so I set a goal to write a script that would print the exact language used. After a bit of tinkering, I came up with this:

print(["ruby", "python"][(0 or 1)])

I've remembered that one of the main surprises the developers who switched from one language to another encountered was the fact that 0 is falsey in Python, whereas in Ruby it is truthy. So the expression 0 or 1 evaluates to 0 and 1 in Ruby and Python, accordingly. And if we use just that to get the value at the corresponding index of the specially crafted list, we can achieve the given task. As a side note, we would need to enclose the boolean expression in parentheses, because otherwise it'll throw a syntax error in Ruby.

I could have settled here, but I decided to raise the bar higher. What if I try to come up with a script that could do more sophisticated things than just printing some values? Now that's where things get more complicated, since you can't just do anything in one languages that would be also correct in another. But I've finally managed to come up with a somewhat decent solution with a few neat tricks:

(0 and eval("""
3.times {
  puts 'ruby code goes here'
""")) or eval(compile("""
for i in range(3):
    print('python code goes here')
""", "", "exec"))

So what's happening in here:

  1. The code above is a single statement of the form (0 and <expr1>) or <expr2>. Both Python and Ruby support short-circuiting for boolean expressions, therefore expr1 evaluates in Ruby and expr2 in Python only.
  2. It heavily relies on metaprogramming, by evaluating only a single function that could contain more complex code inside a string. Ruby's eval needs to have a truthy value as the last statement, which is returned as the function's value. If we fail to do so, Ruby will try to execute expr2. For Python we could have used expr, but I realised that it's not available in Python 2 (which had not reached the EOL at the time when I was experimenting, so as a well-mannered Pythonista I had to support it as well).
  3. Using triple-quote strings is a nice addition to write more readable code without providing escape sequences for newlines where necessary. In Python it's the only way to create multiline strings. Ruby, on the other hand, supports multiline strings by default with single and double quoted strings, and treats """foo""" as three separate strings "" "foo" "" which then get concatenated to one. This, however, makes using double (or single) quote characters tricky in the code placeholders.
January 08, 2020 · code

Writing an ouroboros quine

Smart people write C. Pragmatic people write Go. Only a fool would write C code that outputs Go code that outputs the original C code.

So I made one. Below is the code:

#include <stdio.h>
int main(){
char*c="#include <stdio.h>%cint main(){%cchar*c=%c%s%c,o[1000],g[1000];%csprintf(g,%cpackage main;import%ccfmt%cc;func main(){fmt.Print(%cc%ccs%cc)}%c,34,34,96,37,96);%csprintf(o,c,10,10,34,c,34,10,34,37,37,37,37,37,34,10,10,10,10,10);%cfprintf(stdout,g,o);%creturn 0;%c}%c",o[1000],g[1000];
sprintf(g,"package main;import%cfmt%c;func main(){fmt.Print(%c%cs%c)}",34,34,96,37,96);
return 0;

If you run it, you should get the result below:

$ cc ouroboros.c -o ouroboros
$ ./ouroboros > ouroboros.go
$ go run ouroboros.go > ouroboros2.c
$ diff ouroboros.c ouroboros2.c && cowsay "wtf?"
< wtf? >
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||


One of the simplest quines in C looks like this:

#include <stdio.h>
int main(){
char*c="#include <stdio.h>%cint main(){%cchar*c=%c%s%c;%cprintf(c,10,10,34,c,34,10,10,10,10);%creturn 0;%c}%c";
return 0;

I'm not going to dive into the details - there's a detailed explanation on the tricks and examples for writing quines available in Rosetta Code's wiki page.

Since I wasn't going to waste my time hand-crafting the string that should contain the original source code, I wrote Python code which generates the C code above:

quine = r'''#include <stdio.h>
int main(){
return 0;

quinestr = r''
args = []
for i, ch in enumerate(quine):
    if ch in '\n"':
        quinestr += '%c'
    elif ch == '?':
        quinestr += '%s'
        quinestr += ch

args = ','.join(map(str, args))

                .replace('?', '%s')\
                .replace('!', args))\
        .replace('!', args),

To go further than just a simple quine, I had to make the quine self-aware, storing the output string in a variable. To do so I updated the template C code to the one below:

#include <stdio.h>
int main(){
fputs(o, stdout);
return 0;

The variable o now holds the value of the source code. Next, I came up with Go code snippet that should output it:

package main;import"fmt";func main(){fmt.Print(`<your_quine_here>`)})

I used back-quoted string since they can contain any characters, even newlines, which will be present in the o.

And the final tricky part - I needed to add the Go code as a C string:

#include <stdio.h>
int main(){
sprintf(g,"package main;import%cfmt%c;func main(){fmt.Print(%c%cs%c)}",34,34,96,37,96);
return 0;

The variable g now contains the template Go code. The trick here is making sure that the variable for Go code contains %s and `, but implicitly. The former needs to be used in the next sprintf call to do the substitution, and the latter can't be explicit because back-quoted strings in Go cannot contain back-quote characters.

The final Python code which outputs the C code which outputs the Go code which outputs the C... you got it, is available here.


Why not?

November 20, 2019 · code · unix

Brief history of CSS colors

I've decided to create a personal blog. After a few hours of searching for a perfect template, I went with Rosa theme. The only thing I changed (apart from minor cosmetic HTML tweaks) was the link color. I set it to steelblue, which is visible when you hover over a link. I don't have time for choosing the perfect color, because I have more important things to spend my time on.

Which is - finding out where did steelblue color come from to CSS. The search of color origins revealed a lot of interesting backstory, forgotten memes and even a tragic loss of the people behind the computing history.

History of colors in CSS

In early 90's Tim Berners-Lee publishes the first prototype of what would become WWW. Internet becomes a new hot thing, HTML becomes the language of the Web, and in 1994 Tim Berners-Lee founds W3C - organization behind standardization of the Web.

People start building websites, and one of the most requested features is document customization. Web developers want to be able to layout and add colors to their HTML. So CSS1 recommendation came in 1996 to address this. It specifies a lot of niceties, among which is ability to set colors, both using RGB codes and keywords. Color name keywords were taken from MS Windows 16 color palette. orange was added later on to CSS2.

CSS3 came around not long after. For the reasons unknown to me, 2001 draft spec added even more color keywords, this time from X11 (windowing system for Unix-like operating systems). It had a fair amount of criticism, but ends up in a spec anyways. This spec mentions steelblue for the first time.

In 2014 rebeccapurple was proposed and added to CSS4 spec, in memoriam of Eric A. Meyer's daughter Rebecca, the man who played major role in CSS advocacy.

History of colors in X11

So steelblue came from X11, it turns out. And to answer the initial question we need to go further back in history.

Xerox releases Alto in 1973 - the first computer with what would become today's GUI. During late 70's and early 80's GUI becomes a new hot thing. Microsoft and Apple present operating systems which offer GUIs.

In the meantime enthusiasts from MIT come up with X Window System in 1984. The next year X adds color support. Jim Gettys creates a list of color names, with "steelblue" among them.

Paul Raveling updates the list in 1989. Being a software engineer with a total lack of control, he ends up adding a bunch of new colors (dodgerblue?), changing the existing ones during the course. This update, which includes final "tuned" version of steelblue, ends up in CSS spec along with others' contributions.

The End.

August 10, 2019 · web · unix

Hello World

This is the first post. That's about it.

August 08, 2019