Skip to main content

treasure chest banner image

๐Ÿ๐Ÿ“ฆ Python Gists Importing

Static Badge Python Poetry Ruff

Project Status: Active โ€“ The project has reached a stable, usable state and is being actively developed. Static Badge MIT

๐Ÿ“ฆ Usage โ€ข ๐Ÿ Installation โ€ข ๐Ÿ’ก Motivation โ€ข ๐Ÿ†™ Publishing a Gist

๐Ÿ“ฆ Usage

This package provides a very convenient way to import GitHub gists directly from Python:

from gists import <gist_id>

The first time a gist is imported this way, it will be downloaded using GitHub's API and saved locally as gists/<gist_id>.py (The description of the gist will be saved as gists/<gist_id>.md). Every subsequent import will not require internet access, as the local copy of the gist will be used.

๐Ÿ Installation

The package is published on PyPi, so it can be installed with pip using:

$pip install python-gists-import

๐Ÿ’ก Motivation

During my PhD, I had many ideas and felt confident to work on a few full packages. I discovered that developing packages and everything that comes with them requires a lot of effort and diverse skills including:

  • managing dependencies
  • configuring linters, formatters, task runners ...
  • setting up CI/CD, pre-commit
  • writing technical documentation
  • planning your code organization and namespaces
  • implementing your ideas
  • testing your implementation
  • choosing evocative artwork and emojis ๐Ÿ˜‰
  • choosing a license
  • preparing a repository for contributions by adhering to community guidelines

One also has to be creative and lucky in finding a short and fitting name, that is still available on PyPi.

That the motivation and energy for working on personal projects and ideas often comes in bursts, does not help either.

While writing a proper Python package is without a doubt the appropriate way to implement and share your moderately complex ideas, smaller ideas suffer from a few problems:

  • the package development overhead is too high
  • bundling all of your ideas into one utility package can quickly become chaotic
  • how do you properly name and advertise a package consisting of a single class or function?

GitHub gists provide a lightweight deployment alternative to building and publishing a package on PyPi. The user experience of creating and managing them is as easy as it can get (amazing work there ๐Ÿ’–). Another big advantage is the guaranteed collision-free (though arguably un-relatable) "package name", you get from the gist id.

No one stops you from just downloading a gist manually and placing it in your codebase, but there are other alternatives. gist-import is a package very similar in functionality to this one (shout-out to https://github.com/matteoferla for his work on the package ๐Ÿ’ช). I even use it in the background because it already contained the gist fetching logic ๐Ÿ˜…. I recently watched a PyCon talk where the Python import machinery was explained in detail, and I simply wanted to play around with the new concepts in my free time.

๐Ÿ†™ Publishing a Gist

GitHub has good documentation on how to create and manage gists ๐Ÿ“–.

I will give some additional considerations that are often overlooked.

๐Ÿ—๏ธ Licensing

When you want your gist to be used by as many people as possible, it is important to add a license to your gists.

๐Ÿคš If you want an uncomplicated license that gives your users a lot of freedom, the MIT license is a good default choice.

According to a blog post from Jeff Luszcz there are two good options:

  • state a default license in the README of your GitHub profile
  • state the license in the gist itself

Because the full license text can be quite large, which would diminish the visual appeal of shorter gists, one can also just mention the name of the license with a copyright date and the copyright owner.

I included an example of this approach in the ๐Ÿ“ Template

๐Ÿ“– Referencing

Unfortunately, getting a DOI (document object identifier) from Zenodo only works with repositories.

You can, however, include a bibitem in the description of your gist (see the ๐Ÿ“ Template):

@misc{<bibkey>,
  author={<your name>},
  title={<the title of your gist>},
  year={<year of publication>},
  url={https://gist.github.com/<user name>/<gist id>},
} 

๐Ÿ“ Template

Description

# Title

> ๐Ÿšฎ feel free to delete any section that is not helpful for your description (including this message).

Short description.

## ๐Ÿ“ฆ Usage

How to use the gist with code examples.

## ๐Ÿค” Statement of Need

Describe why the code in your gist is needed.

## ๐Ÿ Installation

This gist can be installed using the [`python-gists-import`]() package:

```sh
$pip install python-gists-import
``` 

After that, simply import the gist in your code via:

```python
from gists import <gist id>
```

### ๐Ÿ“„ Requirements

If your gist depends on other Python packages, include a list of those dependencies here. Users have to manually make sure that they have these dependencies installed. I would suggest using version specifiers used by [poetry](https://python-poetry.org/docs/dependency-specification/):

```toml
python = "^3.11"
```

## ๐Ÿ—๏ธ License and Copyright ยฉ๏ธ

The code inside this gist is licensed under the [MIT](https://opensource.org/license/mit) license.

Copyright <YEAR> <COPYRIGHT HOLDER>

## ๐Ÿ“ Cite Me

```bibtex
@misc{<bibkey>,
  author={<your name>},
  title={<the title of your gist>},
  year={<year of publication>},
  url={https://gist.github.com/<user name>/<gist id>},
}
```

Code

"""# module title

module description.
"""
# Copyright <YEAR> <COPYRIGHT HOLDER>
#
# Licensed under the [MIT](https://opensource.org/license/mit) license.

def hello(name: str = "world") -> None:
  """๐Ÿ—ฃ๏ธ says hello to `name`."""
  print(f"hello, {name}!")

๐Ÿ–ผ๏ธ ยฉ๏ธ Banner Artwork Attribution

Creative Commons License
The art in the banner of this README is licensed under a Creative Commons Attribution-NonCommercial-No Derivatives Works 3.0 License. It was made by sephiroth-art. Check out his beautiful artwork โค๏ธ

Metadata

Release files for python-gists-import 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-gists-import 1.0.2
File Size Uploaded
python_gists_import-1.0.2.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-gists-import 1.0.2
File Interpreter ABI Platform
python_gists_import-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 14.0 kB

Release files / python_gists_import-1.0.2.tar.gz

Download URL python_gists_import-1.0.2.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6335f035d37b8ce39b185bac25ee30fc0e617a421cd0655bcbd32f4f4552c185
BLAKE2b-256 checksum
How to use checksums
2929af49adb338daf716030ec32e54eebe250c804546532829c5b334d18423d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.2 Windows/10

Release files / python_gists_import-1.0.2-py3-none-any.whl

Download URL python_gists_import-1.0.2-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
36c60c4373b33cc5cbe79c9239d10d6a23ea45c9862139fbbd9af3525e98b19f
BLAKE2b-256 checksum
How to use checksums
637ef15ea388fdbbec0fb0667254092115cf047c88c49080917279d451d62747
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.2 Windows/10

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page