I'm trying to use the Google Protobuf API found here and I'm having trouble with the built in PrintField() method with the following info:
PrintField(field, value, out, indent=0, as_utf8=False, as_one_line=False)
Print a single field name/value pair. For repeated fields, the value should be a single element.
After merging my message, I'm able to print out the fully merged layout. However, I'd like the specific field/value pair and I'm a bit unsure how to go about doing that as I can't find any full fledged internet examples.
I have tried the following:
proto.PrintField(1, 1, cStringIO.StringIO())
, proto.PrintField('field1', 'subfield', cStringIO.StringIO())
Where my message looks like:
message field1 {subfield = 1;}
Running as such yields the following error: "AttributeError: 'int' object has no attribute 'is_extension'" this is the same in both cases, the only change being 'int' or 'string'.
Related
def get_ad_full_details(ad_json, current_topics, ad_group_ad, ad):
mandatory_data = {
"ad_group_ad.ad.responsive_search_ad.headlines": ad_group_ad.ad.responsive_search_ad.headlines,
"ad_group_ad.ad.responsive_search_ad.descriptions": ad_group_ad.ad.responsive_search_ad.descriptions}
ad_json["mandatory_data"] = mandatory_data
when I run json.dumps(ad_json) I get AttributeError:'google.protobuf.pyext._message.RepeatedCompositeCo' object has no attribute 'DESCRIPTOR'. I've tried to follow this post but it still gives the same error.
I've tried to iterate and map the ad_group_ad.ad.responsive_search_ad.headlines repeated protof field to its "[text]" values, but the code fails.
Any idea, how I can fetch the "text" member of this repeated proto field?
I can use regex, but thought their might be an easier way
From: https://www.textrazor.com/docs/python#Dictionary
I implemented following code:
client = textrazor.TextRazor("API_key", extractors=["words", "phrases", "topics", "relations","entities"])
client.set_classifiers(["textrazor_iab"])
manager = textrazor.DictionaryManager('API_key')
manager.create_dictionary({'id':'dict_ID'})
new_entity_type_data = {'type': ['cpp_developer']}
manager.add_entries('dict_ID', [{'id': 'DEV1', 'text': 'Andrei Alexandrescu', 'data':new_entity_type_data}, {'id': 'DEV2', 'text':'Bjarne Stroustrup', 'data':new_entity_type_data}])
client.set_entity_dictionaries(['dict_ID'])
response = client.analyze('Although it is very early in the process, higher-level parallelism is slated to be a key theme of the next version of C++, says Bjarne Stroustrup')
I get following error when running response.entities():
AttributeError: 'NoneType' object has no attribute 'encode'
When I don't use the custom dicitionary, I get following output:
[TextRazor Entity b'Bjarne Stroustrup' at positions [26, 27],
TextRazor Entity b'C++' at positions [23]]
I tested this with different sentences, and different entities in the custom dictionary.
I get the same error every time the entity that I added to the custom dictionary occurs in the sentence I am analyzing.
If I create a custom dictionary, but there are no words present in the sentence that are entities in the dictionary, there is no error thrown.
This indicates that the entity is recognized using the custom dictionary, otherwise the error wouldn't be thrown. But for some reason, this entity doesn't have a datatype.
So my question could probably be translated to; how do I add a valid data type to an entity that I add to a custom dictionary?
I got a response from TextRazor:
I can reproduce this and there does appear to be a bug in our Python client when printing entities.
When printing an entity we generate a string that assumes the entity has an ID, custom entities do not, causing this unhelpful error. We’ll fix this in the client. The bug only occurs when printing the entity, you can still access the matched custom entities as normal:
for entity in response.entities():
print(entity.matched_positions, entity.data, entity.custom_entity_id)
I want to update records in the collection books.
I want to create new field whose name and value are the values from variables.
for book in db.books.find():
title = book['title']
author, value = getAuthor(title)
db.dataset.update({"_id": book['_id']}, {"$set": {author: value}})
When I did this I got the error: WriteError: The update path contains an empty field name. It is not allowed which is not true because both variables have values. I googled and resolved this issue by enclosing author into []. So the code looks like this:
for book in db.books.find():
title = book['title']
author, value = getAuthor(title)
db.dataset.update({"_id": book['_id']}, {"$set": {[author]: value}})
But now I am getting this error which I am not able to resolve:
TypeError: unhashable type: 'list'
Does anyone have encountered such problem? How can I resolve it?
It sounds like getAuthor() is returning back nothing for it's first value, so author is getting set to nothing. From what I can see you did not resolve the error, you just changed it to a different error. By making it say [author] (though it's been a while since I've been in Python) I believe you're just trying to set the key to be an empty list, or a list with an empty string as the only value, depending on what author is.
If I were you, I would print out what author is, or do some debugging and figure out what you're getting back from getAuthor(). Since I can't see that code, nor the data in your database, I'm not sure how to help further without more information.
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 currently trying to edit a private dicom tag which is causing problems with a radiotherapy treatment, using pydicom in python. Bit of a python newbie here so bear with me.
The dicom file imports correctly into python; I've attached some of the output in the first image from the commands
ds = dicomio.read_file("xy.dcm")
print(ds)
This returns the following data:
pydicom output
The highlighted tag is the one I need to edit.
When trying something like
ds[0x10,0x10].value
This gives the correct output:
'SABR Spine'
However, trying something along the lines of
ds[3249,1000]
or
ds[3249,1000].value
returns the following output:
> Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
ds[3249,1000].value
File "C:\Users\...\dataset.py", line 317, in __getitem__
data_elem = dict.__getitem__(self, tag)
KeyError: (0cb1, 03e8)
If I try accessing [3249,1010] via the same method, it returns a KeyError of (0cb1, 03f2).
I have tried adding the tag to the _dicom_dict.py file, as highlighted in the second image:
end of _dicom_dict.py
Have I done this right? I'm not even sure if I'm accessing the tags correctly - using
ds[300a,0070]
gives me 'SyntaxError: invalid syntax' as the output, for example, even though this is present in the file as fraction group sequence. I have also been made aware that [3249,1000] is connected to [3249,1010] somehow, and apparently since they are proprietary tags, they cannot be edited in Matlab, however it was suggested they could be edited in python for some reason.
Thanks a lot
It looks like your dicomio lookup is converting all inputs to hexadecimal.
You could try:
ds[0x3249,0x1000]
This should prevent any forced conversion to hexadecimal.
You can apparently access them directly as strings:
ds['3249', '1000']
However, your issue is that you are trying to access a data element that is nested several layers deep. Based on your output at the top, I would suggest trying:
first_list_item = ds['300a', '0070'][0]
for item in first_list_item['300c', '0004']:
print(item['3249','1000'])
Essentially, a data element from the top level Dataset object can be either a list or another Dataset object. Makes parsing the data a little harder, but probably unavoidable.
Have a look at this for more info.
As Andrew Guy notes in his last comment, you need to get the first sequence item for 300a,0070. Then get the second sequence item from the 300c,0004 sequence in that item. In that sequence item, you should be able to get the 3249,1000 attribute.