LBYL vs EAFP

So, it’s been almost a year since I’ve posted something, and it’s been a busy year.

ERATE twice, pushing my CCIE recert to the edge of losing it (23 days left in suspension when I passed), getting married to my best friend (Halloween if anyone is wondering), and yes still working to learn python.

One of my personal projects was a script that used FFMPEG to convert media files to X.265.
While I was doing this, I stumbled upon what appears to be a common…question? oddity? something anyway.

Look Before You Leap LBYL or Easier to Ask For Permission EAFP

Basically when you are getting ready to do something:
Do you check if a value/key is there first?
or
Do you just “try” and if it fails catch an “except” and do something else?

In my case, I was attempting to write a script that could take the base directory having the files in it directly, like:
/Movies/
or have the file in a folder like:
/TV/Constantine/
And expect to have “Season” folders

As I’m lazy, I don’t want to have two scripts to do this, or to have to modify code each time.

So the first thing I did was use split to create a list.(ok technically the second thing I did, as the first thing was to create a list of files that matched my criteria)

with open(found, 'rt') as shows:
for row in shows:
line = row.rstrip().split('/')

When the base is /Movies we only have line[0] = filename
When the base is /TV/Constantine/ we have line[0] = Season and line[1] = filename

Now that I have a list, I tried to check if element [1] exists

if len(line[1]) >= 1:

and if there was something there, it worked!!
if there isn’t anything there, it errors out….

Really? There is an else statement following…if it wasn’t true why didn’t it continue to the else?
after trying different things to make this work, and not being able too, I found a reference to

Once I did this:

try:
if len(line[1]) >= 1:

It actually worked!

Now, I still don’t fully understand why LBYL didn’t work, but a lot of code I found while searching for an answer seemed to follow EAFP using try/except method.

More Python goodness

Still working on learning how to use Python to do scripting and automation. I did hit a bit of a roadblock with one project as the documentation on the site had embedded links to some examples…and the links were broken. Kept trying to figure out how to get to them, tried different ways of creating the link from where the main documentation was at, nothing worked. Months of trying this off and on, and then I struck gold!! “Why don’t I just ask for them?” yeah, seems obvious and it should have been, but I had to do some digging to find an email address to send the question to. But wooo!! I finally have the schemas, so I’ll get back to that project eventually.

On to another subject kind of…

A friend of mine, posted some stuff a while back. Good resources to start learning with, so go take a look at Ashley McNamara’s blog.

And I’ve been working (trying too anyway) my way through the stuff on Cisco DevNet as well. Nice thing here as you go through the courses, they have links to their Cisco DevNet Github so you can clone the repo and have the scripts they use already. You’re always free to create your own, but if you are just using their sandbox server, they work great!

Scripting with Multiple Machines

I’ve been trying to learn python and scripting in general. If I can make certain parts of my job easier by writing a script why not?

With that I’ve taken to doing some customization on my environment.

    Using Zsh instead of bash (pulled from Homebrew)
    oh-my-zsh (customization of zsh)
    Homebrew (package manager, can install all kinds of fun things)
    Pathogen (runtime for VIM)
    help from friends! Tony Mattke, Teren Bryson and others.
    DropBox
    GitHub of course!

The problem I was having was how to keep my customization and scripts in sync between my work machine and my personal machine. The answer I found, well one answer that works for me, is DropBox! Though you could probably use any cloud storage provider you wanted. I created a folder in my DropBox called “sync” and I symlink the files/folders I want to keep in sync on both machines into it.

Now, when I make a change to my .vimrc, zsh custom folder, or basically anything else I have symlinked, the change gets reflected to the other machine.