Wow, I just managed to combine three of my favourite things in a single title! Recently, I’ve been getting further into home brewing, with a book I received as a Christmas present (Home Brewed Beers and Stouts, by C.J.J. Berry). Since I’d never actually measured the Alcohol By Volume (ABV) of my beer I decided to write some Python code to automate the process. The simple modules I came up with work from the command line and also on Android phones via SL4A, which makes them very useful when doing quick measurements.
Measuring ABV involves taking Specific Gravity (SG) measurements at the start and end of fermentation, adjusting them for temperature and pushing them into a simple formula. The SG measurements are taken with a Hydrometer, which is supposed to read at 20C (hence the need to adjust for other temperatures).
The temperature adjustment is done via a simple table (which I took from the book). In the script I used Linear Interpolation, to adapt it for values which weren’t available in the table. Initially I used the numpy.interp() function. However, I found that numpy isn’t present in SL4A and that installing it would be a pain since it is a C module which would need cross compiling for Android.
I therefore wrote my own interp() function, which keeps the same interface:
def interp(x, xp, fp): i = 0 for p in xp: if p > x: i = xp.index(p) - 1 break return ((x - xp[i])*fp[i+1] + (xp[i+1] - x)*fp[i]) / (xp[i+1] - xp[i])
I split up the code into two separate modules, sg.py (which just does specific gravity adjustment) and abv.py (which does ABV calualation). Splitting the code up enables me to take SG measurements and adjust based on temperature, without doing a full ABV measurement. In sg.py the reverse adjusted SG is what you would need to read from the hydrometer at the specified temperature, in order to achieve the adjusted reading you specified.
Since the calculations are fairly trivial the main bulk of the code is in the user interfaces. I implemented a simple command line interface which either takes arguments from the command line, or prompts the user via the python input() function. I also use the SL4A API to implement a simple UI for Android, basically the user is prompted for each quantity by a dialog box.
Anyway, there’s not much else to say, except that I’ve put the code up on Gitorious for anyone who wants it (it’s licenced AGPL). The code is in a git repository for useful Python scripts I’ve written, right now it’s the only thing there, but I’m going to track down some of the scripts I’ve written over the years and add them too – I might even get a few blog posts out of some of them!
Today I’ve been playing with generators in Python. I have to say they are yet another awesome Python feature! They give you a very cool and efficient way to store some state within a function, this is useful if you’re trying to do something like generating a sequence, which you would otherwise need a class for. I’m going to go over a couple of quick examples here in order to demonstrate what generators are and what you can do with them.
Side note: You need Python 2.4 or above to use generators, you’ll probably have this anyway, but it might pay to check before wondering why this stuff doesn’t work.
A Quick Introduction
At it’s heart a generator is a function which can return a value several times within it’s body. Each time the function is called it resumes from where it terminated in the previous call, with all it’s internal state intact (i.e. variables are held between calls). In Python a generator is defined by creating a function which uses the ‘yield’ keyword. Each time that a yield is hit the function will return the specified value. Let’s start with an example which defines a generator implementation of the built-in ‘range’ function:
def genrange(start, end):
i = start
while i < end:
yield i
i += 1
As you can see this is incredibly simple and behaves in the same way as the standard range function. The only difference is that when the generator method is called it will return a generator object rather than a value (in contrast to range, which will return a list of values in the range). These generator objects are iteratable so calling the ‘next’ method on the object will run your code and return the next yielded value. Because the generator object is iteratable, the way in which you are likely to use it should look familiar:
for i in genrange(0, 10):
print i
If you want to go ahead and create a list from the sequence as you generate it, you can use a list comprehension, like so:
l = [i for i in genrange(0, 10)]
Of course, as hist comprehensions can be much more complicated than this you can use it to achieve more than just creating the equivalent list as calling the built-in range function. For example, you could filter the output elements according to some criteria, or call a method on each element returned.
A Trade Off
Although the above example is incredibly simple, it presents an interesting trade off between memory use and the speed of iteration through the ‘list’. The trade off exists because you could just build the list of elements in a separate function and then iterate through it. This is a perfectly valid approach but if there are a large number of elements you will use a significant quantity of memory to store the list. You are also more likely to double handle the data, unless you are building the list up over a period of time. Using a generator in this case is likely to be more efficient, as you are just creating the elements as you need them, so only storing a minimal amount of data at any one time.
The other side of the trade off is performance. If the process that produces your iterative data is computationally expensive, it makes no sense to calculate more values than you will need. So if you have a data processing loop, that can potentially exit early (i.e. before reaching the end of the ‘list’) a generator will be more efficient. But equally, if you require a tight loop which iterates quickly, you would want to pre-compute the values.
When considering using generators you should weigh up these factors and think which method is going to be most efficient for your situation. I think in a lot of cases, you will end up coming down in favour of the generator solution.
Another Example (Dan Brown is going to love me)
I’m going to leave you with another quick example, namely a very efficient way of generating Fibonacci numbers using a generator. Without further ado:
def fibonacci():
p1 = 0
p2 = 1
while True:
f = p1 + p2
p2 = p1
p1 = f
yield f
There you go, easy. Many other Fibonacci implementations use a recursive method, which is horribly inefficient. Most other implementations will require either a class to store persistent data in, or the use of static variables, both of which are more tricky to handle from a programming point of view.
Well that’s it. I think I’ve demonstrated that generators are pretty cool. The examples I’ve presented are very simplistic, but I’m sure you can think of awesome uses for them. Go forth and code!
This is just a quick post to share something cool that I was playing with the other day. I was wondering if it was possible to send a message from an Android application via the Mustard identi.ca client. It turns out it is, and here’s how you do it:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, "Testing intents on android");
startActivity(i);
These four lines of code hook into a key feature of the Android platform: Intents. The intent system is a neat way of communicating via apps and enables different apps to integrate together without knowing about each other specifically.
In this case, we are sending the ACTION_SEND intent, which Mustard is set up to handle. We set the mime-type of the message to ‘text/plain’ and put the contents of our message into the EXTRA_TEXT field. We then start the intent, and we’re done!
When the intent is started Android presents the user with a list of all the possible applications which can handle the ACTION_SEND intent. In my case this is Email, Gmail, Messaging and Mustard, when I select mustard the new message screen pops up and is populated with my message content. Hitting send posts the message and returns control to my app. Neat!
There are probably a whole host of ways you can integrate your apps with other apps on the phone using the intents system, I guess it’s just a matter of knowing what intents are received by each app and what they do.
Thanks to @macno (main developer of Mustard) and @bugabundo on identi.ca for their help working this out!
Anyway, I hope someone finds this useful. This is the first time I’ve posted about Android development, but I’ll hopefully write more about it in the future. Bye for now!
I use LVM on my main desktop machine. This is awesome because it allows me to dynamically allocate space to partitions as I choose, however I always forget how to do a resize, so I’m going to write it down here. This isn’t going to be a full LVM tutorial (there’s plenty of material out there for that), although maybe that’s an idea for the future.
The following commands will resize an ext2, ext3, or ext4 filesystem running on LVM while it is mounted:
$ sudo lvresize -L +XXG <path to fs device>
$ sudo resize2fs <path to fs device>
In the above command you need to replace XX with the number of GB you want the filesystem to grow by and <path to fs device> by the device node (typically /dev/mapper/something).
An there you have it, done! Obviously there is a huge amount more you can do with the two tools above, take a look at their man pages for more info.
Hopefully this post will save me from having to work out how to do this every time!
Apt is awesome. Plain and simple.
But it is kinda static. By this I mean it’s not particularly suited to environments where things change frequently. For example, we have a local mirror at uni, which of course it much faster than using the external Ubuntu or Debian ones, however as this is only available from internal University of Auckland IP addresses I would have to change my /etc/apt/sources.list file if I wanted to install something from home.
Today I knocked together a quick Python script to fix this, all it does is basically manipulate a symlink which points to the real /etc/apt/sources.list file, but I thought I’d share it anyway:
Continue reading »
Categories
Tags
adbard Android archlinux auckland awesome bzr conference crap crunchbang debian dhcp eeepc electricity facebook Fedora foss Free Software gallery3 google google analytics identi.ca java javascript kiwi pycon launchpad linux LVM mu-feeder netboot Open Cloud photos piwik pxe python site news skepticism SwallowCatcher tftp turnitin ubuntu UoA upmotw videos wifi wordpressI Also Contribute To...
Planets I'm On

