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 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 | |
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
%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
my_favorite_numbers = [] | |
for number in range(10): | |
if number == 7 or number == 42: | |
my_favorite_numbers.append(number) | |
my_favorite_numbers = [number for number in range(10) if number == 7 or number == 42] |
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
# Classes | |
class Person(object): | |
def contact(self): | |
self.email() | |
self.call() | |
def email(self): |
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
from peewee import TextField | |
class BetterUUID(TextField): | |
"""Stores a uuid only defining it if it doesn't already exist""" | |
def db_value(self, value): | |
if not value: | |
value = uuid.uuid4() | |
return value |
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
fn main() { | |
let x = the_world_is_not_safe(); | |
} | |
unsafe fn the_world_is_not_safe() -> String { | |
String::from("Hello, world!") | |
} | |
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 sys | |
with open(sys.argv[1]) as file: | |
print(file.read()) | |
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
@hug.cli(validate=contains_one_of('argument_one', 'argument_two')) # you can combine these to make more then one mutually exclusive group | |
def mutually_exclusive_group(argument_one: type, argument_two: type): | |
pass |