vObject (carddav) - how to set multiple parameters for telephone - python

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?

Related

How to solve "ECitMatch() got multiple values for argument 'bdata'"?

I am new to use bioservices Python package. Now I am going to use that to retrieve PMIDs for two citations, given the specified information and this is the code I have tried:
from bioservices import EUtils
s = EUtils()
print(s.ECitMatch("pubmed",retmode="xml", bdata="proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|%0Dscience|1987|235|182|palmenberg+ac|Art2|"))
But it occurs an error:
"TypeError: ECitMatch() got multiple values for argument 'bdata'".
Could anyone help me to solve that problem?
I think the issue is that you have an unnamed argument (pubmed); if you look at the source code, you can see that the first argument should be bdata; if you provide the arguments like you do, it is, however, unclear whether bdata is "pubmed" or the named argument bdata, therefore the error you obtain.
You can reproduce it with this minimal example:
def dummy(a, b):
return a, b
dummy(10, a=3)
will return
TypeError: dummy() got multiple values for argument 'a'
If you remove "pubmed", the error disappears, however, the output is still incomplete:
from bioservices import EUtils
s = EUtils()
print(s.ECitMatch("proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|%0Dscience|1987|235|182|palmenberg+ac|Art2|"))
returns
'proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|2014248\n'
so only the first publication is taken into account. You can get the results for both by using the correct carriage return character \r:
print(s.ECitMatch(bdata="proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|\rscience|1987|235|182|palmenberg+ac|Art2|"))
will return
proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|2014248
science|1987|235|182|palmenberg+ac|Art2|3026048
I think you neither have to specify retmod nor the database (pubmed); if you look at the source code I linked above you can see:
query = "ecitmatch.cgi?db=pubmed&retmode=xml"
so seems it always uses pubmed and xml.
Two issues here: syntaxic and a bug.
The correct syntax is:
from bioservices import EUtils
s = EUtils()
query = "proc+natl+acad+sci+u+s+a|1991|88|3248|mann+bj|Art1|%0Dscience|1987|235|182|palmenberg+ac|Art2|"
print(s.ECitMatch(query))
Indeed, the underlying service related to ICitMatch has only one database (pubmed) and one format (xml) hence, those 2 parameters are not available : there are hard-coded. Therefore, only one argument is required: your query.
As for the second issue, as pointed above and reported on the bioservices issues page, your query would return only one publication. This was an issue with the special character %0D (in place of a return carriage) not being interpreted corectly by the URL request. This carriage character (either \n, \r or %0d) is now taken into account in the latest version on github or from pypi website if you use version 1.7.5
Thanks to willigot for filling the issue on bioservices page and bringing it to my attention.
disclaimer: i'm the main author of bioservices

Django/Postgres - No function matches the given name and argument types

I'm trying to create a search system in my Django and Postgresql project but I keep running into an error when I try to make a query.
Whenever I try these commands in the shell:
vector = SearchVector('title','tags')
query = SearchQuery('book') | SearchQuery('harry')
My_Library.objects.annotate(similarity=TrigramSimilarity(vector,test),).filter(similarity__gt=0.3).order_by('-similarity')
I get the error:
"No function matches the given name and argument types. You might need to add explicit type casts."
I've been testing other options for a while, and the only way I can successfully pass a search query without an error is by using two strings in the place of query and vector.
My_Library.objects.annotate(similarity=TrigramSimilarity('title','my search query'),).filter(similarity__gt=0.3).order_by('-similarity')
This will successfully pass my search with no error.
Why am I getting this error, and how can I fix it?
I've been basing my code off of this Full Text Search documentation
TrigramSimilarity takes 2 strings as arguments
You're trying to pass it a SearchVector and a SearchQuery.
that won't work
If you want to search by multiple tags, you probably need to aggregate multiple of the similarity queries with a | and then sort on similarity, something like:
from django.db.models import Q
My_Library.objects.annotate(
Q(similarity=TrigramSimilarity('title','my search query'),)) |
Q(similarity=TrigramSimilarity('title','my search query'),))
).filter(similarity__gt=0.3).order_by('-similarity')
More details on Q
https://docs.djangoproject.com/en/1.11/ref/models/querysets/#q-objects

LDAP query not working

I'm really running out of ideas.
I recently was assigned to improve a script we have in Python so that it can fetch all users whose email match a string (more exactly, all the users whose email match with the value obtained from a HTML's text input).
It works well by using this filter ("search" is the text obtained from the text input):
user_filter = '(mail=%s)' % search
but it needs for the email value to be exactly so it can match with the user's email, and what I need is to match any written down value(string).
The last filter I used was this:
user_filter = '(mail=*%s*)' % search
and also like this:
user_filter = '(mail=%s*)' % search
(please notice the use of wildcards)
but none of them worked.
Any ideas who can I achieve this? Do you need more context?
I'm using ldap and function search_s
This is a snippet of the code:
def ldap_query(query):
""" returns the members of an LDAP group """
try:
ldap_conn = ldap.initialize(LDAP_URL)
ldap_conn.timeout = LDAP_TIMEOUT
ldap_conn.simple_bind(LDAP_USERNAME, LDAP_PASSWORD)
if not ldap_conn.whoami_s():
raise Exception('503 Unable to authenticate to LDAP server with master user & password')
res = ldap_conn.search_s(LDAP_BASE_DN, ldap.SCOPE_SUBTREE, query)
if res == []:
raise Exception('Group not found in LDAP directory, using filter {}'.format(query))
print res
And I'm using it like this:
print ldap_query('(mail=my.name#mycompany.com)')
but if I use the wildcards, I ended up with the error:
print ldap_query('(mail=a.name*)')
EDITED
just now it started to work, by using the last filter (the one just above here). Dunno why it didn't work before.
It worked well by using just one wildcard:
'(mail=a.name*)
rather than two:
'(mail=*a.name*)
I used that approach because of what I've seen while working with MysQL "LIKE" query %string%, whereas with LDAP filters seems not to be the case.

Issue with "ValueError: Single '}' encountered in format string" in an API Call

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))

does distinct work with GAE Python SDK - 1.9.4 release

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).

Categories