I'm trying to get used to the redis-py Python module. I just can't figure out how the "mrange" function has to be used to get one or more Time-Series by its labels.
In the CLI it looks like this and it works:
TS.MRANGE - + FILTER area_id=32
But I can't get it to work in Python (one of many things that I tried):
import redis
r = redis.Redis()
r.ts().mrange("-","+","area_id:32")
r.ts().mrange("-","+",filters="area_id:32")
r.ts().mrange("-","+",filters='{area_id}32')
#And I tried many more...
I get the following Error:
ResponseError: TSDB: failed parsing labels
r.ts().mrange('-','+',['area_id=32'])
you need [square brackets]
Related
I'm trying to build a vcard using vobject python library. The server uses multiple phone types like this:
TEL;TYPE="HOME","VOICE":111111111
TEL;TYPE="WORK","VOICE":4444444444
So I'm trying to do the same.
vcard_phone = vcard.add('tel')
vcard_phone.value = "111111111"
vcard_phone.type_param = ["HOME","VOICE"]
But this results to:
TEL;TYPE=WORK,VOICE:4444444444
I think that this is considered to be one value (there are no quotes).
I also tried:
vcard_phone.type_param = '"HOME","VOICE"'
which raises an exception:
vobject.base.VObjectError: "Double quotes aren't allowed in parameter values."
I couldn't find anything regarding this in the docs. How to do that?
I have the following code..
.... rest api call >> response
rsp = response.json()
print json2html.convert(rsp)
which results in the following
error: Can't convert NULL!
I therefore started looking into schemes to replace all None / Null's in my JSON response, but I'm having an issue since the JSON returned from the api is complex and nested many levels and I don't know where the NULL will actually appear.
From what I can tell I need to iterate over the dictionary objects recursively and check for any values that are NONE and actually rebuild the object with the values replaced, but I don't really know where to start since dictionary objects are immutable..
If you look at json2html's source it seems like you have a different problem - and the error message is not helping.
Try to use it like this:
print json2html.convert(json=rsp)
btw. because I've already contributed to that project a bit I've opened up the following PR due to this question: https://github.com/softvar/json2html/pull/20
I'm using Python 3 for this.
Basically, I'm making a API call using urllib and getting the error:
"ValueError: Single '}' encountered in format string"
I have looked at a variety of other solutions but they don't seem to work.
Basically what I am doing is:
import urllib.request
import urllib.parse
def query_person(first, last):
person_request = urllib.request.urlopen('http://api.querysite.com/content/search/index:AUTHOR?query=authlast%28%27{last}}%27%29%20AND%20authfirst%28%27{first}}%27%29&'.format(first=first,last=last))
return(person_request)
print(query_person("John", "Doe"))
The actual API will not be reproducible since it requires an API key (ommited obviously) as well as the need to be on a verified network.
I think the issue has to do with "{last}}%27%29%20AND%20authfirst%28%27{first}}" having an extra bracket. For example, if I wanted to just query it in my url bar without python or .format(), it would look like:
http://api.querysite.com/content/search/index:AUTHOR?query=authlast%28%27Doe}%27%29%20AND%20authfirst%28%27John}%27%29&
or more specifically: Doe}%27%29%20AND%20authfirst%28%27John}%27%29&
If I use the latter method in python, I have no issues, but it does not allow me to input names to query of course.
You need to double up on your single brace if you want it to remain in the string:
For example:
'{first}}}'.format(first='John') == 'John}'
In your case:
person_request = urllib.request.urlopen('http://api.querysite.com/content/search/index:AUTHOR?query=authlast%28%27{last}}}%27%29%20AND%20authfirst%28%27{first}}}%27%29&'.format(first=first,last=last))
Code:
pages = Line.gql("where projectNAME='%s'"%(projectName)).run(projection=["projectNAME","pageno"],distinct=True)
This show the following error,
TypeError: Unknown configuration option ('distinct')
Why so?
It looks like the documentation for run may be incorrect. It isn't possible to provide the distinct option there. Instead, try specifying what you want inside the GQL string:
pages = db.GqlQuery('SELECT DISTINCT pageno FROM Line WHERE projectNAME=:1', projectName).run()
Note that here the only projection specified is pageno. It is not possible to specify a projection that is used in an equality (note you know what the projectNAME is already because you specified it).
I'm trying to pass information to a python page via the url. I have the following link text:
"<a href='complete?id=%s'>" % (str(r[0]))
on the complete page, I have this:
import cgi
def complete():
form = cgi.FieldStorage()
db = MySQLdb.connect(user="", passwd="", db="todo")
c = db.cursor()
c.execute("delete from tasks where id =" + str(form["id"]))
return "<html><center>Task completed! Click <a href='/chris'>here</a> to go back!</center></html>"
The problem is that when i go to the complete page, i get a key error on "id". Does anyone know how to fix this?
EDIT
when i run cgi.test() it gives me nothing
I think something is wrong with the way i'm using the url because its not getting passed through.
its basically localhost/chris/complete?id=1
/chris/ is a folder and complete is a function within index.py
Am i formatting the url the wrong way?
The error means that form["id"] failed to find the key "id" in cgi.FieldStorage().
To test what keys are in the called URL, use cgi.test():
cgi.test()
Robust test CGI script, usable as main program. Writes minimal HTTP headers and formats all information provided to the script in HTML form.
EDIT: a basic test script (using the python cgi module with Linux path) is only 3 lines. Make sure you know how to run it on your system, then call it from a browser to check arguments are seen on the CGI side. You may also want to add traceback formatting with import cgitb; cgitb.enable().
#!/usr/bin/python
import cgi
cgi.test()
Have you tried printing out the value of form to make sure you're getting what you think you're getting? You do have a little problem with your code though... you should be doing form["id"].value to get the value of the item from FieldStorage. Another alternative is to just do it yourself, like so:
import os
import cgi
query_string = os.environ.get("QUERY_STRING", "")
form = cgi.parse_qs(query_string)
This should result in something like this:
{'id': ['123']}
First off, you should make dictionary lookups via
possibly_none = my_dict.get( "key_name" )
Because this assigns None to the variable, if the key is not in the dict. You can then use the
if key is not None:
do_stuff
idiom (yes, I'm a fan of null checks and defensive programming in general...). The python documentation suggests something along these lines as well.
Without digging into the code too much, I think you should reference
form.get( 'id' ).value
in order to extract the data you seem to be asking for.