Created
January 15, 2012 18:33
-
-
Save jvanasco/1616707 to your computer and use it in GitHub Desktop.
quick/dirty/incomplete implementation of php var_dump in python - this is to save a python data structure as a php variable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _php_var_dump(data,depth=0): | |
padding= " "*depth | |
keypadding= " "*(depth+1) | |
depth+= 1 | |
if type(data) == types.DictType : | |
items = [] | |
for i in data : | |
item = keypadding + "'%s' => %s" % (i,_php_var_dump(data[i],depth=depth)) | |
items.append(item) | |
items = ',\n'.join(items) | |
return '%sArray(\n%s\n%s)' % ( padding, items, padding) | |
elif type(data) == types.ListType : | |
items = [] | |
for i in data : | |
items.append(_php_var_dump(i,depth=depth)) | |
items = ',\n'.join(items) | |
return '%sArray(\n%s\n%s)' % ( padding, items, padding) | |
elif type(data) == types.StringType : | |
return padding + "'%s'" % data | |
elif type(data) == types.IntType : | |
return padding + "%s" % data | |
elif type(data) == types.NoneType : | |
return padding + "null" | |
else: | |
print type(data) | |
raise | |
def php_var_dump(name,data): | |
depth=0 | |
if type(data) == types.DictType: | |
depth= 1 | |
return "$%s = %s;" % ( name , _php_var_dump(data,depth=depth) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment