Skip to content

Instantly share code, notes, and snippets.

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"
# 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)
# 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)
PizzaMaker().create_pizza(
id=10,
created_at=<any date I choose, ie 1250-12-25>,
created_by=<any_user_I_choose>
)
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"]
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):
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
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):
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):
@inesp
inesp / vimrc
Created October 17, 2019 20:06
" :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
"""""""""""""""""""""""""""""""""""""