Somedays I write more bugs than code…

I posted recently that I had started on Bites of Py Exercises, and i’m glad to say I have finished a few more. Small wins are good!

Along the way i’ve discovered, thanks again to the Python Bytes podcast, a new way to see crashes in my scripts. Currently I am working slowly towards scraping my favorite wallpapers from wallhaven.cc and downloading them. But, as I mention in the title, I seem to be better at writing bugs than functional code.

When a script crashes in Python you get a traceback, which seems to be a backwards listing of the things that caused the crash. With practice I will get better at making sense of things like this:

Traceback (most recent call last):
   File "wff-selenium-no-login.py", line 6, in 
     from urllib import Request
 ImportError: cannot import name 'Request' from 'urllib'  NameError: name 'Request' is not defined
   File "wff-selenium-no-login.py", line 24, in 
     req = request(wallpaper, headers = {"User-Agent": "Mozilla/5.0"})
 TypeError: 'module' object is not callable

But thanks to that episode I learned about friendly_traceback, which gives me this output instead:

Python exception:
         TypeError: 'module' object is not callable
A TypeError is usually caused by trying to combine two incompatible types of objects, by calling a function with the wrong type of object,
or by tring to do an operation not allowed on a given type of object.

Execution stopped on line 24 of file 'wff-selenium-no-login.py'.
-->24:     req = request(wallpaper, headers = {"User-Agent": "Mozilla/5.0"})

request: <module 'urllib.request' from '/Users/davidr/...>

How is this better? Well.. it explains the error, points to the line I need to fix.. and also tells me the module I have issue with. Though I still don’t know what to do to fix it it makes me feel like I have a better handle on it.

Nibbling at Python

Learning Python has been on my radar for many years. I have made a start at it a few times. The shell scripts, Applescript and even the Hypercard stacks I made back in the day are very useful but not quite enough any longer. Working with teams of software developers and tech support I have accumulated a lot of “what programming is and how it works” over the years, so this is not all new.

Recently I listened to another fine episode of Talk Python to Me, this time with Michael, Bob and Julian describing their latest course/adventure, 100 Days of Web. I like their style and the project intrigued me so I went and had a look and found Bites of Py Exercises.. and decided to jump in. I promptly discovered I had setup an account a year ago. Did I mention i’ve tried getting into Python a few times?

I’ve managed to complete 3 out of the 4 Intro Bites.. and with that under my belt, here’s a few things that are making it easier as I go along: I bounce around Google a lot trying to figure things out. The python documentation has lots of examples. Stackoverflow has answers for everything but can be hard to make heads or tails of. The team at RealPython made some fantastic tutorials which definitely helped me understand dicts enough to finish that Bite. But the best has been the Pybites Slack forum. I got really really stuck I went and talked to those folks and they pointed me in the right direction. Thanks!

Working in the browser is nice, but i’m used to using an editor on my own Mac along with all the tools it brings. So I asked.. and apparently I need to learn Emacs. Just kidding. VS Code and PyCharm are both good, along with downloading the Bites and the tests, using git and Github to store your code (and revisions!!!) from which you can then submit your answers. I was already familiar with these tools.. so I gave it a shot. They’re a great distraction from working on Bites. Fiddling with python versions, installing stuff, tweaking settings. Wheee! For now i’m going to stick to using the Python Interpreter on the command line to try things out until it works, then code in BBEdit and run the tests on the command line. Once they pass, I copy and paste the code into the web interface. It’s simple and does the job.

As to the Bites themselves? Hey, i’ve done 3 successfully! The first was hard as I really had no idea what I was doing and how the interface worked. I’ve never used tests before. I have expectations of seeing stuff print on screen but it never happened. I was confused! Eventually I figure out how to get it to work.. The second one? Well, as i’ve told my kids, “Programming is constant failure with the occasional success.” That one I got so frustrated I gave up and read the solution. Yes, you can discuss your solution with others once you give up or solve it. The third one I got eventually.. and the 4th is similar to it but just enough of a difference to throw you off. I felt like I was slogging through each Bite.

By now you’re wondering if I should abandon this as it’s not sounding like fun. Totally the opposite! I’ve learned a ton of new things, and i’ve got a better feel for how to get to the answer without all the distractions around it. Review the goal 4 or 5 times, make an outline of how I think it will work, look up a bunch of things, learn new things, poke around in IDLE until I can get a step to work, code it.. repeat! Eventually the tests pass. Woohoo! I really should be doing my job and instead I spent part of my morning having fun and learning Python. On to Bite 5!

Opening a pile of urls all at once — updated version!

A year or so ago I wrote a post on how to use xargs to open a bunch of urls that were in your clipboard. But it turns out that in newer versions of OS X something broke, deliberately or otherwise, and that method no longer works. Well, it works fine, it’s just *too* fast. So today I needed to check about a hundred urls.. and so it was time to fix that script. I’ve been slowly working on learning Python, so I decided to use that.. and this is what I ended up with:

#! /usr/bin/python
import webbrowser
import pyperclip
import time
url_list = pyperclip.paste()
clean_list = url_list.splitlines(False)
for x in clean_list:

    webbrowser.open(x)
    time.sleep(.5)

It only took me about an hour 😉

What does it do? It grabs the clipboard, splits each line inside the clipboard at the return character, and then there is a loop which tells the browser to open the url .. wait a half second and then do it again.

I’d like to figure out how to grab the clipboard and clean it in one line.. but that’s for another day!

Back to work..