[Maya-Python] Parse function arguments, a.k.a. reverse (*args **kwargs)

200 views
Skip to first unread message

Marcus Ottosson

unread,
Dec 3, 2017, 1:09:32 PM12/3/17
to python_in...@googlegroups.com

Hi all,

I’m putting together a minor DSL whereby it’d be preferable to pass args after kwargs, like so.

my_function(name="Marcus", True)

This is normally a syntax error in Python..

SyntaxError: non-keyword arg after keyword arg

But I’m not looking to actually run this, but rather get a dictionary out of it. Like an “argument signature definition” if you will.

def my_function(*args, **kwargs):
  kwargs["children"] = args
  return kwargs

Cosmetically, it’d looks something like this.

Rig(
  Chain(name="Arm"),
  Chain(name="Leg"),

  name="MyCharacter",
  symmetry=True,
)

But would make somewhat more sense to look at like this.

Rig(
  name="MyCharacter",
  symmetry=True,

  Chain(name="Arm"),
  Chain(name="Leg"),
)

I’ve tried passing it through ast, in the hopes that it’d look past this superficial syntax error, but alas it does not.

import ast
tree = ast.parse("""\
Rig(
  name="MyCharacter",
  symmetry=True,

  Chain(name="Arm"),
  Chain(name="Leg"),
)
""")
ast.dump(tree)
# SyntaxError: non-keyword arg after keyword arg

What I’d rather not do is get into manipulating text “by hand”, such writing it as the above, and then moving any line that looks like a keyword argument to the end of what looks like a closure; but if there’s nothing else that’s where I’m headed.

I’d also not like to assume the exact look of any line, such as asserting that arguments are on a single line, as I can’t assume what the value of an argument might be. It may be another object with arguments of its own.

Any ideas?

Joe Weidenbach

unread,
Dec 3, 2017, 4:13:25 PM12/3/17
to python_in...@googlegroups.com

Hmmm… Makes sense why you’d want this, but it appears that it’s not available in Python 2.7 :( Python 3 on the other hand…

https://stackoverflow.com/questions/33048359/in-python-can-you-pass-variadic-arguments-after-named-parameters

What you could do is something a little more explicit:

Rig(
    name='MyCharacter',
    symmetry=True
,
    chains=(
            Chain(name='Arm'),
            Chain(name='Leg'),
            ...
        )
)

This would, while adding a bit of cruft to the call, get around the limitation.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOD4B-Mu4D%2By97e7RgK8Zfiod3-ZqzTk5QXwnL2q4FHVpg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Joe Weidenbach

unread,
Dec 3, 2017, 4:14:03 PM12/3/17
to python_in...@googlegroups.com
Not sure why my Markdown Here's splitting up my source :/

Marcus Ottosson

unread,
Dec 3, 2017, 4:28:03 PM12/3/17
to python_in...@googlegroups.com

Thanks Joe, didn’t know this about Python 3, that’s really interesting!

What you could do is something a little more explicit:

You’re right, I did this at first, and it works, is readable and makes sense. It’s just a little too verbose, but probably more readable than the args, kwargs version I initially posted.

I’ll see if I can somehow backport whatever is going in Python 3, that could be a winner. Else I’ll probably follow your advice.


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAM33%3Da7jh0%2BPjA5-JcDMFWSB0U-BbZWfzP3WDapwuSw7kpYQ-w%40mail.gmail.com.

Justin Israel

unread,
Dec 3, 2017, 4:36:50 PM12/3/17
to python_in...@googlegroups.com
On Mon, Dec 4, 2017 at 10:27 AM Marcus Ottosson <konstr...@gmail.com> wrote:

Thanks Joe, didn’t know this about Python 3, that’s really interesting!

What you could do is something a little more explicit:

You’re right, I did this at first, and it works, is readable and makes sense. It’s just a little too verbose, but probably more readable than the args, kwargs version I initially posted.

I’ll see if I can somehow backport whatever is going in Python 3, that could be a winner. Else I’ll probably follow your advice.


Otherwise, if you didn't want to have to rely on Pythons grammar parsing, you could define your own DSL grammar in something like pyparsing? It's really just the choice between making something Python compatible and using its existing parsing support, or wanting your own non-compatible DSL.
 

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCzWfLC8z6jhUSXFq907ZwPQL2z%2B90zSbDvpBrApMM1Xw%40mail.gmail.com.

Marcus Ottosson

unread,
Dec 3, 2017, 4:42:10 PM12/3/17
to python_in...@googlegroups.com
I did look into pyparsing, and other similar ones (like lark) and it's a possibility. I haven't decided yet how far down this rabbit hole I'm willing or need to go, but if it holds up then yes that is probably the best option long-term.

The thing I like about this particular method is that a) I won't have to develop and maintain an entirely new language, b) it'll keep me from straying too far from what already works b) it'll be somewhat familiar to those that already know Python.


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3VZfb5t4bv62_dOdAf%3DnXpjqVH2ORC3B7W3kgA8op%2Bdg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages