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
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) |
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
# 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({ |
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
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' | |
} |
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
# 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))) |
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
# 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))) |
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
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): |
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 math | |
class Ramen: | |
# class variable | |
size_radius = { | |
'small': 2, | |
'medium': 3, | |
'large': 4 | |
} |
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
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. |
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
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 |
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 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. |
OlderNewer