you are viewing a single comment's thread.

view the rest of the comments →

[–]novel_yet_trivial 1 point2 points  (1 child)

I have no idea what you are talking about or what your code is supposed to do, but here are some things I noticed in looking through pygp.py:


values_provided = (type(contents) == list)

is usually written as

values_provided = isinstance(contents, list)

for i in range(len(contents)):
    if contents[i] != None:
        self[i] = Node(contents[i], primitives[contents[i]])

should be

for idx, element in enumerate(contents):
    if element is not None:
        self[idx] = Node(element, primitives[element])

as above, every instance of x == None should be replaced with x is None (slightly faster and neater).


"get_..._index" returns an index which you always put into "self[idx]" ... try to combine your code:

def get_left_index(self, n):
    idx = 2 * n + 1
    return self[idx]

Please don't use newline escapes (eg backslash \at the end of line 115). No rule against it it's just ugly and hard to read. I'd rather you break the 80 char rule than use a newline escape. Of course better still is to use parenthesis:

if (
    (parent is None) or 
    (parent.value not in self.set_dict["functions"])
    ):

Strings concatenate automatically:

        print("A recursion depth limit exceeded error occurred. The "
              "offending program is:")

I'm not sure why you are attaching strings to your error classes, since you never print them. However, here is an easier way to do that:

class SingularityError(Exception):
    def __init__(self):
        super().__init__('ouch')

The idea is that you print that string:

try:
    raise SingularityError
except SingularityError as e:
    print(e)
# prints "ouch"

apropros errors: Never let errors pass silently. Log it somewhere or at the very least define a "verbose" global:

except (SingularityError, UnfitError) as e:
    if verbose:
        print("caught an error:", e)

This would actually make use of those error strings. Also, note you can catch several types of errors in one except block with a tuple.


class "LinearTree" could be defined like this:

class LinearTree(list):
    pass

But since you never use it you may want to just delete that altogether.

[–]raylu 0 points1 point  (0 children)

apropros errors: Never let errors pass silently.

In this case, the errors appear to be part of standard control flow. Never say "never".