cairomassimo / py2tex

Convert Python to LaTeX pseudocode (algorithmicx)
29 stars 14 forks source link

Attribute Error on for-in loop #1

Open emablekos opened 4 years ago

emablekos commented 4 years ago

Thanks for this code.

I get an attribute error when doing a for-in loop.

def foo():
    a = ["a", "b"]
    b = "c"
    for val in a:
        b = val

causes:

<_ast.For object at 0x1042ebb90> <class '_ast.For'> <_ast.Name object at 0x1042ebdd0> <class '_ast.Name'>
Traceback (most recent call last):
  File "py2tex.py", line 235, in <module>
    main()
  File "py2tex.py", line 229, in main
    py2tex.visit(ast.parse(source))
  File "py2tex.py", line 33, in visit
    result = super().visit(node)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py", line 271, in visit
    return visitor(node)
  File "py2tex.py", line 44, in visit_Module
    self.visit(stmt)
  File "py2tex.py", line 33, in visit
    result = super().visit(node)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py", line 271, in visit
    return visitor(node)
  File "py2tex.py", line 71, in visit_FunctionDef
    self.body(node.body)
  File "py2tex.py", line 48, in body
    self.visit_all(body)
  File "py2tex.py", line 40, in visit_all
    self.visit(node)
  File "py2tex.py", line 33, in visit
    result = super().visit(node)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ast.py", line 271, in visit
    return visitor(node)
  File "py2tex.py", line 145, in visit_For
    nargs = len(node.iter.args)
AttributeError: 'Name' object has no attribute 'args'

I am able to generate latex from other python code and render it to pdf.

On Python 3.7.7

wil93 commented 4 years ago

Does something like this produce the same error?

def foo():
    a = [1, 2, 3]
    for val in a:
        print(val)

If yes, I suspect that py2tex doesn't support anything that is not a simple "for in range(...)"