Lo-Fi Python

Jan 08, 2022

Experiencing Flow While Coding

Yesterday, I experienced a flow state where I became manically obsessed with perfecting a script I was working on. I think it's beautiful code, about 100 lines long without docstrings. It solves a real need and it felt great to write it. Some scripts feel terrible to write and you know they're bad. However, this one felt like one of the best I've ever written.

Flow seems like a mythical, unattainable state these days as portrayed in media, but we can all agree... we love it. When you're in flow, you know it and you feel a grace in improving your work. For coders, maybe it's by wrapping up a few lines here and there into functions. Refactoring, reordering, handling loose ends or edge cases, writing docstrings with supporting documentation and clarifying that you really understand what's happening... these things are all mundane at times but critical to writing reliable code.

While doing these typical tasks, you're attaining skill and mastery, one of the highest dopamine hits humans can register legally in all 50 states. You know how much better this iteration of code is than when you first learned to write software. You take bits and pieces from past projects and fit them all together into a cohesive, purposeful program. For example, I was tickled to use Python's readlines() file reading function to get the last line of a text file. I learned about this function in my first ever free Python course on Coursera, 7 years ago. Thanks again Dr. Chuck!

This time, I realized my flow when researching ISO 8601 time format strings and guiding them into an HTTP request with the requests library. A new solution emerged, regurgitated from a prior project and mashed up into a more refined form to satisfy the project's requirements. I combined old and new ideas into a better solution than I had ever thought, a fitting complement for the API at hand. Time will tell if the solution will actually work as well as I hope.

Flow is real. You can find work that puts you in a flow state, and it doesn't have to be super interesting work to get there. The learning process pays rewards in competency when exposure to different domains combine. Einstein knew a form of this as combinatory play. Repetition enhances this effect and solidifies your foundation. Flow makes it fun! Only rarely do I feel the highest level of engrossment in my work. I sensed I was flowing on this recent project. You can find these types of challenges too. Keep searching for your flow!

Feb 14, 2021

So You Want to Learn Python?

Here are a few Python concepts for beginners to explore if you are starting out with the language. In this post, I'll highlight my favorite "must-learn" tools to master that come with your Python installation. Understanding them will make you a more capable Python programmer and problem solver.

  1. Built-in Functions. They are awesome! You can do so much with these. Learn to apply them. You won't regret it! See also: An Intro to Python's Built-in Functions
  2. String methods. Want to capitalize, lowercase or replace characters in text? How about checking if a str.isdigit()? Get to know Python's string methods. I use these frequently. Also, the pandas string method implementations are great for applying them to tabular data.
  3. Docstrings. I truly enjoy adding docstrings at the beginning of my functions. They add clarity and ease of understanding.
  4. The Mighty Dictionary. Lists and tuples are useful too, but dictionaries are so handy with the ability to store and access key-value pairs.
  5. List Comprehensions. These allow you to perform transformations on lists in one line of code! I love the feeling when I apply a list comprehension that is concise, yet readable.
  6. Lambda Expressions. These can be used to apply a function "on the fly". I love their succinctness. It took me a few years to become comfortable with them. Sometimes it makes sense to use a lambda expression instead of a regular function to transform data.
  7. Date Objects. Wielding date objects and formatting them to your needs is a pivotal Python skill. Once you have it down, it unlocks a lot of automation and scripting abilities when combined with libraries like pathlib, os or glob for reading file metadata and then executing an action based on the date of the file, for example. I use date.today() a lot when I want to fetch today's date and timedelta to compare two dates. The datetime module is your friend, dive in. Must know for custom date formatting: strftime() and strptime(). See also: Time Format Codes

For tabular data, I often use pd.to_datetime() to convert a series of strings to datetime objects:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# install pandas with this command: python -m pip install pandas
import pandas as pd
events = [
    ["USA Born", "1776-07-04"],
    ["WTC Bombings", "2001-09-11"],
    ["Biden Inauguration", "2021-01-20"],
]
df = pd.DataFrame(events, columns=["events", "dates"])
# convert a pandas series of strings to datetime objects
df.dates = pd.to_datetime(df.dates)
print(df.dtypes)
print(df.head())

Just the tip of the iceberg...

The amazing part of Python is that its community has developed an astonishing plethora of external libraries which can be installed by pip. Usually I'll learn how to use new libraries after googling to find a well-written README on Github or helpful documentation. The language comes with an impressive line-up of baked-in tools and libraries way beyond what I've mentioned here. But I think this is a great start. Get to know these common Python language features and you'll be surprised how much you can do!

Additional Comprehensive Python Learning Resources

How long did it take you to learn Python?

Practical Python Programming (free course)

Google Python Style Guide

What the f*ck Python!

PySanity