| Conditions | 6 | 
| Total Lines | 52 | 
| Code Lines | 18 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | #!/usr/bin/env python3  | 
            ||
| 101 | @contextmanager  | 
            ||
| 102 | def redis_lock(lock_id, blocking=False, expire=True):  | 
            ||
| 103 | """  | 
            ||
| 104 | This function get a lock relying on a lock name and other status. You  | 
            ||
| 105 | can describe more process using the same lock name and give exclusive  | 
            ||
| 106 | access to one of them.  | 
            ||
| 107 | |||
| 108 | Args:  | 
            ||
| 109 | lock_id (str): the name of the lock to take  | 
            ||
| 110 | blocking (bool): if True, we wait until we have the block, if False  | 
            ||
| 111 | we returns immediately False  | 
            ||
| 112 | expire (bool): if True, lock will expire after LOCK_EXPIRE timeout,  | 
            ||
| 113 | if False, it will persist until lock is released  | 
            ||
| 114 | |||
| 115 | Returns:  | 
            ||
| 116 | bool: True if lock acquired, False otherwise  | 
            ||
| 117 | """  | 
            ||
| 118 | |||
| 119 | # read parameters from settings  | 
            ||
| 120 | REDIS_CLIENT = redis.StrictRedis(  | 
            ||
| 121 | host=settings.REDIS_HOST,  | 
            ||
| 122 | port=settings.REDIS_PORT,  | 
            ||
| 123 | db=settings.REDIS_DB)  | 
            ||
| 124 | |||
| 125 | # this will be the redis lock  | 
            ||
| 126 | lock = None  | 
            ||
| 127 | |||
| 128 | # timeout for the lock (if expire condition)  | 
            ||
| 129 | timeout_at = monotonic() + LOCK_EXPIRE - 3  | 
            ||
| 130 | |||
| 131 | if expire:  | 
            ||
| 132 | lock = REDIS_CLIENT.lock(lock_id, timeout=LOCK_EXPIRE)  | 
            ||
| 133 | |||
| 134 | else:  | 
            ||
| 135 | lock = REDIS_CLIENT.lock(lock_id, timeout=None)  | 
            ||
| 136 | |||
| 137 | status = lock.acquire(blocking=blocking)  | 
            ||
| 138 | |||
| 139 | try:  | 
            ||
| 140 |         logger.debug("lock %s acquired is: %s" % (lock_id, status)) | 
            ||
| 141 | yield status  | 
            ||
| 142 | |||
| 143 | finally:  | 
            ||
| 144 | # we take advantage of using add() for atomic locking  | 
            ||
| 145 | # don't release the lock if we didn't acquire it  | 
            ||
| 146 | if status and ((monotonic() < timeout_at and expire) or not expire):  | 
            ||
| 147 |             logger.debug("Releasing lock %s" % lock_id) | 
            ||
| 148 | # don't release the lock if we exceeded the timeout  | 
            ||
| 149 | # to lessen the chance of releasing an expired lock  | 
            ||
| 150 | # owned by someone else  | 
            ||
| 151 | # if no timeout and lock is taken, release it  | 
            ||
| 152 | lock.release()  | 
            ||
| 153 |