How can I modify a Python attribute any time it is accessed?

Originally posted on my Python Wiki

I was designing a UI for a PyGame program I am working on, and needed a way to pass a value defining a “row height” into the functions that rendered my text to the screen. Since the UI could change, I didn’t want to have to hard-code positions into each element, later to modify it and have to redo all the positions.
What I came up with was a simple class with a single usable attribute, called val. Through using properties, I’m able to control how the attr behavies at time time its value is queried:

class Row(object):
    # class to store the current row location in the UI
    # Each time it is called, it will increment its value
    def __init__(self, val):
        self._val = 0
        self.orig = val
    @property
    def val(self):
        self._val = self._val + self.orig
        return self._val
row = Row(16)

print row.val, row.val, row.val, row.val
# 16 32 48 64

Properties have getter, setter, and deleter methods, but the default is getter, which I used above. So as you can see, each time I call to print, it accesses the val property (via the getter), and updates the internal counter.
This is a PyGame code snippet showing it in use:

overlay.blit(bubble, (8, row.val))
overlay.blit(toggleText, (8, row.val))
overlay.blit(lmb, (8, row.val))

Rather than having to specify a Y value for the last arg of the tuple, I can simply pass in my object, and it passes out the current new position, based on how many times it was called before.

I have no doubt there is probably some slicker way in Python, but it’s what I came up with on the spot :)

PyGame Wiki created

It was only a matter of time:  I find Tiddlywiki’s a great way to store online notes.  I currently have made them for a variety of subjects (as shown on the left sidebar of my page).  They can be hosted for free through tiddlyspot.com.  Other than the funny name, and trying to explain it to people, they’re a wonderful data-publication medium in my opinion.

Based on my previous posts on ‘what to make a game in’ (1, 2, 3, 4, 5), I finally settled on PyGame, and have had a local copy of my ‘PyGame’ wiki for a few months now.  But today, I’ve got it hosted online.  Not a huge amount of info on it yet, but no doubt it will grow over time.  Enjoy

http://pygamewiki.tiddlyspot.com/

Electric Bicycle Conversion, phase 2…

IMG_7459Continuing from  my previous post, I’ve (with the help of my visiting father) got the majority of everything assembled on my eBike conversion.  Again, the motor was purchased from AmpedBikes.com,  the SLA batteries from Gruber Power Services, and all other bags and whatnot from my local REI.

You can see more pics on Flickr here.

Took a bit longer than the “one hour” the web site described, but we got it done in the end.  Taking it out for it’s first run, it’s a truly eerie feeling hitting the thumb-throttle, and having the bike pull away with you on it.  A few things about its performance:

  • It won’t win any speed awards 😛  But I have no doubt you can get up to 20-mph with little effort on your own.
  • It will take you up hills without any peddling, but it is slow.  However, peddling with almost no effort will shoot you right up the hill.
  • Those three batteries are quite heavy.  The bike, with the motor, is now much heavier than stock.  You could ride it on the level if you had to unassisted, but I wouldn’t want to go up any hills.  It is top heavy… going to look at methods for moving the batteries down off the rear-rack, into the middle of the lower frame.

Things left to do:

  • Adjust front brakes:  Need to be made stronger.
  • Clean\replace rear brakes.  With that much weight, are almost useless.
  • Maye think about rear disk-brakes.
  • Other standard maintenance.

The only main goof up was with the charging system:  The instructions give you no info on how to wire in the wires for the battery charger that are provided.  I emailed AmpedBikes, and they didn’t give much of an answer other than “people disconnect the battery from the controller and plug it into the charger”.  So we went out, got a quick-disconnect that would mate with the battery line, and wired that into the controller.  However, the positive and negative on the quick disconnect were opposite from what was on the battery lines, and we didn’t realize it until a day later.  But after swapping the connections, the charger is now happily running.  Hopefully I can ride it more tomorrow!

I’ll report more after I’ve ridden it around for a while :)

BubblePaint v0.01

BubblePaint.001

This is the results of my latest efforts to learn PyGame using PyMunk, a Python wrapper around the Chipmunk 2D physics engine.  Eventually I hope to use this knowledge in a physics-based tank game, but right now, I’m just having fun ‘painting’ with the ‘bubbles’.  There’s no great magic going on in my opinion, just some weekend coding fun.

In a nutshell:

  • You paint with “bubbles” on the canvas using the mouse.  It expects you have a 3-button mouse with a scroll wheel in the middle (because that’s what I have).  Since its physics based, the bubbles will push each other around, and none will be overlapping (if given time to settle).
  • LMB-drag draws bubbles
  • MMB-drag up\down: changes bubble size. Bigger bubbles get darker, smaller bubbles get lighter.
  • RMB-drag up\down: change brush hue
  • Mouse-wheel: change pressure of bubbles (number applied at once, from 1->10)
  • ‘s’ will save ‘test.png’ in the install dir.

You can find the Python source here:

And you can find a zipped Windows executable here:

Future plans for this include:

  • Shapes other than circles (square, triangle, random-polygon)
  • Ability to enable gravity, and add static rigid bodies (more of a physics sandbox at that point)
  • Images for the ‘bubbles’ rather than just solid color.
  • The ability to ‘dry’ the canvas to allow for multiple layers of painting.
  • A smarter way to save images.
  • A toggleable overlay layer showing stats.
  • Change the background color.
  • User defined resolution.  Currently set to 768×768

But we’ll see how much of that happens, I have family coming into two for the next few weeks :)

Academy of Science