Regex Groups in Python

Federico Viticci has me hooked on Pythonista. I am not sure what took me so long to buy it because the very premise of scripting on my phone sounds like the best idea ever. I bought it and can confirm it is (in my opinion) the best app ever. At the very least, it is my favorite app right now.

This is not an app review, that may come later. No, this is more of a reminder for myself the next time I go to use regex groups in a Python script. I am extremely new to Python so when I learn how to do something I generally write my self a little text note or store the code snippet for future use, this time I am just going to share it in a blog post.

Groups are incredibly useful. You can use regular expressions to identify a portion of a string and assign it a group name that can be easily referenced when replacing values.

The syntax in Python for defining a regex group is:

(?P<foo>some-regex)

The syntax to reference that group later is:

\g<foo>

This is often better described with a real world example. So here is a link to a tweet on Twitter.com that I am going to break up in to multiple useful groups:

https://twitter.com/binaryghost/statuses/261213781294718976

Using regex to identify the may portions of the URL (domain, user, status, and id) I can assign each chunk of the URL an identifiable name:

Now that I have my groups identified, I can call them in the substitution pattern to do something like convert the format of the link to one that is compatible with Tweetbot’s URI scheme:

tweetbot://\g<user>/status/\g<id>

So when you put all this together in Python you basically get this little code snippet:

So disregarding the import statement, it is possible to identify only specific sub-strings and reuse or replace them at will with only 1 line of code. As you can see, I did not end up using <domain> or <status> and I could have probably left them out but I wanted them to be apart of the example. Every language has its own flavor of regex groups, this syntax however is specific to Python which I why I felt it best to document it with a useful example.

In addition here is a gist with the above code in a working example specifically written for Pythonista:

*Also check out Viticci’s version over at MacStories

Perl Regex Removed From Grep in Mountain Lion

I realized today that many of my shell scripts were no longer working since I upgraded to OS X 10.8. After digging in to it I found out that they were all bombing out on a common grep command I use for finding specific bits of text with Perl regular expressions.

grep -Po '(?<= ).*?(?= )'

I pulled up the grep man page and couldn’t find the -P switch I had always used for Perl regex. It was definitely removed.

To be completely certain, I jumped on a machine running OS X 10.7.4 to see if it was in the last OS, and sure enough there it was.

I am not sure why it was removed, but it was a great way to use powerful Perl regular expressions in a quick and convenient shell script. I really loved being able to whip up a quick shell script for something and use Patterns for the Perl regex. I know egrep has ‘extended’ regex but I like the Perl syntax. I guess it is time to leave my shell scripts behind and start writing in some more sophisticated scripting languages.

Maybe it wasn’t Apple and it was just apart of its open-source development, I don’t know, but it will be missed by me.

Verizon vs. Sprint – iPhone 5 vs. iPhone 4S

This is why I bought an iPhone 5 and switched to Verizon.

How to Implement Multi-File Actions in Alfred 1.3

I wanted to write up a quick post on how I implement multi-file actions in Alfred 1.3 so you can get a better idea on how to use the new feature and hopefully use this to start making your own multi-file extensions.

My language of choice, as usual, is bash; however the concept is the same in any language. Alfred provides your extension with a tab delimited string of files that you parse and perform actions on.

Example:


The code above is a basic implementation in bash that parses the input by tabs and puts each file in to an array. Then iterates through the array and does something with each file. The only line worth specifically noting is the line:

 IFS=$'\t' 

Even though tabs are already apart of the default IFS (input field separators) along with spaces and newlines – I have overwritten the IFS to consist of only tabs so that filenames with spaces don’t break up in to multiple items in the array.

Here is a full blown example implementation in an Alfred shell script extension that is designed to accept multiple files and batch rename the files based on a prefix supplied by the user. A download link for the extension is provided at the end of the post. [1]


*Note: In order for this extension to accept multiple files, it is required to check the new option in the extension’s settings: “Accept multiple files as argument”

To wrap this up, here is a video of the extension in action:


  1. Alfred Extension Download Link: Rename Multiple Files

A Script For Scripting A Script

I make a lot of scripts. I have scripts intended for long term management of tasks and files. I have made short term scripts, or single-use scripts to batch manipulate a directory of files in some custom manner. I have scripts that run at launch and other scripts to interact and automate the gathering of information from web based services.

It occurred to me that I had never automated the process of making a script.

I know there is not much to starting a new script, but thats not really the point of automation. It is simply about reducing the amount of un-necessary repetition in your workflow. I realized that when I start a new script its almost always a shell script, almost always on the desktop, and there is almost always the same bit of text inside the script to get me started.

Here is an example of my own boiler-plate shell script:

Not much to it, but it is a great starting point. You are probably wondering why I have an empty main function even though it does not offer much in the way functionality. I find it beneficial to break up shell scripts in to functions for increased readability as well as code re-usability. A well written shell function can be copied and pasted in to any other future scripts without having to change much code.

Lets take a look at the code to generate this script:

You will probably want to update the NAME variable so that it reflects your own name. You don’t have to change the DIR variable unless you want your scripts to go some where other than your desktop. The script creates the file on the desktop, generates the boiler-plate content, sets executable permissions on the script, then opens it in your default text editor.

I should note that you do have the ability to set a filename when creating a new script. The IF/ELSE statement at the end of this code checks to see if you have sent it a filename, if it does not find one it simply names it script.sh. You do not need to type out the .sh extension because it is automatically added for you in the script. Also, don’t use spaces in your script filenames.

I use this script in conjunction with an Alfred extension for optimal productivity. I am attaching the extension below so feel free to download and try it out.

I simply launch Alfred and type either script or script someFilename

If shell scripts aren’t your thing, this script could be easily adapted to whatever scripting language you prefer. If you need any help modifying it, just let me know and I will do my best to help you out.

Enjoy

Download


Start a new script extension

Archives