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))
Friday, November 22, 2019
How to set TimeZone in Linux Docker Container
#How to set TimeZone in Linux Docker Container
```
apk add tzdata
```
ex:
Dockerfile
```
from python:3.7-alpine
RUN apk add tzdata
ENV TZ=Asia/Bangkok
CMD xxxx
```
#TimeZone in slim and Ubuntu Linux Docker Container
ex:
Dockerfile
```
from python
ENV TZ=Asia/Bangkok
CMD xxxx
```
ex:
Run Container (slim or full linux images)
```
docker run --name container-name -e 'TZ=Asia/Bangkok' python
```
```
apk add tzdata
```
ex:
Dockerfile
```
from python:3.7-alpine
RUN apk add tzdata
ENV TZ=Asia/Bangkok
CMD xxxx
```
#TimeZone in slim and Ubuntu Linux Docker Container
ex:
Dockerfile
```
from python
ENV TZ=Asia/Bangkok
CMD xxxx
```
ex:
Run Container (slim or full linux images)
```
docker run --name container-name -e 'TZ=Asia/Bangkok' python
```
Wednesday, November 20, 2019
SET TimeZone on Ubuntu19 with Command Line
:Find Current TimeZone
$ timedatectl list-timezones | grep -i [zone]
ex:
$ timedatectl list-timezones | grep -i europe
:Remove link
$ sudo unlink /etc/localtime
:Set new TimeZone
sudo ln -s /usr/share/zoneinfo/[Zone/TimeZone] /etc/localtime
ex:
sudo ln -s /usr/share/zoneinfo/Asia/Bangkok /etc/localtime
$ timedatectl list-timezones | grep -i [zone]
ex:
$ timedatectl list-timezones | grep -i europe
:Remove link
$ sudo unlink /etc/localtime
:Set new TimeZone
sudo ln -s /usr/share/zoneinfo/[Zone/TimeZone] /etc/localtime
ex:
sudo ln -s /usr/share/zoneinfo/Asia/Bangkok /etc/localtime
Monday, November 18, 2019
"tail -f" in Windows PowerShell
"tail -f" in Windows PowerShell
Get-Content .\file_name -wait | Where {$_ -match "search_name"}
Get-Content .\file_name -wait -tail 30 | Where {$_ -match "search_name"}
Get-Content .\file_name -wait | Where {$_ -match "search_name-1" -and $_ -match "search_name-2}
Get-Content .\file_name -wait | Where {$_ -match "search_name"}
Get-Content .\file_name -wait -tail 30 | Where {$_ -match "search_name"}
Get-Content .\file_name -wait | Where {$_ -match "search_name-1" -and $_ -match "search_name-2}
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 ...