best way to code

E

eric

hi,

I need to find a "good" design pattern to instanciate, and add
specific code all in one. Let me explain it :

I need to define "some" code, better be in a class, something like

class LinkA(object):
def mystuff(self):
<do something different>

class LinkB(object):
def mystuff(self):
<do something different again>


AND I need an instance of this class
{ "stuff A": LinkA()
"stuff B": LinkB()
}

This kind of code "would" be fine, I mean, the result effect in memory
is fine for me.
But I don't like the way I have to
1/ give a useless name to LinkA, linkB (there can be hundreds of names
like that)
2/ I have to write it down two times (and that's one time too much)

any ideas ?

something like
[
new object():
def mystuff(self):
<do something>
,
new object():
def mystuff(self):
<do something else>
]

would be really perfect (but I know it does not work, or at least, I
don't know how to make it work)

In fact, I would like to define a class, and an instance in a single
statement


thanks
 
P

Peter Otten

eric said:
hi,

I need to find a "good" design pattern to instanciate, and add
specific code all in one. Let me explain it :

I need to define "some" code, better be in a class, something like

class LinkA(object):
def mystuff(self):
<do something different>

class LinkB(object):
def mystuff(self):
<do something different again>


AND I need an instance of this class
{ "stuff A": LinkA()
"stuff B": LinkB()
}

This kind of code "would" be fine, I mean, the result effect in memory
is fine for me.
But I don't like the way I have to
1/ give a useless name to LinkA, linkB (there can be hundreds of names
like that)
2/ I have to write it down two times (and that's one time too much)

any ideas ?

something like
[
new object():
def mystuff(self):
<do something>
,
new object():
def mystuff(self):
<do something else>
]

would be really perfect (but I know it does not work, or at least, I
don't know how to make it work)

In fact, I would like to define a class, and an instance in a single
statement
.... def __init__(self):
.... self.items = []
.... def __call__(self, method):
.... class Link(object):
.... mystuff = method
.... self.items.append(Link())
........ def mystuff(self): print "first"
........ def mystuff(self): print "second"
........ item.mystuff()
....
first
second

Peter
 
E

eric

eric said:
I need to find a "good" design pattern to instanciate, and add
specific code all in one. Let me explain it :
I need to define "some" code, better be in a class, something like
class LinkA(object):
    def mystuff(self):
         <do something different>
class LinkB(object):
    def mystuff(self):
         <do something different again>
AND I need an instance of this class
{ "stuff A": LinkA()
  "stuff B": LinkB()
}
This kind of code "would" be fine, I mean, the result effect in memory
is fine for me.
But I don't like the way I have to
1/ give a useless name to LinkA, linkB (there can be hundreds of names
like that)
2/ I have to write it down two times (and that's one time too much)
any ideas ?
something like
[
new object():
    def mystuff(self):
       <do something>
,
new object():
    def mystuff(self):
       <do something else>
]
would be really perfect (but I know it does not work, or at least, I
don't know how to make it work)
In fact, I would like to define a class, and an instance in a single
statement

...     def __init__(self):
...             self.items = []
...     def __call__(self, method):
...             class Link(object):
...                     mystuff = method
...             self.items.append(Link())
...>>> register = Register()
... def mystuff(self): print "first"
...>>> @register

... def mystuff(self): print "second"
...>>> for item in register.items:

...     item.mystuff()
...
first
second

Peter


hi,

I've tried something like this :

import inspect

class Test(object):
class Inner(object):
def mystuff(self):
print "hello stuff"

class InnerB(object):
def mystuff(self):
print "hello B"


def filter(member):
return inspect.isclass(member) and not member==Test.__class__
d = dict( (name, c()) for name, c in inspect.getmembers(Test,
filter ) )
print d


it works too, but I prefer your method

thanks
 
E

eric

eric said:
hi,
I need to find a "good" design pattern to instanciate, and add
specific code all in one. Let me explain it :
I need to define "some" code, better be in a class, something like
class LinkA(object):
    def mystuff(self):
         <do something different>
class LinkB(object):
    def mystuff(self):
         <do something different again>
AND I need an instance of this class
{ "stuff A": LinkA()
  "stuff B": LinkB()
}
This kind of code "would" be fine, I mean, the result effect in memory
is fine for me.
But I don't like the way I have to
1/ give a useless name to LinkA, linkB (there can be hundreds of names
like that)
2/ I have to write it down two times (and that's one time too much)
any ideas ?
something like
[
new object():
    def mystuff(self):
       <do something>
,
new object():
    def mystuff(self):
       <do something else>
]
would be really perfect (but I know it does not work, or at least, I
don't know how to make it work)
In fact, I would like to define a class, and an instance in a single
statement
class Register:
...     def __init__(self):
...             self.items = []
...     def __call__(self, method):
...             class Link(object):
...                     mystuff = method
...             self.items.append(Link())
...>>> register = Register()
@register
... def mystuff(self): print "first"
...>>> @register
... def mystuff(self): print "second"
...>>> for item in register.items:
...     item.mystuff()
...
first
second

hi,

I've tried something like this :

import inspect

class Test(object):
    class Inner(object):
        def mystuff(self):
            print "hello stuff"

    class InnerB(object):
        def mystuff(self):
            print "hello B"

def filter(member):
    return inspect.isclass(member) and not member==Test.__class__
d = dict( (name, c()) for name, c in inspect.getmembers(Test,
filter ) )
print d

it works too, but I prefer your method

thanks

Finally here is my 'final' shot:

context: when building a glade GUI I wanted to connect 'nicely'
signals (I hate coding the same info in several places)

here is my main :

if __name__=="__main__":
pathname = os.path.dirname(sys.argv[0])
startup = os.path.join(pathname, 'pyshow/pyshow.glade')
xml = gtk.glade.XML(startup) #'filename.glade')

#the stuff starts here
m = MainApp()
for widget_name, codget in m.items():
codget.set_widget( xml.get_widget(widget_name) )

gtk.main()


and here is the 'MainApp' code, based on the question, and finally I
get stuck to my second solution


import inspect
class Controller(dict):
def __init__(self):
dict.__init__(self)
self.update(
(name, c()) for name, c in inspect.getmembers
(self.__class__, lambda member: inspect.isclass(member) and not
member==self.__class__.__class__ )
)



class MainApp(Controller):
def __init__(self):
Controller.__init__(self)

class main_window(codget):
def on_destroy(self, widget, modget):
print "bye bye"

class play(codget):
def on_clicked(self, widget, modget):
print "you have clicked"



the business is hidden in codget ( as COntroller gaDGET), and what's
interesting for me, is that every signal handler has a 'modget' ( as
in model gadget) that it can use to do the job (part of the MVC
pattern)

thanks Peter anyway, I didn't use your solution in this specific case,
but I loved the solution anyway, and I'm sure that I'll use it one
day.
 
M

miya

eric said:
I need to find a "good" design pattern to instanciate, and add
specific code all in one. Let me explain it :
I need to define "some" code, better be in a class, something like
class LinkA(object):
    def mystuff(self):
         <do something different>
class LinkB(object):
    def mystuff(self):
         <do something different again>
AND I need an instance of this class
{ "stuff A": LinkA()
  "stuff B": LinkB()
}
This kind of code "would" be fine, I mean, the result effect in memory
is fine for me.
But I don't like the way I have to
1/ give a useless name to LinkA, linkB (there can be hundreds of names
like that)
2/ I have to write it down two times (and that's one time too much)
any ideas ?
something like
[
new object():
    def mystuff(self):
       <do something>
,
new object():
    def mystuff(self):
       <do something else>
]
would be really perfect (but I know it does not work, or at least, I
don't know how to make it work)
In fact, I would like to define a class, and an instance in a single
statement

...     def __init__(self):
...             self.items = []
...     def __call__(self, method):
...             class Link(object):
...                     mystuff = method
...             self.items.append(Link())
...>>> register = Register()
... def mystuff(self): print "first"
...>>> @register

... def mystuff(self): print "second"
...>>> for item in register.items:

...     item.mystuff()
...
first
second

Peter

Wow, loved this solution. Never thought about using decorators to
solve this kinda problems.

nice

-
Nicolás Miyasato (miya)
http://myPythonNotes.wordpress.com
http://nmiyasato.blogspot.com
 

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,770
Messages
2,569,586
Members
45,096
Latest member
ThurmanCre

Latest Threads

Top