Python: a resume programming language
Tags: python, dsl
Published: 2022-12-04 07:34:21

**tl;dr: I developed a DSL in Python to write my resume with, now it is both readable and executable.**

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mb0cztgnps2f896ughnk.png" alt="a man at a wooden desk with folded arms and a displeased look">
  <figcaption>This resume cannot possibly work</figcaption>
</figure>


_Boring_. That's what I thought when I read it a few weeks ago. I was contacted by a recruiter for a job opportunity and despite I turned down the offer I took a look at my resume to see if it was still in touch with the times (note to boss: don't worry, I don't plan to leave the boat anytime soon).

Well, boring was an euphemism. It could have been written as a restaurant menu, the vibes were the same. I felt that if that document was sent it would never make me look good as a job applicant, even if the recruiter was more interested to know my job history instead of dwell on the formatting details.

The first thing it needed was a graphical restyling, its layout was based on a template popped out of some obscure site many years ago and it showed all its age, so after a short search (nowadays there's plenty of resources on internet on how to write a good resume) I had a nice looking template to start my rewrite.

As soon as the editing went on my enthusiasm progressively decreased, although the document was getting a pleasant appearance.

Sure, it was rewritten in a more modern and coherent way, it was more "achievement oriented" and so on, but in the end it was still a mere pdf file and, most importantly, _it was still boring to me_.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3uevoa5jhchzv5lnrfz.jpg" alt="a man at a wooden desk, facepalm in desperation">
  <figcaption>Nope, telepathy doesn't work either</figcaption>
</figure>


My idea of a remarkable resume was set aside after a few days of unsatisfying researches and experimentation, and I was looking at a very interesting Python library (https://diagrams.mingrammer.com/) whose claim is that one could "draw a cloud system architecture in Python code", when something clicked: _why shouldn't my resume be written as code as well_?

Under the hood Diagrams makes use of [runtime contexts](https://docs.python.org/3/reference/datamodel.html#context-managers) and [context variables](https://docs.python.org/3/library/contextvars.html) to implement its DSL, so I borrowed the idea and started to write my own context managers. It turned out that I needed only one base class: any other class representing a document component would have been a specialization of this one.

As a document is nothing but a _tree_ when it comes to its structure every portion in it can be ultimately represented by an implementation of this base node class. By taking advantage of its recursive nature one can easily define nested higher concepts such as Document, Section or Paragraph.

So, after a few iterations, I came up with my first resume as code which was something like this (the names have been changed to protect the innocent :) )

{% embed https://gist.github.com/manuelvio/bac4682ce6929210555a158a05d6987c %}

My work could have stopped here, with a bunch of hollow classes just to keep the syntax clean, and call it a day. It was real code, ant it was quite readable after all, but as you could have guessed I wasn't satisfied yet.

I wanted it to be not only executable, which it already was, but actually _useful_.

**I wanted it to render itself.**

The project already took a nonconformist path, thus an ordinary solution was simply out of the question, but the implementation had to be basic enough to be written without relying on a potentially heavy external ecosystem and just one library would have been the maximum acceptable dependency at this stage.

To keep things simple my choice fell on console output, but I knew Python has some nice libraries about formatting text in the terminal such as [rich](https://github.com/Textualize/rich) so my derived classes were refactored to implement console rendering.

Rich is another gem among Python libraries, it provides a great number of formatting tools such as (but not limited to) columns, panels, tables and trees, that can be composed to create very articulate, and colorful, layouts. I only scratched the surface of what Rich has to offer adapting the output to render tag groups, multiple columns, bulleted lists and it didn't take long before I got this result:

![Resume console output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/msn78ttwu1gn6frdfnii.png)

Now I have my resume written in real code, in a readable form, executable and self-rendering, which is what I was searching for in the first place: a non boring resume. Honestly I don't think any recruiter would actually read the code or run it, but I could still send a rendered output copy (or the good ol' pdf) if someone asks for it.

What I also got as a bonus is: a deeper knowledge of Python's context managers and a general purpose DSL class that could be extended and used in a number of cases beyond resumes.

The first revision of the base class is shown below, I'm thinking if it's worth creating a library around it given it's just a class after all. Maybe I could provide a couple of implementations in addition to the console one, and I already have some ideas (a few of them are, well, unusual…), I will publish them on Github eventually.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zjkavblkddp7rozmw9jg.jpg" alt="A renaissance woman with a book stares at you disappointed, facepalm">
  <figcaption>I only asked for your resume…</figcaption>
</figure>


{% embed https://gist.github.com/manuelvio/f6be718287ffa65773c3aac2000c6b55 %}