Personal tools
You are here: Home Planet
Aggregate your blog here
Fedora-My Community Planet , alpha stage at the moment, contact kagesenshi or sniffit in the irc channel if you want your blog to be in here.
 

Planet

Note to self: don't use list as a default value in class methods if its going to be modified later, as it might cause some trouble.

Just realized something:

In [1]: class MyClass(object):
...: def __init__(self,data=[]):
...: self.data = data
...:
...: def addData(self,value):
...: self.data.append(value)
...:
...:

In [2]: obj = MyClass()

In [3]: obj.addData('hello')

In [4]: obj.data
Out[4]: ['hello']

In [5]: obj2 = MyClass()

In [6]: obj2.data
Out[6]: ['hello']

In [7]: obj3 = MyClass([])

In [8]: obj3.data
Out[8]: []


In [12]: def myfunc(val,data=[]):
....: data.append(val)
....: return data
....:

In [13]: myfunc(1)
Out[13]: [1]

In [14]: myfunc(2)
Out[14]: [1, 2]

In [15]: myfunc(3)
Out[15]: [1, 2, 3]


At first i thought its a bug/weirdness, but after banging my head a bit to wall and a long stare at the ceiling, it made sense.

During class definition, the list object already instantiated. Therefore, the default value of data variable is a reference to a list object, rather than a new list on each instantiation.

Not quite sure how to explain this in words ..

In [1]: class MyClass(object):
...: def __init__(self,data=[]): # data->List object at 0x0001
...: self.data = data # self.data->List object at 0x0001
...:
...: def addData(self,value):
...: self.data.append(value) # self.data->List object at 0x0001
...:
...:

In [2]: obj = MyClass()

In [3]: obj.addData('hello')

In [4]: obj.data # obj.data->List object at 0x0001
Out[4]: ['hello']

In [5]: obj2 = MyClass()

In [6]: obj2.data # obj2.data->List object at 0x0001
Out[6]: ['hello']

In [7]: obj3 = MyClass([]) # []->List object at 0x0002

In [8]: obj3.data # obj3.data->List object at 0x0002
Out[8]: []


I think you got the idea.
I was looking around for template inheritance solution for repoze.bfg, as it seems like by default, BFG does not have context view traversal support to acquire template macros.

In Plone/Grok template inheritance can be done by registering main_template as a view, and using a template that fill the slots in main_template such as this:
<html xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
metal:use-macro="context/main_template/macros/master">
<head></head>
<body>
<div metal:fill-slot="main">
Some content
</div>
</body>
</html>


However, by default, repoze.bfg does not support getting view macro from a context. So a PageTemplate object need to by passed by a view in order to use the macro from the template. Eg:
from repoze.bfg.chameleon_zpt import get_template

def my_view(context,request):
main_template = get_template('templates/main_template.pt')
return dict(main_template=main_template)


So the view would be using:
<html xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
metal:use-macro="main_template.macros['master']">


Simple enough, but something came up my mind. "How about I theme this using Deliverance?"

What is Deliverance?

In simple terms, its a theming proxy which also available as a WSGI middleware. It allow developers to theme different systems without the need to know the internal of the systems. More information here.

Setting up Deliverance in repoze.bfg buildout

So lets get to the fun stuff.

I'm assuming that repoze.bfg is installed in a buildout, and a project called 'helloworld' is installed in the buildout, similar to the one I've shown in my previous post

First, add the egg:
[buildout]
...
[repoze]
...
eggs =
...
Deliverance
...


and run ./bin/buildout -vvv

Now, configure the Deliverance filter.

Open src/helloworld/helloworld.ini , rename [app:main] to [app:helloworld] and add these:
[filter:deliverance]
use = egg:Deliverance#main
theme_uri = /static/layout.html
rule_uri = /static/rules.xml

[pipeline:main]
pipeline =
deliverance
helloworld


Afterward, create a layout.html and a rules.xml in helloworld/templates/static with these contents:

layout.html
<html>
<head>
<title>The Theme</title>
</head>
<body>
<h1>A theme</h1>
<div id="content">
</div>
<hr>
</body>
</html>


rules.xml
<ruleset>
<rule>
<replace content="children:body" theme="children:#content" if-content="not:#content"/>
<replace content="children:#content" theme="children:#content"/>
</rule>
</ruleset>


Documentation of the rules markup are here: http://packages.python.org/Deliverance/configuration.html#rule

Now you're ready for profit :D

Start the server using ./bin/paster serve src/helloworld/helloworld.ini

Happy Hacking!
2 months without a post here .. T_T ... must .. restart .. blogging ...

Anyway, I've been poking around with repoze.bfg since last week and growing to love it by the days. Been thinking to post a blog on getting started on it but was a bit lazy .. until I saw lowkster's post about bfg at planet.foss.org.my a few days ago.

Full documentation at http://docs.repoze.org/bfg

A little review

I love Zope Component Architecture because the modularity/reusability it offers, but Bluebream, Grok and Zope2 feels a bit big for simple web apps or for introducing ZCA to new people. Then I saw BFG.

BFG simplifies many concepts which I'm familiar with in Zope2/Zope3, and it does it without overwhelming developers with other parts of the framework. Not endorsing any type of storage backend, and giving an option between URL routing and traversal or a mix of both is a plus.

The initial base code is simple and straightforward, and there are no need to subclass any parent class when you want to create your initial project.

The starter project is as simple as:

models.py:
class MyModel(object):
pass

root = MyModel()

def get_root(request):
return root


views.py:
def my_view(request):
return {'project':'helloworld'}


A registration of the view in configure.zcml:
  <view
context=".models.MyModel"
view=".views.my_view"
renderer="templates/mytemplate.pt"
/>


And its template file which uses TAL compatible markup.

As the initial requirement is simple, and theres close to no enforcement on how developers supposed to write something on it, it made it easy for new people to quickly learn it and start being productive. BFG too, being something that uses many Zope concepts, can utilize many existing Zope/Z3C components out there, and if a developer know how to utilize ZCA well, what developed on a BFG project may also be componentized and reused in other frameworks.

What to know more on whats cool with BFG? .. read their Sales Pitch ;)

Installation on Buildout

Depending on your distribution, BFG might be available in your distro repository (Fedora have it). However, being a Zope/Plone developer as my main job, whenever it comes to installing python applications from pypi, I tend to prefer to use buildout to create a self-contained environment. So I'll use buildout here too.

First, if you don't have zc.buildout yet, install it using:
$ easy_install zc.buildout


Now lets create the contained environment
$ mkdir -p ~/repozebuildout/src
$ cd ~/repozebuildout


Then create a buildout.cfg file in the directory with this config:
[buildout]
parts =
repoze
develop =
src/*
versions = versions

[repoze]
recipe = zc.recipe.egg
eggs =
repoze.bfg
interpreter = python
entry-points = paster=paste.script.command:run

[versions]
repoze.bfg = 1.2.1



What the buildout config will do is, it will create a buildout with repoze.bfg framework and its dependencies installed, with an interpreter script configured for the buildout environment, and a paster script.

Now initialize the buildout
$ buildout init
$ ./bin/buildout -vv


After the buildout initialization is done, you may start creating the project skeleton.

Creating your first project

There are several templates to choose from: bfg_starter, bfg_routesalchemy, bfg_alchemy, and bfg_zodb.

bfg_starter template simply give a very basic bfg skeleton to get started. Those who are familiar with Pylons/Django might want to look at bfg_routesalchemy and those who are familiar with Zope might want to look at bfg_alchemy and bfg_zodb.

For this example, i'll just create a simple project using bfg_starter template.

cd src/
../bin/paster create -t bfg_starter helloworld
cd ..


then , edit buildout.cfg and add helloworld into the eggs section:

[buildout]
...
[repoze]
...
eggs =
...
helloworld
...


afterward, rerun buildout
./bin/buildout -vvv


Once that is done, you may start the server using:
./bin/paster serve src/helloworld/helloworld.ini


Now you can start developing on BFG!. Read the documentation to get you started in developing on BFG.

Happy hacking :D
Those who are familiar with BSD Jails or Solaris Zones might be quite familiar to the concept of OS-level virtualization. The Linux world have several options for this, such as the User Mode Linux (UML), OpenVZ, Linux Vserver, and most recently, Linux Container (LXC).

Unlike the other alternatives, which requires specially patched kernels to run it, LXC got fully functional in the upstream kernel since 2.6.29, which is a plus as it made it easier to deploy it in many prexisting distros which ship 2.6.29, including Fedora - without messing around with the distro packages.

In this HOWTO, I'll guide through the process of deploying your own LXC in Fedora 12, with NAT to libvirt's virbr0

Preparation

yum install libvirt lxc


(ping me if i forgot to add any packages)

start libvirt so that virbr0 is configured
/sbin/service libvirtd start


mount control group in /cgroup
echo "none /cgroup cgroup   defaults  0 0" >> /etc/fstab
mkdir /cgroup
mount /cgroup


LXC Fedora Script

The lxc rpm provides a script named lxc-fedora to aid creation of lxc container. However, when I tried it, it doesnt quite work. After some googling, I found this guide http://blog.bodhizazen.net/linux/lxc-configure-fedora-containers/, of which, based on it, I've updated the lxc-fedora script accordingly to make it work using febootstrap.

You can grab the script in my FedoraPeople GIT repository here : lxc-febootstrap

Download the script and save it somewhere in you system path.

Creating the Container

lxc-febootstrap create


The script would install the container rootfs in /var/lib/lxc/rootfs/. So make sure you have enough free space to store it (around 400MB for the base bootstrap). You'll also need approximately 400MB in /var/cache/lxc for the reusable vanilla copy of the rootfs.

Answer the questions from the script, and wait until the process is done. At the end of the process, the script would ask for a password for root. Set it.

Controlling the LXC Container

You can start the container using:
lxc-start -n <containername>


You can start the container in background using:
lxc-start -d -n <containername>


Stop it using:
lxc-stop -n <containername>


Destroy it using:
lxc-febootstrap destroy


Connect to a console of the LXC using:
lxc-console -n <containername>


Networking

The lxc-febootstrap script have been configured to make use of libvirt's virbr0. It is a virtual bridge, so treat it like any other networking bridge in Linux.

The default configuration of virbr0 (if i'm not mistaken) is that it have dhcp and routing already configured for virtual machines that utilize it. So, networking should JustWorks (unless I missed certain steps in this guide, currently it worksforme).

Thats it .. Enjoy and Happy Hacking :D

FOSS.Org.MY (formerly: MyOSS) Meetup is BACK!!!! with a new name, and a new format, and new timing!!

We will be having Lightning Talk session after the speaker's talk, if you have anything to share, do contribute to the Lightning Talk.


==================================================
Open Source Software in Digital Content Creation
==================================================

A general overview of OSS used in Digital Content Creation industries with a focus in animation production from small personal projects to Hollywood production.

==================
About the Speaker
==================

Victor Yap helps companies build cost-efficient production workflow for 3D animation. His current project migrates open source solutions, with programs like Blender to a constantly developing 3D animation environment. Blender is a strong open source contender to mainstream programs such as Maya and Softimage. Armed with an Electronic Arts degree and deeply involved with Open Source Software (OSS) development since 1996, he presently dedicates himself to using open source as a tool for digital content creation. True to the spirit of OSS, he wants to share his experiences, insights and knowledge with everyone.

======
When:
======

Saturday, 20th March 2010

======================
What - Meetup Agenda:
======================

2:30pm - 2:45pm : Registration
2:45pm - 3:00pm : Opening by the organizer
3pm - 4pm : Talk
4pm - 4:30pm : Lightning Talks
4:30pm - 5:00pm: Breakout Session

=====
Cost
====

Free of charge (gratis)

=======
Contact
=======

Mohd Izhar Firdaus Ismail
izhar@foss.org.my
+60172792765

======
Where
======

iTrain (M) Sdn Bhd,

Unit E-7-1, Block E, Megan Avenue 1, 189 Jalan Tun Razak, 50400 Kuala Lumpur, Malaysia

================
About the Meetup
================

This meetup is possible due to the support and facilities provided by iTrain Sdn Bhd (http://www.itrain.com.my/)



Facebook RSVP of this event
Full event details, including map