Maintaining python kwargs both short and long keywords (maya flag)

88 views
Skip to first unread message

David Lantos

unread,
Jan 7, 2015, 6:15:31 AM1/7/15
to python_in...@googlegroups.com
Hi guy!

I am just wondering have you guys a good solution/method to maintain python kwargs both short and long keywords with the same value.
(I have a couple of ideas just I am courius someone has better one.)

For example:

There is a method that eats one keyword at time (for example: "longFlag" or "shortFlag"). It can accept a short and long keyword as well and we want to provide a preset for this keyword if user do not use either of them.
Here is the frame:


def mayaCommandWrapper(**kwargs):
	"""
	:keyword longFlag(shortFlag): This is a keyword flag can accept any value.
	"""
	preset = {} # Some dictionary preset here
	
	# Some solution here to provide long and short flags as well.
	
	preset.update(kwargs) # we want to overwrite preset with value if given any kind of flag.
	mayaCommand(**preset) # we give the dict to the maya command as kwargs.

Jesse Kretschmer

unread,
Jan 7, 2015, 1:37:10 PM1/7/15
to python_in...@googlegroups.com
You could perhaps use tuples as the key for your 'preset' dictionary. This will not allow for a simple .update(), but it's not complicated.

Consider this:
def mayaCommandWrapper(**kwargs):
    preset = {
        ('selection', 'sl'): True,
        ('dagObjects', 'dag'): True,
    }

    for key, value in preset.items():
        long_flag, short_flag = key
        if long_flag not in kwargs and short_flag not in kwargs:
            kwargs[long_flag] = value

    maya.cmds.ls(**kwargs)

--
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/ea14b9fa-7e6b-4c8f-bd9d-8d396b7c5189%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

damon shelton

unread,
Jan 7, 2015, 1:41:17 PM1/7/15
to python_in...@googlegroups.com
longname= kwargs.get('longname', kwargs.get('ln'))
othervalue= kwargs.get('othervalue', kwargs.get('ov'))

is how I handle it

David Lantos

unread,
Jan 12, 2015, 3:45:11 AM1/12/15
to python_in...@googlegroups.com
Thank you guys!
Reply all
Reply to author
Forward
0 new messages