
PYTHON — Creating A New Python Package
The Internet is becoming the town square for the global village of tomorrow. — Bill Gates
Insights in this article were refined using prompt engineering methods.

PYTHON — Migrating Sqlite With Python
# Creating a New Python Package
In this tutorial, you’ll learn how to create a new Python package that you can later publish to PyPI. The package consists of five files: config.txt, __main__.py, __init__.py, feed.py, and viewer.py. I'll guide you through each of these files and explain their purpose.
Let’s start by creating a new folder for your package. Inside this folder, you’ll create the following files: config.txt, __main__.py, __init__.py, feed.py, and viewer.py.
config.txt
# config.txt
[feed]
url = https://realpython.com/atom.xmlThe config.txt file is a configuration file used to specify the URL of the feed of Real Python tutorials. It contains key-value pairs separated into sections, with this particular file containing only one section (feed) and one key (url).
main.py
# __main__.py
from configparser import ConfigParser
from importlib import resources # Python 3.7+
import sys
from reader import feed
from reader import viewer
def main():
"""Read the Real Python article feed"""
cfg = ConfigParser()
cfg.read_string(resources.read_text("reader", "config.txt"))
url = cfg.get("feed", "url")
if len(sys.argv) > 1:
article = feed.get_article(url, sys.argv[1])
viewer.show(article)
else:
site = feed.get_site(url)
titles = feed.get_titles(url)
viewer.show_list(site, titles)
if __name__ == "__main__":
main()The __main__.py file acts as the entry point of the program and takes care of the main flow, calling other parts as needed. It reads the URL of the Real Python feed from the config file and then either shows a specific article or lists all articles based on the command-line arguments.
init.py
# __init__.py
__version__ = "1.0.0"The __init__.py file represents the root of your package. It holds the __version__ variable, which is a convention in Python for adding version numbers to your package.
feed.py
# feed.py
import feedparser
import html2text
_CACHED_FEEDS = dict()
def _feed(url):
"""Only read a feed once, by caching its contents"""
if url not in _CACHED_FEEDS:
_CACHED_FEEDS[url] = feedparser.parse(url)
return _CACHED_FEEDS[url]
def get_site(url):
"""Get name and link to web site of the feed"""
info = _feed(url).feed
return f"{info.title} ({info.link})"
def get_titles(url):
"""List titles in feed"""
articles = _feed(url).entries
return [a.title for a in articles]
def get_article(url, article_id):
"""Get article from feed with the given ID"""
articles = _feed(url).entries
article = articles[int(article_id)]
html = article.content[0].value
text = html2text.html2text(html)
return f"# {article.title}\n\n{text}"The feed.py file contains functions for reading from a web feed and parsing the result. It interacts with external libraries like feedparser and html2text to achieve this.
viewer.py
# viewer.py
def show(article):
"""Show one article"""
print(article)
def show_list(site, titles):
"""Show list of articles"""
print(f"The latest tutorials from {site}")
for article_id, title in enumerate(titles):
print(f"{article_id:>3} {title}")The viewer.py file contains functions for displaying articles and lists of articles. It provides a simple interface for printing the data to the terminal.
Now that you’ve set up the base structure for your package, you can add some configuration to prepare your package for publication. This tutorial demonstrates how to create a package from scratch, including the necessary files and their content. Happy coding!

