Archive for May, 2005

MAKE: Simple motor (and a correction)

Sunday, May 8th, 2005
Motor running

Roger and I finally got around to making the simple motor illustrated in O’Reilly’s MAKE. It works like a charm.

There is one error in the original article. It says to scrape the insulation off of 1/2 of the wire on both ends. You only need to scrape 1/2 of the insulation off one end. The other end should be fully scraped.

This makes it considerably easier to make the motor run in that you don’t have to worry about lining up the two half-scraped wired such that they both touch the safety pins at the same time.

Spinning;  stopped by flash.

The motor also runs at different speeds depending on which you tilt the whole assembly (rotating around the battery). This makes sense. It changes the angle at which the moving magnetic field of the coil interacts with the fixed magnetic fields from the magnets. More importantly, it also changes the position of the coil relative to the magnets when the current is switched on/off because of the half-stripped wire at one end of the rotor. If you click on the photo to the right, it will take you to an annotated photo at flickr that describes the various bits of the motor.

Posted in Science | No Comments »

Updated Flickr Saver & a challenge…

Saturday, May 7th, 2005

I updated the Flickr RSS ScreenSaver (link actually works now) to actually have a configuration sheet and too allow the RSS feed to be customized from the UI. It is far from perfect.

The groundwork has been laid to handle multiple RSS feeds simultaneously. Currently, the UI — the Quartz Composer based presentation layer — needs to b updated to fix the presentation.

Hence, the challenge. I’m out of time on this and, besides, I have the design skills of your average Border Collie.

So, if anyone cares to create a new Quartz Composer presentation for the photo feeds, I will be happy to integrate it into the screen saver. If I get enough good ones, I’ll add a feature to allow the user to choose between different kinds of presentations.

Quartz Composer is a lot of fun to play with and you don’t even need to quit System Preferences to make live changes to the presentation piece.

If you install the saver in ~/Library, then:

open ~/Library/Screen\ Savers/FlickrFeeds.saver/Contents/Resources/FlickrFeed.qtz

And, once opened in Quartz Composer, bring up System Preference’s screen saver module and select the Flickr screen saver. After edit/save in Quartz Composer, hit the “test” button in System Preferences and the test view will reload the newly saved QC document.

Posted in Mac OS X, Technology | No Comments »

Quartz Composer based Flickr photo feed Screen Saver

Saturday, May 7th, 2005

I did a bit of recreational programming this evening and hacked up a screen saver that can be subscribed to a Flickr photo feed. It will grab the images on the feed and present a basic slideshow with each image and its title.

At the moment, it is just a proof of concept. It totally lacks a configuration UI and the Quartz Composer patch could use a total redesign. Actually, it wouldn’t be hard to integrate the RSS screensaver’s patch into this screensaver to give fancy, spinning, thumbnails instead of the flat rendered versions that currently appear.

There are three user defaults that can be used to control the screensaver.

defaults write net.bbum.FlickrFeeds Duration 30 will set the time to display each photo to 30 seconds (default is 20).

defaults write net.bbum.FlickrFeeds FlickrURL …url… can be used to change the RSS URL that the saver downloads images from. It should be able to parse any Flickr RSS or Atom feed, but maybe not even that. Out of the box, it is aimed at my flickr photo feed.

Finally, defaults write net.bbum.FlickrFeeds FlickrURL ShowFPS YES will display the frames-per-second in the lower left corner.

Source is available. What isn’t implemented in Quartz Composer is written in Python via PyObjC. So, you will need to have PyObjC (top-of-tree) and Mark Pilgrim’s feed parser installed to build the source. The binary download contains all the required bits in the bundle thanks to the wonders of py2app.

The source is divided into two pieces. FlickrFeeds.py demonstrates how to embed a QCView (a class to embed Quartz Composer document into a Cocoa view hierarchy) into a screen saver and pass custom values into the embedded QC patch. FeedManager.py contains all of the RSS download-and-parse code. It is designed to eventually do the download in the background, maybe.

Posted in Mac OS X, Software, Technology | No Comments »

OutlineEdit as a PyObjC application

Monday, May 2nd, 2005

The Tiger developer tools includes several Core Data examples, including OutlineEdit which demonstrates how to manage a hierarchical object graph and display said graph in an NSOutlineView.

PyObjC contains a python version of the same application.

The example’s setup.py also demonstrates how to set up a multiple document based application that compiles an Xcode created model file and sets up an Info.plist with several registered document types.

Posted in Mac OS X, PyObjC, Technology | No Comments »

Using Core Data from Python

Sunday, May 1st, 2005

Update #1: Putting this at the top, because it is really important. The example changed since yesterday and this now requires top-of-tree [TOT] PyObjC. In particular, all of the methods that take NSError ** parameters now take one less argument, omitting the error argument, and return a tuple where the first element of the tuple is method’s normal return value and the second element is the resulting NSError, if any.

In other words, you will either receive a tuple of the form (result, None) or a tuple of the form (None, result), but never one that has both items set or both items set to None unless there is a bug in the underlying API.

The current TOT of PyObjC adds support for automatically adjusting the signatures of any methods that take NSError ** arguments. It is less than optimal in that there is no way to pass NULL for the error argument and, therefore, no way to indicate to the underlying framework that it should not pay the potentially expensive price of instantiating and composing the contents of an NSError* instance.

Someday, that will change. But it will be a hard change to make. So, for now, we have a working solution that supports everything needed to build full featured applications with a relatively minor performance penalty in relatively odd situations (it isn’t often that you would want to call one of these methods without collecting the error information).

These changes will be in PyObjC 1.3.1.


Via PyObjC, you can write Python applications that fully take advantage of Core Data.

PyObjC fully supports KVC, KVO, and subclassing of Objective-C from Python, so use of NSManagedObject is fully enabled.

One caveat: models edited in Xcode are saved in a “source” format that is “compiled” into a binary representation as a part of building a project. A compiler is provided to do exactly that and it is called via a standard build rule as a part of Xcode’s build process.

An example would best illustrate how to manually use Core Data from Python, including compiling a model. In this example, we compile the model from the EventManager Core Data example.

First, link momc to somewhere easily accessed (this is all one line and assumes that ~/bin/ exists):

ln -s /Library/Application\ Support/Apple/Developer\ Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc ~/bin/momc

Then, compile the sample model and copy the CranberryFestival sample data to someplace useful:

mkdir test
cd test
momc /Developer/Examples/CoreData/EventManager/MyDocument.xcdatamodel em.mom
cp /Developer/Examples/CoreData/EventManager/Sample\ Event\ Files/CranberryFestival.events .

Now, to fire up python and work with Core Data. This assumes top of tree of PyObjC or the next production release. I ripped out the ‘>>>’ characters for copy/paste convenience. Anywhere Python normally spews something, the output shows up in italics.

First, create the managed object model (and spew a little information about an entity within):

from CoreData import *
momURL = NSURL.fileURLWithPath_("em.mom")
mom = NSManagedObjectModel.alloc().initWithContentsOfURL_(momURL)
mom.entities().valueForKey_("name")
(Event, EventParticipant, Location, Occasion, Person)
eventEntity = mom.entitiesByName()['Event']
eventEntity.attributesByName().keys()
(eventID, detailDescription, endTime, name, startTime, date)

Now, set up the persistent store coordinator and managed object context:

dataURL = NSURL.fileURLWithPath_("CranberryFestival.events")
psc = NSPersistentStoreCoordinator.alloc().initWithManagedObjectModel_(mom)
psc.addPersistentStoreWithType_configuration_URL_options_error_(None, None, dataURL, None)
(, None)
moc = NSManagedObjectContext.new()
moc.setPersistentStoreCoordinator_(psc)

Finally, fetch some Events and display some information:

fetch = NSFetchRequest.new()
fetch.setEntity_(eventEntity)
events, error = moc.executeFetchRequest_error_(fetch)
len(e)
5
for anEvent in events:
  print anEvent.valueForKey_(u'name')
Opening Ceremony
Registration
Lunch by the Bog
Pick your own Cranberries
Educational Workshop

Of course, you can always take the same approach as the Core Data Document Based Application and do everything via Interface Builder and Xcode, thus automating all of this per NSPersistentDocument.

Posted in Mac OS X, PyObjC, Technology | No Comments »

Yerba Buena Nursery

Sunday, May 1st, 2005
Lichens on Fence

We headed over to Pescadero State Beach and Bean Hollow State Beach today.

On the way, we stopped off at Yerba Buena Nursery. Amazing place. They have an incredible collection of California native plants as well as exotic ferns. The nursery is nestled into a valley and is reached by driving down — way down — a winding gravel/dirt road for about 2 miles off of Skyline Boulevard. This is the most “not to scale” map ever as every bit of that drive follows very windy roads over the mountain.

Amazing place. The photo is one of about a billion lichens growing on the various fences and “open walls” used throughout the nursery. I am looking forward to returning for a full tour.

We picked up a couple of drought resistant plants that should do well in the front of the house along the driveway.

Posted in Nature | No Comments »