QuantStack / py2vega

BSD 3-Clause "New" or "Revised" License
9 stars 4 forks source link

Not possible to use a function created with partial or with a decorator as it raises indentation error #43

Closed gioxc88 closed 1 year ago

gioxc88 commented 2 years ago

a simple function like this would not work as it returns indentation error

def make_fn(fmt):
    def fn(cell):
        return cell.value if not isNaN(cell.value) else format(cell.value, fmt)
    return fn

Expr(make_fn('.2f'))
martinRenou commented 2 years ago

I think this is intended. A function definition is not considered a proper expression.

py2vega supports simple if conditions for convenience even though it's not a proper expression too.

gioxc88 commented 2 years ago

honestly without partial function this thing is very difficult to use. Like you can use it in very simple cases but not really useful. For example I can't possibly write a function for every format specifier and hadrcode the specifier in it. It's not practical. I don't think it would be difficult to remove extra indent and the beginning when parsing the function

martinRenou commented 2 years ago

py2vega turns Python expressions into vega expressions. A function definition is not an expression. Python also does not support doing this:

eval('''
def foo():
    pass 
''')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 2
    def foo():
    ^^^
SyntaxError: invalid syntax
martinRenou commented 2 years ago

As I said, py2vega supports if conditions for convenience, same for simple variable assignment. But this is already quite hacky to allow this. Allowing function definition will be really painful to implement and maintain, it's also not the purpose of this library.

martinRenou commented 1 year ago

Closing as wont fix