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
%cpaste | |
import time | |
class Test(object): | |
__slots__ = ('a', 'b', 'c') | |
def __init__(self): | |
self.a = 1 | |
self.b = 1 | |
self.c = 1 |
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
import hug | |
from project import exceptions | |
@hug.exception((exceptions.CustomExceptionTypeOne, exceptions.CustomExceptionTypeTwo)) | |
def handle_custom_exceptions(exception): | |
# handles the passed in custom exception, able to return a different response here: | |
return {'error': 'No error here!'} |
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
import hug | |
# As a pattern for reuse | |
@hug.object.urls('/endpoint', requires=()) | |
class MyClass(object): | |
@hug.object.get() | |
def my_method(self): | |
return 'hi there!' |
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
import hug | |
api = hug.get(on_invalid=hug.redirect.not_found) | |
@api.urls('/do-math', examples='number_1=1&number_2=2') | |
def math(number_1: hug.types.number, number_2: hug.types.number): | |
return number_1 + number_2 | |
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
import os | |
import hug | |
import my_internal_hug_microservice | |
if os.environ.get('API_INTERFACE', None) == 'HTTP':: | |
internal_hug_microservice = hug.use.HTTP('http://localhost:8000/') | |
else: | |
internal_hug_microservice = hug.use.Local(my_internal_hug_microservice) |
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
# Using uwsgi | |
uwsgi --http 0.0.0.0:8000 --wsgi-file first_step_3.py --callable __hug_wsgi__ | |
# Or, using gunicorn | |
gunicorn first_step_3:__hug_wsgi__ |
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
"""First hug API (local, command-line, and HTTP access)""" | |
import hug | |
@hug.cli() | |
@hug.get(examples='name=Timothy&age=26') | |
@hug.local() | |
def happy_birthday(name: hug.types.text, age: hug.types.number, hug_timer=3): | |
"""Says happy birthday to a user""" | |
return {'message': 'Happy {0} Birthday {1}!'.format(age, name), |
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
"""First hug API (local and HTTP access)""" | |
import hug | |
@hug.get(examples='name=Timothy&age=26') | |
@hug.local() | |
def happy_birthday(name: hug.types.text, age: hug.types.number, hug_timer=3): | |
"""Says happy birthday to a user""" | |
return {'message': 'Happy {0} Birthday {1}!'.format(age, name), | |
'took': float(hug_timer)} |
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
"""First API, local access only""" | |
import hug | |
@hug.local() | |
def happy_birthday(name: hug.types.text, age: hug.types.number, hug_timer=3): | |
"""Says happy birthday to a user""" | |
return {'message': 'Happy {0} Birthday {1}!'.format(age, name), | |
'took': float(hug_timer)} |
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
pip3 install hug -U | |
# Or if pip is set to Python3 pip | |
pip install hug -U |