Skip to content

Instantly share code, notes, and snippets.

@adamv
Created August 16, 2010 15:57
Show Gist options
  • Save adamv/527177 to your computer and use it in GitHub Desktop.
Save adamv/527177 to your computer and use it in GitHub Desktop.
Convert named params to dict
#!/usr/bin/env python
# a=2, b=3, c='acd'
# { 'a':2, 'b': 3, 'c': 'acd' }
import sys, os
selection = os.environ['TM_SELECTED_TEXT']
try:
values = [x.strip() for x in selection.split(",")]
pairs = [x.split('=', 2) for x in values]
dict_pairs = ["'%s': %s" % (x[0], x[1]) for x in pairs]
output = '{' + ', '.join(dict_pairs) + '}'
sys.stdout.write(output)
except:
sys.stdout.write(selection)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment