Zhus on First

who's the hare now

To help me build foundational Python skills and so I dig a little more into technical details, I'm going through UMich's Python for All Coursera class. Albeit at a rapid clip.

Or so I think.

one hour later

I'm still fiddling with basic coding exercises like:

    email_addresses = []
    count = 0

    with open('mbox-short.txt', 'r') as file:
        for line in file:
            if line.startswith('From '):
                email = line.split()[1]
                email_addresses.append(email)
                count += 1

    print(f"Email addresses: {email_addresses}")
    print(f"Count of lines with 'From' as first word: {count}")

But now it's turned to this:

def email_and_count():
    """
    1. Open file mbox-short.txt
    2. Read the file line by line
    3. Parse any From line using split()
    4. Print out the second word in the line, ie. should be the entire email address
    5. Print out count of line with From as first word

    Returns:
        tuple: A tuple containing (list of email addresses, count of From lines)
    """
    email_addresses = []
    count = 0

    try:
        with open('mbox-short.txt') as file:
            for line in file:
                if line.startswith('From '):
                    parts = line.split()
                    if len(parts) >= 2: # Guard against index error
                        email = parts[1]
                        email_addresses.append(email)
                        count += 1
    except FileNotFoundError:
        print("Error: mbox-short.txt file not found")
        return [], 0

    return email_addresses, count

if __name__ == "__main__":
    email_addresses, count = email_and_count()
    if email_addresses:
        print(f"Email addresses: {email_addresses}")
        print(f"There were {count} lines in the file with From as the first word")

# Various AI was used to extend my learning: Warp, zed with Claude, Gemini

I don't remember if it's standard styling for inline comments to be # Like this or #like this or #Like this.

Or do comment blocks start with //? Wait, that's Java.

Let's go look at PEP8 or since it's well-known Python doc, just ask generative AI.

And my morning is half-way over...but I reinforced:

  1. How to setup a local Python env using uv
  2. How to use terminal to test random bits of code
  3. How Warp might be helpful in my learning
  4. The importance of turning off Zed's AI features for now
  5. What errno 2 means! It's not a misspelling!
  6. What's ropeproject
  7. Not to mention how correct inline commenting style in Python
  8. Joy of exploring how much goes on in just a few lines of code. Just think about how happens when I type "python ...." in the terminal.

#sketch