AttributeError:'google.protobuf.pyext._message.RepeatedCompositeCo' object has no attribute 'DESCRIPTOR' - python

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

Related

Python Selenium: Alternative to string formatting for lists?

I am trying to take the value from the input and put it into the browser.find_elements_by_xpath("//div[#class='v1Nh3 kIKUG _bz0w']") function. However, the string formatting surely doesn't work, since it's the list, hence it throws the AttributeError.
Does anyone know any alternatives to use with lists (possibly without iterating over each file)?
xpath_to_links = input('Enter the xpath to links: ')
posts = browser.find_elements_by_xpath("//div[#class='{}']").format(devops)
AttributeError: 'list' object has no attribute 'format'
Looks like the reason of error is that you are placing the format function in the wrong place, so instead of operating on string "//div[#class='{}']" you call it for the list returned by find_elements_by_xpath. Could you please try to replace your code with one of the following lines ?
posts = browser.find_elements_by_xpath("//div[#class='{}']".format(devops))
posts = browser.find_elements_by_xpath(f"//div[#class='{devops}']")

Pythonic way of ignoring an XML attribute if it doesn't exist

I'm working on processing some XMLs from an API, however the XML is not as structured as I'd think. I've been struggling with just ignoring when an attribute's text doesn't exist.
I'm iterating trough my tree, getting each text and assigning them to a variable, e.g.:
codigo = produtos.find('sku').text
pais = produtos.find('attributes/country').text
and so on.
However, in some cases, the attributes child doesn't exist. I've attempted doing inline temporary expressions to just assign a blank value to the variables, but no matter what I do I get a 'NoneType' object has no attribute 'text'
For pais, sometimes this child doesn't exist, so I've attempted doing:
pais = produtos.find('attributes/country').text if produtos.find('attributes/country').text else ''
If I remove the .textcall it will not retrieve the text when the attributes exist.
This seems really simple against what I'm doing as whole, but I just can't wrap my head around in fixing this issue. Is there a way to just ignore these inline when the attribute doesn't exist?
After a lot of searching the most pythonic way I've found to do this inline is just suppressing the AttributeError (Python 3):
from contextlib import suppress
with suppress(AttributeError): pais = produtos.find('attributes/country').text

SmartSheet API python extended object

There are several objects in the SmartSheet SDK API which are extensions of objects. For instance, CellLink and ObjectValue are an extensions of the Cell object. I've done some reading and understand that these are parent/child classes and involve inheritance. However, this concept still escapes me and I cannot figure out the syntax for creating a CellLink object.
new_cell = ss.models.Cell()
linked_cell = ss.models.Cell()
linked_cell.column_id = int(columnid)
linked_cell.sheet_id = int(sheetid)
linked_cell.row_id = int(rowid)
new_cell.link_in_from_cell = linked_cell
The example above gives me the most informative error message therefore, I assume it is the closest to the correct syntax of all the variations I have tried. Any help with this example and possibly the underlying concept would be greatly appreciated.
raise ValueError("`{0}` invalid type for {1} value".format(value,
self.object_type))
ValueError: `{"columnId": 2068210422966148}` invalid type for <class
'smartsheet.models.cell_link.CellLink'> value
I believe I have found the answer to this question. It seems as though you just need to create a dictionary of the attributes like:
ex_dict = {sheet_id: 0974792938, column_id: 07263839242, row_id:
2632938474839}
new_cell.link_in_from_cell = ex_dict
The trick is later in the code. Instead of creating a new row like:
row = ss.models.Row()
You need to update an existing row like:
row = ss.Sheets.get_row(sheet_id, row_id)
However, I am still having a weird error of:
Field \"createdAt\" was of unexpected type.
You should be sending Row and Cell objects with only the properties that you wish to change. You do not want to attempt to modify an existing Row object (e.g. with the createdAt property, but rather allocate a new one with appropriate row id and cells to update.
See https://github.com/smartsheet-samples/python-snippets/blob/04951c2ca8ae1a97386bdd3fa6e010f2845e1421/samples.py#L45 for a complete example of creating a cell link.

Clarification on Protobuf PrintField()

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

How to use dynamic variable in python mongodb script

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.

Categories