From b4c5a26a6ef574c8af50936c1e410da2c5db0feb Mon Sep 17 00:00:00 2001 From: Giovanni Campagna Date: Wed, 2 Jan 2013 19:16:08 +0100 Subject: Port to Python 3 Part of the GNOME 3.8 Goal https://live.gnome.org/GnomeGoals/Python3Porting https://bugzilla.gnome.org/show_bug.cgi?id=690975 --- data/check-observations.py | 2 +- data/locations_diff.py | 20 +++++------ data/update-locations.py | 84 ++++++++++++++++++++++------------------------ 3 files changed, 52 insertions(+), 54 deletions(-) (limited to 'data') diff --git a/data/check-observations.py b/data/check-observations.py index 8bd7a1d..aeda3c8 100644 --- a/data/check-observations.py +++ b/data/check-observations.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 from ftplib import FTP import datetime diff --git a/data/locations_diff.py b/data/locations_diff.py index 4f3d311..9b75517 100755 --- a/data/locations_diff.py +++ b/data/locations_diff.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # # locations_diff.py # @@ -27,7 +27,7 @@ except: old_locations = old_dom.getElementsByTagName ('location') new_locations = new_dom.getElementsByTagName ('location') -print 'There are %i new locations' % (len (new_locations) - len (old_locations)) +print('There are %i new locations' % (len (new_locations) - len (old_locations))) old_locations_dict = {} new_locations_dict = {} @@ -46,14 +46,14 @@ for location in new_locations: location_name = node.childNodes[0].nodeValue elif node.nodeType == 1 and node.tagName == 'code': location_code = node.childNodes[0].nodeValue - if not old_locations_dict.has_key (location_code): - print 'New Location %s - %s' % (location_code, location_name) + if location_code not in old_locations_dict: + print('New Location %s - %s' % (location_code, location_name)) elif old_locations_dict[location_code] != location_name: - print 'Location %s changed name %s => %s' % (location_code, - old_locations_dict[location_code], location_name) + print('Location %s changed name %s => %s' % (location_code, + old_locations_dict[location_code], location_name)) new_locations_dict[location_code] = location_name -for location in old_locations_dict.keys(): - if not new_locations_dict.has_key (location): - print 'Location Removed %s - %s' % (location, - old_locations_dict[location]) +for location in list(old_locations_dict.keys()): + if location not in new_locations_dict: + print('Location Removed %s - %s' % (location, + old_locations_dict[location])) diff --git a/data/update-locations.py b/data/update-locations.py index 386e755..c1daef7 100644 --- a/data/update-locations.py +++ b/data/update-locations.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # -*- coding: utf-8 -*- import codecs @@ -8,12 +8,10 @@ import os import re import sqlite3 import sys -import urllib +import urllib.request, urllib.parse, urllib.error from xml.dom import minidom from xml.sax import saxutils - -# Magic incantation to tell python to let us output UTF-8 -sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout) +from functools import reduce # The database... db = sqlite3.connect('locationdb.sqlite') @@ -80,7 +78,7 @@ def printComment(indent, comment, keep_newlines=False): prefix = '%s' % (indent, comment) + print('%s' % (indent, comment)) return while len(comment) > width or comment.find('\n') != -1: @@ -91,13 +89,13 @@ def printComment(indent, comment, keep_newlines=False): brk == comment.find(' ') if brk == -1: break - print '%s%s' % (prefix, comment[:brk]) + print('%s%s' % (prefix, comment[:brk])) prefix = '%s ' % indent brk += 1 comment = comment[brk:] if len(comment): - print '%s%s' % (prefix, comment) - print '%s -->' % indent + print('%s%s' % (prefix, comment)) + print('%s -->' % indent) # other helpers def getFipsCodes(node, container): @@ -137,10 +135,10 @@ class Timezones: self.zones = [Timezone(z) for z in getChildrenByName(node, 'timezone')] def print_xml(self, indent): - print '%s' % indent + print('%s' % indent) for zone in self.zones: zone.print_xml(indent + ' ') - print '%s' % indent + print('%s' % indent) class Timezone: def __init__(self, node): @@ -152,7 +150,7 @@ class Timezone: def print_xml(self, indent): if self.name is not None or len(self.obsoletes): - print '%s' % (indent, self.id) + print('%s' % (indent, self.id)) if self.comment is not None: printComment(indent + ' ', self.comment) if self.name is not None: @@ -160,12 +158,12 @@ class Timezone: msgctxt=' msgctxt="Timezone"' else: msgctxt='' - print '%s <_name%s>%s' % (indent, msgctxt, self.name) + print('%s <_name%s>%s' % (indent, msgctxt, self.name)) for obs in self.obsoletes: - print '%s %s' % (indent, obs) - print '%s' % indent + print('%s %s' % (indent, obs)) + print('%s' % indent) else: - print '%s' % (indent, self.id) + print('%s' % (indent, self.id)) class LocBase: def __init__(self, parent, arg): @@ -204,22 +202,22 @@ class LocBase: def print_xml(self, indent): if self.iso_code is not None: - print '%s %s' % (indent, self.iso_code) + print('%s %s' % (indent, self.iso_code)) if self.fips_codes is not None: for value in self.fips_codes: - print '%s %s' % (indent, value) + print('%s %s' % (indent, value)) if self.pref_lang is not None: - print '%s %s' % (indent, self.pref_lang) + print('%s %s' % (indent, self.pref_lang)) if self.timezones is not None: self.timezones.print_xml(indent + ' ') if self.tz_hint is not None: - print '%s %s' % (indent, self.tz_hint) + print('%s %s' % (indent, self.tz_hint)) if self.zone is not None: - print '%s %s' % (indent, self.zone) + print('%s %s' % (indent, self.zone)) if self.radar is not None: - print '%s %s' % (indent, self.radar) + print('%s %s' % (indent, self.radar)) if self.coordinates is not None: - print '%s %s' % (indent, self.coordinates) + print('%s %s' % (indent, self.coordinates)) for item in self.contents: item.print_xml(indent + ' ') @@ -243,7 +241,7 @@ class LocBase: else: msgctxt = '' - print '%s <_name%s>%s' % (indent, msgctxt, saxutils.escape(self.name)) + print('%s <_name%s>%s' % (indent, msgctxt, saxutils.escape(self.name))) def station_prefixes(self): return reduce(set.__or__, [x.station_prefixes() for x in self.contents], set()) @@ -253,12 +251,12 @@ class Region(LocBase): LocBase.__init__(self, None, elt) def print_xml(self, indent): - print '%s' % indent + print('%s' % indent) if self.comment is not None: printComment(indent + ' ', self.comment) self.print_name(indent) LocBase.print_xml(self, indent) - print '%s' % indent + print('%s' % indent) class Country(LocBase): def __init__(self, elt): @@ -280,7 +278,7 @@ class Country(LocBase): self.in_name = self.name def print_xml(self, indent): - print '%s' % indent + print('%s' % indent) if self.comment is not None: printComment(indent + ' ', self.comment) self.print_name(indent) @@ -304,7 +302,7 @@ class Country(LocBase): comment += ', %s' % city.name printComment(indent + ' ', comment, True) LocBase.print_xml(self, indent) - print '%s' % indent + print('%s' % indent) class State(LocBase): def __init__(self, parent, elt): @@ -313,12 +311,12 @@ class State(LocBase): self.comment = 'A state/province/territory in %s' % self.parent.name def print_xml(self, indent): - print '%s' % indent + print('%s' % indent) if self.comment is not None: printComment(indent + ' ', self.comment) self.print_name(indent) LocBase.print_xml(self, indent) - print '%s' % indent + print('%s' % indent) class City(LocBase): def __init__(self, arg): @@ -361,7 +359,7 @@ class City(LocBase): else: self.comment = 'A city in %s' % self.parent.in_name - print '%s' % indent + print('%s' % indent) comment = self.comment or '' if len(self.name_comment): if len(comment): @@ -373,10 +371,10 @@ class City(LocBase): if len(comment): printComment(indent + ' ', comment, True) self.print_name(indent) - print '%s %s' % (indent, self.coordinates) + print('%s %s' % (indent, self.coordinates)) for item in self.contents: item.print_xml(indent + ' ', self) - print '%s' % indent + print('%s' % indent) class Location(LocBase): def __init__(self, arg): @@ -395,7 +393,7 @@ class Location(LocBase): self.comment = station_comments[self.code] def print_xml(self, indent, city=None): - print '%s' % indent + print('%s' % indent) name = self.name if city is not None: if name.startswith("%s, " % city.name): @@ -408,10 +406,10 @@ class Location(LocBase): name = name[:-len(city.name) - 2] #if self.comment is not None: # print '%s ' % (indent, self.comment) - print '%s %s' % (indent, saxutils.escape(name)) - print '%s %s' % (indent, self.code) + print('%s %s' % (indent, saxutils.escape(name))) + print('%s %s' % (indent, self.code)) LocBase.print_xml(self, indent) - print '%s' % indent + print('%s' % indent) def station_prefixes(self): return set([self.code[:2]]) @@ -664,7 +662,7 @@ def dist_from_station(city, station, cmpcity): return dist observations_url = os.getenv('OBSERVATIONS_URL') or 'http://gnome.org/~danw/observations.txt' -observations = urllib.urlopen(observations_url) +observations = urllib.request.urlopen(observations_url) recent = [obs.rstrip() for obs in observations.readlines()] observations.close() @@ -775,7 +773,7 @@ for id in cities: if city.country_code != "" and city.country_code in fips_codes: city.parent = fips_codes[city.country_code] else: - print "Could not find container for city %s in %s" % (city.name, city.country_code); + print("Could not find container for city %s in %s" % (city.name, city.country_code)); continue city.parent.contents.append(city) @@ -831,9 +829,9 @@ for country_code in fips_codes: for code in fips_codes: fips_codes[code].contents.sort() -print '' -print '' -print '' +print('') +print('') +print('') for region in regions: region.contents.sort() region.print_xml(' ') @@ -842,4 +840,4 @@ if len(recent): for station_code in recent: comment += '%s ' % station_code printComment('', comment, True) -print '' +print('') -- cgit v1.2.1