Skip to content

Instantly share code, notes, and snippets.

@davidvhill
Last active September 6, 2017 06:55
Show Gist options
  • Save davidvhill/0335ba91da9629d5af29d4e0e0e2ee32 to your computer and use it in GitHub Desktop.
Save davidvhill/0335ba91da9629d5af29d4e0e0e2ee32 to your computer and use it in GitHub Desktop.
Some structure.
from datetime import datetime
import d2d.user
import d2d.user.search
import d2d.notifications.send
####################################################################
# Method 1, detach interface from implementation
# Indirectly reference dependencies from vals
####################################################################
def new_user(vals):
user_store = vals.get('d2d.user.store')
return user_store.save(first_name=vals.get('d2d.user.first.name'),
last_name=vals.get('d2d.user.last.name'),
created=datetime.now().isoformat())
def get_user(vals):
user_store = vals.get('d2d.user.store')
return user_store.get(first_name=vals.get('d2d.user.first.name'),
last_name=vals.get('d2d.user.last.name'))
def search_user(vals):
user_store = vals.get('d2d.user.store')
return user_store.search(terms=vals.get('d2d.user.search.terms'))
#####################################################################
# Method 2, apply functions together, restricting structure.
# This results in declarative software.
#####################################################################
def apply(func, vals):
return call(vals.get(func), (vals=vals))
def save_and_notify(vals):
return apply('d2d.notification.send',
apply('d2d.user.save', vals))
#####################################################################
# Method 3, force all parameters and return types to be dicts only
#####################################################################
def save_and_notify_2(vals):
return d2d.notification.send(d2d.user.save(vals))
#####################################################################
# Method 4, Introduce queues and async operations
#####################################################################
def what(vals):
nextq = vals.get('d2d.next.q')
yield nextq.send(do_what_I_do_here(vals))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment