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.
Related
i tried to replace spaces in a variable in python but it returns me this error
AttributeError: 'HTTPHeaders' object has no attribute 'replace'
this is my code
for req in driver.requests:
print(req.headers)
d = req.headers
x = d.replace("""
""", "")
So, if you check out the class HTTPHeaders you'll see it has a __repr__ function and that it's an HTTPMessage object.
Depending on what you exactly want to achieve (which is still not clear to me!, i.e, for which header do you want to replace spaces?) you can go about this two ways. Use the methods on the HTTPMessage object (documented here) or use the string version of it by calling repr on the response. I recommend you use the first approach as it is much cleaner.
I'll give an example in which I remove spaces for all canary values in all of the requests:
for req in driver.requests:
canary = req.headers.get("canary")
canary = canary.replace(" ", "")
P.S., your question is nowhere near clear enough as it stands. Only after asking multiple times and linking your other question it becomes clear that you are using seleniumwire, for example. Ideally, the code you provide can be run by anyone with the installed packages and reproduces the issue you have. BUT, allright, the comments made it more clear.
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!
I am trying to move over some API calls I had working over to python from postman, I am having some issues making a variable callable by my next get request. I've found a few things while searching but never found a 100% answer on how to call the environment variable in the get request...is it correct to use the {{TEST}} to call that var. Example below.
Test = Myaccoount
Json_Response_Test = requests.get('https://thisisjustatesttoaccessmyaccount/{{Test}}')
How can I carry over Test into the request?
Your code will almost work as you have it if you use the feature of newer version of Python called "format strings". These are denoted by a f at the beginning of the string. This works like this in such versions of Python:
Test = Myaccoount
Json_Response_Test = requests.get(f'https://thisisjustatesttoaccessmyaccount/{Test}')
as long as Myaccoount is a valid value that can be expanded by Python into the format string.
If you're using an older version of Python, you could do something like this:
Test = Myaccoount
Json_Response_Test = requests.get('https://thisisjustatesttoaccessmyaccount/{}'.format(Test))
BTW, it's not good form to use uppercase first character names for variables. The convention is to use uppercase only for class and type names, and use lowercase for variable and field names.
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(:
I'm using python Textile to store markup in the database. I would like to yield the following HTML snippet:
(<em>asdf</em>)
The obvious doesn't get encoded:
(_asdf_) -> <p>(_asdf_)</p>
The following works, but yields an ugly space:
( _asdf_) -> <p>( <em>asdf</em>)
Am I missing something obvious or is this just not possible using python Textile?
It's hard to say if this is a bug or not; in the form on the Textile website, (_foo_) works as you want, but in the downloadable PHP implementation, it doesn't.
You should be able to do this:
([_asdf_]) -> <p>(<em>asdf</em>)</p>
However, this doesn't work, which is a bug in py-textile. You either need to use this:
(]_asdf_])
or patch textile.py by changing line 918 (in the Textile.span() method) to:
(?:^|(?<=[\s>%(pnct)s])|([{[]))
(the difference is in the final group; the brackets are incorrectly reversed.)
You could also change the line to:
(?:^|(?<=[\s>(%(pnct)s])|([{[]))
(note the added parenthesis) to get the behavior you desire for (_foo_), but I'm not sure if that would break anything else.
Follow up: the latest version of the PHP Textile class does indeed make a similar change to the one I suggested.