Pythonista 101: Building Blocks

If you’ve been with us since the beginning of Pythonista 101, you know that we’ve covered quite a bit of ground. Going from simply installing the app, navigating around the potentially intimidating interface, and even installing some scripts we found on the web. Then we graduated to breaking down those scripts we found, and making some modifications to them.

Well now it’s time to graduate even further, and look at where the real power and potential in Pythonista comes from: modules.

What Exactly Are Modules?

Modules seem like special, magical little things that expand the capabilities of Pythonista and the scripts you write. That’s really only half true. Yes, modules expand what you can do with Python, but they’re not magical or unknowable.

The Python community as a whole discourages code that’s indecipherable or behaves without a logical explanation. Code like that is often deemed “magical”. Modules aren’t like that. They’re simply scripts by a different name. Modules solve the problem of how to reuse code from one project to another. Developers try to be as efficient as possible, so when they write something once, it’s considered best practice to write it in such a way that it can be easily used later.

That’s what modules do. The features you’re calling with things like “module_name.feature” are really functions contained within the module. So don’t be afraid of modules or think they’re unexplainable. Think of them instead as a gigantic library of scripts full of useful functions you get to leverage in the scripts you write.

The modules found in Pythonista are baked into the app by the developer, and he’s done a fine job of offering a wide array of features and functionality. The module library is also continuing to grow with each update to Pythonista. Lets look at some of these modules and what you can do with them.

Module Review

calendar

Designed to output a calendar, the calendar function also contains other useful functions related to the calendar. I’ll be the first one to admit that dealing with dates and time within programming can be tricky. But the simplicity of this is cool:

That prints a plaintext calendar to the console, the days of the week perfectly lined up with the number for each day. It’s pretty neat. Try running the code above and you’ll see what I mean.

canvas

Programmatically draw shapes and lines. Maybe I simplified things down too much with that definition, but that is what the canvas module lets you do. There’s tremendous power in this though, and also the chance for a lot of complexity.

console

This is an important module to learn about. The console is the environment you output your code to in Pythonista. But there’s more to the console than simply a place to spit plain text messages out to.

console.set_font
Lets you change the font being used to something else for the duration of your script. For a full list of what fonts are available and what their proper names are on iOS, I recommend visiting iOSFonts.com.

console.alert
This is a cool one, console.alert lets you prompt the user with a dialog box containing up to 3 different buttons. You can then read the response back into your code and act accordingly within your script.

console.showactivity and console.hideactivity
These two work in conjunction to show and hide the network activity indicator. In case you aren’t sure what that is, it’s the little spinning circle that appears up in the top left hand corner of the iOS status bar. Cool thing to have control over, right? So if your script is accessing the Web for anything you can throw that activity indicator up there and let your users know that.

console.write_link
Rather than outputting a URL which then would need to be selected, copied, and pasted into your browser, this function lets you right a tappable link to the console. What adds to the fun is that this doesn’t need to be an HTTP URL, it could be a URL scheme which launches another app.

csv

Perhaps you’re unfamiliar with the term CSV, or Comma Separated Values, but it’s a generic, plaintext format for displaying tables of data. Nearly every database and every spreadsheet program, including Excel, supports the CSV format for both importing and exporting data. If you’re unfamiliar with what type of delimiter is used, that’s no problem. The csv module has a built-in function which examines the contents of the CSV file and determines how to interpret it.

If you have databases or spreadsheets of information you’d like to manipulate, the csv module is the place to do it.

dropbox

I could write an entire series of articles just about the dropbox module, and if you’re interested in one, sound off in the comments. The dropbox module contain’s three main parts: the DropboxSession, DropboxClient, and REST Client. The REST Client shouldn’t really be need be accessed by you when trying to manipulate Dropbox. Instead, it’s the combination of a DropboxSession with the DropboxClient functions that lets you do all the things you want to.

Something important to keep in mind is that for the Dropbox module to work, to be able to create a DropboxSession, you need to be registered with Dropbox as a developer and have the necessary tokens. If you have no idea what I’m talking about, then give the Dropbox developer documentation a read, as well as the Pythonista Dropbox module documentation.

And We’re Just Getting Started…

Ok, so there’s a brief overview of five of the modules found in Pythonista. But we’re not done. We’ve only scratched the surface of what Pythonista is capable of. Next time we’ll look at even more modules, and look at some ways we can pull these modules together to accomplish some useful tasks.