can't find the right simplification

S

Stef Mientki

hello,

I've a program where you can connect snippets of code (which I call a
"Brick") together to create a program.
To make it easier to create these code snippets, I need some
simplifications.
For simple parameters ( integer, tupple, list etc) this works ok,
and is done like this:


class _Simple_Par ( object ) :
"""
Class to make it more easy to set a Bricks.Par from a control.
So instead of :
self.Brick.Par [ self.EP[0] ] = Some_Value
you can write
self.P[0] = Some_Value
"""
def __init__ ( self, control ) :
self.Control = control
def __setitem__ ( self, index, value ) :
i = self.Control.EP [ index ]
if i :
self.Control.Brick.Par [ i ] = value

Now I want a similar simplification for the case that Par is a dictionary:
So instead of writing:
self.Brick.Par [ self.EP[0] ] [ 'Filename' ] = Some_Value

I would like to write:
self.P[0] [ 'Filename' ] = Some_Value

But I can't figure out how to accomplish that ?

Any suggestions ?

thanks,
Stef Mientki
 
P

Peter Otten

Stef said:
I've a program where you can connect snippets of code (which I call a
"Brick") together to create a program.
To make it easier to create these code snippets, I need some
simplifications.
For simple parameters ( integer, tupple, list etc)  this works ok,
and is done like this:


class _Simple_Par ( object ) :
    """
    Class to make it more easy to set a Bricks.Par from a control.
    So instead of :
      self.Brick.Par [ self.EP[0] ] = Some_Value
    you can write
      self.P[0] = Some_Value
    """
    def __init__ ( self, control ) :
        self.Control = control
    def __setitem__ ( self, index, value ) :
        i = self.Control.EP [ index ]
        if i :
            self.Control.Brick.Par [ i ] = value

Now I want a similar simplification for the case that Par is a dictionary:
So instead of writing:
      self.Brick.Par [ self.EP[0] ] [ 'Filename' ] = Some_Value

I would like to write:
      self.P[0] [ 'Filename' ] = Some_Value

But I can't figure out how to accomplish that ?

Any suggestions ?

Are you about to confuse your cat, too? Here's the modified example:

class Brick(object):
def __init__(self):
self.Par = [dict(a=1), dict(b=2), dict(c=3)]

class P(object):
def __init__(self, parent):
self.parent = parent
def __getitem__(self, index):
p = self.parent
return p.Brick.Par[p.EP[index]]

class A(object):
def __init__(self):
self.Brick = Brick()
self.EP = [2,1,0]
self.P = P(self)

def demo(self):
print "before:", self.Brick.Par
self.P[0]["yadda"] = "whatever"
print "after:", self.Brick.Par

a = A()
a.demo()

Peter
 
L

Lie Ryan

Stef said:
hello,

I've a program where you can connect snippets of code (which I call a
"Brick") together to create a program.
To make it easier to create these code snippets, I need some
simplifications.
For simple parameters ( integer, tupple, list etc) this works ok,
and is done like this:


class _Simple_Par ( object ) :
"""
Class to make it more easy to set a Bricks.Par from a control.
So instead of :
self.Brick.Par [ self.EP[0] ] = Some_Value
you can write
self.P[0] = Some_Value
"""
def __init__ ( self, control ) :
self.Control = control
def __setitem__ ( self, index, value ) :
i = self.Control.EP [ index ]
if i :
self.Control.Brick.Par [ i ] = value

Now I want a similar simplification for the case that Par is a dictionary:
So instead of writing:
self.Brick.Par [ self.EP[0] ] [ 'Filename' ] = Some_Value

I would like to write:
self.P[0] [ 'Filename' ] = Some_Value

But I can't figure out how to accomplish that ?

Any suggestions ?

thanks,
Stef Mientki

Do this work?

class _Simple_Par (object):
def __init__(self, control):
##
class P(object):
def __init__(self, parent):
self.parent = parent
def __getitem__(self, key):
return self.parent.Brick.Par[self.parent.EP[key]]

self.P = P(self)
##
self.Control = control

btw, it seems your code violated many of PEP 8 style recommendations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top