Question

I have a code which creates a graph, with nodes, and which keeps track of edges. This code itself appears to be working fine, but I cannot get the repr override to work how I would expect it to, after reading a few of the str vs repo and repr() returned non-string threads, I can't seem to isolate the cause of my troubles.

!/usr/bin/python
class Edge:
    def __init__(self, here, there, txt):
        self.description = txt     # why am i making this jump?
        self.here = here    # where do i start
        self.there = there   # where to i end
        self.here.out += [self]  # btw, tell here that they can go there

    def __repr__(self):
        return "E(" + self.here.name + " > " + self.there.name + ")"


class Node:
    end = "."
    start = "!"

    def __init__(self, g, nid, name, stop=False, start=False):
        self.nid = nid
        self.graph = g  # where do i live?
        self.name = name  # what is my name?
        self.description = ""  # tell me about myself
        self.stop = stop  # am i a stop node?
        self.start = start  # am i a start node?
        self.out = []  # where do i connect to

    def also(self, txt):
        """adds text to description"""
        sep = "\n" if self.description else ""
        self.description += sep + txt

    def __repr__(self):
        y = "N( :id " + self.nid + \
            "\n   :name " + self.name + \
            "\n   :about '" + self.description + "'" + \
            "\n   :out   " + ",".join(str(n) for n in self.out) + " )"


class Graph:
    def __init__(self):
        self.nodes = []  # nodes, stored in creation order
        self.keys = {}  # nodes indexed by name
        self.m = None  # adjacency matrix
        self.mPrime = None  # transitive closure matrix

    def __repr__(self):
        return "\n".join(str(u) for u in self.nodes)

    def node(self, name):
        """returns a old node from cache or a new node"""
        if not name in self.keys:
            self.keys[name] = self.newNode(name)
        return self.keys[name]

    def newNode(self, name):
        """ create a new node"""
        nid = len(self.nodes)
        tmp = Node(self, nid, name)
        tmp.start = Node.start in name
        tmp.end = Node.end in name
        self.nodes += [tmp]
        return tmp

It is my understanding that repr returns a string whenever its function is called by a string handler. More specifically, it references and instance as a string, looks for str, str points to repr and that gets returned as a string to its calling method. So for my program I have a graph, which is created just fine, but when I print it, it gets stuck concatenating the list it joins, returning it as an int instead of a string?

I have tried casting different parts as a string thinking maybe repr wasn't being called because it had to be explicitly cast a string to work properly and a few other things. The short of my story is my repr is not being handled by the "\n".join() properly and somehow getting an int value from ",".join(str(n) for n in self.out), telling me it cannot concatenate a str and an int.

I'm really at a loss, I've read through many google answers and texts on the use of repr, as well as many answers on here and haven't figure out what exactly I'm overlooking here.

Here is how the graph itself is built, it reads in stagetext, where the first line is the node name, the exits start with >, and everything in between is a description of the node's location.

The graph is what I'm supposed to edit, and it is supposed to simply print with print graph("mystageText.txt"), though I have seen most uses of repr with repr(graph("myStageText.txt"). I am thusly quite confused on the matter as a clear concise example of repr with lists of instances doesn't seem to be available.

Thanks in advance for the help, hopefully this is concise enough for StackOverflow. Sorry if I was a bit too wordy while I was trying to be thorough about my problem.

Was it helpful?

Solution

It looks like your problem is really coming from

        y = "N( :id " + self.nid + \
        #                   ^

self.nid is an int.

Another bug: Node.__repr__ doesn't return anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top