Lo-Fi Python

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 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.

Update

After this post was originally written, I migrated this blog to Pelican in May 2023.

Jul 06, 2022

The Things They Don't Tell You About Ampersands and XML

In an XML document, you need to escape any ampersands in your text as &

I began a new coding project. Sure, there's documentation for the API that solves my problem. I find out it uses XML. Extensible Markup Language, a classic API format. Cool. I craft a beautiful script that works at first. Or so it seems!

Later on, I realize it doesn't work as well as I believed. It turns out, if I want a server to accept my XML document, escaping certain characters might be required. The documentation didn't mention this. It was my first time using XML, how would I know?

I noticed a script only worked for a handful of requests. It failed for most, returning a 400 status code. Suspecting the issue was likely in my payload, I studied the data of the request bodies that failed compared to the others that succeeded. All of the payload bodies that failed contained text with an ampersand.

Suspecting it might be an XML + ampersand related issue, I googled this Stack Overflow post which explains the ampersand escaping situation. There are a handful of characters that must be escaped. Otherwise, you might not be able to connect to the server.

These are the things they often don't tell you. Those little details you must sometimes realize for yourself, unless someone bothers to mention it or write it down. Now you know something that cost me an hour or two of tinkering to realize!

`Image Source <https://github.com/sichkar-valentyn/XML_files_in_Python/blob/master/example.xml>`__

Image Source

Want to read more on HTTP? Check out my guide on making HTTP requests with Python to read more about HTTP requests.

Jun 24, 2022

Hammock-Driven Development Notes

Occasionally you will find a video or talk that connects or resonates with you in a great way. Rich Hickey's "Hammock Driven Development", a self-described "rant" is packed with wisdom. I keep coming back to re-watch and today, I have written down some key points from this amazing rant!

Key Ideas

Take more time to think through your problem.

When was the last time you...

thought about something for a whole day?

thought about something for a whole month or year?

Hammock Driven Development, https://www.youtube.com/watch?v=f84n5oFoZBc

On Bugs

  • Bugs are cheaper to fix in development.
  • Least expensive to avoid in design
  • Most expensive in to fix in production

Analysis & Design, Simplified

  • Identify problem trying to solve.
  • Assess whether it solves that problem.

On Problem Solving

solving problems by Rich Hickey

Problem Solving (cont.)

  • State the problem out loud.
  • Understand the problem's facts, context and constraints.
  • What don't you know?
  • Find problems in your solution.
  • Write it all down.

More Input, Better Output

  • Read in and around your space.
  • Look critically at other solutions.
  • You can't connect things you don't know about.

On Focus

  • On the hammock, no one knows if you're sleeping and they don't bother you because of this.
  • Computers are distracting.
  • Let loved ones know you are going to be "gone", focusing deeply for some time.

Waking Mind vs Background Mind

  • The waking mind is good at critical thinking.
  • Use waking time to assign tasks to background mind.
  • The background mind is good at making connections and good at strategy.

Sleep According to Scientific American:

  • The brain processes info learned while sleeping.
  • Sleep makes memories stonger and weeds out irrelevant details.
  • Our brain finds hidden relations among memories to solve waking problems.

Closing Ideas

Write the proposed solution down. Hammock time is important "mind's eye time". We switch from "input mode" to "recall mode" during hammock time. Wait overnight, or sometimes months, to think about your problem, sleep sober for best results! Eventually coding is required, and your feedback loop is important, but "don't lean on it too much". You will be wrong, facts and requirements will change. Mistakes happen. That's fine, do not be afraid of being wrong. /rant

The notes in this blog post are paraphrased from this rant.

Jun 11, 2022

A Quick Windows Command Line Tab Key Shortcut

You might know you can use the tab key to view the files in a folder one at a time on the command line. However, I recently stumbled into another way to help navigate folders with a lot of files in the command line. Let's say you're looking for a file named "stuff.txt".

  1. Type "s".
  2. Hit the tab key.

You just jumped right to the files that start with "s". If there are not many files, you might go straight to "stuff.txt". The point is that you can jump straight to the letter of your file rather than scrolling through a mountain of files by hitting the tab key 50 times. Once you get to the relative place in the alphabet of your target file, you can scroll forward in the file list with the tab key or move backward by pressing shift + tab.

Multi-Letter Example

Type "st" + tab to target more precise multi-letter sequences.

I felt a productive zen when I accidentally realized this little command line quirk can save a lot of time. Especially when you have a folder with 50 or hundreds of files. Of course, you can always type in the file name directly. Using the tab key is very handy to browse if you don't know the exact file name. This is a nice little trick to add to your bag of Windows command line skills!

command prompt typing example

May 09, 2022

An Ode to Code

Making time to code can be done every day. Carve out those little moments where you can automate tedious tasks or study up on that hot new Python library that takes your quality to another level.

Take time to reorganize and refactor in your favorite text editor. Break your script, then break it again. Break it until it works. Absorb your new abilities as a machine literate human and build skills on top of skills. Make a breakthrough. The code is great. It makes sense. Another tweak here, another tweak there. Run black on it and then have a go at PEP-8 to brush up on your style. More tweaks, and need to add some docstrings for more clarity.

Another one bites the dust. Who knows where your skills could grow. Following the code is a delightful road. Some days it's hard. Some days it's easy. But it's fulfilling if you treat it like a locksmith does keys. Knowledge is flowing. The craft is built in each moment. Challenges overcome. Battles won. New innovations to munge.

Code is the medium to communicate with machines and leverage their efficiency for convenient means. Tighter the web we weave with transistors and screens, the more we'll need dignified intermediaries of man and machine. Here's to the good code and the bad code we all will write. May we never let something stop our logical flights to code a better dream.

Mar 31, 2022

Documentation is the Gateway to Delegation

The first step to offloading a task is writing down how to complete it. Once the process is typed into a lean text file or wiki page, it can be learned by another human. This helps your team, but what's in it for you?

The definition of delegation

The most selfish programmers probably want to free up their time to focus on more interesting work instead of repetitive processes and maintenance. Therefore, the most selfish programmers should love writing documentation. These types of tasks are somewhat unavoidable, so committing to writing documentation will help you push it over to your teammates.

Write to free yourself of your laundry list of recurring tasks. It's the first step to someone else to be able to step in for you or to delegate to someone else completely. On the plus side, you'll be less reliant on your meaty human brain to remember how to do it. Be selfish, take time to write down how to do your work!

Feb 23, 2022

6 Must-read Python Enhancement Proposals (PEPs)

These are a few of my favorite PEPs, or Python Enhancements Proposals I've encountered recently. PEP 0, PEP 8 and PEP 20 are great reads for those aspiring to hone their Python skills. The documentation sometimes references specific PEPs directly also. They document Python's evolution over time and the rationale behind it. Here are some good ones to remember!

PEP 0 -- Index of Python Enhancement Proposals

PEP 8 -- Style Guide for Python Code

PEP 20 -- The Zen of Python

PEP 249 -- Python Database API Specification v2.0

PEP 3131 -- Supporting Non-ASCII Identifiers

PEP 3333 -- Python Web Server Gateway Interface v1.0.1

← Previous Next → Page 6 of 14