How to access the response object using elasticsearch DSL for python - python

I have the following code:
s = Search(using=Elasticsearch('http://user:passwd#ipaddress'), index="myindex")
q = Q("multi_match", query='some query', fields=['_all'])
s = s.query(q)
response = s.execute()
print('Total %d hits found.' % response.hits.total)
for hit in response:
print(hit.title)
And I get the error:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/elasticsearch_dsl/utils.py", line 102, in __getattr__
return _wrap(self._d_[attr_name])
KeyError: 'title'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "FindImage.py", line 89, in <module>
main(args.image_file)
File "FindImage.py", line 82, in main
query_db([1], [2])
File "FindImage.py", line 77, in query_db
print(hit.title)
File "/usr/local/lib/python3.5/dist-packages/elasticsearch_dsl/utils.py", line 105, in __getattr__
'%r object has no attribute %r' % (self.__class__.__name__, attr_name))
AttributeError: 'Result' object has no attribute 'title'
However that is in direct contradiction to what the docs state:
Docs
What am I doing wrong? How can I correctly extract the hits and my values from the response?
EDIT
Also the response object ist supposed to have a method "toDict" but when I try to call it I again get an AttributeError.

For the "toDict" question, response.to_dict() works for me. Not sure if this behaviour is the same across lib versions.

Apparently the ".title" references an actual column in their example.
When I used hit.doc.FIRSTTAG, FIRSTTAG being a column in MY NoSQL-db it worked.
Still does not explain the missing method, but I am happy with it for now.
So to anyone having the same problem:
Use your own columns names when evaluating the response object e.g. in my example:
for hit in response:
print(hit.doc.FIRSTTAG)

Related

PyAD- Date based attributes Mostly return None

Copying from Github-
When I try to adquery or pull an attribute that has a date involved (such as lastLogon, lastLogoff, or even a log off method... the attribute always comes up None. However other attributes seem to work. What am I doing wrong?
user1=aduser.ADUser.from_cn("First Last")
date=user1.get_last_login() //comes back as nonetype
Traceback (most recent call last):
File "<pyshell#80>", line 1, in
date=user1.get_last_login()
File "C:\Users\administrator.RESOURCECENTER\AppData\Local\Programs\Python\Python39\lib\site-packages\pyad\adobject.py", line 407, in get_last_login
return pyadutils.convert_datetime(self.get_attribute('lastLogonTimestamp', False))
File "C:\Users\administrator.RESOURCECENTER\AppData\Local\Programs\Python\Python39\lib\site-packages\pyad\pyadutils.py", line 71, in convert_datetime
high_part = int(adsi_time_com_obj.highpart) << 32
AttributeError: 'NoneType' object has no attribute 'highpart'
Not really well documentation on this issue so not sure what I can try.

How do I make python run the current version of my code? Not a git problem

I keep getting the error listed below. The problem isn't that there's an error. It's that the segment of code that is throwing the error no longer exists. I modified it to address the error. It now calls a method .cooridnates() rather than the propery .extent, but it seems to keep running the old code some how (shown in bold). I tried swapping out my IDE and restarting the machine, but still nothing. It's not currently tracked in a repo. So I'm out of ideas. It's a windows 10 machine.
Traceback (most recent call last):
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\PGE_Pro_Py3\lib\site-packages\arcgis\geometry\_types.py", line 183, in __getattr__
return self.__getitem__(name)
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\PGE_Pro_Py3\lib\site-packages\arcgis\geometry\_types.py", line 190, in __getitem__
return dict.__getitem__(self, k)
KeyError: 'extent'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File \\rcnas01-smb\NRM_DataTeam\e_Scripts\~DS\Defensible_space_pre_inspection_form_export_2022_20211029.py, line 192, in <module>
map_server_response = temp.site_map(item_webmap, 'Asset Centroid', "ASSET_NAME = '{}'".format(which_one))
File \\rcnas01-smb\NRM_DataTeam\e_Scripts\template_fxn.py, line 154, in site_map
**extent = sdf_zoomer.spatial.bbox.buffer(400).extent**
File "C:\Program Files\ArcGIS\Pro\bin\Python\envs\PGE_Pro_Py3\lib\site-packages\arcgis\geometry\_types.py", line 186, in __getattr__
"'%s' object has no attribute '%s'" % (type(self).__name__, name)
AttributeError: 'Geometry' object has no attribute 'extent'
Thanks everyone for the replies. I figured out the issue. Some how an old copy of the imported function library was saved in another directory that was also appended in. The program refenerced the first appended path with the old file first. So as I update the script the program continue to used the old copy.

Specified type 'google_ads_failure' does not exist in Google Ads API v8

I have two issues:
I'm calling G-Ads API to bulk remove ads (code)
for error_detail in error_details:
# Retrieve an instance of the google_ads_failure class from the client
failure_message = gAdsServiceWrapper.client.get_type("google_ads_failure")
# Parse the string into a google_ads_failure message instance.
# To access class-only methods on the message we retrieve its type.
google_ads_failure = type(failure_message)
failure_object = google_ads_failure.deserialize(error_detail.value)
for error in failure_object.errors:
# Construct and print a string that details which element in
# the above ad_group_operations list failed (by index number)
# as well as the error message and error code.
print("A partial failure at index "
f"{error.location.field_path_elements[0].index} occurred "
f"\nError message: {error.message}\nError code: "
f"{error.error_code}")
index_array.append(error.location.field_path_elements[0].index)
error_array.append({"error_message": error.message, "error_code": error.error_code})
I get a partial error
And the code fails to parse it (taken form the official website)
My terminal shows:
Partial failures occurred. Details will be shown below.
Traceback (most recent call last):
File "D:\projects\bowling\venv\lib\site-packages\google\ads\googleads\client.py", line 426, in get_type
message_class = getattr(type_classes, name)
File "D:\projects\bowling\venv\lib\site-packages\google\ads\googleads\v8\__init__.py", line 1753, in __getattr__
raise AttributeError(f"unknown type {name!r}.")
AttributeError: unknown type 'google_ads_failure'.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\projects\bowling\src\main.py", line 535, in <module>
main(args.top_id)
File "D:\projects\bowling\src\main.py", line 141, in main
removed_ads_count = remove_disapproved_ads_for_account(account)
File "D:\projects\bowling\src\main.py", line 206, in remove_disapproved_ads_for_account
remove_ads(ad_removal_operations, ads_to_remove_json, account_id)
File "D:\projects\bowling\src\main.py", line 300, in remove_ads
index_array, error_array = _print_results(response_chunk)
File "D:\projects\bowling\src\main.py", line 439, in _print_results
failure_message = gAdsServiceWrapper.client.get_type("google_ads_failure")
File "D:\projects\bowling\venv\lib\site-packages\google\ads\googleads\client.py", line 428, in get_type
raise ValueError(
ValueError: Specified type 'google_ads_failure' does not exist in Google Ads API v8
failure_message = gAdsServiceWrapper.client.get_type("google_ads_failure")
I think this should be -
failure_message = gAdsServiceWrapper.client.get_type("GoogleAdsFailure")

twitterImgBot stops working after some hours

I'm trying to get this, https://github.com/joaquinlpereyra/twitterImgBot, to work
and it works and it seems ok.
But after some hours, it stops working and this error comes up:
*python3 twitterbot.py
Traceback (most recent call last):
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 118, in build_path
value = quote(self.session.params[name])
KeyError: 'id'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "twitterbot.py", line 209, in <module>
main()
File "twitterbot.py", line 200, in main
orders()
File "twitterbot.py", line 118, in orders
timeline.delete_tweet_by_id(tweet.in_reply_to_status_id, api)
File "/home/user/Skrivebord/twitterboot/lo/bot/timeline.py", line 12, in delete_tweet_by_id
api.destroy_status(id_to_delete)
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 245, in _call
method = APIMethod(args, kwargs)
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 71, in __init__
self.build_path()
File "/home/user/.local/lib/python3.7/site-packages/tweepy/binder.py", line 120, in build_path
raise TweepError('No parameter value found for path variable: %s' % name)
tweepy.error.TweepError: No parameter value found for path variable: id*
It seems like the Python has some problem because if I make a new install on a another PC it works for some hours and then stops.
Strange.
This is likely because tweet is not in reply to a status, so has an in_reply_to_status_id attribute that's None, so API.destroy_status is called with an id of None.

Missing shape reference even though shape exists

I have this Python code:
cos = ibm_boto3.client('s3',
verify=False,
ibm_api_key_id=api_key,
ibm_service_instance_id=service_instance_id,
ibm_auth_endpoint=auth_endpoint,
config=Config(signature_version='oauth'),
endpoint_url=service_endpoint)
response = cos.list_buckets(IBMServiceInstanceId=service_instance_id)
print('Response %s' % response)
It gives me this error(Note. Some of the trace has been removed):
Traceback (most recent call last):
File "/venv/lib/python3.7/site-packages/ibm_botocore/model.py", line 588, in resolve_shape_ref
shape_name = member_traits.pop('shape')
KeyError: 'shape'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "listing.py", line 21, in <module>
response2 = cos.list_buckets(IBMServiceInstanceId=service_instance_id)
File "/venv/lib/python3.7/site-packages/ibm_botocore/model.py", line 187, in members
shape_members[name] = self._resolve_shape_ref(shape_ref)
File "/venv/lib/python3.7/site-packages/ibm_botocore/model.py", line 164, in _resolve_shape_ref
return self._shape_resolver.resolve_shape_ref(shape_ref)
File "/venv/lib/python3.7/site-packages/ibm_botocore/model.py", line 591, in resolve_shape_ref
"Invalid model, missing shape reference: %s" % shape_ref)
ibm_botocore.model.InvalidShapeReferenceError: Invalid model, missing shape reference: OrderedDict([('location', 'header'), ('locationName', 'Ibm-Service-Instance-Id')])
I checked the shape and the list_buckets seems to be there. I'm not sure what else I need to check. Can anybody advice where the issue might be?
list_buckets wasn't configured correctly on the device. Once configured I was able to perform the operation.

Categories