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!

Using SMS URIs with OC Transpo

Ottawa’s OC Transpo service provides real-time bus schedules through text-messages on your cell phone. Basically you text 560560 your stop number, like 7595, with the option to specify a bus, like the number 18. It will shortly reply with a GPS adjusted schedule. In some cases you will be asked to text back a 1 or 2 for specifying the direction, when you are at a transitway stop or loop that services both directions.

There’s a mobile app for iOS, but I like the simplicity of sending a text without needing to fiddle with an app…

At some point I ran across an article explaining how to embed an SMS link into a web page (such as this one) and decided I needed to try it out.

A live example:

SMS:560-560&body=7040%2020%202

The components:

SMS: defines this as an sms link
560-560 or 560560 is the OCTranspo number to text to
&body= the contents of the text message
7040 the stop we want data on
%20 html code for a space
20 is our bus number
%20 another space
2 as we want to go towards St. Laurent.

A bunch of these urls got set up and stored in Apple Notes for all the most common stops I use.

Once you tap one it will load up a new message with the contents defined, touch the send key and wait a few seconds. Here’s the result I got back in this case:

Stop 7040 route 20 St-Laurent at 18:46 to St-Laurent; 19:14 to St-Laurent; 19:44 to St-Laurent.

Now if only I could figure out how to send it automatically i’d save a whole tap!

Fun with SED!

(Found this sitting in my blog’s drafts folder.. only 11 years old but all of it still just as valid today!)

A couple of shell scripts for renaming files on the command line.

# Replaces JPG in any file with jpg 
for ii in * ; do ARG1=`echo $ii | sed 's/JPG/jpg/g'` ; mv $ii $ARG1 ; done

Stany’s renaming scripts:

#!/bin/bash
# Mass REname of english filenames
# relies on fixfilename regexp engine

PATH=$PATH:/Users/stany/bin
echo
for ii in * ; do ARG1=`echo $ii | fixfilename `   ; echo -n "." ; mv "$ii"  "$ARG1" ; done
echo
#!/bin/bash
# the fixfilename regexp engine

PATH=$PATH:/raid0/tools
sed 's@(@@g;s@)@@g;s/ /_/g;s/\,//g;s/\&/and/g;s/_-_/-/g;s/-_/-/g;s/\!//g;s/\;//g;s/\]//g;s/\[//g;s/%20/_/g' 

A combined version…

#!/bin/bash
for ii in * ; do ARG1=`echo $ii | sed 's@(@@g' | sed 's@)@@g' | sed 's/ /_/g' | sed 's/\,//g' | sed 's/\&/and/g' | sed 's/_-_/-/g' | sed 's/-_/-/g' | sed 's/\!//g' |  sed s/\;//g | sed 's/\]//g' |  sed 's/\[//g' | sed 's/%20/_/g'  `  ; echo -n "." ; mv "$ii"  "$ARG1" ; done
echo

This script will remove the first 4 characters of a filename:

for ii in * ; do FOO=`echo $ii | sed 's/^....//g'`; echo mv \"$ii\" \"$FOO\" ; done

This will echo what it actually does, and place quotes in front and after each filename. In the for ii in * bit you can adjust the regex to match the files you need. So for ii in *.txt will match all the files ending in .txt

Remove the echo to run it, or pipe into shell: | sh

Intro to iOS: Thursday Dec. 6

Tom and I are doing another Intro class this Thursday, Dec. 6th, at 6:30 to 8:30 p.m. at the Main Library Branch.

The class is free but registration is required.

Join us for an introduction iOS, the operating system that runs iPhones and iPads, with Dave and Tom, founders of the Bytown Mac User Group. ByMUG has been helping Ottawans using Mac and iOS devices since 2006. Bring your device and follow along. No device? No problem! This informative workshop will cover the basics and much more! 

If you are attending and have questions.. please send them in in advance so we can be prepared.

Capture all tabs in Safari as URLs to the clipboard

Sometimes you’re doing research and have a pile of tabs (and windows) open and need to get them into a document to share with coworkers… so I went digging and tweaking and found an Applescript that does the job:

tell application “Safari”
set docText to “”
set windowCount to count (every window where visible is true)
repeat with x from 1 to windowCount
set tabCount to number of tabs in window x
repeat with y from 1 to tabCount
set tabName to name of tab y of window x
set tabURL to URL of tab y of window x as string
set docText to docText & tabName & ” – ” & tabURL & linefeed as string
end repeat
set the clipboard to the docText
end repeat
end tell

You can save it as a standalone script and run it from there, or stick into a script item in Automator and run it as a service.. or even call it from the command line as an Osascript.

Looking up Mac specs..

Did I mention I love TextExpander? Yeah.. so, to make a long story short: clients ask me questions about their computers, upgrades, lifespans, etc. I don’t normally remember all the details of their Macs so I get them to send me the Serial Number (Open the Apple menu, click About This Mac and it should be right there.)

Once you have it you can go by Apple’s support site and look it up… but that takes too long.

Make a new TextExpander snippet with Applescript as the content type:
property theURL : ""
set theURL to "http://support.apple.com/specs/#" & (the clipboard)
do shell script "open " & theURL

Then you just copy the serial number, and anywhere you can type use !specs to invoke it and up pops open Apple spec page a few seconds later.

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

All about games..

There are a few places to get Mac games, other than the App Store.

A few of the larger ones:

Steam lists 2500 games for Mac.
GOG or Good Old Games has 1297 games for Mac.
macgamestore.com, has hundreds in each category.

There are quite a few others, and reviews of them are available online if you want to know which is better or worse than another, and why.

gamersgate.com
gamesrocket.com
amazon.com/mac-game-downloads
gameagent.com
aspyr.com
ubisoft.com
ea.com

The reason we’re here is for games.. and knowing what’s good *before spending money* is important, go read reviews. There are many review sites, and they all have “best of” lists.

http://www.macgamerhq.com/top-games-for-mac/

What do I play?
More like what do I still play? aka what still works on El Capitan.

Osmos
Enigmo

Quinn, a Tetris game, which you can still download from macintoshgarden.org

The Tiny Bang Story, a kids puzzle game.

Cave Story “Doukutsu Monogatari” is a great side scroller ported to the Mac. A bit of fiddling required to get it to work, but worth it! Start here: http://www.cavestory.org/download/cave-story.php

Bungie. What else do I need to say? Yep. Halo. Myth, and many more. Halo 5 just came out.. for Xbox. Although Microsoft did port 2 games to iOS (instead of Windows Phone, what a surprise!) : Halo Spartan Assault and Halo Spartan Strike.

Yes, You can still play Halo, from 2004, using http://halomd.net
You can stil play Myth as well, using Project Magma.

But we’re not limited to just Mac games.. we can run emulators as well. One that I have used on occasion is Boxer, which lets you run DOS games.

Shout out to Ambrosia Software and one of my all time favorites: Avara! Apparently I can run it under emulation. Still making games after 20 years!

I’m sure i’ve missed many, what are your favorites?