473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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.getObjectHandle (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.someAttribute
print handle.someOperation (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["someAttribute"]
print handle.someOperation (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 1114
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.someAttribute
print handle.someOperation (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_AttributeName and
_set_AttributeName 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.org> 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.getObjectHandle (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.someAttribute
print handle.someOperation (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***@REMOVETHIScyber.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.org>, Mike Meyer <mw*@mired.org>
wrote:
Fnorb (a pure-Python CORBA orb) dealt with this by not allowing
attributes per se. Instead, it provided _get_AttributeName and
_set_AttributeName 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.someAttribute
print handle.someOperation (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.someOperation
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.getObjectHandle (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.someAttribute
print handle.someOperation (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.net>, Ernst Noch <en***@gmx.net>
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
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 =...
3
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. ...
2
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...
13
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...
6
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)'...
11
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...
2
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 ...
6
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...
4
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.