Something to pass the time...

Nothing particularly interesting to report, while I'm sitting here watching a custom-built automated test suite chug away (which incidentally takes approximately 5 hours to run... at 100% CPU usage on 1 core... ugh) - I thought I'd muck around with Iron python and base4... my inspiration was this "On the fly" schema creation on the base4 site, however I wanted to just grab an existing assembly (from the server no less) and start using it's types in Iron Python... it's pretty easy (and quite cool, think about the implications of no references, every time you run your app the schema might have magically grown some more features ;o)

Getting Started...

First off, lets fire up the trusty iron python console... I like colours and being able to tab through member lists, so I'm going to throw in some command line parameters...

ipy -X:TabCompletion -X:ColorfulConsole

Cool, consoles up and running so lets get down to business... first things first, loading up the Base4.Storage assembly...

import sys
import clr
sys.path.Add("C:Program FilesBase4 Solutions LtdBase4 version 2.1.1.85")
clr.AddReferenceToFile("Base4.Storage.dll")
from Base4.Storage import *

All done... now before we can do anything useful, we'll need to setup our default connection string (read: default context)...

StorageContext.SetDefault("tcp://Server:@localhost:999/Base4_default")

This is the amiga speaking...

Now the fun begins... if you have System.Speech available, grab that puppy too... because deep down we all know that the thrill of making your computer talk still exists... especially if you started out your days on an Apple or better yet Amiga...

clr.AddReference("System.Speech")
from System.Speech.Synthesis import *
synth = SpeechSynthesizer()

Sweet, now lets view (or listen) for all the available schema assemblies...

for asmFile in StorageContext.FindAll[SchemaAssemblyFile]():
synth.SpeakAsync(asmFile.Name)
print asmFile.Name

Loading the schema...

At this point you'll end up with a list of the assembly files (and hopefully, your machine will be droning away notifying of
you just what they're called... ;o) in my case I have a little "Experiment.dll", so lets load that up... I'm pressed for time so I won't be putting in speech detection for selecting the appropriate schema, though it's both entirely possible, and pretty easy to do...

asmFile = StorageContext.FindOne[SchemaAssemblyFile]("Name='Experiment.dll'","")
assembly = experimentAssembly.LoadAssembly()
clr.AddReference(assembly)
from Experiment import *

And yes, the strongly typed schema is now there for me to play with...

The experiment schema contains among other things an "Order" type, which holds a collection of "OrderLine" line's... so we can now do this:

order = Order()
line1 = OrderLine()
line1.UnitCost = 30
line1.NumberOfUnits = 2
order.OrderLines.Add(line1)
order.Save()

I think it makes quite a handy diagnostic tool, especially when you start considering that your dev, test and live environments may all have different schema versions...  it could certainly save a lot of stuffing around - it would be fairly tedious to repeat this exercise in C# by comparison (especially once you've written a few useful helper functions in your own "base4" python module... I could shorten this entire post to 3 or 4 lines instead).

Written on November 8, 2006