Take the 2-minute tour ×

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)]
                                print 'Valid: ', moduleconfigcopy, elm
                                elm = elm[1:]
                                print 'Invalid: ', moduleconfigcopy, elm
        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...

share|improve this question
Quick tip: Use except to catch specific exceptions (such as NameError, or TypeError. Try to minimize the amount of code in your try block to just the code where you expect an error. And only catch errors that you can handle. It seems like your try..except block is doing none of these things. Doing things like this will help with the "I lost track of what's going on" part of your code. –  Joel Cornett Jul 21 '12 at 7:06
I'm just using the try...except blocks as makeshift debugging-points until I can figure the code out and move on to specifics....point noted, though –  Aaron Tp Jul 21 '12 at 7:08
The naked except is not going to help you debug, as it gives you less, not more information. Instead of IndexError (and the specific line of the error) for example, now you just get an ambiguous "Invalid!!" printed to stdout. –  Joel Cornett Jul 21 '12 at 7:09
Anyway, what's the contents of moduleconfigcopy? –  Joel Cornett Jul 21 '12 at 7:11
It (moduleconfigcopy) would be taking in arguments supplied to the class, it is a copy of a tuple of tuples: ((0, 1), (0, 0, 1), (0, 1)), essentially. I just copied the argument so I could manipulate it.. –  Aaron Tp Jul 21 '12 at 7:12

1 Answer 1

up vote 0 down vote accepted

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.
                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:


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.

share|improve this answer
Thanks, I'll plug it in and see if it works, I guess? As for the dictionary's odd setup, the editor I was using (Pydev for Eclipse) decided that that particular setup would raise an error of sorts for whatever reason, though I'd used the exact same setup.... –  Aaron Tp Jul 21 '12 at 7:47
@AaronSnipsTp: I only mean this to be a starting point. I doubt that the above code will "work" as is. It's meant to be much easier to debug than the code you posted. I'd have to know how this code is interacting with the rest of your class in order to write something that "works". –  Joel Cornett Jul 21 '12 at 7:51
Noted, but it does look....a lot cleaner now, so that's something to work with, thanks! –  Aaron Tp Jul 21 '12 at 7:54
it actually looks like logical code now, but since the "item" being iterated in the for... loop is an integer, it doesn't seem to work, and I really can't think of any way around calling individual items in the tuple to finish this code off...how can I get around the "TypeError: 'int' object is not iterable"? –  Aaron Tp Jul 21 '12 at 8:07
Which one is an int, element_tuple or item? item is not being iterated over in the code, so it must be element_tuple. That means that at least one of the elements in tuple_of_tuples is not a tuple. –  Joel Cornett Jul 21 '12 at 8:16

Your Answer


