From 3b4b3044c041025588201f194005aa273f2d2026 Mon Sep 17 00:00:00 2001 From: Sebastian Villarreal Date: Tue, 5 May 2015 19:23:38 -0500 Subject: [PATCH 1/5] vim and local egg-info added to gitignore --- .gitignore | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.gitignore b/.gitignore index a6234ef..a05cfec 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,11 @@ env/** texts/** .idea/** # }}} + +# Local development installs {{{ +*.egg-info/ +# }}} + +# Text editor cruft {{{ +*.swp +# }}} From 0154b25f6ef40f04d1d871b61cb2e37af606ccb7 Mon Sep 17 00:00:00 2001 From: Sebastian Villarreal Date: Thu, 7 May 2015 20:11:53 -0500 Subject: [PATCH 2/5] Added a decorator to avoid boilerplate --- .gitignore | 2 +- blockspring/__init__.py | 13 ++++++++++ blockspring/example.py | 25 +++++++++++++++++++ blockspring/test_decorators.py | 45 ++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 blockspring/example.py create mode 100644 blockspring/test_decorators.py diff --git a/.gitignore b/.gitignore index a05cfec..c94b19d 100644 --- a/.gitignore +++ b/.gitignore @@ -63,5 +63,5 @@ texts/** # }}} # Text editor cruft {{{ -*.swp +*.sw[po] # }}} diff --git a/blockspring/__init__.py b/blockspring/__init__.py index 301960c..8a1b9e3 100644 --- a/blockspring/__init__.py +++ b/blockspring/__init__.py @@ -6,6 +6,8 @@ import base64 import re import tempfile +import inspect +import functools try: from urlparse import urlparse @@ -206,6 +208,17 @@ def processArgs(request): response = Response() block(request, response) +def decorate(func): + @functools.wraps(func) + def block(request, response): + arguments = inspect.getargspec(func).args + parameters = tuple(request.params[arg] for arg in arguments) + output = func(*parameters) + response.addOutput('output', output) + response.end + + return block + class Request: def __init__(self): self.params = {} diff --git a/blockspring/example.py b/blockspring/example.py new file mode 100644 index 0000000..27bb010 --- /dev/null +++ b/blockspring/example.py @@ -0,0 +1,25 @@ +import blockspring + +@blockspring.decorate +def fibonacci(x): + """This will be the test function for our decorator""" + print "doing something"+x + x = int(x) + if x < 0: + raise ValueError('Must be greater than 0') + elif x == 0: + return 1 + elif x == 1: + return 1 + else: + return x*x + + +def blibonacci(request, response): + x = request.params['x'] + output = fibonacci(x) + response.addOutput('output', output) + response.end() + + +blockspring.define(fibonacci) diff --git a/blockspring/test_decorators.py b/blockspring/test_decorators.py new file mode 100644 index 0000000..06f4961 --- /dev/null +++ b/blockspring/test_decorators.py @@ -0,0 +1,45 @@ +import pytest +import blockspring + +# ***** Fixtures ****** # + +def decorator_fixture(a,b=0): + """This will be the test function for our decorator""" + return a+b + +def block_fixture(request, response): + mySum = request.params["num1"] + request.params["num2"] + response.addOutput('sum', mySum) + response.end() + +@pytest.fixture +def request(): + request = blockspring.Request() + return request + +@pytest.fixture +def response(): + response = blockspring.Response() + return response + +@pytest.fixture +def wrapped(): + wrapped = blockspring.decorate(decorator_fixture) + return wrapped + +########## Tests ######### + +def test_decorator_respects_name(wrapped): + assert wrapped.__name__ == 'decorator_fixture' + +def test_creates_blockspring_function(wrapped,request, response): + assert response.result['_blockspring_spec']==True + + +def test_adds_output(wrapped, request, response): + + request.addHeaders({'b':1, 'c':3}) #<--this doesn't work + test = wrapped(request, response) + assert response.response['output']==4 + pass + From 8eb7343827bf16d3bffe55e9009cd40d471422df Mon Sep 17 00:00:00 2001 From: Sebastian Villarreal Date: Thu, 7 May 2015 20:33:07 -0500 Subject: [PATCH 3/5] removed example.py This file shouldn't have been here. --- blockspring/example.py | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/blockspring/example.py b/blockspring/example.py index 27bb010..8b13789 100644 --- a/blockspring/example.py +++ b/blockspring/example.py @@ -1,25 +1 @@ -import blockspring -@blockspring.decorate -def fibonacci(x): - """This will be the test function for our decorator""" - print "doing something"+x - x = int(x) - if x < 0: - raise ValueError('Must be greater than 0') - elif x == 0: - return 1 - elif x == 1: - return 1 - else: - return x*x - - -def blibonacci(request, response): - x = request.params['x'] - output = fibonacci(x) - response.addOutput('output', output) - response.end() - - -blockspring.define(fibonacci) From c34d19fc81efd12622cf24f7543a7182543e2897 Mon Sep 17 00:00:00 2001 From: Sebastian Villarreal Date: Fri, 8 May 2015 16:53:27 -0500 Subject: [PATCH 4/5] passing all specs --- blockspring/__init__.py | 2 +- blockspring/example.py | 25 ----------------------- blockspring/test_decorators.py | 37 +++++++++++++++++++++++++--------- 3 files changed, 28 insertions(+), 36 deletions(-) delete mode 100644 blockspring/example.py diff --git a/blockspring/__init__.py b/blockspring/__init__.py index 8a1b9e3..1e215bd 100644 --- a/blockspring/__init__.py +++ b/blockspring/__init__.py @@ -215,7 +215,7 @@ def block(request, response): parameters = tuple(request.params[arg] for arg in arguments) output = func(*parameters) response.addOutput('output', output) - response.end + response.end() return block diff --git a/blockspring/example.py b/blockspring/example.py deleted file mode 100644 index 27bb010..0000000 --- a/blockspring/example.py +++ /dev/null @@ -1,25 +0,0 @@ -import blockspring - -@blockspring.decorate -def fibonacci(x): - """This will be the test function for our decorator""" - print "doing something"+x - x = int(x) - if x < 0: - raise ValueError('Must be greater than 0') - elif x == 0: - return 1 - elif x == 1: - return 1 - else: - return x*x - - -def blibonacci(request, response): - x = request.params['x'] - output = fibonacci(x) - response.addOutput('output', output) - response.end() - - -blockspring.define(fibonacci) diff --git a/blockspring/test_decorators.py b/blockspring/test_decorators.py index 06f4961..3d50465 100644 --- a/blockspring/test_decorators.py +++ b/blockspring/test_decorators.py @@ -7,14 +7,18 @@ def decorator_fixture(a,b=0): """This will be the test function for our decorator""" return a+b -def block_fixture(request, response): - mySum = request.params["num1"] + request.params["num2"] - response.addOutput('sum', mySum) - response.end() +def dict_outputs(a, b): + """returns a dict""" + output = {} + return {'a_squared':a*a, 'b_squared':b*b} + +def list_outputs(a, b): + """returns a list""" + return [a*a, b*b] @pytest.fixture def request(): - request = blockspring.Request() + request = blockspring.parse({'a':1, 'b':3}) return request @pytest.fixture @@ -27,6 +31,16 @@ def wrapped(): wrapped = blockspring.decorate(decorator_fixture) return wrapped +@pytest.fixture +def dict_output_wrapped(): + wrapped = blockspring.decorate(dict_outputs) + return wrapped + +@pytest.fixture +def list_output_wrapped(): + wrapped = blockspring.decorate(list_outputs) + return wrapped + ########## Tests ######### def test_decorator_respects_name(wrapped): @@ -35,11 +49,14 @@ def test_decorator_respects_name(wrapped): def test_creates_blockspring_function(wrapped,request, response): assert response.result['_blockspring_spec']==True - def test_adds_output(wrapped, request, response): - - request.addHeaders({'b':1, 'c':3}) #<--this doesn't work test = wrapped(request, response) - assert response.response['output']==4 - pass + assert response.result['output']==4 + +def test_wraps_functions_that_returns_dicts(dict_output_wrapped, request, response): + test = dict_output_wrapped(request, response) + assert response.result['output']['b_squared']==9 +def test_wraps_functions_that_return_lists(list_output_wrapped, request, response): + test = list_output_wrapped(request, response) + assert response.result['output'][1]==9 From cd247ee57a4e9f2d9a37c0f36cc863f399827718 Mon Sep 17 00:00:00 2001 From: Sebastian Villarreal Date: Fri, 8 May 2015 17:04:33 -0500 Subject: [PATCH 5/5] Annotations to decorator --- blockspring/__init__.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/blockspring/__init__.py b/blockspring/__init__.py index 1e215bd..424f87f 100644 --- a/blockspring/__init__.py +++ b/blockspring/__init__.py @@ -209,11 +209,15 @@ def processArgs(request): block(request, response) def decorate(func): + """ Decorate any function into one readily parsed by blockspring. + The arguments to the function will be looked for by name in request.params + TODO: respect default values in wrapped function. + """ @functools.wraps(func) def block(request, response): - arguments = inspect.getargspec(func).args - parameters = tuple(request.params[arg] for arg in arguments) - output = func(*parameters) + arguments = inspect.getargspec(func).args #get a list of arguments accepted by wrapped + parameters = tuple(request.params[arg] for arg in arguments) + output = func(*parameters) #unpack them and call them into func response.addOutput('output', output) response.end()