python output different when printed to std out than stored in dict

355 Views Asked by At

I'm trying to do a zonetransfer with dnspython, and then store the A record's into a dictionary that i can pop values from later. See below:

import dns.zone
import dns.query
from dns.exception import DNSException
from dns.rdataclass import *
from dns.rdatatype import *

domain = "mydomain.com"
ns = '1.2.3.4'

try:
    zone = dns.zone.from_xfr(dns.query.xfr(ns, domain))
    print "Zone origin:", zone.origin
except DNSException, e:
    print e.__class__, e

for (name, ttl, rdata) in zone.iterate_rdatas('A'):
    record = { name : rdata }
    print name, rdata
    print record

why is the output when i use print is different than when i store the same variables name, rdata in a dictionary, and print that dict? See output below:

www 1.1.1.1
{<DNS name www>: <DNS IN A rdata: 1.1.1.1>}

I guess to clarify my question: How do I make the dict look like : { 'www' : '1.1.1.1' }

3

There are 3 best solutions below

0
On BEST ANSWER

This is happening because the contents of a dict are represented by calling repr on each of its keys/values, whereas printing an object directly will call str.

Example:

>>> class Fred:
...     def __repr__(self):
...         return "value returned by repr"
...     def __str__(self):
...         return "value returned by str"
...
>>> d = {1: Fred()}
>>> print d
{1: value returned by repr}
>>> print d[1]
value returned by str

There's not a whole lot you can do about this... You could write your own version of the classes you're trying to print and define __repr__ so it behaves identically to __str__. Or you could write a custom dict-printing function that renders the result of __str__ instead of __repr__. But there's no built-in way to force print somedict to use __str__ instead of __repr__.

>>> print "{" + ", ".join("{} : {}".format(repr(str(key)), repr(str(value))) for key, value in d.iteritems()) + "}"
{'1' : 'value returned by str'}
0
On

to add a value in a dictionary

d['key'] = 'value'
0
On

I know this is an old question, but came here because I just had the same issue. After reading Kevin's ansewr, I just tried replacing where I put the name of the answer, let's say "rdata" with "str(rdata)". That returned for me just the IP address, rather than the whole string including "DNS IN A rdata:"

hope it helps