Renaming in Python

I needed to rename several files.  I thought perhaps using a script would make it a little faster.  Well, the renaming went faster.  Writing the script of course took much longer.  But I suppose I learned something.

The image files were individual pages from a book of about 64 pages.  They were named one number off from their actual page numbers.  You can imagine how annoying that can be.  You can’t?  Well, try harder.  It’s pretty annoying when 3 isn’t three but 4.  Three isn’t four!

That doesn’t irritate you?  Well, I wrote a script to fix it nonetheless.

You can find the script over at my GitHub.

It’s modifiable enough so perhaps it can suit your needs.  Feedback is always welcome.


Useful bash Line for Debugging

I’ve started using this little code snippet to help debug problems within a bash script. It just shouts out the line where it appears when called. This is useful when you are trying to determine where a problem is located within the various twists and turns of the function calls within a script.

# Here is a print statement useful in debugging:  
printf '%s\n' "" "${LINENO}" "" 
# Pepper a script with those to see where your having trouble line-by-line! 

Have fun with that!


A Couple Recent Conversions for Audio and Video

I’ve had to make a couple of unusual conversion of late and wanted to make note of the solutions for my future self.  Hey, future self, git with it!

First, converting dsf (and I think also dff) files into flac files I just used ffmpeg as follows:

# first cd into the directory containing the dsf files in question, then… 
ffmpeg -i 15-Penny\ Lane.dsf 15-Penny\ Lane.flac 

Then I also needed to convert an ISO into some constituent mkv files. For this I used makemkv.

# first you'll need to install it from the Snap repositories.  
snap install makemkv 
# then open the graphical application and point it at your ISO.

Both pretty handy, easy, and fool-proof. I’m the fool. I’m the proof. Good hunting.


Take Multi-Line User Input and Add that to a Variable in Bash

We are using a construct using xargs and ctrl-d for breaking.  I’m not perfectly satisfied with it, but it certainly does the job of taking multi-line user input and stuffing that into a variable (formatting intact).

(The first and third assignments add quotes around the contents of the xargs input.  You may or may not require that.  In our case the variable is the body of a message sent by mail.  It must be quoted.)

printf '%s\n' "What would you like the body of the message to contain?  " 
printf "\\033[1mWhen finished hit ctrl-d on a new line to proceed.\\033[0m  \\n\\n" 
# this will load the user's input into a variable instead of a file 
reminderBody+=$( xargs -0 ) 

This basic construct could be used for a variety of needs.

(The \033[0m‘s are used to change font colors. Again, optional.)


Go, Speed R-AI-cer!

I’d like to see a competition between autonomous driving vehicles on some sort of track.  Something like an AI-500.  Let’s see what they can do in this kind of situation.  Let’s see what that does for their autonomy.

Probably be best if a variety of manufacturers are participating in the competition.

Must have something like the flag system for signaling to the vehicles concerning course conditions.  Perhaps a course boundary that shows the various signals all around the track (a yellow boundary for a yellow flag which I believe is traditionally an accident on the course).

Cars that incorporate IR (see my previous article) may have some advantages in anticipating what the other vehicles are doing on the course.


NTP and the Delay

We were setting up a check to ensure our time servers were pointed correctly from the firewall, but the standard time query was taking six seconds for each check.  With four time servers that’s nearly thirty seconds to make a simple “are you there” sort of check.  We didn’t want to do a simple ping test since this would not ensure the machine queried was in fact an actual time server.

After some digging and testing we found that if we limited the packets to a single packet the test was instantaneous.  So we added the -p argument and called it good.  (In our case we were not concerned with checking the time status but rather only the status of the server as an available time server.)

This is the basics of the command:

time ntpdate -qp

And that’s about it.  Very much faster with the p in the mix.


Get git on a Server of Your Own

The trouble with searching the Web for instructions relating to git and using your own git server is that mostly you will find articles for working with someone else’s repository server (like GitHub or so many others).  You can find quite good instructions for interacting with a remote server from your local development machine, but there are so many such instructions out there that locating useful information about using your own server to host git gets buried pretty deep.

Let’s go over some of the most basic pieces, and if you know how to use git with someone else’s repository server then you will be in good enough shape to sort out your specific situation.

First we need to differentiate between the served repository and any local copy of the files you might like to keep.  You don’t necessarily need to keep a local copy of the files on the server since the repo contains enough information to rebuild the files at any point, but I’m going to show you how because I wanted mine to include server-held local copies of the files.

On your server you’ll want to create a bucket for holding any and all of your git repositories (I broke mine into projects plus an archive folder).  So your paths may look like this:


In the above example, the folder I’ve called git is just the bucket which holds the local copies of the repository files, and should not be used itself as a repository.  (If you are only planning a single repository I would still recommend using this structure as a way of being ready for the future.)  The folder I’ve called .repos is the bucket which contains the git repositories; these sub-folders do not contain any of the actual files but rather just diffs which allow git to rebuild the files at various stages.  You will see that I have a one-to-one correspondence between the .repos sub-foldders and the local copy folders above.

Move into each directory under .repos in turn and perform these actions.  Here we will just pick project1 and go through the steps.

cd /media/storage/git/.repos/project1
git init --bare

This will create an empty repository which you can clone, add files, and make commits.  This is how to make your first copy (of the empty repo) and add files.

cd /media/storage/git
git clone yourusername@localhost://media/storage/.repos/project1
# now move into the newly cloned directory... 
cd project1
# here you will want to add any existing files to this folder or create a new file then...
git add .
git commit -m "initial commit of new repo"

Now you have a good master to begin.

From your laptop or workstation or any other computer you can perform these cloning steps above but substitute the name of your server machine for localhost in the clone command above.  (This uses ssh for reading and writing to git.  You can find instructions out there for http if you’d rather use that.  I prefer ssh.)

You won’t need to use git add until there is at least one file you want git to know about.  Commits just let git understand that anything git knows you have changed is to be regarded as canon.

Add some files and make some changes. Then move into the project directory to add, commit, and push.

# move into some folder, probably called git, where you want to store your git repos
git clone yourusername@yourserver://media/storage/.repos/project1
# now move into the newly cloned directory... 
cd project1
git add .
git commit -m "useful commit message so you remember what the fuck you did"
git push

If you set up a local copy on your server like I did above, you will want to regularly git pull into that copy so the files stored there are as up to date as possible (when you run your backups for example).

As near as I can tell this is the best way to manage that for oneself.  If there are better practices than those I’m using here, I’d like to see the detailed explanations for making them work and why they are a best practice.  Let me know.

Otherwise, have a great time with your newly minted git server.



Another idea for improvements in the autonomous automobile space is the use of Generative Adversarial Networks (GAN’s) in the training thereof.

Normally in a GAN you might have one network attempting to identify birds in photographs, while the other network is doctoring photographs to get the first network to misidentify photoseither false-positives (birds where there are no birds) or false-negatives (non-identified birds).

In training autonomous vehicles, it may be possible to create virtual test environments where one network is working to successfully drive through simulations, while the second network is attempting to thwart that driving—either by causing the first network to crash its vehicle or by forcing the first network to halt its progress for trivial reasons.

I don’t know that anyone is doing this today, but it does seem a potentially fruitful avenue of testing and training.


Change Default Mail on Mac without Launching Mail

I clicked on a mailto link at work again.  Happens from time to time.  The Mac popped up Apple’s Mail as though that might help.  I kept meaning to change my default mail application; maybe today was the day?

Anyway, all of the instructions you will find tend to be, well, the same instructions:  open Mail, open Preferences, do some other stuff.  The trouble is that unless you set up a mail account in Mail you cannot open the Preferences.

Outlook used to have a setting for taking default, but that has gone away because Security!

Anyway, I found a solution (here) involving a small amount of Python which worked perfectly.  Nothing to install.  Just copy and paste and you’re good to go.  If you are not using Outlook, you’ll have to look up whatever the bundle identifier would be for your application of choice.  Here’s the code.

/usr/bin/python2.7 <<EOF
import LaunchServices;
result = LaunchServices.LSSetDefaultHandlerForURLScheme(
print("Result: %d (%s)" % (
    "Success" if result == 0 else "Error"))

(The use of EOF should allow you to copy and paste the entire block into a terminal without having to separately paste each line.)

Have fun with that!