Thursday, December 5, 2019
Tuesday, December 3, 2019
How to Caching Post Requests to redis Server in Python Flask, Redis Using Decorators
from flask import Flask, jsonify, request
from functools import wraps
import redis
import json
r = redis.Redis('__ip_address_redis_server__')
cache_time = 10
def xcaching(f):
@wraps(f)
def decor(*args, **kwargs):
check = request.get_json()
get_from_redis = r.get(str(check))
if get_from_redis:
data = get_from_redis.decode('utf-8')
ret = json.loads(data)
r.expire(str(check), cache_time)
return jsonify(ret)
ret = f(*args, **kwargs)
cache_to_redis = ret.get_data()
r.set(str(check), cache_to_redis ex=cache_time)
return ret
return decor
@app.route('/search', methods=['POST'])
@xcaching
def main():
req = request.get_json()
ret = call_api_function(req)
return jsonify(ret)
from functools import wraps
import redis
import json
r = redis.Redis('__ip_address_redis_server__')
cache_time = 10
def xcaching(f):
@wraps(f)
def decor(*args, **kwargs):
check = request.get_json()
get_from_redis = r.get(str(check))
if get_from_redis:
data = get_from_redis.decode('utf-8')
ret = json.loads(data)
r.expire(str(check), cache_time)
return jsonify(ret)
ret = f(*args, **kwargs)
cache_to_redis = ret.get_data()
r.set(str(check), cache_to_redis ex=cache_time)
return ret
return decor
@app.route('/search', methods=['POST'])
@xcaching
def main():
req = request.get_json()
ret = call_api_function(req)
return jsonify(ret)
'__repr__' example
class TestClass:
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return '{self.__class__.__name__}({self.a}, {self.b})'.format(self=self)
def __init__(self, a, b):
self.a = a
self.b = b
def __repr__(self):
return '{self.__class__.__name__}({self.a}, {self.b})'.format(self=self)
Sunday, December 1, 2019
Convert dictionary 'key' in list to lowercase (python3)
Using List Comprehension and Dictionary Comprehension
>>> rs = [{'ABC': '123', 'DEF': '456'}]
>>> ret = [{k.lower():v for k,v in i.items()} for i in rs]
>>> ret
>>> [{'abc': '123', 'def': '456'}]
Using Lambda Function
>>> rs = list(map((lambda i: { k.lower(): v for k,v in i.items()}), rs))
>>> rs = [{'ABC': '123', 'DEF': '456'}]
>>> ret = [{k.lower():v for k,v in i.items()} for i in rs]
>>> ret
>>> [{'abc': '123', 'def': '456'}]
Using Lambda Function
>>> rs = list(map((lambda i: { k.lower(): v for k,v in i.items()}), rs))
Subscribe to:
Posts (Atom)
ALCATEL 6900
write memory copy running certified reload from working no rollback-timeout
-
SET LLDP System NAME: system name ALL-Uplink system location SIAM-FL11 SHOW LLDP Configure: show lldp config or show lldp local-syste...
-
grant execute on utl_http to wc grant execute on dbms_lock to wc; BEGIN DBMS_NETWORK_ACL_ADMIN.create_acl ( acl => ...
-
:system view system-view or sys :show all config dis current-configuration :create vlan vlan vlan_number :show ip interface ...