Guest User

Untitled

a guest
May 21st, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. """ STEP 0: original code --------------------------------------------------- """
  2.  
  3. def get_cached_user(user_id=None, username=None):
  4.    """
  5.    Returns a user object either by their user_id or by their username.
  6.    Hits the database if no cached version is available.
  7.    """
  8.     user = cache.get_user_by_id(user_id)
  9.     if not user:
  10.         user = cache.get_user_by_username(username)
  11.         if not user:
  12.             user = db.get_user_by_id(user_id)
  13.             if not user:
  14.                 user = db.get_user_by_username(username)
  15.                 if not user:
  16.                     raise ValueError('User not found')
  17.             cache.set_user(user)
  18.     return user
  19.  
  20.  
  21.  
  22. """ STEP 1: group responsabilities -------------------------------------------- """
  23.  
  24. def get_cached_user(user_id=None, username=None):
  25.    """
  26.    Returns a user object either by their user_id or by their username.
  27.    Hits the database if no cached version is available.
  28.    """
  29.     user = cache.get_user_by_id(user_id) or cache.get_user_by_username(username)
  30.     if not user:
  31.        user = db.get_user_by_id(user_id) or db.get_user_by_username(username)
  32.                if not user:
  33.             raise ValueError('User not found')
  34.        cache.set_user(user)
  35.     return user
  36.  
  37.  
  38.  
  39. """ STEP 2: move responsabilities to independent objects ----------------------- """
  40.  
  41. def get_cached_user(user_id=None, username=None):
  42.    """
  43.    Returns a user object either by their user_id or by their username.
  44.    Hits the database if no cached version is available.
  45.    """
  46.     return cache.get_user(user_id, username)
  47.  
  48. class cache:
  49.     def  get_user(user_id, username):
  50.         user = get_user_from_cache(user_id, username)
  51.         if not user:
  52.             user = database.get_user(user_id, username)
  53.             cache.set_user(user)
  54.         return user
  55.        
  56.     def get_user_from_cache(user_id, username):
  57.         return get_user_by_id(user_id) or get_user_by_username(username)
  58.  
  59. class database:
  60.     def get_user(user_id, username):
  61.         user = get_user_by_id(user_id) or db.get_user_by_username(username)
  62.         if not user:
  63.             raise ValueError('User not found')
  64.  
  65.            
  66. """ STEP 3: generalize conditions --------------------------------------------- """
  67.  
  68. def get_cached_user(user_id=None, username=None):
  69.    """
  70.    Returns a user object either by their user_id or by their username.
  71.    Hits the database if no cached version is available.
  72.    """
  73.     return get_cached_user_v2({"user_id" : user_id, "username" : username})
  74.  
  75.  
  76. def get_cached_user_v2(conditions):
  77.    """
  78.     Require a dictionary with the conditions to match (all!)
  79.     and returns a user object. Es: {"user_id" : "1234"} or
  80.         {"username" : "login", "password" : "1234"}
  81.         Hits the database if no cached version is available.
  82.         """
  83.     return cache.get_user(conditions)
  84.  
  85. class cache:
  86.     def  get_user(conditions):
  87.         user = get_user_from_cache(conditions)
  88.         if not user:
  89.             user = database.get_user(conditions)
  90.             cache.set_user(user)
  91.         return user
  92.        
  93.     def get_user_from_cache(conditions):
  94.         """ some come to dynamically adapt query througt conditions"""
  95.  
  96. class database:
  97.     def get_user(user_id, username):
  98.         user = """ some come to dynamically adapt query througt conditions"""
  99.         if not user:
  100.             raise ValueError('User not found')
  101.  
  102. """ What I like about this result is the extraction of the 'Repository' interface
  103.         that lets us define a generic kind of recursive cache/db.
  104.         Whenever the dynamic conditions parsing it's not the best, OO speacking, it seems
  105.         a pragmatic and fast solution. """
Add Comment
Please, Sign In to add comment