-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Block decorator #5
base: master
Are you sure you want to change the base?
Conversation
This file shouldn't have been here.
First: This is awesome:exclamation: Second: One bug I noticed is that Third: This works perfectly for a function that has one return value. I wonder how we could extend this for functions that have multiple outputs. Here is a basic example:
import blockspring
def mathstuff(request, response):
x = request.params['x']
y = request.params['y']
response.addOutput('sum', x + y)
response.addOutput('difference', x - y)
response.addOutput('product', x * y)
response.addOutput('quotient', x / y)
response.end()
blockspring.define(mathstuff)
$ echo '{ "x": 10, "y": 5 }' | python test-math.py | json_pp
{
"_blockspring_spec" : true,
"product" : 50,
"sum" : 15,
"difference" : 5,
"_errors" : [],
"quotient" : 2
} One idea is to allow the decorator to include name arguments where the names map to items in a returned list: import blockspring
@blockspring.decorate("sum", "difference", "product", "quotient")
def mathstuff(x, y):
return [x + y, x - y, x * y, x / y]
blockspring.define(mathstuff) Another option might be detecting dict returns and adding the corresponding outputs: import blockspring
@blockspring.decorate
def mathstuff(x, y):
return { "sum": x + y, "difference": x - y, "product": x * y, "quotient": x / y }
blockspring.define(mathstuff) |
I think multiple returns is a lesser concern, since they aren't allowed in a typical python function anyway. This would work because We're checking for json.loads when we receive arguments anyway. |
…ing.py into block-decorator Conflicts: blockspring/example.py
I added tests to test against multiple outputs--it worked without having to make a single change, because you're serializing output in Response.end(). Try it! |
This is a great idea. Removing this barrier to creating a blockspring function is amazing. Ok let me list out all the things the library does now, so we make sure we know what we still enable here / are losing:
... i think that's it! |
Ok, that sounds like the beginnings of a (very necessary) test suite. I'll start that suite and pull-request it in a separate request. That way we can all rebase our pull requests and make sure tests pass. |
I added a decorator function to the module. This function should help avoid boilerplate code.
Imagine that a user has a function:
They can now copy that directly into blockspring by simply using the decorator:
No need to change the function name, arguments or add blockspring boilerplate code!
The decorator will find the names of the arguments and transform
foo
into: