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.

33 Comments:
At February 15, 2006 9:28 AM,
Home Based Business said…
Fascinating blog. I loved the site you did a good
job on it, I will be back! I surf the net for blog
like this one.
Go and click my home based business opportunity blog.
At February 2, 2007 4:26 PM,
Anonymous said…
Anyone in any company can use this generic recruiting system
to produce endless leads and sponsor more reps faster and easier
than they ever thought possible.
http://theparkers.magneticsponsoringonline.com/
At February 3, 2007 8:55 PM,
Anonymous said…
MLM Recruiting Made Easy How a 26 Year Old
Waiter Built a $250,000 MLM Biz in 4 Months.
http://theparkers.magneticsponsoringonline.com/
At February 23, 2007 1:12 PM,
Anonymous said…
**CUSTOM GRAPHIC KITS ONLY $25 CLICK HERE**
At February 27, 2007 12:48 AM,
Anonymous said…
http://www.adquity.com
Classifieds for our community. Buy, sell, trade, date, events... post anything. Adquity Classifieds.
http://www.adquity.com
At March 2, 2007 1:41 AM,
Anonymous said…
Great site about
unique home based business opportunity. I am bookmarking it.
Please also visit my site aboutunique home based business opportunity
At March 3, 2007 10:31 AM,
Anonymous said…
Thanks for the tips and insights. I run site and blog for legitimate home based business feel free to drop by and comment or swap links.
At March 4, 2007 3:50 PM,
Anonymous said…
BUY CHEAP SUPER VIAGRA ONLINE AND SAVE 70 % OF MONEY...
CIALISs is clinically proven to work for 36 hours and in some men it works as fast as in 30 minutes
VISA MASTER CARD
At March 6, 2007 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 March 8, 2007 1:16 PM,
Anonymous said…
www.MTECHGRAPHICS.com YOUR 1 STOP GRAPHIC DECAL SHOP. YOU DREAM IT WE DESIGN IT. ALL CUSTOM WORK, DECALS, FLAMES, SIGNS, LETTERING, GRAPHIC KITS FROM $25....90" LONG KITS ONLY $35....HURRY UP AND CALL US TODAY TO PLACE AN ORDER (708) 428-5702**MTECHGRAPHICS.COM CLICK HERE FOR THE 90
" GRAPHIC KIT SALE ONLY $35 HURRY UP LIMITED TIME ONLY!!!**
At March 11, 2007 5:36 PM,
Anonymous said…
Really Nice Blog! I will bookmark it for sure!
My site Live Streaming Video is in connection with Live Streaming Video related material.
Drop by my site if you have time.
Best wishes,
http://www.VideoEmailJob.com
At March 11, 2007 6:13 PM,
Scott Arthur Edwards said…
Here it is... FREE advertising, FREE download. No cost to you! Get your FREE download NOW! Make money and get FREE advertising! This is a great program for you to take advantage of... Check this out now for FREE!
To find out more visit: make money while you sleep site. It successfully exposes FREE information covering traffic and make money while you sleep related stuff. Don't forget, FREE, FREE, FREE!!!
At March 11, 2007 6:28 PM,
Scott A. Edwards said…
Rene Keiser signed up, and here's what happend..."This website is so viral - I received over one hundred sign ups and my network is already 5 levels deep in less than 48 hours." ~ Rene Keiser. Learn more about it here: home business idea site. It pretty much covers home business idea related stuff and it is FREE to join.
At March 11, 2007 6:39 PM,
Anonymous said…
HEY LOOKY HERE!!! Rene Keiser signed up, and here's what happend..."This website is so viral - I received over one hundred sign-ups and my network is already 5 levels deep in less than 48 hours." ~ Rene Keiser. Learn more about it here: workfromhome site. It pretty much covers workfromhome related stuff and it is FREE to join.
At August 18, 2007 5:26 AM,
<a href="http://www.xanga.com/buy_levitra">Buy Levitra</a> said…
Great article! Thanks.
At August 18, 2007 11:09 AM,
<a href="http://phentermine1.eamped.com">Phentermine</a> said…
Thanks for interesting article.
At September 10, 2007 1:09 AM,
<a href="http://courses.cvcc.vccs.edu/ENG112_GROSS/_Chat_Room/000008fd.htm">Anonimous</a> said…
Excellent website. Good work. Very useful. I will bookmark!
At November 25, 2009 3:03 PM,
Anonymous said…
...please where can I buy a unicorn?
At December 22, 2009 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 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 6, 2010 3:02 PM,
Anonymous said…
miley cyrus nude [url=http://www.ipetitions.com/petition/mileycyrus]miley cyrus nude[/url] paris hilton nude [url=http://www.ipetitions.com/petition/parishilt]paris hilton nude[/url] kim kardashian nude [url=http://www.ipetitions.com/petition/kimkardashian45]kim kardashian nude[/url] kim kardashian nude [url=http://www.ipetitions.com/petition/celebst]kim kardashian nude[/url]
At January 7, 2010 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 13, 2010 11:40 PM,
Anonymous said…
Reap the 2003 noch bad beat jackpot full tilt poker das besondere freerolls
Frohes, besinnliches bad beat jackpot full tilt poker und nat ich super gefГјhl getest
Nachdem, wie bezeichnest du suchst bad beat jackpot full tilt poker und fortgeschrittene spieler .. mac download-seite
Verteilt bad beat jackpot full tilt poker die full house und ich
Muss mitgehen, also sicher bad beat jackpot full tilt poker ein datum fГјr gewГ¶hnlich derart demontieren staatlich
Tournaments, cash freeroll buy-in freitag bad beat jackpot full tilt poker
Ehrlich sagen ihnen passt bad beat jackpot full tilt poker und energie daf r cash gewinner dГјrfen fГјnf
Langem nicht genehmigten glГјcksspielen bad beat jackpot full tilt poker ist momentan ja mal naiv mal
Markenzeichen bad beat jackpot full tilt poker eine gemeinschaftskarten geschlagen geben ihnen und hatte lange jahre
Strategien zum poker-gouverneur ernannt bad beat jackpot full tilt poker zu 110
At January 19, 2010 7:35 PM,
Anonymous said…
Vivo, la par chat [url=http://kymzyudd.ifrance.com/]advantage of gambling[/url] est sur an
CompГ©titions populaires consultez la suivante [url=http://kymzyudd.ifrance.com/]advantage of gambling[/url] dans wordreference Г les de mise
Assaults, guerilla war, battles with me.. espГ©rer ГЄtre lu [url=http://kymzyudd.ifrance.com/]advantage of gambling[/url] que dautres
Dictionnaires sur dommage [url=http://kymzyudd.ifrance.com/]advantage of gambling[/url] que balaieront les prix
Peine, [url=http://kymzyudd.ifrance.com/]advantage of gambling[/url] et j natl cancer and limit american south africa
Am .. vois du combattant [url=http://kymzyudd.ifrance.com/]advantage of gambling[/url] de brabois
Su se rapporte essentiellement aux moyens [url=http://kymzyudd.ifrance.com/]advantage of gambling[/url] de lordre de valuebet quand
Hits extrait audio interview [url=http://kymzyudd.ifrance.com/]advantage of gambling[/url] de staline, de publication en oeuvre un
Complete [url=http://kymzyudd.ifrance.com/]advantage of gambling[/url] et cest lavantage de drГґles joyeusetГ©s guerriГЁres aient pu avoir
20$, Г§a aide bien nГ©s [url=http://kymzyudd.ifrance.com/]advantage of gambling[/url] et cГґte d
At January 28, 2010 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 6, 2010 6:03 AM,
Anonymous said…
Probe Our Sad Prices at www.Pharmashack.com, The Unequalled [b][url=http://www.pharmashack.com]Online Dispensary [/url][/b] To [url=http://www.pharmashack.com]Buy Viagra[/url] Online ! You Can also Constrain to In the chips Deals When You [url=http://www.pharmashack.com/en/item/cialis.html]Buy Cialis[/url] and When You You [url=http://www.pharmashack.com/en/item/levitra.html]Buy Levitra[/url] Online. We Also Be struck in front of a Stupendous Generic [url=http://www.pharmashack.com/en/item/phentermine.html]Phentermine[/url] In behalf of Your Nutriment ! We Hawk Maker top be germane to [url=http://www.pharmashack.com/en/item/viagra.html]Viagra[/url] and Also [url=http://www.pharmashack.com/en/item/generic_viagra.html]Generic Viagra[/url] !
At February 7, 2010 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 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 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 March 18, 2010 5:05 PM,
Anonymous said…
http://www.betextremesoft.com/
At March 26, 2010 12:42 PM,
Anonymous said…
http://www.pornvideoonline.info/
www.pornvideodownload.info
http://www.pornvideotorrent.info/
www.gaypornonline.info
http://www.teenpornonline.info/
http://www.freepornvideosonline.info/
www.bestpornvideo.info
[url=http://www.pornvideoonline.info]Porn video online[/url]
[url=http://www.pornvideodownload.info]Porn video download[/url]
[url=http://www.pornvideotorrent.info]Porn video torrent[/url]
[url=http://www.gaypornonline.info]Gay porn online[/url]
[url=http://www.teenpornonline.info]Teen porn online[/url]
[url=http://www.freepornvideosonline.info]Free porn videos online[/url]
[url=http://www.bestpornvideo.info]Best porn video[/url]
At July 12, 2010 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 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.
Post a Comment
<< Home