Lo-Fi Python

Jan 22, 2023

6 of the Hottest Python Libraries in 2023

Here's a short list of Python packages making a splash this year:

  • pyscript: execute Python code in your web browser
  • ruff: code linting package built with Rust and Python
  • FastAPI: a rapidly adopted web framework for building APIs
  • polars: high performance pandas drop in replacement, also built with Rust and Python
  • buzz: Python package that accesses the Whisper API's text transcription of audio
  • tomllib: new in the Python 3.11 standard library. It's quietly picking up steam amongst Python developers. Tom's Obvious Minimal Language, TOML, "a config file format for humans"
Python + Rust logos

Python + Rust = High Performance

A common theme I've noticed is the emergence of Rust as a performance complement to Python code. It seems we can expect modularity between the two languages to strengthen their collective abilities.

example polars dataframe code

example polars dataframe code

Python is still growing and evolving.

It's great to see. Including the ability to parse TOML config files in the standard library is a vote of confidence by the Python community. I will now seek to use TOML in my own projects.

Web frameworks and web browsers are king.

FastAPI is the new option in a space dominated by Flask and Django. Web frameworks allow developers to quickly create websites.

Running Python in a browser has been a movement in recent years. First Pyodide, WASM euphoria, and now a Python library that takes Python in a browser to new levels. Excited to see where these projects go and what new buzzy libraries will emerge this year!

pyscript landing page

Jan 08, 2023

pymarketer: an HTTP + Spreadsheet Wrangling Python package

Typically, this blog reviews the other Python libraries in its vast ecosystem. This time, it's my own package I made for fun, pymarketer. This was created in a single day and can be installed from the Github repo. Have a go at my most read post if you need help with pip.

Install with pip from the source Github repo:

python -m pip install git+https://github.com/erickbytes/pymarketer.git

The pymarketer package helps you do things like:

  • merging all the tabs of an Excel file into one CSV
  • generate HTTP code
  • make a word cloud image
  • splitting a CSV
  • merging CSVs

Generating a Word Cloud with the pymarketer Package** via wordcloud

1
2
3
4
5
6
7
8
import pandas as pd
import pymarketer as pm

xl = "Chicago Breweries.xlsx"
df = pd.read_excel(xl)
# Make a wordcloud from a pandas dataframe.
wordcloud = pm.word_cloud(df)
wordcloud.to_file("Text Word Cloud Visualization.jpg")
Python wordcloud example

This package relied on several Python libraries to complete:

I'll likely expand on this in the future. Anyone who wrangles data might be able to apply this package to good profit. At minimum, you might find it interesting to take a look at the project's __init__.py to see how some of the functions are implemented.

Additional Resources

Dec 04, 2022

An Example Pelican + Git Blog Post Workflow

In my Ubuntu Linux environment, I'm now publishing new blog posts following this Pelican + Python + git workflow.

  1. Activate the Python environment:
# Create with a virtual env with venv: python -m venv env_name
source env_name/bin/activate
  1. Clone your repo and go to the project folder:
git clone https://github.com/erickbytes/lofipython.git && cd lofipython && ls
  1. Run Python script to create new markdown or .rst file from a template:
python new_post.py
  1. Compile the new post with the Pelican content command:
pelican content
  1. Use the Pelican listen command to serve blog to the default port 8000:
pelican -l
  1. Preview your new post at localhost:8000 in Firefox:
firefox -new-tab http://127.0.0.1:8000
  1. Use git to stage, commit and push the files to a Github repo:
git add .
git commit -m "new post edits and fixes"
git push -u origin main

The new blog post is now live! This is my own workflow for my Pelican blog, this blog which is hosted for free with Cloudflare Pages. You can read more about connecting Pelican and Cloudflare in this past post I wrote.


Github SSH Required

You will need to create a SSH key and connect it to your Github account to get this working, as it's required by Github now. Make sure you write down your passphrase. I was able to create an ssh key with this command:

ssh-keygen -t ed25519 -C "yourname@example.com"

Scripting New Post Creation

Below is the short Python script I wrote for generating the markdown file for a new post, "new_post.py" in the above workflow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from datetime import date


def post_name():
    """Ask for the new post's name."""
    name = input("Enter the new post's title:\n")
    return name


def post_template(title):
    """Return str, post draft"""
    post = f"Title: {title} \nDate: {date.today()} 4:20 \nCategory: Essay"
    return post


def save_draft(name, post):
    """Save new post draft to content folder."""
    content = "~/projects/lofipython/content"
    name = name.replace(" ", "-")
    md = f"{content}/{name}.md"
    with open(md, "w") as fhand:
        fhand.write(post)
    return None


name = post_name()
post = post_template(name)
save_draft(name, post)

I've enjoyed working this out on my new blog. I can easily edit, improve and fire off blog posts rapidly with this command line workflow.

Oct 18, 2022

How to Find and Open a Public Wi-Fi Login Page

Sometimes, we find ourselves on a public Wi-Fi connection that is "connected", but unable to get to the login page. While the login page is triggered automatically on most mobile devices, computers may not connect so easily. As usual, I turned to Google to find a way to get to a public Wi-Fi login page.

This helpful Zapier blog post outlines a few different approaches you can take. My solution below is one of the IP addresses they suggested trying. Decide if you need to edit your computer's network settings. Alternatively, you may just need to find the public login page. I was able to load my public Wi-Fi login page with this IP address in my web browser:

192.168.1.11
web browser navigation bar

This is a fun little trick to keep in mind if you're having difficulty connecting to public Wi-Fi. Problem solved!

Oct 10, 2022

How to Check Github Repo Star Counts With Python

Snooping through my package list, I noticed the PyGithub library was installed. Its repo boasts "Typed interactions with the GitHub API v3". I googled the package, wanting to check in on the repos I profiled in an earlier post about static site generators.

I drafted the code below after noticing the repo.stargazer_count function in its documentation. This is neat to have if you want to keep tabs on a batch of repos, instead of tediously checking the Github web interface! If you're new to Github, the trending page is an easy way to find new, interesting repos.

Getting Started

  1. You'll need to create a personal access token for your Github account. See the Github docs, "Creating a personal access token".
  2. Install PyGithub and pandas:
pip install PyGithub
pip install pandas
  1. Run the below code as a Python script.
python github_stars.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import pandas as pd
from github import Github


def stars(repo, g):
    """Retrieve github repo star count.
    Accepts: str, repo "username/repo name",ex: "getpelican/pelican"
    Returns: int, github repo stargazers number"""
    repo = g.get_repo(repo)
    return repo.stargazers_count


# static site repos: http://lofipython.com/a-brief-summary-of-promising-python-static-site-generators/
urls = [
    "https://github.com/getpelican/pelican",
    "https://github.com/lektor/lektor",
    "https://github.com/eudicots/Cactus",
    "https://github.com/getnikola/nikola",
    "https://github.com/sunainapai/makesite",
    "https://github.com/hyde/hyde",
    "https://github.com/Anomareh/mynt",
    "https://github.com/staticjinja/staticjinja",
]
repos = [url.replace("https://github.com/", "") for url in urls]
g = Github("access_token")
counts = [(repo, stars(repo, g)) for repo in repos]
stars_df = pd.DataFrame(counts, columns=["repo","stars"])
stars_df.to_csv("Stars.csv", index=False)

On Linux, I was able to check the results of the CSV with the cat command:

View repo github stars with Python

I confirmed the API was accurate against the web interface in pelican's repo!

Github browser UX

Github Repo Stargazer API Reference

Oct 08, 2022

Pelican Fix for "No valid files found in content"...

Pelican is a popular static site generator library in Python. I didn't know why my pelican blog was not working. I've used the "pelican content" command many times for my blog. This time, when attempting to write a new post on a new computer, I was getting this error where none of my posts were visible to Pelican:

1
2
3
4
5
WARNING  No valid files found in content for the active         log.py:91
         readers:
           | BaseReader (static)
           | HTMLReader (htm, html)
           | RstReader (rst)
no valid files found in pelican content

Solution

Install the markdown library, which is stated in the pelican docs. This Github issue also provides some background on this warning.

pip install markdown
python pip installing markdown

Now my "pelican content" command works!

pelican content
successful pelican content build

Sep 15, 2022

Retrieve Random Numbers Via Python's random Module + range() Built-in

There are usually many ways to do most things in Python. I've retrieved random numbers a few different ways at various times within the random module, often after reading a Stack Overflow post. This time in my most recent search for random digits, I discovered in the Python docs the random.sample() function with its k parameter:

Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

https://docs.python.org/3/library/random.html#random.sample

When combined with the range() built-in, it makes doing this easy. Being able to specify a length and return a list of random numbers is mighty convenient. This function seems a Pythonic way to randomize to me. Have a look!

1
2
3
4
5
6
7
import random
# Returns a list of 5 random numbers.
numbers = random.sample(range(10000000), k=5)
print(numbers)
# Returns a single random number.
number = random.sample(10000000), k=1)[0]
print(number)
Python Random Number Code

To choose a sample from a range of integers, use a range() object as an argument.

"This is especially fast and space efficient for sampling from a large population":

1
sample(range(10000000), k=60)

- Python Docs, https://docs.python.org/3/library/random.html#random.sample

Sep 07, 2022

Beyond the Standard Library, An External Python Modules Starter Pack

Here is a list of external Python modules you can install with pip. These are not included with your Python version and need to be installed separately. In some cases, other options may be suitable. To keep the list shorter, I picked libraries that were chosen based on having at least one of these qualities:

  • most obvious library for its category
  • a library I have experience using effectively
  • provide a niche, useful functionality or tool

In alphabetical order:

  1. black: code formatting
  2. chardet: detect file encoding (useful for reading CSVs)
  3. diagrams: make cloud architecture diagrams
  4. Django: web framework
  5. fastapi: web framework for building APIs
  6. ffn: financial function library
  7. Flask: web framework
  8. ftfy: fix mojibake in text
  9. fuzzywuzzy: fuzzy matching library
  10. matplotlib: data visualization
  11. numpy: array operations
  12. openpyxl: foundational Excel library
  13. pandas: working with tabular data
  14. pelican: static site generator
  15. psutil: process and system utilities
  16. pygame: video game creation
  17. pyodbc: Open Database Connection client
  18. py-spy: sampling profiler to visualize your program
  19. pyheat: visualize code bottlenecks
  20. pytest: testing framework
  21. pytrends: research trends in the Google Trends API
  22. pytube3: YouTube video downloading library
  23. pywin32: Python for Win32 Extensions
  24. requests: HTTP requests
  25. scikit-learn: machine learning
  26. soltrack: calculate the location of the sun
  27. sqlfluff: SQL linter
  28. streamlit: build data apps
  29. tqdm: add a progress bar to loops
  30. wxPython: Graphical User Interface
  31. xlrd: supplementary Excel library
  32. xmltodict: convert XML to nested Python dicts

Python Documentation: Installing Python Modules

python pip install shell

Aug 14, 2022

Recycling Old Electronics in Chicago

Though we feel powerless at times, our individual choices can have ripple effects which can help us preserve Earth's precious materials. Here is how to recycle your old electronics in Chicago. Definitely check out if your city has a similar program for recycling electronics.

Look up the schedule for any Chicago address here: https://www.recyclebycity.com/chicago/schedule

Chicago Recycling Schedule Website

I was able to look up my address, see the recycling pickup schedule for my street, and see collection events for old electronics and waste. I scheduled an email reminder for the day before an upcoming event near me. I have a graveyard of broken phones and a busted Macbook to dispose of.

Sign up for a reminder to recycle.

Our recycling program also accepts nail polish, chemicals, paints, auto fluids, pharmaceuticals and "Misc. Hazardous Waste". I learned today that the city says alkaline batteries are ok to throw away in the trash since they no longer contain Mercury like they did in the '90s.

The website also has an option for Spanish language speakers. You can sign up for email updates on the city's recycling program at the bottom of their website. Overall, I give the city's recycling service an A+ for its accessibility. Happy to see a wealth of resources available to re-purpose our planet's resources. If we all choose to care in our day to day lives, we can make a better future for generations to come.

Here's what to take:
• All computer-related equipment (monitors, mice, hard drives, CPU’s, computer cables, keyboards, laptops, etc.)
• Cell phones
• Fax machines
• Scanners
• Printers/copiers
• Televisions
• DVD players
• VHS players
• MP3/digital music players
• PDAs
• Video game consoles
• Zip drive
• Computer cables

https://www.recyclebycity.com/chicago/brief/household-chemicals-computer-recycling-facility

Additional Resources

Jul 08, 2022

Launching a Live Static Site Blog via Pelican, Github and Cloudflare Pages

Proud to announce my newest side project blog, Diversified Bullish, is live at divbull.com. It is made with Pelican and the Blue Penguin theme. I'm planning to write about stocks and investing there moving forward in addition to this blog which focuses on Python programming.

The divbull.com Github repo serves the static files generated by Pelican via Cloudflare pages. It's free, unless you purchase a domain. I purchased my .com domain with Namecheap before I learned about Cloudflare pages. I followed these instructions to set up my new financial blog. If you're interested, you can subscribe to an RSS feed here to follow when I post something new.

The dashboard provides a number of framework-specific presets. These presets provide the default build command and build output directory values for the selected framework. If you are unsure what the correct values are for this section, refer to Build configuration. If you do not need a build step, leave the Build command field blank.

https://developers.cloudflare.com/pages/get-started/

Cloudflare pages deployment details

Working in the Cloudflare pages build dashboard is sweet. It took me about 5 failed Pelican build commands to get the site to deploy. Finally, I was able to get the site build to complete by leaving the build command blank. Cloudflare was able to scoop up my Pelican "output" folder contents and render the blog. How cool. I feel like I've done the impossible, launching a passable quality blog with top shelf tools this quickly for under $10!

Initially, I spent a few hours getting to know Pelican. Once I correctly installed a theme I liked, I banged out a few philosophical financial musings to give the blog some posts. Then I had the static files generated but no clue how to serve them. Enter Cloudflare pages, a free option to host a blog.

Connecting the repo to Cloudflare pages, adding the files to the repo and finding the correct build command added a few more hours. In total, it took me about 1-2 days to make a live site since I did not know about Pelican or Cloudflare pages when I began playing with a Pelican blog in April. This was my first static site launch!

Cloudflare build settings

Generating a Blue Penguin themed Pelican blog.

showing Pelican blog workflow

Head over to divbull.com to see this Pelican, Github and Cloudflare pages stack in action.

Like static site generators? Check out this post about static site generator libraries in Python.
← Previous Next → Page 4 of 13