YAML and SKDB: The Super Lego Tutorial

images/gnusha-only.png

Topics covered:

SKDB, YAML, packages ... plus how to design, make and share a "super lego"

SKDB makes it easy to download hardware from the web. It does this by leveraging volunteers known as "package maintainers". Package maintainers look at an open source hardware project and put all of the project resources in one place, the same way each time. You don't have to reinvent the wheel each time you do a project. How does this work? These packages are literally re-usable building blocks.

images/lego_snapping4.png

Under the hood, the legos above are represented as python classes. You can play with these objects just as you would any other objects in any programming language. Sometimes you want to make new instances of legos, with particular parameters and specifications. Maybe you want a super-lego, with a width of 20m and a thousand pegs. YAML makes it easy to share your "super lego".

>>> import skdb
>>> legos = skdb.Package("lego")
>>> #pick a lego from the catalog
>>> my_lego = legos.parts[0]
>>> print my_lego
Lego(name=2x2 round brick, num_pegs=4, num_holes=4)

>>> #i wanna make a super lego
>>> super_lego = legos.Lego("my super lego", num_pegs=1000)
Lego(name=my super lego, num_pegs=1000, num_holes=4)

>>> #convert it into YAML for sharing
>>> data = skdb.yaml.dump(super_lego)
>>> #now save it to a file
>>> file_handler = open("super_lego.yaml", "w")
>>> file_handler.write(data)
>>> file_handler.close() #because we're not evil
>>> print "hey bob, would you like super_lego.yaml?"
>>> #go give the file to bryan bishop if you want it to be included in the 'official' repository

What is YAML? YAML is a human friendly data serialization standard for all programming languages. For python, you should use PyYAML. Please read the fantastic PyYAML tutorial for more details.

Here's a little taste of why you should be interested in YAML for SKDB.

>>> import skdb
>>> legos = skdb.Package("lego")
>>> #pick a lego from the catalog
>>> lego1 = legos.parts[0]
>>> lego2 = legos.parts[1]
>>> #make a generic lego
>>> lego3 = legos.Lego(name="lego name goes here", num_pegs=4, num_holes=4)
>>> #how can we connect them?
>>> print lego1.options(lego2)
[Connection(Feature(name=stud), Feature(name=anti stud)), Connection(Feature(name=anti stud), Feature(name=technic stud)), Connection(Feature(name=anti stud)
, Feature(name=technic stud)), Connection(Feature(name=stud), Feature(name=anti stud)), ...]

Where's the YAML? Hidden. If you wanted to work on the "lego" package, you would point your shell to /usr/local/share/skdb/lego/ and look at metadata.yaml, data.yaml (the part catalog), and lego.py (the Lego class).