Structuring the program. Classes and Functions in Python

I am writing a program that uses genetic methods to develop equations. I want to be able to send the 'mainfunc' function to the Parallel Python submit function. The mainfunc function calls two or three methods defined in the Utility class. They instantiate other classes and invoke various methods. I think I want it all to be in one NAMESPACE. Therefore, I created several (perhaps everything should) classes inside the mainfunc function. I call the Utility method "generate ()". If we followed it with a chain of execution, it would include all classes and methods in the code.

Now the equations are saved in the tree. Each time a tree is generated, mutated, or intersected by bred, nodes must be provided with a new key so that they can be obtained from the attribute of the tree dictionary. KeySeq classes generate these keys.

In Parallel Python, I am going to send multiple instances of "mainfunc" to the "submit" PP function. Each of them should have access to "KeySeq". It would be nice if they all got access to the same KeySeq instance, so that none of the nodes on the returned trees had the same key, but I could get around this if necessary.

So: my question is to populate EVERYTHING in mainfunc. thanks (Edit) If I don’t include everything in mainfunc, I should try to tell PP about the dependent functions, etc., Passing different arguments in different places. I try to avoid this.

(late Edit), if ks.next () is called inside the generate () function, it returns the error 'NameError: global name' ks 'not defined'

class KeySeq:
    "Iterator to produce sequential \
    integers for keys in dict"
    def __init__(self, data = 0):
        self.data = data
    def __iter__(self):
        return self
    def next(self):
        self.data = self.data + 1
        return self.data
class One:
    'some code'
class Two:
    'some code'
class Three:
    'some code'
class Utilities:
    def generate(x):
        '___________'
    def obfiscate(y):
        '___________'
    def ruminate(z):
        '__________'


def mainfunc(z):
    ks = KeySeq()
    one = One()
    two = Two()
    three = Three()
    utilities = Utilities()
    list_of_interest = utilities.generate(5)
    return list_of_interest

result = mainfunc(params)
+3
source share
3 answers

If you want all instances to mainfuncuse the same object KeySeq, you can use the default parameter trick:

def mainfunc(ks=KeySeq()):
   key = ks.next()

ks, mainfunc KeySeq, .

, : - . . func_defaults; , , . , , func_defaults. , mainfunc ks, KeySeq() func_defaults. mainfunc KeySeq.

, " mainfunc submit PP". ? , , , .

( , ). , g :

>>> def f():
        def g(x=[]):
            return x
        return g
>>> g1 = f()
>>> g2 = f()
>>> g1().append('a')
>>> g2().append('b')
>>> g1()
['a']
>>> g2()
['b']

g() , ( ) func_defaults. g1 g2 g, x , .

, , :

def mainfunc():      hasattr (mainfunc, "ks" ):         setattr (mainfunc, "ks", KeySeq())     key = mainfunc.ks.next()

, , : , , , . callback.py Parallel Python , Sum .

+1

, . :

#imports, utilities, other functions

def main(arg):
    #...

if __name__ == '__main__':
    import sys
    main(sys.argv[1])

main , , .

+3

Your class concept in Python does not sound, I think. Perhaps it would be nice to review the basics. This link will help.

Python Basics - Classes

0
source

Source: https://habr.com/ru/post/1720002/


All Articles