wildcards in keys of couchdb-python views - python

The published way to use wildcards in couchdb keys is:
key=somekeyname\ufff0
but that doesn't seem to work in python.
Specifically, my view query is:
results = db.view(docname, key='mykey\ufff0')
I've tried zillions of combinations of ",',\, etc.
I either get no data or the error: TypeError: 'str' object is not callable
I need to find: mykey, mykey=0, mykey=1 mykey_somethingelse, etc.
Any help is appreciated.

"key" parameter doesn't provides wildcard functionality, but direct match with specified value. Probably you'd like to receive all keys that starts with "mykey" value, but only, right? Than you need to use startkey and endkey arguments that defines range of possible view key values to match.
I wounder how and why you get TypeError exception with such description there, but probably you better to describe that problem at couchdb-python issue tracker with full trackeback and used versions. Thanks(:

Related

Rasgo Error - Dataset with fqtn does not exist or this API key does not have access

I am trying to use rasgo.get.dataset(fqtn='vw_orders_main') but I am getting an error.
APIError: Dataset with fqtn 'vw_orders_main' does not exist or this API key does not have access.
When using rasgo.get.dataset(), you can either:
pass in a dataset_id
rasgo.get.dataset(123)
pass in a fully qualified table name (fqtn)
rasgo.get.dataset(fqtn="DB.SCHEMA.TABLE")
pass in a resource_key
rasgo.get.dataset(resource_key='mykey')
From the appearance of the string you are using, I believe that is a resource key.
If you are using a variable called vw_orders_main to hold the FQTN string, then try it without the single quotes.
Examples:
vw_orders_main = "DB.SCHEMA.TABLE"
rasgo.get.dataset(fqtn=vw_orders_main)
or
rasgo.get.dataset(fqtn="DB.SCHEMA.TABLE")
or, if what you meant was resource_key,
rasgo.get.dataset(resource_key='vw_orders_main')
A resource_key is randomly assigned when a dataset is published, unless you specify the string yourself (like it appears that you did). It provides you the ability to tie multiple datasets as 1, thus allowing “versions”.
Resource Links: get dataset, publish dataset

How to get Document Name from DocumentReference in Firestore Python

I have a document reference that I am retreiving from a query on my Firestore database. I want to use the DocumentReference as a query parameter for another query. However, when I do that, it says
TypeError: sequence item 1: expected str instance, DocumentReference found
This makes sense, because I am trying to pass a DocumentReference in my update statement:
db.collection("Teams").document(team).update("Dictionary here") # team is a DocumentReference
Is there a way to get the document name from a DocumentReference? Now before you mark this as duplicate: I tried looking at the docs here, and the question here, although the docs were so confusing and the question had no answer.
Any help is appreciated, Thank You in advance!
Yes,split the .refPath. The document "name" is always the last element after the split; something like lodash _.last() can work, or any other technique that identifies the last element in the array.
Note, btw, the refPath is the full path to the document. This is extremely useful (as in: I use it a lot) when you find documents via collectionGroup() - it allows you to parse to find parent document(s)/collection(s) a particular document came from.
Also note: there is a pseudo-field __name__ available. (really an alias of documentID()). In spite of it's name(s), it returns the FULL PATH (i.e. refPath) to the document NOT the documentID by itself.
I think I figured out - by doing team.path.split("/")[1] I could get the document name. Although this might not work for all firestore databases (like subcollections) so if anyone has a better solution, please go ahead. Thanks!

Escape reserved keyword in Python [Protocol Buffer autogenerated class]

so I'm sort of in a fix.
I'm using Google protocol buffers, and it just so happens that one of the fields in the schema is named "from".
I'm using python, so everytime I try to access it, I get a syntax error.
[ex - SomeClass.from -> Syntax error ]
Is there anyway to somehow access the field without using its identifier?
Maybe a way to escape reserved keywords in Python ? (One of the answers already says no, but...)
Or maybe some protobuf specific solution?
Thanks
After you pull your data, you can always save the from into from_ (the pythonic way of avoiding namespace clashes) by using the getattr(var, "from") statement; Ie
SomeClass # is a protocol-buffer
SomeClass.from_ = getattr(SomeClass, "from")
And then you an just use .from_ as you would otherwise.

TypeError: documents must be a non-empty list

I'm doing a program using Twitter API and MongoDB in 2.7 Python language.
I get a timeline and put it in a dictionary, which I want to store in a MongoDB database. To do this I have next code:
def saveOnBD(self, dic):
client = MongoClient("xxxx", "port")
db = client.DB_Tweets_User_Date
collection = db.tweets
collection.insert_many(dic)
I'm debbuging and dic it's not empty but I get next error:
TypeError: documents must be a non-empty list
How can I fix it?
I trying many options, but i solved that question changing the post method.
Instead of:
collection.insert_many(dic)
I used this:
collection.insert_one(dic)
I supose that, as I try to post only a variable(dic) and "insert_many()" is for many variables that retun me the error. That change solved me the question
you can either put in an entry before running the bulk entry function or use insert()
A list of documents must be passed to insert_many method
E.g.:
collection.insert_many([dic])

Combining separate self.request.sessions into one request

I am trying to optimize and reduce some of my code, and just generally understand it better as this is my first development project.
The below works fine but is it possible to simplify it?
self.request.session['path_one_images'] = PATH_ONE_IMAGES
self.request.session['images'] = images
self.request.session['slider_DV_values'] = slider_DV_values
self.request.session['instruction_task_one_images'] = INSTRUCTION_TASK_ONE_IMAGES
self.request.session['instruction_task_two_images'] = INSTRUCTION_TASK_TWO_IMAGES
I tried to combine the separate requests in one using a dict but get the error:
Exception Value: unhashable type: 'list'
self.request.session({['path_one_images'] : PATH_ONE_IMAGES,
['images'] : images,
['slider_DV_values'] : slider_DV_values,
['instruction_task_one_images'] : INSTRUCTION_TASK_ONE_IMAGES,
['instruction_task_two_images'] : INSTRUCTION_TASK_TWO_IMAGES,})
request.session is a basically a Python mapping just like a dictionary, and it supports all dictionary methods. Like dict.update() to set multiple key-value pairs:
self.request.session.update({
'path_one_images': PATH_ONE_IMAGES,
'images': images,
'slider_DV_values': slider_DV_values,
'instruction_task_one_images': INSTRUCTION_TASK_ONE_IMAGES,
'instruction_task_two_images': INSTRUCTION_TASK_TWO_IMAGES
})
Note that the keys are not lists; you were getting confused by the object[...] subscription syntax there.
you know this is wrong syntax for a dict, yes?
{['path_one_images'] : PATH_ONE_IMAGES}
...should be
{'path_one_images': PATH_ONE_IMAGES, etc}
https://docs.python.org/2/library/stdtypes.html#dict
this explains the error you're getting ("unhashable type: 'list'")... Python thinks you're trying to use a list ['path_one_images'] as the dict key. Dict keys don't have to be strings but they have to be hashable. In this case you just want to use the string 'path_one_images'.
Then additionally, as #Martijn Pieters pointed out, the session dict itself isn't callable, you should use the update method, eg:
self.request.session.update({
'path_one_images': PATH_ONE_IMAGES,
'images': images,
'slider_DV_values': slider_DV_values,
'instruction_task_one_images': INSTRUCTION_TASK_ONE_IMAGES,
'instruction_task_two_images': INSTRUCTION_TASK_TWO_IMAGES
})

Categories

Resources