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.
Subete 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:
- Practice Your Coding Skills With the Sample Programs Template
- Sample Programs 500 Code Snippet Release
- Image Titler 2.2.0 Features Preloaded GUI Settings
Otherwise, I appreciate you taking some time to check out Subete. See you next time!
Recent Posts
It's a special day when I cover a Java topic. In this one, we're talking about Enums, and the problem(s) they are intended to solve.
Chances are, if you're reading this article, you've written some Python code and you're wondering how to automate the testing process. Lucky for you, this article covers the concept of unit testing...