Python Forum
How to print the content of the object? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to print the content of the object? (/thread-19949.html)



How to print the content of the object? - janaki26794 - Jul-21-2019

I have a python file in which I have written the following data

@dataclass
class Email:
    resource_uri : str
    address      : str
    person       : str
    time         : str
    origin       : str
    primary      : bool
    active       : bool

class DataTracker:
    """
    A class for interacting with the IETF DataTracker.
    """
    def __init__(self):
        self.session      = requests.Session()
        self.base_url     = "https://datatracker.ietf.org"


    def __del__(self):
        self.session.close()

    def email(self, email: str):
        """
        Lookup information about an email address in the datatracker.

        Parameters:
           email : the email address to lookup

        Returns:
            An Email object
        """
        response = self.session.get(self.base_url + "/api/v1/person/email/" + email + "/", verify=True)
        if response.status_code == 200:
            return Pavlova().from_mapping(response.json(), Email)
        else:
            return None
How do I print the details of an object of email type and a particular attribute say "address" of the email??

I have written the code snippet but I am not sure how to print the result as it brought upon errors

import datatracker

##Creating an object of Datatracker type

datatracker_object=datatracker.DataTracker()

## calling the email function and storing in object
email_object=datatracker_object.email('https://datatracker.ietf.org/api/v1/person/email/[email protected]/')

print(email_object['address'])
Kindly help.Thanks


RE: How to print the content of the object? - Yoriz - Jul-21-2019

print(email_object.address)
Should work if

Pavlova().from_mapping(response.json(), Email)
is returning a instance of Email.


RE: How to print the content of the object? - janaki26794 - Jul-21-2019

I did the same,but it returns an error like this: AttributeError: 'NoneType' object has no attribute 'address'..It in short says that the value is null, but it is not


RE: How to print the content of the object? - Yoriz - Jul-21-2019

        if response.status_code == 200:
            return Pavlova().from_mapping(response.json(), Email)
        else:
            return None
When the if statement is not True it will return None


RE: How to print the content of the object? - Glowyee - Jul-29-2019

Hi @janaki26794,

Here is how I worked around it. Save the result of the mapping in a variable then call the attribute on the variable like shown below.

response = self.session.get(self.base_url + section + area_id + "/", verify=True)
        if response.status_code == 200:
           [b] result = Pavlova().from_mapping(response.json(), Group)[/b]
            [b]return result.name[/b]
        else:
            return None
Seems we are working on the same project Big Grin message me if you want to. Goodluck!