Skip to content

Commit eeb75d9

Browse files
authored
Use print() function in both Python 2 and Python 3
Legacy __print__ statements are syntax errors in Python 3 but __print()__ function works as expected in both Python 2 and Python 3. There are 250 days until Python 2 end of life.
1 parent ce51690 commit eeb75d9

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

example/confbuilder.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
# This is a hack I use to generate my tinydns configuration
23
# It serves as e test for converting from Perl Net::IP to
34
# Python and IPy
@@ -15,11 +16,11 @@
1516
'ns.dorsch.org': '195.143.234.25',
1617
'ns.c0re.jp': '217.6.214.130'}
1718

18-
print "# *** nameservers ***"
19-
for x in ns.keys():
20-
print "=%s:%s" % (x, ns[x])
19+
print("# *** nameservers ***")
20+
for x in ns:
21+
print("=%s:%s" % (x, ns[x]))
2122

22-
print "\n# *** domains ***"
23+
print("\n# *** domains ***")
2324

2425
fd = open('domains')
2526

@@ -28,13 +29,13 @@
2829
if x[-1] == '\n':
2930
x = x[:-1]
3031
(domain, owner) = x.split(':')
31-
print "'%s:Contact for this domain is %s" % (domain, owner)
32-
for y in ns.keys():
33-
print ".%s::%s" % (domain, y)
32+
print("'%s:Contact for this domain is %s" % (domain, owner))
33+
for y in ns:
34+
print(".%s::%s" % (domain, y))
3435

3536
fd.close()
3637

37-
print "\n# *** Networks ***"
38+
print("\n# *** Networks ***")
3839

3940
fd = open('networks')
4041
ip6map = {}
@@ -47,12 +48,12 @@
4748
if len(x) > 0 and x[0] != '#':
4849
nets = x.split(',')
4950
name = nets.pop(0)
50-
print "# Network: %s" % name
51+
print("# Network: %s" % name)
5152
for y in nets:
5253
ip = IPy.IP(y)
53-
print "# Address range: %s (%s), %d addresses" % (ip.strCompressed(), ip.iptype(), ip.len())
54-
print "=net.%s:%s" % (name, ip.net())
55-
print "=broadcast.%s:%s" % (name, ip.broadcast())
54+
print("# Address range: %s (%s), %d addresses" % (ip.strCompressed(), ip.iptype(), ip.len()))
55+
print("=net.%s:%s" % (name, ip.net()))
56+
print("=broadcast.%s:%s" % (name, ip.broadcast()))
5657

5758
if ip.version() == 4:
5859
for z in ip:
@@ -61,14 +62,14 @@
6162
rmap[z.int()] = z.strBin() + "." + name
6263
else:
6364
# IPv6
64-
for z in ns.keys():
65+
for z in ns:
6566
for v in ip.reverseName():
66-
print ".%s::%s" % (v, z)
67+
print(".%s::%s" % (v, z))
6768
ip6map[ip.strFullsize(0)] = name
6869

6970
fd.close()
7071

71-
print "\n# *** hosts ***"
72+
print("\n# *** hosts ***")
7273

7374
fd = open('hosts')
7475

@@ -77,12 +78,12 @@
7778
x = x[:-1]
7879
if x != '' and x[0] != '#':
7980
if "@Z'.".find(x[0]) >= 0:
80-
print x
81+
print(x)
8182
else:
8283
if "=+'".find(x[0]) >= 0:
8384
i = x.split(':')
8485
rmap[IPy.IP(i[1]).int()] = ''
85-
print x
86+
print(x)
8687
else:
8788
x = x[1:]
8889
x += '||||'
@@ -107,35 +108,33 @@
107108
ip = IPy.IP(y)
108109
if ip.version() == 4:
109110
# IPv4 is easy
110-
if not nmap.has_key(ip.int()):
111-
print >>sys.stderr, "*** warning: no network for %s (%s) - ignoring" % (y, name)
112-
print "# no network for %s (%s)" % (y, name)
111+
if ip.int() not in nmap:
112+
print("*** warning: no network for %s (%s) - ignoring" % (y, name), file=sys.stderr)
113+
print("# no network for %s (%s)" % (y, name))
113114
else:
114-
print "=%s.%s:%s" % (name, nmap[ip.int()], y)
115-
print "'%s.%s:Host contact is %s" % (name, nmap[ip.int()], admin)
115+
print("=%s.%s:%s" % (name, nmap[ip.int()], y))
116+
print("'%s.%s:Host contact is %s" % (name, nmap[ip.int()], admin))
116117
rmap[ip.int()] = ''
117118
for z in aliases:
118-
print "+%s:%s" % (z, ip)
119-
print "'%s:Host contact is %s" % (z, admin)
119+
print("+%s:%s" % (z, ip))
120+
print("'%s:Host contact is %s" % (z, admin))
120121
else:
121122
#IPv6 here
122123
net = ip.strFullsize(0)
123124
net = net[:19] + ':0000:0000:0000:0000'
124-
if ip6map.has_key(net):
125-
print >>sys.stderr, "*** warning: no network for %s (%s) - ignoring" % (ip, name)
126-
print "# no network for %s (%s) - ignoring" % (ip, name)
125+
if net in ip6map:
126+
print("*** warning: no network for %s (%s) - ignoring" % (ip, name), file=sys.stderr)
127+
print("# no network for %s (%s) - ignoring" % (ip, name))
127128
else:
128-
print "6%s.%s:%s"; (name, ip6map[net], ip.strHex()[2:])
129+
print("6%s.%s:%s"); (name, ip6map[net], ip.strHex()[2:])
129130
for z in aliases:
130-
print "3%s:%s" % (name, ip.strHex()[2:])
131-
print "'%s:Host contact is %s" % (name, admin)
131+
print("3%s:%s" % (name, ip.strHex()[2:]))
132+
print("'%s:Host contact is %s" % (name, admin))
132133

133134
fd.close()
134135

135-
print "\n# *** reverse lookup ***"
136-
k = nmap.keys()
137-
k.sort()
138-
for x in k:
139-
if rmap.has_key(x) and rmap[x] != '':
140-
print "=%s:%s" % (rmap[x], str(IPy.IP(x)))
136+
print("\n# *** reverse lookup ***")
137+
for x in sorted(nmap):
138+
if rmap.get(x):
139+
print("=%s:%s" % (rmap[x], str(IPy.IP(x))))
141140

0 commit comments

Comments
 (0)