Python: get list of memcached key values by wildcards - python

I am using memcached with pylibmc as binaries in my Django app. Now what I want to get list of key values from cache.
Suppose I have this key value pair data in cache,
{'Key_1':[1,2,3]} {'Key_2':[4,5,6]} {'Key_3':[6,7,8]}
I can get a single record by
cache.get('Key_1')
I want to get all Key_*data
cache.get('Key_*')
Anyone suggest a way? or is it possible?
Thanks!

If you have dictionary than you can do something like this:
import re
dict = { 'Key_1':[1,2,3], 'Key_2':[4,5,6], 'Key_3':[6,7,8] }
r = re.compile(r"Key_\d+") // matching expression
matching_keys = filter(r.match, dict.keys())
This way you can get all matching keys and then simply iterate on those keys.

You could either use the mcdict library and iterate through memcached like a normal dictionary or else you could look at the mcdict source code and apply the same technique in your own code.

Related

How can I get a key from the value in Firebase?

My Firebase structure is as below.
I'm using python to use firebase,
and also I use Pyrebase.
I want to get a key, which looks like '288xxxxxx'
from the value(busStopName).
Can I get the answer in Pyrebase?
Thank you in advance...
The following should work, based on the documentation
searched_bus = db.child("asanbus").order_by_child("busStopName").equal_to("XYZ").get()
for bus in searched_bus.each():
print(bus.key())
Assumption: there is only one record corresponding to busStopname = "XYZ". If not the case, you may do:
searched_bus = db.child("asanbus").order_by_child("busStopName").equal_to("XYZ").limit_to_first(1).get()

how to request multiple values from same key using eve

I have an eve app running on my mongodb collection col10. I am trying to get a response where I have multiple values selected from the same key, example:
http://127.0.0.1:4567/col10?where={"var0053":[1130,1113]}
## returns 0 objects
I have also tried:
http://127.0.0.1:4567/col10?where=var0053==[1130,1113]
## returns just the objects with var0053 = 1113
is there a way of requesting to server more than one value from the same key?
If you are using GET method your url should be looking like that :
http://IP_ADDRESS:8080/test?list=1&list=2&list=3
for retrieving it:
String[] arrlist=request.getParameterValues('list');
your array will be filled with separated values:
//["1","2","3"]
When you retrieving your list parameters it wouldn't be parsed as array but as a series of String which will be grouped later on into an array.
Which means even if you write it list[]=1&list[]=2&list[]=3, list[=1&list[=2&list[=3, list*=1&list*=2&list*=3 or list=1&list=2&list=3 it would always be giving you the same answer whether you retrieve it as
request.getParameterValues('list[]') //["1","2","3"]
request.getParameterValues('list[') //["1","2","3"]
request.getParameterValues('list*') //["1","2","3"]
request.getParameterValues('list') //["1","2","3"]
With Eve, you can use mongodb syntax for queries, like this:
http://127.0.0.1:4567/col10?where={"var0053": {"$in": ["1130", "1113"]}}
Documentation is here https://docs.python-eve.org/en/stable/features.html#filtering.

Pymongo.find() only return answer

I am working on a code which will fetch data from the database using pymongo. After that I'll show it in a GUI using Tkinter.
I am using
.find()
to find specific documents. However I don't want anything else then 'name' to show up. So I used {"name":1}, now it returns:
{u'name':u'**returned_name**'}
How do I remove the u'name': so it will only return returned_name?
Thanks in advance,
Max
P.s. I have searched a lot around the web but couldn't find anything which would give me some argument to help me.
What you see returned by find() call is a cursor. Just iterate over the cursor and get the value by the name key for every document found:
result = db.col.find({"some": "condition"}, {"name": 1})
print([document["name"] for document in result])
As a result, you'll get a list of names.
Or, if you want and expect a single document to be matched, use find_one():
document = db.col.find_one({"some": "condition"}, {"name": 1})
print(document["name"])
Mongo will return the data with keys, though you can as workaround use something like this
var result = []
db.Resellers_accounts.find({"name":1, "_id":0}).forEach(function(u) { result.push(u.name) })
This example is for NodeJS driver, similar can be done for Python
Edit (Python Code) -
res = db.Resellers_accounts.find({},{"name":1, "_id":0})
result = []
for each in res:
result.append(res['name'])
Edit 2 -
No pymongo doesn't support returning only values, everything is key-value paired in MongoDB.

How to get a list of all indexes in python-elasticsearch

How would I get a list of the names of an index in Python? Here is what I have so far:
>>> es=e.es
>>> es
<Elasticsearch([{'host': '14555f777d8097.us-east-1.aws.found.io', 'port': 9200}])>
>>> es.indices
<elasticsearch.client.indices.IndicesClient object at 0x10de86790>
# how to get a list of all indexes in this cluster?
This question comes up when searching for information on retrieving aliases using the python-elasticsearch library. The accepted answer says to use get_aliases but that method has been removed (as of 2017). To get aliases, you can use the following:
es.indices.get_alias("*")
UPDATE
The latest usage should be with a keyword arg:
es.indices.get_alias(index="*")
how to get a list of all indexes in this cluster?
Use the wildcard. Works with elasticsearch-py.
for index in es.indices.get('*'):
print index
Here is one way to do it with the get_alias() method:
>>> indices=es.indices.get_alias().keys()
>>> sorted(indices)
[u'avails', u'hey', u'kibana-int']
If you are willing to use pyelasticsearch module they have support for the GET _mapping command, which produces the schema of the cluster. This will allow you to see the indices, and drill into each index to see doc_types, and their fields, etc. Here's an example:
import pyelasticsearch as pyes
es = pyes.ElasticSearch(["http://hostname0:9200", "http://hostname1:9200"]) ## don't accidentally type Elasticsearch, the class from the other two modules
schema = es.get_mapping() ## python dict with the map of the cluster
To get just the list of indices,
indices_full_list = schema.keys()
just_indices = [index for index in indices_full_list if not index.startswith(".")] ## remove the objects created by marvel, e.g. ".marvel-date"
This is related to this question
You can use the Cat API:es.cat.indices(h='index', s='index').split()
I use curl to call the stats API and get information about the indices. Then I parse the JSON object that is returned to find the index names.
curl localhost:9200/_stats
In Python you can call curl using the requests library. I don't know of a way to do this using the Elasticsearch or Elasticsearch-DSL Python library.
You can get _mapping to get list of all indexes by doing something like that.
requests.get(full_elastic_url + "/_mapping")
_cat API seems the right way to do this, since the _aliases way of doing will soon be removed by elasticsearch since it exposes the system indices.
indices = es.cat.indices(h='index', s='index').split()
It did the job for me.
If you want 'alias name' and not 'index name', here the perfect solution:
response = es.indices.get(indexname)
alias_names = list(response[indexname]['aliases'].keys())
In alias_names we get list of alias names on a particular index.

Convert web.py iterbetter to dictionary or list

I'm trying to get the some data from a MySQL table (ipb_members) using web.py's database.select. This is the code I'm using: members = db.select("ipb_members", where="name=\"asdfquerty\"")
it returns an instance of iterbetter. What I'm trying to figure out is how to convert that to a dictionary or a list. I saw this, which recommends using list(), but that just puts everything in the first index. I've also tried dict(), but that didn't work either. What am I doing wrong?
This should work:
members = db.select("ipb_members", where="name=\"asdfquerty\"")
for member in members #members is IterBetter
print member.posts #member is Storage
Note that you dont have to call .list() on the result of db.select if you need to iterate over it only once.
Have you tried dict(members)?
iterBetter is not very convenient as it can only iterate once, here is code convert iterBetter to list:
list_conv_from_db = []
for menber in menbers:
temp = dict()
for key in menber:
temp[key]=menber[key]
list_conv_from_db.append(temp)
then you get list_conv_from_db you can do everything you want!

Categories

Resources