The PEAK Developers' Center   PeakDatabaseApplications UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View
Version as of 2004-09-20 08:42:53

Clear message


1 Introduction

PEAK is an excellent platform for building Python database applications. Using a combination of several PEAK packages one can build powerful and flexible database applications quickly and easily.

2 First Steps: Binding and Naming

We should first discuss briefly the PEAK packages we'll be working with, starting with peak.binding and peak.naming.

Peak's binding package provides powerful utilities for building component-based applications. This typically means that you create a top-level application class of some sort which instantiates the various component objects needed to accomplish the task at hand. With peak.binding these "component objects" will be able to find each other, most often by defining properties that "bind to" other objects in the hierarchy. Here's a quick example:

    1 from peak.api import binding, storage
    2 
    3 class Worker(binding.Component):
    4     db = binding.Obtain(storage.ISQLConnection)
    5     def doWork(self):
    6         for row in self.db('select name, description from aTable'):
    7             print row.name, row.description
    8 
    9 class Application(binding.Component):
   10     db     = binding.Obtain('sqlite:test.db', offerAs=[storage.ISQLConnection])
   11     worker = binding.Make(Worker)
   12 
   13     def run(self):
   14         storage.beginTransaction(self)
   15         self.worker.doWork()
   16         storage.commitTransaction(self)
   17 
   18 if __name__ == '__main__':
   19     from peak.api import config
   20     root = config.makeRoot()
   21     app = Application(root)
   22     app.run()

As subclasses of binding.Component, the Application and Worker classes are capable of taking part in a component hierarchy. Such a hierarchy needs a "root" object at its base -- we use config.makeRoot() to create a component root object on line 20. On line 21 we instantiate an Application object, specifying that root will be its parent. When creating binding.Component-based objects by hand like this, one must always pass a parent object as the first parameter. When using bindings within a class definition (as done on lines 4, 10, and 11) the parent object for the new component will be supplied automatically, if needed.

This small example is already a working database application. It opens a connection to a [WWW]SQLite database file, runs a query against a table, and prints the results. It also demonstrates one of the most useful aspects of the binding package: the ability to create an object once and make use of it in another part of the application. In database applications you usually want a single connection to a database server which will be shared throughout the application. Without binding one would have to either create the connection object and pass it to every new object that needed it, or provide it to the rest of the application using a singleton mechanism of some kind.

The former technique is highly error prone and forces the application to be "tightly coupled" -- that is, objects need to know quite a bit about each other and are usually highly dependent on each other's interfaces and structure. Changing one object in a tightly coupled application often requires changes to a number of other objects, and in large applications these inter-dependencies are not always known by every programmer or are easily missed. Tightly coupled software tends to be extremely brittle as a result. [WWW]深圳机票[WWW]胶袋,深圳胶袋[WWW]胶带,深圳胶带[WWW]机票[WWW]光端机[WWW]钢管[WWW]风机[WWW]锅炉[WWW]变频器[WWW]润滑油[WWW]离合器[WWW]狐臭[WWW]糖尿病[WWW]电容[WWW]灰指甲[WWW]近视眼[WWW]脱毛[WWW]净水器[WWW]蜂蜜[WWW]紧固件[WWW]制服[WWW]健身器材[WWW]太阳能[WWW]婚纱摄影[WWW]LED[WWW]酒店[WWW]水表[WWW]锅炉[WWW]打包机[WWW]痔疮[WWW]发电机[WWW]深圳装饰[WWW]切割机[WWW]深圳印刷[WWW]深圳条码[WWW]激光设备[WWW]照排机[WWW]餐具[WWW]私家侦探[WWW]六合彩[WWW]脱发治疗网[WWW]深圳机票[WWW]快递|速递公司[WWW]网络电话[WWW]水晶[WWW]融资[WWW]电脑维修[WWW]出国[WWW]开关电源[WWW]模具[WWW]纳米[WWW]污水处理[WWW]物业管理[WWW]VOIP网关[WWW]IP电话[WWW]软交换[WWW]GK[WWW]网络电话[WWW]治疗脱发[WWW]治疗脱头发[WWW]网络电话卡[WWW]VOIP[WWW]VOIP网络电话[WWW]移民[WWW]幕墙[WWW]五金[WWW]脂溢性脱发[WWW]脱发特效药[WWW]生发产品[WWW]脱发产品[WWW]治疗白发[WWW]毛发移植[WWW]头发移植[WWW]视频会议[WWW]汽车租赁[WWW]贴纸[WWW]饮水机[WWW]不锈钢[WWW]自体毛发移植[WWW]皮肤病[WWW]伟哥[WWW]高血压[WWW]交换机[WWW]注册香港公司[WWW]深圳机票[WWW]六合彩[WWW]视频点播[WWW]速递公司[WWW]国际货运[WWW]报警器[WWW]茶叶[WWW]补品[WWW]增高[WWW]壮阳[WWW]青春痘[WWW]化妆品[WWW]治脚气[WWW]鱼鳞病[WWW]白癜风[WWW]实木地板[WWW]签证[WWW]考勤机[WWW]深圳酒店[WWW]科迅公用电话通用计费系统[WWW]语音网关[WWW]ip超市[WWW]话吧[WWW]ip公话超市[WWW]voip在线交易中心[WWW]集团电话[WWW]保健品[WWW]板蓝根[WWW]防盗门[WWW]监控[WWW]包装机[WWW]电缆[WWW]门禁[WWW]瓷砖[WWW]电机[WWW]耳机[WWW]卫星电视[WWW]监视器[WWW]变压器[WWW]对讲机[WWW]传感器[WWW]电子元器件[WWW]刀具[WWW]地毯[WWW]竹炭 The singleton approach can help quite a bit by ensuring that only one instance of a certain type of object exists and that it can be found relatively easily. In Python this is often achieved through a separate module and module-level variables. The great drawback of a singleton, however, is that there's only one of it. Classes that use singletons are less reusable, because you can't create instances that use different values for a singleton. (Because then it wouldn't be a singleton!) So, classes that use singletons are still tightly coupled.

What is typically needed in larger applications is a flexible way to create, lookup, and use "singleton-like" objects. PEAK calls such objects "service components", and part of what PEAK's binding package provides is easy access to such components, using looser forms of coupling such as:

When a class embeds all its dependencies in "attribute bindings" like these, the class can easily be reused via subclassing or by simply setting attributes on instances.

... to be continued ...


PythonPowered
EditText of this page (last modified 2004-09-20 08:42:53)
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck