Skip to main content

A Brief Introduction to Context Free Grammars

I spent much of my afternoon and evening writing a specification in Extended Backus-Naur Form (one form of a context free grammar). While trying to convey my activities to a friend, I stumbled upon an analogy. I realized the analogy works quite well to explain in a jargon free way, that anyone could understand, what is arguably a fairly simple concept – context free grammars – but that usually gets obfuscated in formal language and technical terms.

Python (and likely many other languages) use Backus-Naur Form to describe the syntax of the language. I used it to describe a format for a specific part of the project I’m working on. What both of these use cases have in common is that context free grammars are a powerful way to compactly describe something with a lot of options.

So imagine you want to open up a design-your-own-taco shop. While thinking about the menu, you realize that there’s a ton of options, what do you do?

Read more…

String concatenation methods runtime comparison tests

/images/string_concat_runtimes.png

While working on the rendering part of Plié–my terminal user interface library–I decided test the time it took for various implementations to run. Normally I don’t worry about timing things in Python, as that seems to be the general advice, but as this would be the core of the library and called many times, it seemed wise to at least look a little into how different implementations compare.

Read more…

Python Blessed not drawing to the last line?

While working with Blessed, I stumbled across this gotcha. Where even if I would tell blessed to explicitly move the cursor to the last row, and print text there, it would always print one line above the last line/row.

print(the_text_to_print, end='')

The end='' part of that snippet is supremely important. Without it, a newline will always be printed after your text, thus bumping up what you printed by a line.

Posting this because a google search matching the title yielded no useful information. I hope this helps.

Saving state for rendering to the terminal with coroutines and curtsies

For the project I’m working on I’m using curtsies. Which although I like the coding style more than Urwid or npyscreen, curtsies requires a lot more low level creation of screen primitives.

The app I’m working on—Llat—has a terminal user interface (TUI) that’s interactive both by keyboard input and voice commands. While pondering implementation details of Llat, I realized that rendering to the terminal could be viewed as an infinite sequence of generated strings rendered upon receiving some sort of input. Something like: \(c’= f(c)\) Where \(c\) is the current context (or state), and \(f(c)\) is a function that creates a new state \(c’\) based off the input. What came out of this thought process was using a coroutine style generator to save state much like a closure or a class. This way the program can iterate through the generator, producing outputs based on the input. This could—and perhaps should—be done with a class, but I like the generator approach, and I think it will yield advantages latter.

Read more…

Nonsense words and random boards

I quite like Soybomb’s nonsense word generator. It creates words that sound like they could be real words, but are usually made up words. They come in handy for more memorable nearly random character strings. I didn’t like having to load a website to get them though, and individually copy and paste them. So in about ten minutes, and about as many lines of code, I made this script to print out a selection of randomly generated words. BeautifulSoup and requests feel like such a wonderful swiss army knife of tools to have.

While visiting my mom for a few days, we played Boggle together. Whenever I play Boggle or put together puzzles, my mind veers off on tangents of how to implement an algorithm to do it for me. This time I realized I could bang out a board generator really easily. The random module comes with the useful shuffle and choice functions. So with a list of dice, all it took was shuffling them, then choosing a random face off of each one, then using fill from the textwrap module to shape the board.

In case you’re wondering — like I was — here are some stats about the dice in Boggle:

letter count percentage of total difference
e 11 11.458% -0.562
t 9 9.375% 0.275
o 7 7.292% -0.388
a 6 6.250% -1.870
i 6 6.250% -1.060
s 6 6.250% -0.030
n 6 6.250% -0.700
h 5 5.208% -0.712
r 5 5.208% -0.812
l 4 4.167% 0.187
u 3 3.125% 0.245
d 3 3.125% -1.195
w 3 3.125% 1.035
y 3 3.125% 1.015
m 2 2.083% -0.527
v 2 2.083% 0.973
g 2 2.083% 0.053
c 2 2.083% -0.627
p 2 2.083% 0.263
b 2 2.083% 0.593
f 2 2.083% -0.217
qu 1 1.042% 0.932
j 1 1.042% 0.942
z 1 1.042% 0.972
k 1 1.042% 0.352
x 1 1.042% 0.872

The difference is in percent, from the representation in Boggle to a 40,000 word sample statistical average.

Sublime 3 Syntax highlighting for Befunge source code

While working on the /r/dailyprogrammer challenge for the day, I got sidetracked with not having any syntax highlighting while writing Befunge-98 code. After getting bogged down in a quagmire of string escape sequences through different file types. Writing regex to match befunge, in YAML that got automatically translated to XML was frustrating, but became much more straightforward when I realized I could work directly in the final XML and skip the translation step. Here’s my current syntax highlighting definition file:

media test

A test post to sort out various media hosting options. I’m unsure of whether I want to store photos locally on this EC2 instance, an S3 bucket, or link to them posted on something like flickr or 500px.

Read more…

Setting Up This Site

After listening to Podcast.__init__ Episode 20 on static site generators, I decided to switch how this is run. I don’t really know why I picked Nikola over Pelican, but something about this felt right. This site runs on an Amazon EC2 instance, and used to be served by Apache. But after reading the nginx chapter in The Architecture of Open Source Applications this week, I realized nginx is totally awesome and I want to play around with it.

Editing the configuration of Nikola proved to be pretty easy and straight forward. The CSS might still warrant some tweaking, but mostly all I did was remove author information from the posts, since it will only just be me, and I didn’t like the visual clutter. The title bothered me. Yes just the simple “Jesquik” up there. If you resize the window, at certain widths the hamburger menu on the left initially overlapped the Jesquik. At 48em the masthead title switches to being centered. To get around the overlapping problem in the title, I bumped up the margin-left to 3em. I don’t love the indented title, but I have yet to figure out how to sort out collisions with the hamburger menu in a css/template/html way. I do really like how well the Lanyon them handles resizing. Coming from a background of writing html in px and percentages, using em and rem now seems way more appealing and versatile.

Upcoming I still have to figure out how to handle images, the reStructedtext markup for them seems straight forward enough, as does the actual linking to them. Where to host the images though seems like a more nuanced issue, not from a practical standpoint, but having to weigh the pros and cons of hosting them locally on this instance or from an S3 bucket.

Learning how linear gradients work in css was a fascinating tangent. The hamburger menu is done with a stack of linear gradients. This is the relevant line of css:

background-image: linear-gradient(to bottom, #555, #555 20%, #fff 20%, #fff 40%, #555 40%, #555 60%, #fff 60%, #fff 80%, #555 80%, #555 100%);

I found that rather neat, more so than the code block specification in reStructuredText. The white space seems so fiddily important, and the documentation seems lacking of sufficient examples. Much like how the documentation on the links only lists single word links (the wikipedia page shows multiple word link text examples), and doesn’t show anything about escaping special characters in link text. That’s why only the “Episode 20” above linked, rather than the whole “Podcast.__init__ Episode 20”.

Altogether, this was a nice evening project.