473,804 Members | 3,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

predicting function calls?

I think I know the answer to this, but I'll ask it just in case
there's something I hadn't considered...

I'm working on a python interface to a OODB. Communication with the
DB is over a TCP connection, using a model vaguely based on CORBA.
I'll be creating object handles in Python which are proxies for the
real objects in the database by doing something like:

handle = connection.getO bjectHandle (className, instanceName)

Objects can have attributes (data) and operations associated with
them. It would be very convenient to use the "." syntax to access
both of these, i.e. be able to say:

print handle.someAttr ibute
print handle.someOper ation (arg1, arg2)

I'm using __getattr__() to process both of these constructs, and
herein lies the rub; I need to do different things depending on
whether the name is an attribute or an operation. I can ask the DB
for a list of the names of all the operations supported by a given
object, but that's a fairly expensive thing to do, so I'd rather avoid
it if possible. It would be really nice if I had some way to find
out, from inside __getattr__(), if the value I'm about to return will
get called as a function (i.e., the name is followed by an open
paren). I can't see any way to do that, but maybe I'm missing
something?

One possibility would be to use different syntaxes for attributes and
operations, i.e:

print handle["someAttrib ute"]
print handle.someOper ation (arg1, arg2)

but I really want to avoid having to do that for a variety of reasons,
not the least of which is syntax similarity with a legacy system.

The best I've come up with so far is doing the expensive "get a list
of operations for this class" call the first time I see an object of a
given class and caching the list. The problem there is that this is a
very dynamic system. It may be possible to add new operations on the
fly and I don't have any good way to invalidate the cache.

Any ideas?
Dec 28 '05 #1
7 1137
ro*@panix.com (Roy Smith) writes:
Objects can have attributes (data) and operations associated with
them. It would be very convenient to use the "." syntax to access
both of these, i.e. be able to say:

print handle.someAttr ibute
print handle.someOper ation (arg1, arg2)

I'm using __getattr__() to process both of these constructs, and
herein lies the rub; I need to do different things depending on
whether the name is an attribute or an operation. I can ask the DB
for a list of the names of all the operations supported by a given
object, but that's a fairly expensive thing to do, so I'd rather avoid
it if possible. It would be really nice if I had some way to find
out, from inside __getattr__(), if the value I'm about to return will
get called as a function (i.e., the name is followed by an open
paren). I can't see any way to do that, but maybe I'm missing
something?

Any ideas?


Fnorb (a pure-Python CORBA orb) dealt with this by not allowing
attributes per se. Instead, it provided _get_AttributeN ame and
_set_AttributeN ame as appropriate for each attribute in the IDL. Doing
this means that *everything* you hand back from __getattr__ is going
to be a callable, and presumably called at some point.

Properties postdate Fnorb, so weren't considered in it's design. You
might be able to do something with them as well.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Dec 28 '05 #2
On Tue, 27 Dec 2005 23:53:18 -0500, Roy Smith wrote:
I think I know the answer to this, but I'll ask it just in case
there's something I hadn't considered...

I'm working on a python interface to a OODB. Communication with the
DB is over a TCP connection, using a model vaguely based on CORBA.
I'll be creating object handles in Python which are proxies for the
real objects in the database by doing something like:

handle = connection.getO bjectHandle (className, instanceName)

Objects can have attributes (data) and operations associated with
them. It would be very convenient to use the "." syntax to access
both of these, i.e. be able to say:

print handle.someAttr ibute
print handle.someOper ation (arg1, arg2)

I'm using __getattr__() to process both of these constructs, and
herein lies the rub; I need to do different things depending on
whether the name is an attribute or an operation.
[snip]
It would be really nice if I had some way to find
out, from inside __getattr__(), if the value I'm about to return will
get called as a function (i.e., the name is followed by an open
paren).


How do you decide whether handle.foo should be treated as an attribute or
an operation?

If your object has an attribute foo, then what should you do when somebody
calls handle.foo()? That is, they treat an attribute as if it were an
operation? And vice versa.

In other words, it is not sufficient to know whether handle.foo is being
used as a operation or an attribute, but you need to know whether it
*should* be called as an operation or an attribute.
--
Steven.
Dec 28 '05 #3
Steven D'Aprano <st***@REMOVETH IScyber.com.au> wrote:
How do you decide whether handle.foo should be treated as an attribute or
an operation?
That's exactly what I'm trying to figure out. In the legacy system I'm
trying to emulate, the interpreter knows the from syntax (i.e. whether
handle.foo is followed by an open paren or not). I'm looking for some way
I can emulate that behavior in Python.
If your object has an attribute foo, then what should you do when somebody
calls handle.foo()? That is, they treat an attribute as if it were an
operation? And vice versa.


That's easy. In such a case, the database will generate an error, which I
can then pass on to the user, probably by raising some subclass of
TypeError.
Dec 28 '05 #4
In article <86************ @bhuda.mired.or g>, Mike Meyer <mw*@mired.or g>
wrote:
Fnorb (a pure-Python CORBA orb) dealt with this by not allowing
attributes per se. Instead, it provided _get_AttributeN ame and
_set_AttributeN ame as appropriate for each attribute in the IDL. Doing
this means that *everything* you hand back from __getattr__ is going
to be a callable, and presumably called at some point.


That's exactly what the low-level C/C++ API does in the system I'm working
with. It is, unfortunately, not the interface that's exposed by the serial
protocol.
Dec 28 '05 #5
Roy Smith wrote:
print handle.someAttr ibute
print handle.someOper ation (arg1, arg2)

I'm using __getattr__() to process both of these constructs, and
herein lies the rub; I need to do different things depending on
whether the name is an attribute or an operation. I can ask the DB
for a list of the names of all the operations supported by a given
object, but that's a fairly expensive thing to do, so I'd rather avoid
it if possible. It would be really nice if I had some way to find
out, from inside __getattr__(), if the value I'm about to return will
get called as a function (i.e., the name is followed by an open
paren).


What do you do if the person does:

x = handle.someOper ation
x(arg1, arg2)

--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
Dec 28 '05 #6
Roy Smith wrote:
I think I know the answer to this, but I'll ask it just in case
there's something I hadn't considered...

I'm working on a python interface to a OODB. Communication with the
DB is over a TCP connection, using a model vaguely based on CORBA.
I'll be creating object handles in Python which are proxies for the
real objects in the database by doing something like:

handle = connection.getO bjectHandle (className, instanceName)

Objects can have attributes (data) and operations associated with
them. It would be very convenient to use the "." syntax to access
both of these, i.e. be able to say:

print handle.someAttr ibute
print handle.someOper ation (arg1, arg2)

I'm using __getattr__() to process both of these constructs, and
herein lies the rub; I need to do different things depending on
whether the name is an attribute or an operation. I can ask the DB
for a list of the names of all the operations supported by a given
object, but that's a fairly expensive thing to do, so I'd rather avoid
it if possible. It would be really nice if I had some way to find
out, from inside __getattr__(), if the value I'm about to return will
get called as a function (i.e., the name is followed by an open
paren). I can't see any way to do that, but maybe I'm missing
something?


For various reasons also mentioned by other posters it's also not clear
to me how relying on user input should work. Esp. for the

x=obj.meth
print x(args)

case.

Couldn't you just, for every access to a member of your object, first
try to treat is as an access to an operation? If this fails (you
mentioned the db will throw an error if this is an attribute instead of
an operation), fall back to ask the db for an attribute of that name.

Or maybe just ask the db to look up this attribute in the list of
operations, depending which is faster.

Btw. if the system is very dynamic, you might have to think about also
implementing the attributes as proxies.
Dec 31 '05 #7
In article <41************ *@individual.ne t>, Ernst Noch <en***@gmx.ne t>
wrote:
Couldn't you just, for every access to a member of your object, first
try to treat is as an access to an operation? If this fails (you
mentioned the db will throw an error if this is an attribute instead of
an operation), fall back to ask the db for an attribute of that name.

Or maybe just ask the db to look up this attribute in the list of
operations, depending which is faster.

Btw. if the system is very dynamic, you might have to think about also
implementing the attributes as proxies.


Well, what I ended up doing is having a proxy for each object. The first
time I access any instance of a given class, I get from the DB a list of
operations for that class and cache it (keyed by class).
Dec 31 '05 #8

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

Similar topics

31
2707
by: Bo Peng | last post by:
Dear list, I have many dictionaries with the same set of keys and I would like to write a function to calculate something based on these values. For example, I have a = {'x':1, 'y':2} b = {'x':3, 'y':3} def fun(dict):
3
4808
by: Janross | last post by:
I'm having trouble with a query that's prohibitively slow. On my free-standing office computer it's fine (well, 2-4 seconds), but on the client's network, it takes at least 5 minutes to run. Obviously not workable! I know where the problem is, I just don't know how to fix it. The query calls a function, and I assume it gets slow because the function runs on every record. So--is there a way to rewrite the function so it's quicker?...
2
2328
by: Matthew Caesar | last post by:
I've written some code in the C# programming language. I have a lot of calls to a function that prints out some debugging information. I would like to occasionally remove all calls to these functions when I want the program to run quickly (when I'm measuring how fast it runs) but I want the calls to be in when I'm debugging. More detail: I have a class called MyDebug with a member called Trace. I periodically call MyDebug.Trace("string...
13
4152
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make sense to punch out from managed code to native code (I was using IJW) in order to do some amount of floating point work and, if so, what that certain amount of floating point work was approximately. To attempt to do this I made a program that...
6
2265
by: Dasn | last post by:
Hi, there. 'lines' is a large list of strings each of which is seperated by '\t' I wanna split each string into a list. For speed, using map() instead of 'for' loop. 'map(str.split, lines)' works fine , but... when I was trying: I got "TypeError: 'list' object is not callable".
11
3458
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the vtable pointer is made use of.. eg. class one {
2
1925
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function The question may be silly or even meaning less but please............
6
5933
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
4
1665
by: electromania | last post by:
I need to write a program that can predict the stock market price indicies. The predictions are based on a very naive method that assumes that the market index is a linear function of time. Real life estimates are, of course, much more complicated and keep many people busy all over the globe. Our assumption that the market index, y, is a linear function of time, x, which means that the index can be written as: y = ax + b where a is the slope...
0
9705
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...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10074
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
9138
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...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
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
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
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

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.