extract dicitonary values from non-subscriptable object-type in python

Viewed 21

i'm a python novice, trying to learn and be useful at work at the same time

we use DespatchBay to send parcels. they have a SOAP API which i don't entirely understand, and am using an SDK they released.

before booking a collection and producing a label my code queries the api to get available services, and available dates, and returns what i think are custom object-types containing the info i need. i want to extract and then print information from these objects so that i can confirm the correct details have been used.

postcode = "NW1 4RY"
street_num = 1
recipient_address = client.find_address(postcode, street_num)
print (recipient_address)

yields:

(AddressType){
   CompanyName = "London Zoo"
   Street = "Regents Park"
   Locality = None
   TownCity = "London"
   County = None
   PostalCode = "NW1 4RY"
   CountryCode = "GB"
 }

i can see there's a dictionary there, and i want to drill down into it to extract details, but i don't understand the "(AddressType)" before the dictionary - how do i get past it and call values from the dictionary?

AddressType has some ref here but it doesn't shine much light for me

thanks for any help you can offer!

full code: sdk ref

import os
from despatchbay.despatchbay_sdk import DespatchBaySDK
from pprint import pprint

api_user = os.getenv('DESPATCH_API_USER')
api_key = os.getenv('DESPATCH_API_KEY')
client = DespatchBaySDK(api_user=api_user, api_key=api_key)
sender_id = '5536'


# inputs
postcode = "NW1 4RY"
street_num = 1
customer = "Testy Mctestson"
phone = "07666666666"
email = "testicles@tested.com"
num_boxes = 2
collection_date = '2022-09-11'

recipient_address = client.find_address(postcode, street_num)

recipient = client.recipient(
    name=customer,
    telephone=phone,
    email=email,
    recipient_address=recipient_address
)

print (recipient_address)

parcels = []
parcel_names = []
for x in range(num_boxes):
    parcelname = "my_parcel_" + str(x + 1)
    parcel_names.append(parcelname)

for my_parcel in parcel_names:
    go_parcel = client.parcel(
        contents="Radios",
        value=500,
        weight=6,
        length=60,
        width=40,
        height=40,
    )
    parcels.append(go_parcel)

sender = client.sender(
    address_id=sender_id
)

shipment_request = client.shipment_request(
    parcels=parcels,
    client_reference=customer,
    collection_date=collection_date,
    sender_address=sender,
    recipient_address=recipient,
    follow_shipment='true'
)

services = client.get_available_services(shipment_request)
shipment_request.service_id = services[0].service_id
dates = client.get_available_collection_dates(sender, services[0].courier.courier_id)
print(customer + "'s shipment of",num_boxes, "parcels will be collected from: ",recipient['RecipientAddress'], "on", dates[0])
shipment_request.collection_date = dates[0]
added_shipment = client.add_shipment(shipment_request)
client.book_shipments([added_shipment])
shipment_return = client.get_shipment(added_shipment)
label_pdf = client.get_labels(shipment_return.shipment_document_id)
label_pdf.download('./' + customer + '.pdf')

1 Answers

You do not know exactly how they store the data inside of their object, even if it looks like a dictionary. you could run print(dir(recipient_address)) and see what the "inside of the object looks like". once you get the output you start inspecting which attributes or methods may have the data you want. Of course, you should always follow the published contract for interacting withe these objects, as the implementation details can always change. I've examine the source code of this object published here

https://github.com/despatchbay/despatchbay-python-sdk/blob/master/despatchbay/despatchbay_entities.py#L169

It turns out that it doesn't use a dictionary. It looks like you are meant to just access the data via attributes like follows:

   recipient_address.company_name 
   recipient_address.street 
   recipient_address.locality
   recipient_address.town_city 
   recipient_address.county 
   recipient_address.postal_code
   recipient_address.country_code

I agree, their python sdk could use improved documentation..

Related