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 PrimaryTitleFinder: | |
def get_title(object_): | |
return object_.name if isinstance(object_, Person) else object_.title | |
person = Person(name="Artemisia Lomi", ...) | |
TitleFinder.get_title(person) # => returns "Artemisia Lomi" | |
issue = Issue(title="Find famous painters", assignee=...) | |
TitleFinder.get_title(issue) # => returns "Find famous painters" |
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
# Here we define all possible parameter combinations. | |
# We also add a little bit of validation (which fields | |
# are required, what are the default values). | |
from marshmallow import Schema, fields | |
class PMargharitaSchema(Schema): | |
with_bazil = fields.Boolean(missing=True) | |
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
# We create dedicated pizza maker classes, each class | |
# creates just 1 type of pizza. | |
# We will also register the makers. | |
@register_pizza_maker | |
class _PizzaMargheritaMaker: | |
pizza_class_name = "PizzaMargherita" | |
def create(with_bazil=True): | |
pizza = PizzaMargherita(with_bazil=True) |
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
PizzaMaker().create_pizza( | |
id=10, | |
created_at=<any date I choose, ie 1250-12-25>, | |
created_by=<any_user_I_choose> | |
) |
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 PizzaMaker: | |
def create_pizza(self, **pizza_properties): | |
pizza_type = pizza_properties["ptype"] | |
pizza_class = get_pizza_class(pizza_type) | |
if pizza_class is None: | |
raise UnknownPizaType(pizza_type) | |
del pizza_properties["ptype"] |
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
def calculate_users_favorite_color(user, use_cache=True): | |
pass | |
def calculate_users_favorite_color(user, use_cache=False): | |
pass | |
def calculate_users_favorite_color(user, ignore_cache=True): | |
pass | |
def calculate_users_favorite_color(user, ignore_cache=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
from datetime import datetime | |
def calculate_users_favorite_color(user, ignore_cache=False): | |
now = datetime.now() | |
use_cache = not ignore_cache | |
color, valid_until = None, None | |
if use_cache: | |
color, valid_until = cache.get(f"user_color_{user.id}") | |
# handle the `valid_until` immediatelly |
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 datetime import datetime | |
def calculate_users_favorite_color(user, ignore_cache=False): | |
now = datetime.now() | |
color, valid_until = None, None | |
if not ignore_cache: | |
color, valid_until = cache.get(f"user_color_{user.id}") | |
if ignore_cache or (valid_until is not None and valid_until > now): |
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 datetime import datetime | |
def calculate_users_favorite_color(user, use_cache=True): | |
now = datetime.now() | |
color, valid_until = None, None | |
if use_cache: | |
color, valid_until = cache.get(f"user_color_{user.id}") | |
if not use_cache or (valid_until is not None and valid_until > now): |
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
" :W sudo saves the file when the file is open in readonly mode | |
command W w !sudo tee % > /dev/null | |
"""""""""""""""""""""""""""""""""""" | |
" Line | |
"""""""""""""""""""""""""""""""""""" | |
" show line numbers | |
set number | |
""""""""""""""""""""""""""""""""""""" |
NewerOlder