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

Home Posts Topics Members FAQ

Overriding builtin getattr method

Hello guys,
I have data stored in the database which has special characters
like <, etc.
Case 1: Whenever I wanted to present the output to a browser
I need to escape these special characters into the browser
equivalent like &lt; &gt; etc.( for example by using the cgi module)
Case 2: Whenever I wanted to present the output to some client other
than a browser, I wanted to present the data as it is stored in the
database.

For doing this I thought of overriding the __builtin__.__g etattr__
method.
I am wondering if there is any other way of achieving this. I have
loads of files that get the attribute values of objects stored in the
database and I do not want to manually change the way of DB access in
those files. I rather prefer a centralized way to achieve this.

Good inputs are always appreciated.
:-)
Raja

Oct 3 '06 #1
4 1789
Correction: I meant __builtin__.get attr method and not the other one I
mentioned.
:-)
Thanks
Raja

Raja Raman Sundararajan skrev:
Hello guys,
I have data stored in the database which has special characters
like <, etc.
Case 1: Whenever I wanted to present the output to a browser
I need to escape these special characters into the browser
equivalent like &lt; &gt; etc.( for example by using the cgi module)
Case 2: Whenever I wanted to present the output to some client other
than a browser, I wanted to present the data as it is stored in the
database.

For doing this I thought of overriding the __builtin__.__g etattr__
method.
I am wondering if there is any other way of achieving this. I have
loads of files that get the attribute values of objects stored in the
database and I do not want to manually change the way of DB access in
those files. I rather prefer a centralized way to achieve this.

Good inputs are always appreciated.
:-)
Raja
Oct 3 '06 #2
At Tuesday 3/10/2006 05:24, Raja Raman Sundararajan wrote:
>Hello guys,
I have data stored in the database which has special characters
like <, etc.
Case 1: Whenever I wanted to present the output to a browser
I need to escape these special characters into the browser
equivalent like &lt; &gt; etc.( for example by using the cgi module)
Case 2: Whenever I wanted to present the output to some client other
than a browser, I wanted to present the data as it is stored in the
database.

For doing this I thought of overriding the __builtin__.__g etattr__
method.
Not very reasonable on the source object itself. Escaping <&is a
*presentation* requirement and should be managed at that level. For
example, in a PageTemplate, using tal:content or tal:attribute does
the right escaping.
>I am wondering if there is any other way of achieving this. I have
loads of files that get the attribute values of objects stored in the
database and I do not want to manually change the way of DB access in
those files. I rather prefer a centralized way to achieve this.
If your data contains a '<', that's OK, it's the real contents and
should remain that way.
If you have to build an HTML page, escape those characters at *that* stage.
If you have to write a CSV file, perhaps a '<' is irrelevant but a
',' is problematic.
For some Windows text controls you have to escape '&' characters.
None of these operations should make you to change your data, or the
way you access your data. You invoke them at the presentation stage,
depending on the final target.

Gabriel Genellina
Softlab SRL


_______________ _______________ _______________ _____
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Oct 3 '06 #3
> I have data stored in the database which has special characters
>like <, etc.
Case 1: Whenever I wanted to present the output to a browser
I need to escape these special characters into the browser
equivalent like &lt; &gt; etc.( for example by using the cgi module)
Case 2: Whenever I wanted to present the output to some client other
than a browser, I wanted to present the data as it is stored in the
database.

For doing this I thought of overriding the __builtin__.__g etattr__
method.
I am wondering if there is any other way of achieving this. I have
loads of files that get the attribute values of objects stored in the
database and I do not want to manually change the way of DB access in
those files. I rather prefer a centralized way to achieve this.
The centralized approach to this is certainly not achieved by
overloading getattr alone - because even if it was possible to do so
(which it isn't), the implementation needed a way to know when to use
escaping or not. Which would be signaled by some means, e.g. a
thread-local variable or even a global (shudder).

This signal gets set in the code that decides that it wants the data
escaped - or not. And thus the code looks like this:

needs_escaping( )
work_with_objec ts()
no_escaping_any more()

But that is fragile, think of work_with_objec ts() throwing an exception
and the no_escaping_any more() isn't called again.

So the better approach is to gather the data as it is, and when you
transform it to something that requires escaping, do it there.

That means: the way to do it is to use one of the gazillion web
templating systems that already do this escaping for you.

Diez
Oct 3 '06 #4
Hello Gabriel Genellina and Diez B. Roggisch,
Thanks for sharing your opinions. I agree with Gabriel when he
talks about the separation between the presentation and the DB level
access and the drawbacks of introducing character manipulation. The
problem that I am facing right now is that the presentation layer uses
several different page rendering machines (about 12).
Some of them use the escaping techniques that presents browser reserved
characters to be "unrunnable " <,we just some examples, so far so
great. But the other presentation layer engines(about 9) used are basic
(optimized for fast rendering) and do not use such filtering/encoding
mechanisms. Since the amount of files that is used by the "basic"
rendering machines are quite high, I am forced to fix the problem in a
much lower level.

I have found a work around for it...since the pages presented by the
basic rendering machines knows about objects fetched from the DB and
the type of request(which presentation engine to use) generated, I
collect information about these in a struct. This struct also contains
information about the level of encoding needed.

I overrode __getattribute_ _ method (which is always called when
one fetches an attribute of an object) in the base class and introduced
encoding level in the class. The __getattribute_ _ then checks for the
level of encoding needed depending on the infor from the struct and
returns the encoded data to the presentation layer when needed. The
business object or the functional layer is unaffected as the default
value returned by the __getattribute_ _ is the unencoded data from the
DB.

Raja
Diez B. Roggisch skrev:
I have data stored in the database which has special characters
like <, etc.
Case 1: Whenever I wanted to present the output to a browser
I need to escape these special characters into the browser
equivalent like &lt; &gt; etc.( for example by using the cgi module)
Case 2: Whenever I wanted to present the output to some client other
than a browser, I wanted to present the data as it is stored in the
database.

For doing this I thought of overriding the __builtin__.__g etattr__
method.
I am wondering if there is any other way of achieving this. I have
loads of files that get the attribute values of objects stored in the
database and I do not want to manually change the way of DB access in
those files. I rather prefer a centralized way to achieve this.

The centralized approach to this is certainly not achieved by
overloading getattr alone - because even if it was possible to do so
(which it isn't), the implementation needed a way to know when to use
escaping or not. Which would be signaled by some means, e.g. a
thread-local variable or even a global (shudder).

This signal gets set in the code that decides that it wants the data
escaped - or not. And thus the code looks like this:

needs_escaping( )
work_with_objec ts()
no_escaping_any more()

But that is fragile, think of work_with_objec ts() throwing an exception
and the no_escaping_any more() isn't called again.

So the better approach is to gather the data as it is, and when you
transform it to something that requires escaping, do it there.

That means: the way to do it is to use one of the gazillion web
templating systems that already do this escaping for you.

Diez
Oct 3 '06 #5

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

Similar topics

5
1967
by: Blair Hall | last post by:
Can anyone please tell me how to correctly use a built in function when there is a function of the same name in local scope? Here is an example. Suppose the following is in myApply.py: def apply(func,seq): # # Code can default to # built-in definition in some cases: return __builtins__.apply(func,seq)
10
1682
by: Stuart McGraw | last post by:
I have a class A from a third party that I cannot change and is implemented in C. I derive my own class B from A and add a couple new methods and override a method. The problem is that A has a method (call it A.f() ) that creates and returns a new A object. I need B.f() to return a B object derived from A.f(). What is the best way to make that happen?
2
1721
by: Nick Patavalis | last post by:
Why does the following print "0 0" instead of "0 1"? What is the canonical way to rewrite it in order to get what I obviously expect? class C(object): __val = 0 def set_value(self, val): if val < 0 : self.__val = 0 else : self.__val = val def get_value(self): return self.__val
8
1905
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>> getattr(Parrot, "__dict__") is Parrot.__dict__ False
13
4190
by: Pierre | last post by:
Hi, Sorry in advance, english is not my main language :/ I'd like to customize the result obtained by getattr on an object : if the object has the requested property then return it BUT if the object doesn't has actually this property return something else. In my case, I can't use getattr(object, property, default_value).
4
2428
by: Hole | last post by:
Hi There! I'm trying to use Zope and the product OpenFlow. I got the following error while I was using the built-in function getattr() to retrieve an OpenFlow object: attribute name must be string
4
3688
by: Emin | last post by:
Dear experts, I got some unexpected behavior in getattr and copy.deepcopy (see transcript below). I'm not sure if this is actually a bug in copy.deepcopy or if I'm doing something too magical with getattr. Comments would be appreciated. Thanks, -Emin
2
1149
by: Francesco Guerrieri | last post by:
Hello, this is my first post to the list :-) I've looked around a bit before asking, and since I haven't found... I'm here to ask my question. I'm trying to ovveride attribute setting, but I haven't still found the right way to use all the fancy __get__, __set__ and __getattribute__ :-) I would like to retain initialization of an object by use of the = and not as a function call. A simple example is this:
3
3081
numberwhun
by: numberwhun | last post by:
Hello everyone! I am presently going through the "Dive Into Python" tutorial which I obtained from their website. No, this is not for any class, I am self-learning the Python language. I am in Chapter 4 and reading about "Getting Object References with getattr". First, it say in there that, "you can get a reference to a function without knowing its name until run−time, by using the getattr function". That is the first thing that is a...
0
9708
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
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10085
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
9161
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
6857
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
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
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
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.