Skip to content

Instantly share code, notes, and snippets.

Created October 5, 2010 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/611646 to your computer and use it in GitHub Desktop.
Save anonymous/611646 to your computer and use it in GitHub Desktop.
from sigwhite import swlist, swdict, swtuple
@swlist()
def complexstruct():
yield 1
yield 2
@swlist()
def innerlist():
yield 5
yield 3
yield innerlist
@swdict()
def innerdict():
yield ("a", "b")
yield ("x", 20)
@swlist("lst")
def innerinnerlist(): # maybe do introspection to just take the function name? hmm.
yield 1
yield 2
yield 3
yield innerinnerlist
yield innerdict
@swtuple()
def innertuple():
yield 20
@swdict()
def innerinnerdict():
yield (1, 5)
yield (20, "your mom")
yield innerinnerdict
yield innertuple
print complexstruct
# normally you can't put multiline functions into these structures, only lambdas.
from sigwhite import swlist
@swlist()
def listOfFuncs():
def a(x):
x = x * 2
print x + 5
yield a
def b(x):
x = x * 3
print x + 4
yield b
def c(x):
x = x * 5
print x + 3
yield c
for f in listOfFuncs:
f(2)
# my own "None". I want something that people wouldn't realistically use as a dict key. None might be used
# if there's a less atrocious way to do this, let me know
class myNone:
pass
def swlist(key = myNone): # key is in case the data structure is a value within a dict
def swify(gen):
list_out = [x for x in gen()]
if key != myNone:
return (key, list_out)
else:
return list_out
return swify
def swdict(key = myNone): # key is in case the data structure is a value within a dict
def swify(gen):
dict_out = {}
for k,v in gen():
dict_out[k]=v
if key != myNone:
return (key, dict_out)
else:
return dict_out
return swify
def swtuple(key = myNone): # key is in case the data structure is a value within a dict
def swify(gen):
return tuple(swlist(key)(gen))
return swify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment