cjdrake / ipython-magic

IPython Magic Functions
BSD 2-Clause "Simplified" License
16 stars 7 forks source link

'Dot' object has no attribute 'encode' #4

Open haf opened 8 years ago

haf commented 8 years ago

I'm trying to make one of your extensions work (completely new to IPython, first notebook, first code I'm writing in python)... It's going well, trying to plot strings, like one would expect, but I'm not sure how to make objects plottable... I'm getting this error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-b880cdad6015> in <module>()
----> 1 get_ipython().magic('dotobj fig6')

/Users/h/.pyenv/versions/3.5.0/lib/python3.5/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2334         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2335         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2336         return self.run_line_magic(magic_name, magic_arg_s)
   2337 
   2338     #-------------------------------------------------------------------------

/Users/h/.pyenv/versions/3.5.0/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2255                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2256             with self.builtin_trap:
-> 2257                 result = fn(*args,**kwargs)
   2258             return result
   2259 

/Users/h/.ipython/extensions/gvmagic.py in dotobj(self, line)

/Users/h/.pyenv/versions/3.5.0/lib/python3.5/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/Users/h/.ipython/extensions/gvmagic.py in dotobj(self, line)
     52     @line_magic
     53     def dotobj(self, line):
---> 54         self._from_obj(line, 'dot')
     55 
     56     @line_magic

/Users/h/.ipython/extensions/gvmagic.py in _from_obj(self, line, layout_engine)
    162             error("expected to_dot method to be callable w/o args")
    163         else:
--> 164             data = run_graphviz(s, layout_engine)
    165             if data:
    166                 display_svg(data, raw=True)

/Users/h/.ipython/extensions/gvmagic.py in run_graphviz(s, layout_engine)
     29 
     30     dot = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
---> 31     stdoutdata, stderrdata = dot.communicate(s.encode('utf-8'))
     32     status = dot.wait()
     33     if status == 0:

AttributeError: 'Dot' object has no attribute 'encode'

For code on the instance fig6, like:

    def to_dot(self, Args=set()):
        if type(Args) is not set:
            Args = set(Args)

        try:
            graph = pydot.Dot()
        except NameError:
            print("Not able to import pydot. This functionality will not work.")
            return

        for arg in self._Ar:
            graph.add_node(pydot.Node(str(arg), shape='circle'))

        for attack in self._df:
            graph.add_edge(pydot.Edge(str(attack.attacker), str(attack.attacked)))

        for arg in Args:
            graph.get_node(str(arg)).pop().set_shape('doublecircle')

        return graph

With requirements.txt:

-e git+https://github.com/nlhepler/pydot.git@adf18a858a63b321b7e4ffd964a24d73add1bf4f#egg=pydot-master
pyparsing==2.0.6

Since I'm so new at this, I don't really know how to continue...?

PS, also getting. Is this normal?

/Users/h/.pyenv/versions/3.5.0/lib/python3.5/site-packages/IPython/core/magics/extension.py:47: UserWarning: %install_ext` is deprecated, please distribute your extension(s)as a python packages.
  "as a python packages.", UserWarning)
cjdrake commented 8 years ago

The warning doesn't surprise me. IPython has been changing, but I haven't stayed current with its preferred interface.

As for the error about "encode", It looks like you might be using the wrong type. The %dotobj decorator expects to take as an argument an object that implements a to_dot() method which returns a str. The return string should just be the dot graph description. Both Python 2.7 and 3.X strings implement an encode method.