473,756 Members | 6,250 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parent-child object design question

Hi,

I am having trouble designing my classes.

I have two classes. The first one wraps around an old-style class
called oref
Class CacheClass(obje ct):
def __init__(self, obj):
self.__data = obj
def __getattr__(sel f, attr):
return getattr(self.__ data, attr)

The second class is much the same, also wrapping, but has some
attributes.

class CacheProperty(o bject):
def __init__(self, obj, dicProperties={ }):
self.__data = obj
lisProperties=[]
for key, val in dicProperties.i teritems():
setattr(self, key, val)
lisProperties.a ppend(key)
self.Properties = lisProperties
def __getattr__(sel f, attr):
return getattr(self.__ data, attr)

Here is my code:
>>MyClass = CacheClass(oref )
MyProperty = CacheProperty(o ref2,{'Name':'S urname', 'Value':'Peter' })
setattr(MyCla ss,MyProperty.N ame,MyProperty)
Now, the problem is that I want a method MyClass.MyPrope rty.Save() to
save the value of MyClass.MyPrope rty to the database backend on disk,
but the code for this is part of the wrapped oref code, and thus is
invoked only by MyClass.set(MyP roperty.Name,My Property.Value) .

How can I access this method inside the MyProperty class?

I hope this is clear!

regard,s
matthew

Jan 30 '07
11 2858
Hi,

There was a mistake above, and then I'll explain what we're doing:
>>insCacheCla ss = CacheClass(oref )
insCachePrope rty = CacheProperty(i nsOref,'Chapter ')
should have been
>>insCacheCla ss = CacheClass(oref )
insCachePrope rty = CacheProperty(i nsCacheClass ,'Chapter')
Now, to answer some questions.
1. Cache refers to Intersystems Cache database, an powerful oo dbase
that holds our data.
2. It comes with a pythonbinding, but unfortunatley the API, written
in C as a python extension, only provides old-style classes, which is
why we wrap the oref (an in-memory instance of a Cache class/table)
with CacheClass. This allows us to add attributes and methods to the
CacheClass, the most important being CacheProperty, which is a class
corresponding to the Cache property/field classes of the dbase.
3. The pythonbind also has a strange behaviour, to our view. It gets
and sets values of its properties (which are classes), via the
'parent' oref, i.e. oref.set('Name' ,'Peter'). But we want a pythonic
way to interact with Cache, such as, insCacheClass.N ame='Peter'. and
insOref.Name.Se t(). The code above (in post 7) does this, but as I
asked, by storing the parent instance (insCacheClass) inside each of
its attributes (insCacheProper ty1,2, etc).
4. Another reason we want a new-style class wrapped around the oref,
is that each oref has many methods on the server-side Cache database,
but they are all called the same way, namely,
oref.run_obj_me thod('%METHODNA ME',[lisArgs]). We want to call these in
python in the much better insCacheClass.M ETHODNAME(Args) . E.g. we
prefer insCacheClass.S ave() to oref.run_obj_me thod('%Save',[None]).
More importantly, the in-memory version of Cache lists and arrays are
Cache lists and arrays, but these are not Python lists and
dictionaries, but they can be easily converted into them. So having a
class for each dbase property allows us to have Cache on disk
(server), get the Cache list/array to python in memory, but still in
Cache dataformat (e.g. %ListOfDataType s) and convert it to a python
list. Tweaking the set and get methods then lets us use python lists
and dics without ever worrying about cache dataformat.

I hope this clarifies what we are doing. We are not experienced
programmers, but we want to do it this way.

Feb 2 '07 #11
On Fri, 02 Feb 2007 13:53:21 -0800, manstey wrote:
Hi,

There was a mistake above, and then I'll explain what we're doing:
>>>insCacheClas s = CacheClass(oref )
insCacheProp erty = CacheProperty(i nsOref,'Chapter ')

should have been
>>>insCacheClas s = CacheClass(oref )
insCacheProp erty = CacheProperty(i nsCacheClass ,'Chapter')

Now, to answer some questions.
1. Cache refers to Intersystems Cache database, an powerful oo dbase
that holds our data.
2. It comes with a pythonbinding, but unfortunatley the API, written
in C as a python extension, only provides old-style classes, which is
why we wrap the oref (an in-memory instance of a Cache class/table)
with CacheClass. This allows us to add attributes and methods to the
CacheClass, the most important being CacheProperty, which is a class
corresponding to the Cache property/field classes of the dbase.
You can add attributes and methods to old-style classes.

3. The pythonbind also has a strange behaviour, to our view. It gets
and sets values of its properties (which are classes),
Are you sure you're using the word "classes" correctly? That seems ...
strange. I'm wondering whether the properties are class _instances_.

via the
'parent' oref, i.e. oref.set('Name' ,'Peter'). But we want a pythonic
way to interact with Cache, such as, insCacheClass.N ame='Peter'. and
insOref.Name.Se t().
Okay, let me see that I understand what happens. Here's some pseudo-code
of what I think you are currently doing:
oref = get_a_reference _to_Cache_datab ase() # or whatever
# now do work on it using the python bindings.
oref.set("Name" , "Peter")
oref.set("Somet hing", "Else")
Here's a wrapper for the bindings, so they work more pythonically:

class Oref_Wrapper(ob ject):
def __init__(self, oref):
self._oref = oref # save the oref for later use
# Delegate attribute access to the oref
def __setattr__(sel f, name, value):
return self._oref.set( name, value)
def __getattr__(sel f, name):
return self._oref.get( name) # or whatever the oref does
def __delattr__(sel f, name):
return self._oref.del( name) # or whatever the oref does
This is how you use it:
oref = get_a_reference _to_Cache_datab ase() # or whatever
# re-bind the name to a wrapped version
oref = Oref_Wrapper(or ef)
# now do work on it
oref.Name = "Peter"
oref.Something = "Else"

The code above (in post 7) does this, but as I
asked, by storing the parent instance (insCacheClass) inside each of
its attributes (insCacheProper ty1,2, etc).
Do you use insCacheClass and insCachePropert y for anything other than
making the bindings more Pythonic? Because for the life of me I can't work
out what they're supposed to do!

4. Another reason we want a new-style class wrapped around the oref,
is that each oref has many methods on the server-side Cache database,
but they are all called the same way, namely,
oref.run_obj_me thod('%METHODNA ME',[lisArgs]). We want to call these in
python in the much better insCacheClass.M ETHODNAME(Args) . E.g. we
prefer insCacheClass.S ave() to oref.run_obj_me thod('%Save',[None]).
Using my code above, oref.METHODNAME (args) would resolve to:

oref._oref.get( "METHODNAME")(a rgs)

which may not do what you want.

If you know all the possible METHODNAMES, then that's easy enough to fix:
create methods at runtime. (If you don't know the methods, the problem
becomes too hard for me to work out at 3am, but will probably still be
solvable.)

Change the __init__ method above to something like this (untested):

import new

class Oref_Wrapper(ob ject):
def __init__(self, oref):
self._oref = oref # save the oref for later use
METHODNAMES = ["Save", "Clear", "Something"]
for name in METHODNAMES:
function = lambda self, *args: \
self._oref.run_ obj_method("%"+ name, args)
method = new.instancemet hod(function, name)
self.__dict__[name] = method

then define the __getattr__ etc. methods as before, and (hopefully!) you
are done:

oref.Save()
oref.Clear(1, 2, 3, 7)

I hope this was of some help.

--
Steven.

Feb 4 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3652
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the parent class, and I can't make some "helper" class a friend of the parent class to help in accessing the data.) Problem: I want to derive a class that has a copy constructor that properly copies those data members.
5
7672
by: Steve Richter | last post by:
In my user control I want to read the ViewState dictionary of the Parent control. But this sensible idea is not permitted by the compiler: Compiler Error Message: CS1540: Cannot access protected member 'System.Web.UI.Control.ViewState' via a qualifier of type 'System.Web.UI.Control'; the qualifier must be of type 'ASP.ItemOrderGrid' (or derived from it) Source Error:
1
4658
by: John A Grandy | last post by:
Let's say I have an .aspx Page instance derived from class "MyPage" (which of course inherits from System.Web.UI.Page). On the .aspx Page , I have "uc1", an instance of a UserControl dervived from "MyUserControl". uc1 is contained in a <td> in a <tr> in a <table> in a <td> in a <tr> of another <table> ... or some other form of deep nesting. In uc1 's code-behind I'd like to reference some properties of the parent
4
9743
by: Phil Powell | last post by:
I thought this would work but it seems to not work neither in Netscape nor in IE: <script type="text/javascript"> <!-- // OBTAINED FROM http://www.javascripter.net/faq/settinga.htm // NOTE THAT IF YOU SET days TO -1 THE COOKIE WILL BE SET TO YESTERDAY
13
3596
tpgames
by: tpgames | last post by:
What is the onload event handler needed to get # assigned to parent variable? What I tried didn't work! Code is from JS bible, page 127-8 with some minor alterations by me. Parent Page (in part) <!-- start function goNext() { var currOffset = parseInt(parent.currTitle); if (currOffset < 40) { currOffset +=1; parent.cryptogram.location.href = "http://www.tpgames.net/gaming/2/word/cipher/f/c/c" + currOffset + ".html";...
1
4552
by: IframeLearner | last post by:
Hi , I am trying to upload a file from a parent.jsp using Iframes. From Parent page. I have to save Subject, Desc, File and file name. to upload the file i am using Iframe. I want the iframe to save file in the Db and come back to parent page with file name in the form of the parent , so that i can save the parent after updaing other details. I am able to save the file in the DB and come back to parent page. While coming back...
5
2184
by: gnewsgroup | last post by:
In my user control, I would like to find a Label control in the parent page (the page that uses my user control). I need to update that Label.Text when something happens in the user control. I don't want to go through the hassle of creating events in the user control, and then let the parent handle the event. What is the easiest way to find a control in the parent page? Right now, I am simply manually traversing it from the user...
1
3544
by: SunshineInTheRain | last post by:
My project has 3 files, File1 has included master page. file1 consists of iframe1 that load file2. File2 consists of iframe2 that load file3. Javascript used on each file to resize the iframe height based on the each iframe's content height. It is work well on IE, but the error "has no properties" occured with firefox on code as below. Where both code is to get the id of iframe on file1....
1
3800
by: bnchs | last post by:
This is C code. I am trying to fill each node's Parent field with its parent because I am drawing nodes to the screen. However, I have not been able to get this working. Reading the output from the code and tracing it, it seems that the code does not continue recursing down into the tree to insert. Can someone please point me to what is wrong and help me understand why it is wrong? Thank you. struct AvlNode* Insert( gint X, struct AvlNode*...
0
9303
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9541
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8542
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4955
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3651
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3141
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.