Storing persistent classes with SQLObject
I've been playing with extensions to store persistent classes and custom methods using SQLObject. The need arised as I was trying to write a relational representation of the Petri net model for a business framework. The standard model boasts few basic concepts (basically speaking, transitions, places, tokens and arcs). The relationship between the entities is simple, and easy to map with SQLObject. But the details are harder to model correctly, specially when transitions are concerned.
In a Petri net, a transition is the entity that tells what happens when one action is executed. In a workflow model it represents the status changes as the work is done. In a object-oriented system, the obvious way to do it is to represent each transition as a custom descendant of the base Transition class.
At this point things get complicated. A real-life workflow application has typically hundreds, or even thousands, of transitions. Implementing each transition as a standard Python class turns out to be a problem. It makes customization harder; simple customizations may require a server restart to have any effect. Python modules are known to be hard to reload properly (even for Zope wizards). What is really required is a solution to have persistent classes: class definitions that can be saved and loaded from the same database which holds the relational representation of the Petri Net model. Note the difference; it's not a persistent instance, so pickle, or even more advanced solutions as the ZODB do not apply here. But if we want to store it in a database, we really want to have only one Transition table. Having one table for each custom transition makes things way too complicated to handle.
The simplest solution is to store the class definition itself into the database. Code can be stored into a string, and later read and executed on demand. It's important to make sure that class instances generated this way are short lived, to avoid problems with obsolete instances in memory. For a web application, this can be achieved by working with instances that are valid only for one request and discarded later. For example (untested!)
While this solution works, it involves an intermediate object. Another solution is to dynamically attach custom methods to the transition instances. The idea is to implement a special MethodCol column that would store the code for a single method in the database, either as text or in compiled format. On read, the MethodCol automatically executes the function definition and binds it to the instance, as if it was a method of the class. For example:
To work as presented above, MethodCol needs to do a few tricks. On get, it must return an special object: a callable, which calls the custom method, but that on repr returns the source code of the object (which is necessary to allow customization using a web interface). On set, it would take the source code.
Right now, I don't know which approach is better. I'm willing towards the MethodCol solution, but I don't really know if it's going to work in practice. There are also some questions about security; however, any customizable system is subject to security problems anyway. The code in the database should only be modified by someone with the proper qualifications and credentials; the same individual could just as easily make a much greater damage with access to the file system to modify the system's code.
In a Petri net, a transition is the entity that tells what happens when one action is executed. In a workflow model it represents the status changes as the work is done. In a object-oriented system, the obvious way to do it is to represent each transition as a custom descendant of the base Transition class.
At this point things get complicated. A real-life workflow application has typically hundreds, or even thousands, of transitions. Implementing each transition as a standard Python class turns out to be a problem. It makes customization harder; simple customizations may require a server restart to have any effect. Python modules are known to be hard to reload properly (even for Zope wizards). What is really required is a solution to have persistent classes: class definitions that can be saved and loaded from the same database which holds the relational representation of the Petri Net model. Note the difference; it's not a persistent instance, so pickle, or even more advanced solutions as the ZODB do not apply here. But if we want to store it in a database, we really want to have only one Transition table. Having one table for each custom transition makes things way too complicated to handle.
The simplest solution is to store the class definition itself into the database. Code can be stored into a string, and later read and executed on demand. It's important to make sure that class instances generated this way are short lived, to avoid problems with obsolete instances in memory. For a web application, this can be achieved by working with instances that are valid only for one request and discarded later. For example (untested!)
class Transition(SQLObject):
name = StringCol()
transition_class = StringCol()
def _init(self, *args, **kw):
SQLObject._init(self, *args, **kw)
exec self.transition_class
self.transition = locals()[self.name]()
...
t = Transition(
name='ChargeCreditCard',
transition_class='class ChargeCreditCard(CustomTransition): pass'
)
...
t.transition.execute()
While this solution works, it involves an intermediate object. Another solution is to dynamically attach custom methods to the transition instances. The idea is to implement a special MethodCol column that would store the code for a single method in the database, either as text or in compiled format. On read, the MethodCol automatically executes the function definition and binds it to the instance, as if it was a method of the class. For example:
class Transition(SQLObject):
name = StringCol()
execute = MethodCol()
...
t = Transition(
name='ChargeCreditCard',
execute='def execute(self, fromPlace, toPlace): pass'
)
...
t.execute(fromPlace, toPlace)
To work as presented above, MethodCol needs to do a few tricks. On get, it must return an special object: a callable, which calls the custom method, but that on repr returns the source code of the object (which is necessary to allow customization using a web interface). On set, it would take the source code.
Right now, I don't know which approach is better. I'm willing towards the MethodCol solution, but I don't really know if it's going to work in practice. There are also some questions about security; however, any customizable system is subject to security problems anyway. The code in the database should only be modified by someone with the proper qualifications and credentials; the same individual could just as easily make a much greater damage with access to the file system to modify the system's code.
40 Comments:
At March 6, 2007 at 12:40 PM, Anonymous said…
www.mtechgraphics.com GRAPHIC KITS FROM ONLY $25 LOWEST PRICE ANYWHERE ONLINE AND OFFLINE THE LOWEST PRICE IN THE WORLD!!!!!! 11" X 90" KITS ARE ONLY $35 A SET HURRY UP LIMITED TIME ONLY!!!!!!!!!!!
At August 18, 2007 at 5:26 AM, Anonymous said…
Great article! Thanks.
At August 18, 2007 at 11:09 AM, Anonymous said…
Thanks for interesting article.
At September 10, 2007 at 1:09 AM, Anonymous said…
Excellent website. Good work. Very useful. I will bookmark!
At November 25, 2009 at 3:03 PM, Anonymous said…
...please where can I buy a unicorn?
At December 22, 2009 at 10:13 PM, Anonymous said…
[url=http://italtubi.com/levitra/ ]levitra compra [/url] igMagnifico messaggio, molto interessante per me:) acquisto levitra generico uodprhvmmg [url=http://www.mister-wong.es/user/COMPRARCIALIS/comprar-viagra/]cialis online[/url]
At December 30, 2009 at 1:49 AM, Anonymous said…
Bravo, la risposta ideale. [url=http://lacasadicavour.com/tag/cialis/ ]cialis prezzo [/url]Credo che si sbaglia. Sono sicuro. Scrivere a me in PM, ti parla. acquisto cialis generico NON non puГІ credere voi:)
At January 7, 2010 at 5:26 AM, Anonymous said…
Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!
At January 28, 2010 at 10:07 PM, Anonymous said…
[u][b]Xrumer[/b][/u]
[b]Xrumer SEO Professionals
As Xrumer experts, we secure been using [url=http://www.xrumer-seo.com]Xrumer[/url] fitted a sustained immediately for the time being and recollect how to harness the titanic power of Xrumer and build it into a Bills machine.
We also provide the cheapest prices on the market. Assorted competitors see fit cost 2x or square 3x and a end of the term 5x what we pervade you. But we have faith in providing gigantic help at a small affordable rate. The unbroken point of purchasing Xrumer blasts is because it is a cheaper alternative to buying Xrumer. So we focusing to support that thought in recollection and yield you with the cheapest censure possible.
Not simply do we cause the best prices but our turnaround in the good old days b simultaneously payment your Xrumer posting is super fast. We intention take your posting done in the forefront you discern it.
We also produce you with a full log of affluent posts on contrary forums. So that you can catch a glimpse of also in behalf of yourself the power of Xrumer and how we get harnessed it to emoluments your site.[/b]
[b]Search Engine Optimization
Using Xrumer you can think to distinguish thousands upon thousands of backlinks exchange for your site. Myriad of the forums that your Site you settle upon be posted on oblige exalted PageRank. Having your association on these sites can categorically help establish up some top-grade quality back links and really boost your Alexa Rating and Google PageRank rating owing to the roof.
This is making your position more and more popular. And with this inflate in reputation as well as PageRank you can keep in view to lead your site absolutely downright expensive in those Search Mechanism Results.
Traffic
The amount of see trade that can be obtained nearby harnessing the power of Xrumer is enormous. You are publishing your situation to tens of thousands of forums. With our higher packages you may equivalent be publishing your locality to HUNDREDS of THOUSANDS of forums. Visualize 1 collection on a popular forum drive by cotton on to a leave 1000 or so views, with communicate 100 of those people visiting your site. At once imagine tens of thousands of posts on in demand forums all getting 1000 views each. Your traffic longing withdraw sometimes non-standard due to the roof.
These are all targeted visitors that are interested or bizarre about your site. Deem how many sales or leads you can execute with this considerable figure up of targeted visitors. You are truly stumbling upon a goldmine friendly to be picked and profited from.
Retain, Above is Money.
[/b]
TRAVERSE B RECOVER YOUR INFERIOR BURST TODAY:
http://www.xrumer-seo.com
At February 7, 2010 at 1:24 AM, Anonymous said…
Offer together the unintentional with two backs casinos? assess this modish [url=http://www.realcazinoz.com]casino[/url] instruction and wing it denigrate online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also go into our untrained [url=http://freecasinogames2010.webs.com]casino[/url] barter something at http://freecasinogames2010.webs.com and be the conqueror in straightforward folding moolah !
another restored [url=http://www.ttittancasino.com]casino spiele[/url] intrigue is www.ttittancasino.com , because german gamblers, blurt outdoors in unstinting online casino bonus.
At February 24, 2010 at 11:11 PM, Anonymous said…
Dear Paul,
For long time I use this freeware: [url=http://www.freeflvtomp3converter.com]FLV to MP3 free converter[/url].
FLV to MP3 free converter is a free YouTube, MegaVideo, Dailymotion and similar video sites to MP3 Converter and allows you to convert a video to MP3 file.
This software is fast, free, and requires no signup. All you need is a FLV Video file, and this software will extract the MP3, and give you an audio file.
So you are able to listen to your favorite YouTube tracks on every MP3 player.
You can download it for free at [url=http://www.freeflvtomp3converter.com]www.freeflvtomp3converter.com[/url].
This may be useful to you.
At March 17, 2010 at 7:03 PM, Anonymous said…
You could easily be making money online in the hush-hush world of [URL=http://www.www.blackhatmoneymaker.com]blackhat seo techniques[/URL], You are far from alone if you have no clue about blackhat marketing. Blackhat marketing uses alternative or misunderstood ways to generate an income online.
At July 12, 2010 at 1:12 PM, Anonymous said…
resource for [b]buy software for windows[/b] is available at:
buy software for windows
[url=http://www.buysoftwareforwindows.com]buy software for windows[/url]
[url=http://www.buysoftwareforwindows.com/products/buy-zune-video-converter/productpage.php]buy zune video converter[/url]
buy dvd to iphone converter
At August 26, 2010 at 4:54 AM, Anonymous said…
Cede to to pass the physical with two backs casinos? note into this advanced [url=http://www.realcazinoz.com]casino[/url] advisor and waver online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also vet our blooming [url=http://freecasinogames2010.webs.com]casino[/url] clasp at http://freecasinogames2010.webs.com and swell in veracious chary bread !
another arcane [url=http://www.ttittancasino.com]casino spiele[/url] bit is www.ttittancasino.com , in compensation german gamblers, submit c be communicated during forbearing [url=http://www.realcazinoz.com]online casino[/url] bonus. so check this leading [url=http://www.omniget.co.il]online casino[/url] for free [url=http://www.casinosaction.com]casino bonus[/url] and 100's of online [url=http://www.thecasino.co.il]casino[/url] games.
At January 11, 2013 at 2:25 AM, Anonymous said…
viagra online viagra for women results - buy viagra online yahoo answers
At January 24, 2013 at 9:03 AM, Anonymous said…
viagra online without prescription buy female viagra us - online pharmacy viagra fast delivery
At January 26, 2013 at 11:54 AM, Anonymous said…
generic viagra viagra for women forum - reputable online pharmacy viagra
At February 3, 2013 at 3:18 PM, Anonymous said…
buy soma generic generic soma good - soma bras commercial
At February 4, 2013 at 5:44 AM, Anonymous said…
top [url=http://www.xgambling.org/]online casinos[/url] brake the latest [url=http://www.casinolasvegass.com/]casino online[/url] autonomous no store reward at the chief [url=http://www.baywatchcasino.com/]online casinos
[/url].
At February 7, 2013 at 1:39 AM, Anonymous said…
buy soma online soma san diego past shows - soma massage
At February 7, 2013 at 7:32 PM, Anonymous said…
buy soma buy generic soma no prescription - soma ingredients
At February 7, 2013 at 8:24 PM, Anonymous said…
soma muscle relaxant muscle relaxant soma carisoprodol - safely buy soma online
At February 10, 2013 at 12:03 PM, Anonymous said…
tramadol 50 mg what is tramadol generic for - tramadol 50 mg recommended dosage
At February 11, 2013 at 10:03 PM, Anonymous said…
buy tramadol online buy tramadol online overnight - order tramadol australia
At February 12, 2013 at 3:15 AM, Anonymous said…
buy cialis online order cialis online prescription - cialis 40 mg online
At February 12, 2013 at 5:51 PM, Anonymous said…
buy tramadol online tramadol 58 93 dosage - order tramadol online saturday delivery
At February 21, 2013 at 8:34 AM, Anonymous said…
xanax antidepressant buy xanax online cheap - much 2mg xanax bar
At February 22, 2013 at 12:30 AM, Anonymous said…
buy tramadol cheapest place buy tramadol - tramadol vs hydrocodone withdrawal
At February 27, 2013 at 1:41 PM, Anonymous said…
buy carisoprodol soma carisoprodol ingredients - carisoprodol found drug test
At February 28, 2013 at 11:12 PM, Anonymous said…
xanax online xanax withdrawal taper - 2mg xanax bars online
At March 3, 2013 at 1:41 AM, Anonymous said…
buy cialis online cialis online lloyds - cialis dosage
At March 3, 2013 at 6:55 AM, Anonymous said…
cialis online caverta cheap cialis generic viagra - cialis 5 mg coupon
At March 6, 2013 at 8:49 PM, Anonymous said…
http://buytramadolonlinecool.com/#91646 tramadol-ratiopharm 50 mg kapszula - tramadol 50 mg tablets dosage
At March 8, 2013 at 10:35 PM, Anonymous said…
http://www.integrativeonc.org/adminsio/buyklonopinonline/#6817 ativan vs klonopin high - klonopin side effects hiccups
At March 10, 2013 at 12:50 AM, Anonymous said…
http://landvoicelearning.com/#74967 metacam and tramadol for dogs - tramadol hcl 50 mg recreational use
At March 10, 2013 at 5:36 PM, Anonymous said…
learn how to buy tramdadol tramadol 50 mg espanol - tramadol hydrochloride 50 mg get you high
At April 26, 2013 at 9:42 PM, Anonymous said…
cialis 5mg comprar viagra en espana cialis viagra prezzo
At March 4, 2016 at 7:54 PM, Anonymous said…
ninest123 16.03
prada outlet, nike outlet, longchamp outlet, burberry outlet, gucci handbags, ralph lauren polo, cheap jordans, louis vuitton outlet, louis vuitton outlet online, oakley sunglasses, uggs on sale, louis vuitton outlet, tiffany jewelry, replica watches, ralph lauren outlet, michael kors outlet, longchamp outlet, cheap oakley sunglasses, ray ban sunglasses, michael kors handbags, replica watches, michael kors outlet online, oakley sunglasses, uggs on sale, louboutin shoes, christian louboutin, ray ban sunglasses, uggs outlet, nike free, christian louboutin, uggs outlet, tiffany jewelry, nike air max, oakley sunglasses, longchamp bags, uggs on sale, michael kors outlet online, michael kors outlet online, nike air max, louis vuitton handbags, oakley sunglasses, louis vuitton, ray ban sunglasses, louboutin uk, michael kors, tory burch outlet, burberry factory outlet, prada handbags
At March 4, 2016 at 7:56 PM, Anonymous said…
lunette oakley pas cher, nike roshe run pas cher, lululemon outlet, hollister uk, jordan pas cher, coach purses, lunette ray ban pas cher, nike tn pas cher, hogan sito ufficiale, abercrombie and fitch, new balance, longchamp soldes, nike blazer pas cher, true religion outlet, true religion outlet, michael kors, replica handbags, michael kors, sac guess pas cher, ray ban uk, nike air max, mulberry uk, vans pas cher, abercrombie and fitch UK, louboutin pas cher, nike air max pas cher, coach outlet store online, nike free pas cher, true religion jeans, polo lacoste pas cher, true religion outlet, nike air max uk, ralph lauren pas cher, kate spade outlet, longchamp pas cher, nike air force, polo ralph lauren uk, hermes pas cher, michael kors uk, north face uk, timberland pas cher, vanessa bruno pas cher, nike free, michael kors outlet online, coach outlet, nike air max uk, burberry pas cher, north face pas cher, converse
At March 4, 2016 at 8:00 PM, Anonymous said…
swarovski jewelry, coach outlet, links of london uk, canada goose, canada goose pas cher, moncler, barbour, ugg pas cher, louis vuitton, supra shoes, converse, wedding dresses uk, pandora charms, canada goose uk, karen millen uk, ugg,uggs,uggs canada, canada goose outlet, louis vuitton uk, pandora uk, replica watches, moncler, moncler pas cher, ugg,ugg australia,ugg italia, moncler, swarovski uk, canada goose outlet, canada goose jackets, moncler, converse shoes outlet, bottes ugg pas cher, barbour jackets uk, canada goose jackets, hollister, marc jacobs, thomas sabo uk, ugg uk, toms shoes, moncler uk, sac louis vuitton, sac louis vuitton, gucci, pandora jewelry, moncler jackets, juicy couture outlet, pandora jewelry, montre pas cher, juicy couture outlet, canada goose, louis vuitton, moncler outlet, ray ban, lancel
ninest123 16.03
Post a Comment
<< Home