Programmatically Explore Code Snippets of Many Languages Using Python

Programmatically Explore Code Snippets of Many Languages Using Python Featured Image

Yet another library has come out of the Sample Programs repo. This one is called Subete, and you can use it to browse the Sample Programs repo in Python.

Table of Contents

Introducing Subete

Many of you are probably already familiar with the Sample Programs repo, a collection of code snippets in as many programming languages as possible. Well since then, many projects have come out of that. For example, we developed a a Python library called SnakeMD which allows you to programmatically generate markdown using Python. No surprise another library that fell out of the Sample Programs repo is Subete.

SubeteOpens in a new tab. is a Python library which allows you to browse all of the code snippets in the Sample Programs repo. To put that into perspective, at the time of writing, the Sample Programs repo contained 603 code snippets across 162 programming languages. All of which can be browsed in Python using Subete.

The library works by installing the Sample Programs repo in a temporary directory where the files can be processed into a set of data structures. These data structures can then be traversed to your needs. For example, you might have a website where you want to display some code snippets. Subete can help with that.

How to Use Subete

If Subete has piqued your interest in any way, I recommend downloading it and giving it a try. To do that, you can use pip:

pip install subete

With subete installed, all that’s left is to import it in a script:

import subete

Of course, if you actually want to use Subete, you need to take advantage of the load() function:

repo = subete.load()

With the repo loaded, you have access to any language that you can find in the Sample Programs repo. For example, if you want to poke at Python code, you can get the collection as follows:

repo["Python"]

And, if you have a particular program you’d like to see, you can key into it directly:

repo["Python"]["Hello World"]

Of course, this will return an object. To get the actual code snippet, you call the code() method directly:

>>> repo["Python"]["Hello World"].code()
"print('Hello, World!')\n"

How cool is that? That said, if you didn’t want to browse the code yourself, there are a few convenience functions for pulling code snippets. For instance, there is the random_program() method which can be used to retrieve a random program from the collection:

>>> repo.random_program().code()
'class HELLO_WORLD is\n   main is\n      #OUT+"Hello, World!";\n   end;\nend;\n'

I’ll leave it up to you to figure out what language this is and what the program is doing.

Looking for Users

After creating this library, I’ve used it four times in various places. For example, you might have seen my article on how to automate a GitHub profile. In my profile, I automatically share a random code snippet from the collection once a week. In other places, I’ve used the library to help generate documentation. And once, I even used it to do some visualization.

That said, I’d love to see what other folks could do with it. Do you have any applications that might need code snippets on the fly? I’d love to see if anyone could make some use for it. If not, maybe you have some use for some of my other libraries:

Otherwise, I appreciate you taking some time to check out Subete. See you next time!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife and kid, playing Overwatch 2, Lethal Company, and Baldur's Gate 3, reading manga, watching Penguins hockey, and traveling the world.

Recent Posts