Python Forum
How to print the content of the object?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print the content of the object?
#1
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
Reply
#2
print(email_object.address)
Should work if

Pavlova().from_mapping(response.json(), Email)
is returning a instance of Email.
Reply
#3
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
Reply
#4
        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
Reply
#5
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print all method and property of list object engmoh 4 2,831 Oct-26-2019, 05:33 PM
Last Post: engmoh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020