Skip to content

Instantly share code, notes, and snippets.

# method for making tea
def make_tea(tea_name, cup_size, customer_name):
water = get_hot_water()
tea_leaf = get_tea(tea_name)
tea = add_ingridients(water, tea_leaf)
cup = get_cup(cup_size)
order = pour_tea(cup, tea)
price = get_price(tea_name, cup_size)
return JsonResponse({
@dichharai
dichharai / tea_api_test.py
Last active April 21, 2019 22:26
django tidbits
form_data = {'username': 'Ann',
'city': 'San Francisco',
# this key is optional
'tea_lover': 'false'
}
# sending the form_data through Django's test client
c = Client()
response = c.post('/link/to/endpoint', form_data)
class ChocolateFactory:
list_of_nuts = ['macadamia', 'hazelnut', 'walnut', 'pistachio', 'peanut']
list_of_choco_with_nuts = ['assorted chocolate', 'mouna loa', 'ferrero rocher', 'ritter sport']
list_of_choco_ingrie = {
'IS_NUTTY': 'is_nutty',
'IRON': 'iron',
'CACAO': 'cacao'
}
# continuation of settattr-eg.py example
# output: reese's contains nuts? : False
print('reese\'s contains nuts? : ' + str(getattr(reese, reese.list_of_choco_ingrie['IS_NUTTY'], False)))
# output: godvia's contains nuts? : True
print('godvia\'s contains nuts? : ' + str(getattr(godiva, godiva.list_of_choco_ingrie['IS_NUTTY'], False)))
import math
class Ramen:
# class variable
size_radius = {
'small': 2,
'medium': 3,
'large': 4
}
# continuation of settattr-eg.py example
# output: reese's contains nuts? : False
print('reese\'s contains nuts? : ' + str(getattr(reese, reese.list_of_choco_ingrie['IS_NUTTY'], False)))
# output: godvia contains nuts? : True
print('godvia contains nuts? : ' + str(getattr(godiva, godiva.list_of_choco_ingrie['IS_NUTTY'], False)))
class Ramen:
# class attribute
ingredients = ['nori', 'seasoned pork', 'boiled eggs', 'kamaboko', 'scallions', 'menma']
# constructor
def __init__(self, ramen_type):
self.ramen_type = ramen_type
# string representation of Ramen object
def __repr__(self):
a = 5
b = 3
print(f'Value of a={a} and value of b={b}. Their sum is {a+b}. Their product is {a*b}.')
# output: Value of a=5 and value of b=3. Their sum is 8. Their product is 15.
# multiline f-strings
print(f'Value of a={a} and value of b={b}.'\
f'Their sum is {a+b}).'\
f'Their product is {a*b}.')
# output: Value of a=5 and value of b=3. Their sum is 8. Their product is 15.
class PlantKingdom:
def __init__(self, scientific_name=None, common_name=None):
self.kingdom = 'Plantae'
self.scientific_name = scientific_name
self.common_name = common_name
class AquaticPlant(PlantKingdom):
def __init__(self, scientific_name, common_name):
if (not scientific_name.isalpha() or not common_name.isalpha()):
return
from django.db import models
class Pet(models.Model):
name = models.CharField(max_length=30)
owner = models.CharField(max_length=30)
vaccinated = models.NullBooleanField()
# The use of
# vaccinated = models.BooleanField(null=True)
# is anti-pattern/counterproductive.