On Mon, Dec 4, 2017 at 10:27 AM Marcus Ottosson <[email protected]> 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. > > > On 3 December 2017 at 21:13, Joe Weidenbach <[email protected]> wrote: > >> Not sure why my Markdown Here's splitting up my source :/ >> >> On Mon, Dec 4, 2017 at 10:13 AM Joe Weidenbach <[email protected]> wrote: >> >>> 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. >>> >>> >>> On Mon, Dec 4, 2017 at 7:09 AM Marcus Ottosson <[email protected]> >>> wrote: >>> >>>> 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? >>>> >>>> >>>> -- >>>> 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 [email protected]. >>>> To view this discussion on the web visit >>>> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOD4B-Mu4D%2By97e7RgK8Zfiod3-ZqzTk5QXwnL2q4FHVpg%40mail.gmail.com >>>> <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOD4B-Mu4D%2By97e7RgK8Zfiod3-ZqzTk5QXwnL2q4FHVpg%40mail.gmail.com?utm_medium=email&utm_source=footer> >>>> . >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> -- >> 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 [email protected]. >> 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 >> <https://groups.google.com/d/msgid/python_inside_maya/CAM33%3Da7jh0%2BPjA5-JcDMFWSB0U-BbZWfzP3WDapwuSw7kpYQ-w%40mail.gmail.com?utm_medium=email&utm_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCzWfLC8z6jhUSXFq907ZwPQL2z%2B90zSbDvpBrApMM1Xw%40mail.gmail.com > <https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCzWfLC8z6jhUSXFq907ZwPQL2z%2B90zSbDvpBrApMM1Xw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3VZfb5t4bv62_dOdAf%3DnXpjqVH2ORC3B7W3kgA8op%2Bdg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
