Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f6d5344

Browse files
committed
A rewrite for better Python-ish style
1 parent e41d00b commit f6d5344

1 file changed

Lines changed: 35 additions & 24 deletions

File tree

Tools/world/world

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,45 @@
1-
#! /depot/sundry/plat/bin/python
2-
#
3-
# Note: you may have to edit the top line in this file.
1+
#! /usr/bin/env python
42
#
53
# Usage: world addr1 [addr2 ...]
64
#
7-
# $Id$
85

96
# This little script will take an Internet address of the form
107
# [email protected] and will print out where in the world that
118
# message originated from. Its pretty dumb in that it just matches
129
# the `domain' part against a hard-coded list, which can probably
1310
# change fairly quickly given the world's political fluidity.
1411

12+
# TBD: it would be cool if this script could update itself. I can't
13+
# remember where I got the original list of top level domain
14+
# abbreviations -- probably from the InterNIC. So far I haven't hit
15+
# any that this script can't resolve, so I assume they don't change
16+
# too frequently.
17+
1518
import sys
16-
prog = sys.argv[0]
17-
del sys.argv[0]
18-
if not sys.argv:
19-
print "No addresses provided.\nUsage:", prog, "addr1 [addr2 ...]\n"
19+
import string
20+
21+
22+
def usage(msg=None, exit=0):
23+
if msg: print msg
24+
print 'Usage:', sys.argv[0], 'addr [addr ...]'
25+
sys.exit(exit)
26+
27+
28+
def resolve(rawaddr):
29+
parts = string.splitfields(rawaddr, '.')
30+
if not len(parts):
31+
print 'No top-level domain in:', rawaddr
32+
return
33+
addr = parts[-1]
34+
if nameorg.has_key(addr):
35+
print addr, 'is from a USA', nameorg[addr], 'organization'
36+
elif country.has_key(addr):
37+
print addr, 'originated from', country[addr]
38+
else:
39+
print 'Where in the world is', addr, '?'
2040

2141

42+
2243
# The mappings
2344
nameorg = {
2445
"arpa": "Arpanet",
@@ -28,10 +49,11 @@ nameorg = {
2849
"mil": "military",
2950
"net": "networking",
3051
"org": "non-commercial",
31-
"int": "international"
52+
"int": "international",
3253
}
3354

3455

56+
3557
country = {
3658
"ag": "Antigua and Barbuda",
3759
"al": "Albania",
@@ -123,21 +145,10 @@ country = {
123145
"vi": "Virgin Islands",
124146
"yu": "Yugoslavia",
125147
"za": "South Africa",
126-
"zw": "Zimbabwe"
148+
"zw": "Zimbabwe",
127149
}
128150

129-
import string
130-
131-
while sys.argv:
132-
rawaddr = sys.argv[0]
133-
del sys.argv[0]
134151

135-
components = string.splitfields(rawaddr, ".")
136-
addr = components[-1]
137-
138-
if nameorg.has_key(addr):
139-
print addr, "is from a USA", nameorg[addr], "organization"
140-
elif country.has_key(addr):
141-
print addr, "originated from", country[addr]
142-
else:
143-
print "I have no idea where", addr, "came from!"
152+
153+
if __name__ == '__main__':
154+
map(resolve, sys.argv[1:])

0 commit comments

Comments
 (0)