Fetching data from memcached - python

I have to fetch data from memcached. But my code is fetching it from the database.
The pseudo code is as follows:
given a URL, try finding that page in the cache
if the page is in the cache:
return the cached page
else:
generate the page
save the generated page in the cache (for next time)
return the generated page
My python Code is as follows:
class CachedAPIView(APIView):
def get_queryset(self,request):
return function(self,request.data)
def get_object(self,queryset=None):
obj = cache.get('%s-%s'%(self.modelName.lower(),self.kwargs['pk']),None)
if not obj:
obj=super(CachedAPIView,self).get_object(queryset)
cache.set('%s-%s'%(self.modelName.lower(),self.kwargs['pk']),obj)
class ABC(CachedAPIView):
def fun(self,request,format=None):
request.data['PubIp']=getUserIP(request)
returnData=CachedAPIView.get_queryset(self,request)
if returnData == "TOKEN_ERROR":#token error
.....
elif returnData == "RECORD_NOT_FOUND":#bad request
......
else:
......
Any help would be appreciated. Thanks!

I have tried this code and it worked.
import hashlib
import json
from django.core.cache import cache
from .views import *
class CacheService():
def __init__(self, view=None, **kwargs):
self.data = kwargs
self.data.update({'view_name': view})
self.key = self.prepare_key()
def prepare_key(self):
return hashlib.md5(json.dumps(self.data, sort_keys=True).encode('utf-8')).hexdigest()
def set_to_cache(self, qs):
cache.set(self.key, qs)
def unset_cache(self, qs):
cache.set(self.key, None)
def delete_key_value(self):
cache.delete(self.key)
def get_from_cache(self):
return cache.get(self.key, None)
def clear_all_cache(self):
cache.clear()
This is cache.py file. The view to be cached is given below:
class MyView(APIView):
def post(self,request,format=None):
'''
'''
Data=my_function()
cache_service = CacheService(qs_type='my_function()')
event_queryset = cache_service.get_from_cache()
if not event_queryset:
event_queryset = my_function()
cache_service.set_to_cache(event_queryset)
if event_queryset == "TOKEN_ERROR":#token error
............
elif event_queryset == "RECORD_NOT_FOUND":#bad request
.............
else:
......................

Related

Python | check if dict{} item is in list[]

Attempting to check an in-memory list, plant_list[] against a JSON payload from an api.
If the incoming payload's dict name matches inside of plant_list the if should fire off.
Instead my script only returns null
Please point out my mistakes.
The JSON sent over the api call is:
{ "name":"swizz", "days": "7", "price": 2.00 }
Source Code
from flask import Flask, request
from flask_restful import Api, Resource
app = Flask(__name__)
api = Api(app)
#app.route('/', methods=['GET', 'POST'])
def home():
return 'Tiny'
plant_list = []
class Plant(Resource):
def get(self, name):
return {'Name':name}, 200
def post(self, name):
payload = request.get_json()
for x in range(0, len(plant_list)):
if payload['name'] == plant_list[x]['name']:
return {'message': f'''Item {payload['name']} already stored in database.'''}
else:
plant_list.append(payload)
return plant_list, 201
api.add_resource(Plant, '/plant/<string:name>')
if __name__ == '__main__':
app.run(port=9004, debug=True)
You can test for key in dict simply by: if key_name in my_dict:... So, if "price" in plant_list[x]:
You are setting plant_list as a global. If you want to use that inside a Class, you should define it inside the function in your class:
plant_list = []
class Plant(Resource):
....
def post(self, name):
global plant_list
....
Not sure why you need it as a global variable. Perhaps you want:
class Plant(Resource):
plant_list = []
....
def post(self, name):
....

How do I get and test the state of requests.head in this code?

I have the following code:
def verify_pseudo_streaming(self, publishedName, path, start):
cname = self.get_cname(publishedName)
params = {'start': start}
url = 'http://{}{}'.format(cname, path)
origin_size = int(requests.head(url).headers['Content-Length'])
start_headers = requests.head(url, params=params).headers
start_size = int(start_headers['Content-Length'])
msg = "Start size is not lower than origin size"
assert start_size < origin_size, msg
In my test I have mocked the requests.head in my unit test, how do I get the value of headers the first and second time when running requests.head without really running it ?
I finally ended up doing the one below which worked ...
class MockHeaders(object):
def __init__(self):
pass
def streaming_headers(self, *args, **kwargs):
start = kwargs.get('params', {})
self.headers['Content-Length'] = start.get('start', 10)
stuff = Mock()
stuff.headers = self.headers
return stuff
<snip> ... </snip>
#patch("FrontEnd.requests.head")
#patch("FrontEnd.FrontEnd.get_cname")
def test_verify_pseudo_streaming(self, mock_get_cname,mock_head):
mock_get_cname.return_value = 'hulahoop'
mock_header = MockHeaders()
mock_head.side_effect = mock_header.streaming_headers
mock_head.return_value = mock_header
try:
self.fe.verify_pseudo_streaming('publishedName', 'path', 5)
except AssertionError:
self.fail("Unexpected Assertion Error")
I am just going to keep this open to see if others got other more elegant ideas.
you can mock \ monkeypatch only this method
requests.sessions.Session.send
this is what requests use to send the request, so if you change that to do nothing
it will not send the request
def x(*args, **kwarg):
pass
requests.sessions.Session.send = x
I would mock out requests like this:
class FakeHeaders(object):
def __init__(self):
self.headers = {'Content-Length': 1}
def inc_headers():
self.headers['Content-Length'] += 1
def testVerifyPsuedoStreaming(self):
fake_header = FakeHeader()
with mock.patch.object(
request, 'head', side_effect=fake_header.inc_headers,
return_value=fake_header) as mock_head:
...

flask: How to store and retrieve POST calls?

#app.route('/path/<user>/<id>', methods=['POST'])
#cache.cached(key_prefix='/path/<user>', unless=True)
def post_kv(user, id):
cache.set(user, id)
return value
#app.route('/path/<user>', methods=['GET'])
#cache.cached(key_prefix='/path/<user>', unless=True)
def getkv(user):
cache.get(**kwargs)
I want to be able to make POST calls to the path described, store them, and GET their values from the user. The above code runs, but has bugs and doesn't perform as needed. Frankly, the flask-cache docs aren't helping. How can I properly implement cache.set and cache.get to perform as needed?
In Flask, implementing your custom cache wrapper is very simple.
from werkzeug.contrib.cache import SimpleCache, MemcachedCache
class Cache(object):
cache = SimpleCache(threshold = 1000, default_timeout = 100)
# cache = MemcachedCache(servers = ['127.0.0.1:11211'], default_timeout = 100, key_prefix = 'my_prefix_')
#classmethod
def get(cls, key = None):
return cls.cache.get(key)
#classmethod
def delete(cls, key = None):
return cls.cache.delete(key)
#classmethod
def set(cls, key = None, value = None, timeout = 0):
if timeout:
return cls.cache.set(key, value, timeout = timeout)
else:
return cls.cache.set(key, value)
#classmethod
def clear(cls):
return cls.cache.clear()
...
#app.route('/path/<user>/<id>', methods=['POST'])
def post_kv(user, id):
Cache.set(user, id)
return "Cached {0}".format(id)
#app.route('/path/<user>', methods=['GET'])
def get_kv(user):
id = Cache.get(user)
return "From cache {0}".format(id)
Also, the simple memory cache is for single process environments and the SimpleCache class exists mainly for the development server and is not 100% thread safe. In production environments you should use MemcachedCache or RedisCache.
Make sure you implement logic when item is not found in the cache.

Return list function python

I am new to python and I'm trying this code below q Objects1 return to list
how can I do this?
it returns me the following error
File "/ home/paulo/Desktop/testepy2/objectMIB.py", line 53
     return
SyntaxError: 'return' outside function
thank you
from pysnmp.entity import engine, config
from pysnmp import debug
from pysnmp.entity.rfc3413 import cmdrsp, context, ntforg
from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.smi import builder
import threading
import collections
import time
MibObject = collections.namedtuple('MibObject', ['mibName',
'objectType', 'valueFunc'])
class Mib(object):
"""Stores the data we want to serve.
"""
def __init__(self):
self._lock = threading.RLock()
self._test_count = 0
self._test_get = 10
self._test_set = 0
def getTestDescription(self):
return "My Description"
def getTestCount(self):
with self._lock:
return self._test_count
def setTestCount(self, value):
with self._lock:
self._test_count = value
def getTestGet(self):
return self._test_get
def getTestSet(self):
return self._test_set
def setTestSet(self):
self._test_set = value
class ListObejtc ():
mib = objectMIB.Mib()
objects1 = [MibObject('MY-MIB', 'testDescription', mib.getTestDescription),
MibObject('MY-MIB', 'testCount', mib.getTestCount),MibObject('MY-MIB', 'testGet', mib.getTestGet), MibObject('MY-MIB', 'testSet', mib.getTestSet) ]
print objects1
return
It's normal for the code you have shown nested inside "ListObejtc" to be in a method, like so:
class ListObejtc ():
def __init__(self):
pass
def doObjects(self):
mib = objectMIB.Mib()
objects1 = [MibObject('MY-MIB', 'testDescription', mib.getTestDescription),
MibObject('MY-MIB', 'testCount', mib.getTestCount),MibObject('MY-MIB', 'testGet', mib.getTestGet), MibObject('MY-MIB', 'testSet', mib.getTestSet) ]
print objects1
return objects1
You got a SyntaxError because the return as you had it was in class context, and it makes no sense there.

How would I search for values in a dictionary?

I am making an opensource twitch.tv API wrapper for python, so far I have:
import urllib2
import json
import time
waittime = 1
baseurl = 'https://api.twitch.tv/kraken/'
class twitchchannelinfo():
def __init__ (self,channel):
self.channel = channel
time.sleep(waittime)
self.dict1 = json.loads(urllib2.urlopen(baseurl + 'channels/' + channel).read())
def getstatus(self):
return self.dict1 ['status']
def getdisplay_name(self):
return self.dict1 ['display_name']
def getmature(self):
return self.dict1 ['mature']
def getchanurl(self):
return self.dict1 ['url']
def getcreated_at(self):
return self.dict1 ['created_at']
def getteams(self):
return self.dict1 ['teams']
def getgame(self):
return self.dict1 ['game']
def getupdated_at(self):
return self.dict1 ['updated_at']
and I would like to add error checking to this API. The server will return a json response like this for an error:
{
"error": "Unprocessable Entity",
"status": 422,
"message": "Channel 'deeman' is not available on Twitch"
}
which I then convert to a dictionary using json.loads. How would I check this dictionary for the value "error" or is there a better way of doing this?
I would recommend to do:
if 'error' in self.dict1:
raise ValueError("%s: %s" % (self.dict1["error"], self.dict1["message"]))
try:
self.dict1[u'error']
except KeyError:
## Do something here
This is just another approach using try...except
if 'error' in self.dict1:
# do blah

Categories