This month it was my turn to give a talk at the West Yorkshire Python Usergroup. In attendance were Simon Davy, Bernie Czenkusz, Peter Russell, Ed Saxton and myself. The aim of the talk was to present the structure of the Zope Toolkit, its place and role in other frameworks, and to explain the technical details Zope Component Architecture in a balanced and fair way.
Overall I was happy with the talk: people seemed to already have a lot of knowledge about the various topics I was presenting, which made for some good questions and discussion. If I could criticise any of it, I'd say it was probably too long - weighing in at around 1hr 40mins, but perhaps that was because there was a lot of questions. I expected it to be about an hour in total!
To prepare the images for the slides I used Xara on Linux. It's very good for drawing and manipulating vector diagrams and ideal for illustrations. I put the slides together in Open Office Presentation and exported them as a PDF. This worked extremely well and can highly recommend this approach for simple presentations such as mine. I've made them available here.
Looking forward to next month: Peter Russell committed to a talk on the XML and HTML parsing library 'lxml'. Bernie tried to pin some of us down for writing a Python Rag article for March, although I'm not totally sure what the outcome of that was. Simon pitched the idea of setting up a 'WyPy Blog', which we all contribute to regularly. Perhaps Bernie could cherry pick the best of the bunch for his Python Rag magazine?
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Friday, 22 January 2010
Thursday, 24 December 2009
Article in Python Rag
Next month's Python Rag contains an article written by me on the topic of namespaces. Knowing what namespaces are and where they are found helps you understand how the language works and explains how simple its concepts of functions, classes and modules are.
On balance I'm happy with the article; I've never written anything like this before and I think I've communicated the point. However, I've definitely identified areas that could be improved. For example:
Fortunately, next month (Feb), I'm writing a follow-up on Python scopes which ties in quite nicely with namespaces. This is the perfect opportunity to address these issues and write a better article!
On balance I'm happy with the article; I've never written anything like this before and I think I've communicated the point. However, I've definitely identified areas that could be improved. For example:
- The article doesn't flow. This is because I've tried to back each assertion I make with proof at the Python interpreter. As a result the narrative flow gets over punctuated with example excerpts, and becomes difficult to read and hard to follow. Perhaps I'm too used to writing doctests! Instead, I think I should have dedicated a large set of more creative narrative to each point, and offered bigger and fuller examples to back things up.
- Not enough visuals. Many of us are visual thinkers, tending to draw pictures in our minds representing abstract concepts. One thing that I think is important about understanding namespaces is that they allow you to visualize python programs, as a sort of containment hierarchy. I really think I could of draw some attractive images to help communicate my point a little better.
- Not creative or imaginative. Although the article is technical in nature, it doesn't hurt to use a few metaphors here and there, or provide some historical context to certain features or throw in some personal reflection. Perhaps I was preoccupied with being technically accurate, so the result is a little dry.
Fortunately, next month (Feb), I'm writing a follow-up on Python scopes which ties in quite nicely with namespaces. This is the perfect opportunity to address these issues and write a better article!
Tuesday, 22 September 2009
Generator for distributing calls
At work I was confronted with the problem of 'How to distribute calls between 2 call centers in a fair and even way according to some yet-to-be-determined percentage split ration?' Taking 30% split, we cannot send the first 30 to call centre 1 and the remaining 70 to call centre 2. The calls must be distributed as evenly as possible throughout; for example split 30% or 10 calls would give: [1,2,1,1,2,1,1,2,1,1] and not [1,1,1,1,1,1,1,2,2,2]!
When thinking about these kinds of problems, it helps to abstract away from the context of calls and call centres and think about the underlying problem; distribution over a descrete choice space. From here, we might find a solution in another domain that solves our problem...
It turns out that the problem of call distribution distils to the same problem that Bresenham tackled when trying to draw a line on a computer screen matrix. Imagine that you have a graph of cells 100 pixels on the X axis and 100 pixels on the Y axis. Now imagine you are using the line drawing algorithm to paint a line from the X/Y origin to, say, the coordinates '100,30'.
The line drawing algorithm will, after 100 interations, have distributed pixels evenly between 70 in the X direction and 30 in the Y direction (but always moving towards X). This is exactly the same as distributing 30 calls to call centre 1 and the rest to call centre 2.
To model this, it makes sense to write a python generator that 'yields' the correct call centre when asked:
If I have chance I'd like to expand this to take a 'number of call centres' parameter, rather than having it restricted to 2.
When thinking about these kinds of problems, it helps to abstract away from the context of calls and call centres and think about the underlying problem; distribution over a descrete choice space. From here, we might find a solution in another domain that solves our problem...
It turns out that the problem of call distribution distils to the same problem that Bresenham tackled when trying to draw a line on a computer screen matrix. Imagine that you have a graph of cells 100 pixels on the X axis and 100 pixels on the Y axis. Now imagine you are using the line drawing algorithm to paint a line from the X/Y origin to, say, the coordinates '100,30'.
The line drawing algorithm will, after 100 interations, have distributed pixels evenly between 70 in the X direction and 30 in the Y direction (but always moving towards X). This is exactly the same as distributing 30 calls to call centre 1 and the rest to call centre 2.
To model this, it makes sense to write a python generator that 'yields' the correct call centre when asked:
class CallDistributor():
def __init__(self, split=50):
self.split = split
if split > 100 or split <= 0:
raise ValueError
def _execute(self):
error = 0.0
step = 0
deltaErr = self.split / 100.00
yield 1
while True:
error += deltaErr
if abs(error) >= 0.5:
yield 2
error -= 1.0
else:
yield 1
def __iter__(self):
return iter(self._execute( ))
if __name__ == "__main__":
dist1 = CallDistributor(split=50)
anIter = iter(dist1)
for i in range(10):
print anIter.next()
If I have chance I'd like to expand this to take a 'number of call centres' parameter, rather than having it restricted to 2.
SqlSoup is cool
I've been playing with our internal wiki's database, and with SqlSoup. I've found that SqlSoup did exactly what I wanted it to do; read a database, infer its table layout, create classes that map attributes to those table columns and allow me to manipulate those classes before adding the modifications back.
This code prints out all the rows in the 'People' table:
How simple is that! :-)
This code prints out all the rows in the 'People' table:
from sqlalchemy.ext.sqlsoup import SqlSoup
db = SqlSoup("mysql://root@localhost/movements")
people = db.People.all()
people.sort()
import pprint
pprint.pprint(people)
How simple is that! :-)
Tuesday, 13 January 2009
Grok Configuration Phase and its Extension
~~ DRAFT: Updated 20th Jan 13:53 GMT ~~
Prerequisites:
Martian PyPi Documentation
Understand 'grokking' and 'grokkers'.
setuptools
zc.buildout
Audience:
Developers
To understand how Grok is organised and how to extend Grok, we must first understand how Grok locates, registers and configures components. At startup, Grok discovers utilities and adapters so they can be registered the Zope Component Architecture for later retrieval by interface. The registration is done by Martian, and the process of scanning packages and grokking them is known as configuration.
A Martian Refresher
A Grok project consists of a number of components (predominantly classes) that have different roles, such as views, containers and models, characterised by an interface, that are grokked during the configuration stage. Grokking is made possible by the definition of three things:
As you can see, the name and PersonGrokker classes both inherit from Martian classes, and this allows Martian to register our PersonGrokker using something known as a meta-grokker. A meta-grokker is responsible for registering grokkers into Martian's own registry, which is constructed as modules are scanned during the application's configuration stage. A meta-grokker is conceptually the same as regular grokking, except that it extends Martian. Naturally, before classes can be registered, Martian must know about PersonGrokker. So, Martian must scan modules that contain Grokker definitions, conventionally named meta.py, before classes that inherit from Person.
Martian can then go onto scan subclasses of Person:
If any of this is confusing, then I suggest you read the Martian documentation and write your own grokkers. Otherwise, the important thing to remember is that we must first register a grokker before Martian scans our class.
The ZCML Configuration Chain
How does Martian discover the various components and directives during configuration, and how does Grok ensure that meta-grokking occurs before regular grokking? Both are achieved using a extensions to Zope's XML based configuration process; ZCML. ZCML is based on extensible directives that, in our examples, are completely separate from Martian directives.
When the configuration stage begins (during startup) for a grokproject created package (which we'll call myproject), it first looks at the
This instructs the Zope configuration machinery to visit the specified package and include/process the directives found within the configure.zcml file. (The include directive can also take an additional file parameter that overrides which file it includes from the specified package.) Our configure.zcml file looks like:
These directives are processed sequentially - so let's look at them in turn. First, it includes the Grok package's configure.zcml. This is quite a large configuration file which we'll mostly ignore; the important directive is:
We now process meta.zcml, which contains the following three relevant directives:
The z3c.autoinclude and grokcore.component lines enable two new ZCML configuration directives to be used from here:
The 'grok' ZCML Directive
The 'grok' directive allows us to specify packages to grok for configurable components. If you use the following:
Grok will, using the Martian system described above, scan the target package or module, (meta.py in the example given) registering anything that subclasses a known grokkable baseclass. After the grokcore.component configuration has registered this new 'grok' directive, it instructs itself to meta-grok its own meta.py that contains various grokkers for all the basic Zope building blocks - such as grok.Adapter, grok.GlobalUtility. So, from here files grokked containing subclasses of any of these will be registered in the Zope Component Architecture.
The 'includeDependencies' ZCML Directive
The includeDependencies directive, used as follows:
will iterate (in order) over the given package's setup.py for entries in the "install_requires" section and include any meta.zcml and configure.zcml files automatically (in that order) from those. Since meta.zcml will conventionally contain the directive to scan meta.py, our grokkers will be discovered before configure.zcml instructs Martian to grok any configured components.
---
Thus - returning to the meta.zcml in the Grok package - once Grok's meta.py is scanned, the system will know how to go on and grok classes that inherit from grok.Site, grok.Application, grok.REST etc, and all the other central Grok baseclasses that we use to construct our applications. Any packages that are scanned from now onwards can register these types of components.
Moving back to our package's configure.zcml, the next directive is:
So, our setup.py's "install_requires" is scanned for packages. We have no new entries in here at the moment, but this mechanism will be later used to include custom grokkers as independent and separate packages.
Finally, the last directive is:
This gives the order to scan through the current package (all .py files), grokking anything it knows how to. So, this is how our main application is registered (it inherits from grok.Application), how all our views are registered (grok.View) and indeed all other grok.* subclasses.
The Truth about Configuration
Until now, you'll probably be under the impression that the Grok machinery is registering components as it goes, and that when a global utility is found, for example, it immediately adds it to the Zope component registry. This is only partly true: Martian is indeed registering grokkers, but when these grokkers grok classes they are not immediately placed into the component registry.
Instead a function/position args/keyword args set, known as an action, is passed to the configuration machinery to be invoked later. Along with this set a 'discriminator' is passed, which if not None, will raise a configuration conflict exception if two actions contain the same discriminator. This allows Grok/Zope to enforce configuration polices, such as "we cannot allow two or more adapters with the same name, adapting the same context and providing the same interface".
Additionally, an order parameter can be specified optionally which dictates the order in which actions are called. Once configuration is completed, all the actions are processed in the order they were created unless order values dictate otherwise.
Extending Grok with Custom Grokkers
We now know everything to equip our Grok project with our own grokkers as a separate package, so that custom components can be created and registered at startup. For the purposes of example, we will define some class grokkers that register according to some 'flavour'.
The first step is to create a package somewhere outside of our grokproject with a setup.py. Let's assume it's called customgrokkers and the source is in src/customgrokkers/.
In the src/customgrokkers/ directory of this package we'll need a components.py, a meta.py, directives.py and meta.zcml.
components.py will contain the baseclass that our custom components will inherit from, meta.py will contain the ClassGrokker, directives.py will contain a single directive class that we'd like our custom components to contain. The meta.zcml file will contain a directive to grok our package:
So, components.py:
directives.py
__init__.py:
meta.py:
meta.zcml
We can now add our customgrokkers package to our myproject's setup.py file and rerun buildout. (Of course, you'd have to install customgrokkers into your python path before doing so using setuptool's develop command).
Packages that actually define compatible components (i.e. inherit from customgrokkers.CustomComponent in this case) should have a configure.zcml that looks something like this in their package directory:
Define your subclasses of customgrokkers.CustomComponent in any .py file in the package and they will be registered. Again, this package needs a mention in the setup.py too. Alternatively such python scripts and ZCML could live safely in your customgrokkers package thanks to precedence of meta.zcml over configure.zcml in z3c.autoinclude.
Conclusion
Once the underlying concepts described have been understood, you can understand how Grok fits together internally (making reading and naviagting the source code easier) and you can build flexible and decoupled systems with declarative python directives for your own extensions.
Further Reading
Prerequisites:
Martian PyPi Documentation
Understand 'grokking' and 'grokkers'.
setuptools
zc.buildout
Audience:
Developers
To understand how Grok is organised and how to extend Grok, we must first understand how Grok locates, registers and configures components. At startup, Grok discovers utilities and adapters so they can be registered the Zope Component Architecture for later retrieval by interface. The registration is done by Martian, and the process of scanning packages and grokking them is known as configuration.
A Martian Refresher
A Grok project consists of a number of components (predominantly classes) that have different roles, such as views, containers and models, characterised by an interface, that are grokked during the configuration stage. Grokking is made possible by the definition of three things:
- A child class inheriting from martian.*Grokker with an execute method
- Some baseclass from which Grokkable components will inherit
- A directive class
class Person(object): # A baseclass for grokkable classes
pass
class name(martian.Directive):
scope = CLASS
class PersonGrokker(martian.ClassGrokker):
martian.component(Person)
martian.directive(name)
def execute(self, class_, name, **kw):
register_people(class_, name)
As you can see, the name and PersonGrokker classes both inherit from Martian classes, and this allows Martian to register our PersonGrokker using something known as a meta-grokker. A meta-grokker is responsible for registering grokkers into Martian's own registry, which is constructed as modules are scanned during the application's configuration stage. A meta-grokker is conceptually the same as regular grokking, except that it extends Martian. Naturally, before classes can be registered, Martian must know about PersonGrokker. So, Martian must scan modules that contain Grokker definitions, conventionally named meta.py, before classes that inherit from Person.
Martian can then go onto scan subclasses of Person:
class Santa(Person):
name('Santa Claus')
If any of this is confusing, then I suggest you read the Martian documentation and write your own grokkers. Otherwise, the important thing to remember is that we must first register a grokker before Martian scans our class.
The ZCML Configuration Chain
How does Martian discover the various components and directives during configuration, and how does Grok ensure that meta-grokking occurs before regular grokking? Both are achieved using a extensions to Zope's XML based configuration process; ZCML. ZCML is based on extensible directives that, in our examples, are completely separate from Martian directives.
When the configuration stage begins (during startup) for a grokproject created package (which we'll call myproject), it first looks at the
parts/app/site.zcmlfile and executes the configuration there. This file contains the ZCML directive:
<include package="myproject" />
This instructs the Zope configuration machinery to visit the specified package and include/process the directives found within the configure.zcml file. (The include directive can also take an additional file parameter that overrides which file it includes from the specified package.) Our configure.zcml file looks like:
<configure xmlns="http://namespaces.zope.org/zope"
xmlns:grok="http://namespaces.zope.org/grok">
<include package="grok" />
<includeDependencies package="." />
<grok:grok package="." />
</configure>
These directives are processed sequentially - so let's look at them in turn. First, it includes the Grok package's configure.zcml. This is quite a large configuration file which we'll mostly ignore; the important directive is:
<include package="grok" file="meta.zcml" />
We now process meta.zcml, which contains the following three relevant directives:
The z3c.autoinclude and grokcore.component lines enable two new ZCML configuration directives to be used from here:
- grok
- includeDependencies (and includePlugins)
The 'grok' ZCML Directive
The 'grok' directive allows us to specify packages to grok for configurable components. If you use the following:
<grok:grok package=".meta" />
Grok will, using the Martian system described above, scan the target package or module, (meta.py in the example given) registering anything that subclasses a known grokkable baseclass. After the grokcore.component configuration has registered this new 'grok' directive, it instructs itself to meta-grok its own meta.py that contains various grokkers for all the basic Zope building blocks - such as grok.Adapter, grok.GlobalUtility. So, from here files grokked containing subclasses of any of these will be registered in the Zope Component Architecture.
The 'includeDependencies' ZCML Directive
The includeDependencies directive, used as follows:
<includeDependencies package="." />
will iterate (in order) over the given package's setup.py for entries in the "install_requires" section and include any meta.zcml and configure.zcml files automatically (in that order) from those. Since meta.zcml will conventionally contain the directive to scan meta.py, our grokkers will be discovered before configure.zcml instructs Martian to grok any configured components.
---
Thus - returning to the meta.zcml in the Grok package - once Grok's meta.py is scanned, the system will know how to go on and grok classes that inherit from grok.Site, grok.Application, grok.REST etc, and all the other central Grok baseclasses that we use to construct our applications. Any packages that are scanned from now onwards can register these types of components.
Moving back to our package's configure.zcml, the next directive is:
<includeDependencies package="." />
So, our setup.py's "install_requires" is scanned for packages. We have no new entries in here at the moment, but this mechanism will be later used to include custom grokkers as independent and separate packages.
Finally, the last directive is:
<grok:grok package="." />
This gives the order to scan through the current package (all .py files), grokking anything it knows how to. So, this is how our main application is registered (it inherits from grok.Application), how all our views are registered (grok.View) and indeed all other grok.* subclasses.
The Truth about Configuration
Until now, you'll probably be under the impression that the Grok machinery is registering components as it goes, and that when a global utility is found, for example, it immediately adds it to the Zope component registry. This is only partly true: Martian is indeed registering grokkers, but when these grokkers grok classes they are not immediately placed into the component registry.
Instead a function/position args/keyword args set, known as an action, is passed to the configuration machinery to be invoked later. Along with this set a 'discriminator' is passed, which if not None, will raise a configuration conflict exception if two actions contain the same discriminator. This allows Grok/Zope to enforce configuration polices, such as "we cannot allow two or more adapters with the same name, adapting the same context and providing the same interface".
Additionally, an order parameter can be specified optionally which dictates the order in which actions are called. Once configuration is completed, all the actions are processed in the order they were created unless order values dictate otherwise.
Extending Grok with Custom Grokkers
We now know everything to equip our Grok project with our own grokkers as a separate package, so that custom components can be created and registered at startup. For the purposes of example, we will define some class grokkers that register according to some 'flavour'.
The first step is to create a package somewhere outside of our grokproject with a setup.py. Let's assume it's called customgrokkers and the source is in src/customgrokkers/.
In the src/customgrokkers/ directory of this package we'll need a components.py, a meta.py, directives.py and meta.zcml.
components.py will contain the baseclass that our custom components will inherit from, meta.py will contain the ClassGrokker, directives.py will contain a single directive class that we'd like our custom components to contain. The meta.zcml file will contain a directive to grok our package:
So, components.py:
import grok
class CustomComponent(object):
grok.baseclass()
directives.py
import martian
class flavour(martian.Directive):
scope = martian.CLASS
store = martian.ONCE
default = 'banana'
__init__.py:
registry = {}
meta.py:
import martian
from customgrokkers.component import CustomComponent
from customgrokkers.directive import flavour
def register_custom_class(class_, flavour):
customgrokkers.registry[flavour] = class_
class CustomComponentGrokker(martian.ClassGrokker):
martian.component(CustomComponent)
martian.directive(flavour)
def execute(self, class_, config, flavour, **kwds):
config.action( # defer actual registration
discriminator = None,
callable = register_custom_class,
args = (class_, flavour),
order = 5
)
return True
meta.zcml
<configure grok="http://namespaces.zope.org/grok">
<grok:grok package=".meta" />
</configure>
We can now add our customgrokkers package to our myproject's setup.py file and rerun buildout. (Of course, you'd have to install customgrokkers into your python path before doing so using setuptool's develop command).
Packages that actually define compatible components (i.e. inherit from customgrokkers.CustomComponent in this case) should have a configure.zcml that looks something like this in their package directory:
<configure grok="http://namespaces.zope.org/grok">
<include package="customgrokkers" />
<grok:grok package="." />
</configure>
Define your subclasses of customgrokkers.CustomComponent in any .py file in the package and they will be registered. Again, this package needs a mention in the setup.py too. Alternatively such python scripts and ZCML could live safely in your customgrokkers package thanks to precedence of meta.zcml over configure.zcml in z3c.autoinclude.
Conclusion
Once the underlying concepts described have been understood, you can understand how Grok fits together internally (making reading and naviagting the source code easier) and you can build flexible and decoupled systems with declarative python directives for your own extensions.
Further Reading
- z3c.autoinclude - Explanation of the includeDependencies ZCML directive
- Grok SVN Repository - Contains the ZCML files we've talked about and Grok specific grokkers.
- The grokcore.component SVN Repository - Contains the definition of the grok directive and grokkers for adapter/utilities/multiadapters etc...
Subscribe to:
Posts (Atom)