Matching Binary operators in Tuples to Dictionary Items

我的未来我决定 提交于 2019-12-13 02:47:16

问题


So, I'm working on a Pybrain-type project and I'm stuck on part of it. So far the program takes in a tuple and assigns a variable to it using 'one of them fancy vars()['string'] statements. Specifically, it takes in a tuple of numbers and assigns it to a 'layerx' value, where x is the number of the layer (in order, layer 1, 2, 3, etc), such that the numbers are the dimensions of that layer.

The part of the program I desperately and humbly come to you for help in is what should be the next step in the program; it takes in a tuple of tuples (the number of tuples must = the number of layers), and the tuples contain 1/0's.

It is supposed to determine what type of Pybrain Layer to use in what layer, and then plugs in that layer's dimension value and, essentially, creates that layer-variable. I've...played with it for a while, and I've gotten a really...twisted...confusing block of code.

Please pardon the convoluted variable names, I thought I was being smart by making them somewhat specific:

    moduleconbuff = 0
    modulebuffer = 'module'
    correspondinglayerbuff = 0
    moduleconfigcopy = tuple(moduleconfig)

    try:  #Always triggers except, but it's pretty screwed up
                while correspondinglayerbuff <= len(self.layers):     #keeps track of how many layer/module pairs have been assigned
                    for elm in moduleconfigcopy:
                        for x in elm:
                            if x == 1:
                                moduledimmension = [layerbuff+'%s'%(correspondinglayerbuff)]
                                modulesdict = {1: pybrain.GaussianLayer(moduledimmension), 2: pybrain.LinearLayer(moduledimmension),\
                                3: pybrain.LSTMLayer(moduledimmension),4: pybrain.SigmoidLayer(moduledimmension),5: pybrain.TanhLayer(moduledimmension)}   #this dict pairs integers with pybrain modules
                                vars()[modulebuffer +'%s'%(correspondinglayerbuff)]=modulesdict(moduleconbuff)  #should return something like 'Module1 = pybrain.GaussianLayer(5) when complete
                                print vars()[modulebuffer+'%s'%(correspondinglayerbuff)]
                                moduleconbuff=0
                                correspondinglayerbuff+=1
                                print 'Valid: ', moduleconfigcopy, elm
                                continue
                            else:
                                elm = elm[1:]
                                print 'Invalid: ', moduleconfigcopy, elm
                                moduleconbuff+=1
    except:  
        print 'Invalid!!!'

I honestly lost track of what was going on in it. The tuple "moduleconfig" in the beginning was supposed to be a tuple of tuples (nested tuples) with binary operators, it was supposed to stop when one of the tuples has a 1, match that operator with the right module in Pybrain, and then plug this in so the corresponding layer = that module with the dimmensions already listed.

Obviously something went terribly wrong, and it's so fargone that my brain can't make any sense of it...it's lost all it's reason and every time I look at it I get scared...please help me or tell me I created an abomination or something, I guess...


回答1:


One huge hindrance that's affecting code readability for you is variable naming and style. I've tried to clean it up a little bit for you. It still might not work, but now it's a LOT easier to see what's going on. Please refer to PEP 8, the Python style guide

For starters, I renamed some variables, below. Note that in python, variables should be all lowercase, with separate words connected by an underscore. Constants should be ALL_UPPERCASE:

assigned_layers = correspondinglayerbuff = 0
tuple_of_tuples = moduleconfigcopy = ((0, 1), (0, 0, 1), (0, 1))
dimension = moduledimension
MOD_BUFFER = modulebuffer = 'buffer'
c_buff = moduleconbuff = 0

And here is the while loop (with variable names replaced, and properly indented, with the try... except block removed:

while assigned_layers <= len(self.layers):
    for element_tuple in tuple_of_tuples:
        for item in element_tuple:
            if item: # in python, 0 is treated as boolean False, 1 or any other value is treated as boolean True.
                dimension = [layerbuff + str(assigned_layers)] #what is layerbuff?
                modules_dict = {
                    1: pybrain.GaussianLayer(dimension),
                    2: pybrain.LinearLayer(dimension),
                    3: pybrain.LSTMLayer(dimension),
                    4: pybrain.SigmoidLayer(dimension),
                    5: pybrain.TanhLayer(dimension)
                    } # Notice how this dict is much easier to read.

                vars()[MOD_BUFFER + str(assigned_layers)] = modules_dict[c_buff]  #modules_dict is a dict and not a callable object
                c_buff = 0
                assigned_layers +=1
                #No need for continue here, since that's what the if...else does here.
            else:
                element_tuple = element_tuple[1:] #what is this for?
                print 'Invalid: ', tuple_of_tuples, element_tuple

I'm not sure exactly what you are trying to do in this line:

vars()[MOD_BUFFER + str(assigned_layers)] = modules_dict[c_buff]  #modules_dict is a dict and not a callable object

Also, you originally had modules_dict(moduleconbuff) which will raise a TypeError as a dict is not a callable object. I'm assuming you meant to retrieve a value by key.

As I said, I'm not quite sure what your trying to do here (probably because I haven't seen the rest of your code), but renaming your variables and using good style should go a long way towards you being able to debug your code. I will continue to edit if you answer my questions/comment.



来源:https://stackoverflow.com/questions/11590138/matching-binary-operators-in-tuples-to-dictionary-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!