开发者

Most elegant way to configure a class in Python?

开发者 https://www.devze.com 2023-03-19 19:59 出处:网络
I\'m simulating a distributed system in which all nodes follow some protocol. This includes assessing some small variations in the protocol. A variation means alternative implementation of a single me

I'm simulating a distributed system in which all nodes follow some protocol. This includes assessing some small variations in the protocol. A variation means alternative implementation of a single method. All nodes always follow the same variation, which is determined by experiment configuration (only one configuration is active at any given time). What is the clearest way to do it, without sacrificing performance?

As an experiment can be quite extensive, I clearly don't want any conditionals. Before I've just used inheritance, like:

class Node(object):

    def dumb_method(self, argument):
        # ...    

    def slow_method(self, argument):
        # ...

    # A lot more methods

class SmarterNode(Node):

    def dumb_method(self, argument):
        # A somewhat smarter variant ...

class FasterNode(SmarterNode):

    def slow_method(self, argument):
        # A faster variant ...

But now I need to test all possible variants and don't want an exponential number of classes cluttering the source. I also want other people peeping at the code to understand it with minimal effort. What are your suggestions?

Edit: One thing I failed to emphasize enough: for all envisioned use cases, it seems that patching the class upon configuration is g开发者_运维技巧ood. I mean: it can be made to work by simple Node.dumb_method = smart_method. But somehow it didn't feel right. Would this kind of solution cause major headache to a random smart reader?


You can create new subtypes with the type function. You just have to give it the subclasses namespace as a dict.

# these are supposed to overwrite methods
def foo(self):
    return "foo"

def bar(self):
    return "bar"


def variants(base, methods):
    """
        given a base class and list of dicts like [{ foo = <function foo> }] 
         returns types T(base) where foo was overwritten
    """
    for d in methods:
        yield type('NodeVariant', (base,), d)


from itertools import combinations
def subdicts(**fulldict):
    """ returns all dicts that are subsets of `fulldict` """
    items = fulldict.items()
    for i in range(len(items)+1):
        for subset in combinations(items, i):
            yield dict(subset)

# a list of method variants
combos = subdicts(slow_method=foo, dumb_method=bar)

# base class
class Node(object):
        def dumb_method(self):
            return "dumb"
        def slow_method(self):
            return "slow"

# use the base and our variants to make a number of types
types = variants(Node, combos)

# instantiate each type and call boths methods on it for demonstration
print [(var.dumb_method(), var.slow_method()) for var
          in (cls() for cls in types)]

# [('dumb', 'slow'), ('dumb', 'foo'), ('bar', 'slow'), ('bar', 'foo')]


You could use the __slots__ mechanism and a factory class. You would need to instantiate a NodeFactory for each experiment, but it would handle creating Node instances for you from there on. Example:

class Node(object):
    __slots__ = ["slow","dumb"]

class NodeFactory(object):
     def __init__(self, slow_method, dumb_method):
         self.slow = slow_method
         self.dumb = dumb_method
     def makenode(self):
         n = Node()
         n.dumb = self.dumb
         n.slow = self.slow
         return n

an example run:

>>> def foo():
...     print "foo"
...
>>> def bar():
...     print "bar"
...
>>> nf = NodeFactory(foo, bar)
>>> n = nf.makenode()
>>> n.dumb()
bar
>>> n.slow()
foo


I'm not sure if you're trying to do something akin to this (allows swap-out runtime "inheritance"):

class Node(object):
    __methnames = ('method','method1')    

    def __init__(self, type):
        for i in self.__methnames:
            setattr(self, i, getattr(self, i+"_"+type))

    def dumb_method(self, argument):
        # ...    

    def slow_method(self, argument):
        # ...

n = Node('dumb')
n.method() # calls dumb_method

n = Node('slow')
n.method() # calls slow_method

Or if you're looking for something like this (allows running (and therefore testing) of all methods of the class):

class Node(object):
    #do something

class NodeTest(Node):

    def run_tests(self, ending = ''):
        for i in dir(self):
            if(i.endswith(ending)):
                meth = getattr(self, i)
                if(callable(meth)):
                    meth() #needs some default args.
                    # or yield meth if you can


You can use a metaclass for this.

If will let you create a class on the fly with method names according to every variations.


Should the method to be called be decided when the class is instantiated or after? Assuming it is when the class is instantiated, how about the following:

class Node():
    def Fast(self):
        print "Fast"

    def Slow(self):
        print "Slow"

class NodeFactory():
    def __init__(self, method):
        self.method = method

    def SetMethod(self, method):
        self.method = method

    def New(self):
        n = Node()
        n.Run = getattr(n, self.method)
        return n

nf = NodeFactory("Fast")

nf.New().Run()
# Prints "Fast"

nf.SetMethod("Slow")

nf.New().Run()
# Prints "Slow"
0

精彩评论

暂无评论...
验证码 换一张
取 消