Skip to main content

A simple "untwisted" approach to event-driven programming

Project description

(NOTE: As of 0.6a1, many advanced parts of the Trellis API have changed significantly. If you’ve done a lot with the 0.5 version API, we strongly suggest reading the new manuals, especially the parts dealing with state change. Many old restrictions have been lifted, but some were replaced with new ones.)

Whether it’s an application server or a desktop application, any sufficiently complex system is event-driven – and that usually means callbacks.

Unfortunately, explicit callback management is to event-driven programming what explicit memory management is to most other kinds of programming: a tedious hassle and a significant source of unnecessary bugs.

For example, even in a single-threaded program, callbacks can create race conditions, if the callbacks are fired in an unexpected order. If a piece of code can cause callbacks to be fired “in the middle of something”, both that code and the callbacks can get confused.

Of course, that’s why most GUI libraries and other large event-driven systems usually have some way for you to temporarily block callbacks from happening. This lets you fix or workaround your callback order dependency bugs… at the cost of adding even more tedious callback management. And it still doesn’t fix the problem of forgetting to cancel callbacks… or register needed ones in the first place!

The Trellis solves all of these problems by introducing automatic callback management, in much the same way that Python does automatic memory management. Instead of worrying about subscribing or “listening” to events and managing the order of callbacks, you just write rules to compute values. The Trellis “sees” what values your rules access, and thus knows what rules may need to be rerun when something changes – not unlike the operation of a spreadsheet.

But even more important, it also ensures that callbacks can’t happen while code is “in the middle of something”. Any action a rule takes that would cause a new event to fire is automatically deferred until all of the applicable rules have had a chance to respond to the event(s) in progress. And, if you try to access the value of a rule that hasn’t been updated yet, it’s automatically updated on-the-fly so that it reflects the current event in progress.

No stale data. No race conditions. No callback management. That’s what the Trellis gives you.

Here’s a super-trivial example:

>>> from peak.events import trellis

>>> class TempConverter(trellis.Component):
...     trellis.values(
...         F = 32,
...         C = 0,
...     )
...     trellis.rules(
...         F = lambda self: self.C * 1.8 + 32,
...         C = lambda self: (self.F - 32)/1.8,
...     )
...     @trellis.observer
...     def show_values(self):
...         print "Celsius......", self.C
...         print "Fahrenheit...", self.F

>>> tc = TempConverter(C=100)
Celsius...... 100
Fahrenheit... 212.0

>>> tc.F = 32
Celsius...... 0.0
Fahrenheit... 32

>>> tc.C = -40
Celsius...... -40
Fahrenheit... -40.0

As you can see, each attribute is updated if the other one changes, and the show_values action is invoked any time the dependent values change… but not if they don’t:

>>> tc.C = -40

Since the value didn’t change, none of the rules based on it were recalculated.

Now, imagine all this, but scaled up to include rules that can depend on things like how long it’s been since something happened… whether a mouse button was clicked… whether a socket is readable… or whether a Twisted “deferred” object has fired. With automatic dependency tracking that spans function calls, so you don’t even need to know what values your rule depends on, let alone having to explicitly code any dependencies in!

Imagine painless MVC, where you simply write rules like the above to update GUI widgets with application values… and vice versa.

And then, you’ll have the tiny beginning of a mere glimpse… of what the Trellis can do for you.

Other Python libraries exist which attempt to do similar things, of course; PyCells and Cellulose are two. However, only the Trellis supports fully circular rules (like the temperature conversion example above), and intra-pulse write conflict detection. The Trellis also uses less memory for each cell (rule/value object), and offers many other features that either PyCells or Cellulose lack.

The Trellis package can can be downloaded from the Python Package Index or installed using Easy Install, and it has a fair amount of documentation, including the following manuals:

Questions, discussion, and bug reports for the Trellis should be directed to the PEAK mailing list.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Trellis-0.6a1.zip (102.4 kB view details)

Uploaded Source

Built Distributions

Trellis-0.6a1-py2.5.egg (81.4 kB view details)

Uploaded Source

Trellis-0.6a1-py2.4.egg (82.6 kB view details)

Uploaded Source

Trellis-0.6a1-py2.3.egg (310.0 kB view details)

Uploaded Source

File details

Details for the file Trellis-0.6a1.zip.

File metadata

  • Download URL: Trellis-0.6a1.zip
  • Upload date:
  • Size: 102.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Trellis-0.6a1.zip
Algorithm Hash digest
SHA256 88a40931661284553106d09279ca11dc07d03c98642342dbfe2adc42d833fbfa
MD5 673f1db10125fb3175979df9e4c682e2
BLAKE2b-256 5bf50050905338cce495cb026138e76fb4ffe7dcb5e2307877b9bc0364ff2379

See more details on using hashes here.

File details

Details for the file Trellis-0.6a1-py2.5.egg.

File metadata

  • Download URL: Trellis-0.6a1-py2.5.egg
  • Upload date:
  • Size: 81.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Trellis-0.6a1-py2.5.egg
Algorithm Hash digest
SHA256 87b2060279dc143fdb1050aac19591511a2a3a4c3e9003a50ae40d75df7f5a85
MD5 abd4d9aaf313e75c49252d0c9d5e820d
BLAKE2b-256 19c9a3f76e6d25f2e8c28ae6b8b8f077470424631ff6812ebfa82b6746e96c6e

See more details on using hashes here.

File details

Details for the file Trellis-0.6a1-py2.4.egg.

File metadata

  • Download URL: Trellis-0.6a1-py2.4.egg
  • Upload date:
  • Size: 82.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Trellis-0.6a1-py2.4.egg
Algorithm Hash digest
SHA256 2d8e2fff8f33c9c7317c8433c7236f3ffc04740d7185aea9aa1511b30ced0b43
MD5 3796c8756f7a621ecb8cbb8d9e3854dc
BLAKE2b-256 3c6423a0fadbc3afde2a21b62e11c096294e653a4f8b729c5e2a0f7365810581

See more details on using hashes here.

File details

Details for the file Trellis-0.6a1-py2.3.egg.

File metadata

  • Download URL: Trellis-0.6a1-py2.3.egg
  • Upload date:
  • Size: 310.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Trellis-0.6a1-py2.3.egg
Algorithm Hash digest
SHA256 6f8eef2e536ff1eff59f812885150135a1b26facf6759b8b1522ceb4f3deb776
MD5 34a0e4af7e9dc882406db91b6c1551ad
BLAKE2b-256 f848a57b2a4ef046757e77ab4798578f8f187fcfc53bdc962d5fb5dbf473b005

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page