[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

is it possible to set namespace to an object.

glomde

1/21/2008 5:26:00 PM

Hi,

is it somehow possible to set the current namespace so that is in an
object.
Somthing like.

class Test():
....

testObj = Test()

set namespace testObj
Name = "Test"

Name would set testObj.Name to "Test".

I was thinking this could be done with the with statement somehow
(without using as) in the __enter__
function.

Is the above possible?

/T
6 Answers

Wildemar Wildenburger

1/21/2008 5:59:00 PM

0

glomde wrote:
> Hi,
>
> is it somehow possible to set the current namespace so that is in an
> object.
> [snip]
> set namespace testObj
> Name = "Test"
>
> Name would set testObj.Name to "Test".
>
> [snip]
>
> Is the above possible?
>
Don't know, sorry. But let me ask you this: Why do you want to do this?
Maybe there is another way to solve the problem that you want to solve.

/W

glomde

1/21/2008 6:57:00 PM

0

On 21 Jan, 18:59, Wildemar Wildenburger
<lasses_w...@klapptsowieso.net> wrote:
> glomde wrote:
> > Hi,
>
> > is it somehow possible to set the current namespace so that is in an
> > object.
> > [snip]
> > set namespace testObj
> > Name = "Test"
>
> > Name would set testObj.Name to "Test".
>
> > [snip]
>
> > Is the above possible?
>
> Don't know, sorry. But let me ask you this: Why do you want to do this?
> Maybe there is another way to solve the problem that you want to solve.

The reason is that I do not want to repeat myself. It is to set up XML
type like
trees and I would like to be able to do something like.

with ElemA():
Name = "Top"
Description "Blahaha..."
with ElemB():
Name = "ChildA"
Description "Blahaha..."
....

This would be the instead of.
with ElemA() as node:
node.Name = "Top"
node.Description "Blahaha..."
with ElemB() as node:
node.Name = "ChildA"
node.Description "Blahaha..."
....

So to save typing and have something that I think looks nicer.

BR

/T





>
> /W

George Sakkis

1/21/2008 7:16:00 PM

0

On Jan 21, 1:56 pm, glomde <brk...@gmail.com> wrote:
> On 21 Jan, 18:59, Wildemar Wildenburger
>
>
>
> <lasses_w...@klapptsowieso.net> wrote:
> > glomde wrote:
> > > Hi,
>
> > > is it somehow possible to set the current namespace so that is in an
> > > object.
> > > [snip]
> > > set namespace testObj
> > > Name = "Test"
>
> > > Name would set testObj.Name to "Test".
>
> > > [snip]
>
> > > Is the above possible?
>
> > Don't know, sorry. But let me ask you this: Why do you want to do this?
> > Maybe there is another way to solve the problem that you want to solve.
>
> The reason is that I do not want to repeat myself. It is to set up XML
> type like
> trees and I would like to be able to do something like.
>
> with ElemA():
> Name = "Top"
> Description "Blahaha..."
> with ElemB():
> Name = "ChildA"
> Description "Blahaha..."
> ....
>
> This would be the instead of.
> with ElemA() as node:
> node.Name = "Top"
> node.Description "Blahaha..."
> with ElemB() as node:
> node.Name = "ChildA"
> node.Description "Blahaha..."
> ....
>
> So to save typing and have something that I think looks nicer.

.... and more confusing for anyone reading the code (including you
after a few weeks/months). If you want to save a few keystrokes, you
may use 'n' instead of 'node' or use an editor with easy auto
completion.

By the way, is there any particular reason for generating the XML
programmatically like this ? Why not have a separate template and use
one of the dozen template engines to populate it ?

George

glomde

1/21/2008 7:52:00 PM

0

On 21 Jan, 20:16, George Sakkis <george.sak...@gmail.com> wrote:
> On Jan 21, 1:56 pm, glomde <brk...@gmail.com> wrote:
>
>
>
> > On 21 Jan, 18:59, Wildemar Wildenburger
>
> > <lasses_w...@klapptsowieso.net> wrote:
> > > glomde wrote:
> > > > Hi,
>
> > > > is it somehow possible to set the current namespace so that is in an
> > > > object.
> > > > [snip]
> > > > set namespace testObj
> > > > Name = "Test"
>
> > > > Name would set testObj.Name to "Test".
>
> > > > [snip]
>
> > > > Is the above possible?
>
> > > Don't know, sorry. But let me ask you this: Why do you want to do this?
> > > Maybe there is another way to solve the problem that you want to solve.
>
> > The reason is that I do not want to repeat myself. It is to set up XML
> > type like
> > trees and I would like to be able to do something like.
>
> > with ElemA():
> > Name = "Top"
> > Description "Blahaha..."
> > with ElemB():
> > Name = "ChildA"
> > Description "Blahaha..."
> > ....
>
> > This would be the instead of.
> > with ElemA() as node:
> > node.Name = "Top"
> > node.Description "Blahaha..."
> > with ElemB() as node:
> > node.Name = "ChildA"
> > node.Description "Blahaha..."
> > ....
>
> > So to save typing and have something that I think looks nicer.
>
> ... and more confusing for anyone reading the code (including you
> after a few weeks/months). If you want to save a few keystrokes, you
> may use 'n' instead of 'node' or use an editor with easy auto
> completion.
>
> By the way, is there any particular reason for generating the XML
> programmatically like this ? Why not have a separate template and use
> one of the dozen template engines to populate it ?
>
> George

I am not using it for XML generation. It was only an example. But
the reason for using it programmatically is that you mix power
of python with templating. Using for loops and so on.
XIST(www.livinglogic.de) is
doing something similar. But I would like the namespace thing.

The above was only an example. And yes it might be confusing if you
read the code. But I still want to do it, the question is it possible?





George Sakkis

1/21/2008 9:01:00 PM

0

On Jan 21, 2:52 pm, glomde <brk...@gmail.com> wrote:
> On 21 Jan, 20:16, George Sakkis <george.sak...@gmail.com> wrote:
>
>
>
> > On Jan 21, 1:56 pm, glomde <brk...@gmail.com> wrote:
>
> > > On 21 Jan, 18:59, Wildemar Wildenburger
>
> > > <lasses_w...@klapptsowieso.net> wrote:
> > > > glomde wrote:
> > > > > Hi,
>
> > > > > is it somehow possible to set the current namespace so that is in an
> > > > > object.
> > > > > [snip]
> > > > > set namespace testObj
> > > > > Name = "Test"
>
> > > > > Name would set testObj.Name to "Test".
>
> > > > > [snip]
>
> > > > > Is the above possible?
>
> > > > Don't know, sorry. But let me ask you this: Why do you want to do this?
> > > > Maybe there is another way to solve the problem that you want to solve.
>
> > > The reason is that I do not want to repeat myself. It is to set up XML
> > > type like
> > > trees and I would like to be able to do something like.
>
> > > with ElemA():
> > > Name = "Top"
> > > Description "Blahaha..."
> > > with ElemB():
> > > Name = "ChildA"
> > > Description "Blahaha..."
> > > ....
>
> > > This would be the instead of.
> > > with ElemA() as node:
> > > node.Name = "Top"
> > > node.Description "Blahaha..."
> > > with ElemB() as node:
> > > node.Name = "ChildA"
> > > node.Description "Blahaha..."
> > > ....
>
> > > So to save typing and have something that I think looks nicer.
>
> > ... and more confusing for anyone reading the code (including you
> > after a few weeks/months). If you want to save a few keystrokes, you
> > may use 'n' instead of 'node' or use an editor with easy auto
> > completion.
>
> > By the way, is there any particular reason for generating the XML
> > programmatically like this ? Why not have a separate template and use
> > one of the dozen template engines to populate it ?
>
> > George
>
> I am not using it for XML generation. It was only an example. But
> the reason for using it programmatically is that you mix power
> of python with templating. Using for loops and so on.

Any template engine worth its name supports loops. Other than that,
various engines provide different degrees of integration with Python,
from pretty limited (e.g. Django templates) to quite extensive (e.g.
Mako, Tenjin).

> The above was only an example. And yes it might be confusing if you
> read the code. But I still want to do it, the question is it possible?

I would be surprised if it is. Yet another idea you may want to
explore if you want that syntax so much is by (ab)using the class
statement since it introduces a new namespace:

class ElemA:
Name = "Top"
Description "Blahaha..."
class ElemB:
Name = "ChildA"
Description "Blahaha..."


PEP 359 would address this easily (it's actually the first use case
shown) but unfortunately it was withdrawn.

George


[1] http://www.python.org/dev/peps/pep-0359/#example-simple-...

glomde

1/22/2008 8:24:00 AM

0

On 21 Jan, 22:00, George Sakkis <george.sak...@gmail.com> wrote:
> On Jan 21, 2:52 pm, glomde <brk...@gmail.com> wrote:
>
>
>
> > On 21 Jan, 20:16, George Sakkis <george.sak...@gmail.com> wrote:
>
> > > On Jan 21, 1:56 pm, glomde <brk...@gmail.com> wrote:
>
> > > > On 21 Jan, 18:59, Wildemar Wildenburger
>
> > > > <lasses_w...@klapptsowieso.net> wrote:
> > > > > glomde wrote:
> > > > > > Hi,
>
> > > > > > is it somehow possible to set the current namespace so that is in an
> > > > > > object.
> > > > > > [snip]
> > > > > > set namespace testObj
> > > > > > Name = "Test"
>
> > > > > > Name would set testObj.Name to "Test".
>
> > > > > > [snip]
>
> > > > > > Is the above possible?
>
> > > > > Don't know, sorry. But let me ask you this: Why do you want to do this?
> > > > > Maybe there is another way to solve the problem that you want to solve.
>
> > > > The reason is that I do not want to repeat myself. It is to set up XML
> > > > type like
> > > > trees and I would like to be able to do something like.
>
> > > > with ElemA():
> > > > Name = "Top"
> > > > Description "Blahaha..."
> > > > with ElemB():
> > > > Name = "ChildA"
> > > > Description "Blahaha..."
> > > > ....
>
> > > > This would be the instead of.
> > > > with ElemA() as node:
> > > > node.Name = "Top"
> > > > node.Description "Blahaha..."
> > > > with ElemB() as node:
> > > > node.Name = "ChildA"
> > > > node.Description "Blahaha..."
> > > > ....
>
> > > > So to save typing and have something that I think looks nicer.
>
> > > ... and more confusing for anyone reading the code (including you
> > > after a few weeks/months). If you want to save a few keystrokes, you
> > > may use 'n' instead of 'node' or use an editor with easy auto
> > > completion.
>
> > > By the way, is there any particular reason for generating the XML
> > > programmatically like this ? Why not have a separate template and use
> > > one of the dozen template engines to populate it ?
>
> > > George
>
> > I am not using it for XML generation. It was only an example. But
> > the reason for using it programmatically is that you mix power
> > of python with templating. Using for loops and so on.
>
> Any template engine worth its name supports loops. Other than that,
> various engines provide different degrees of integration with Python,
> from pretty limited (e.g. Django templates) to quite extensive (e.g.
> Mako, Tenjin).
>
> > The above was only an example. And yes it might be confusing if you
> > read the code. But I still want to do it, the question is it possible?
>
> I would be surprised if it is. Yet another idea you may want to
> explore if you want that syntax so much is by (ab)using the class
> statement since it introduces a new namespace:
>
> class ElemA:
> Name = "Top"
> Description "Blahaha..."
> class ElemB:
> Name = "ChildA"
> Description "Blahaha..."
>
> PEP 359 would address this easily (it's actually the first use case
> shown) but unfortunately it was withdrawn.
>
> George
>
> [1]http://www.python.org/dev/peps/pep-0359/#example-simple-...

Yes the make statement would have done it. But I realized that it
might be possible if it is possible to override the __setattr__ of
local. Then the enter function would set a global variable and the
default setattr would set/get variables from this variable.

Is this possible?