Skip to content

Instantly share code, notes, and snippets.

@amckinley
Created March 12, 2015 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amckinley/a70303ff90e09d0a8f72 to your computer and use it in GitHub Desktop.
Save amckinley/a70303ff90e09d0a8f72 to your computer and use it in GitHub Desktop.
def params_to_dict(param_str):
splits = param_str.split(" ")
data = {}
prev_key = None
prev_val = None
for s in splits:
if "=" in s:
key, val = s.split("=")
if prev_key:
data[prev_key] = prev_val
prev_val = val
prev_key = key
else:
new_val = prev_val + " " + s
prev_val = new_val
data[prev_key] = prev_val
# and now some sanity checking, because this is so stupid
expected_params = param_str.count("=")
expected_len = len(param_str) - expected_params
actual_len = 0
for k, v in data.items():
actual_len += len(k), len(v)
if expected_params != len(data.items()):
raise Exception("fucked param count. expected {}, got {}".format(expected_params, len(data.items())))
if expected_len != actual_len:
raise Exception("fucked param len. expected {}, got {}".format(expected_len, actual_len))
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment