Skip to content

Instantly share code, notes, and snippets.

@whardier
Created March 7, 2012 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whardier/1990434 to your computer and use it in GitHub Desktop.
Save whardier/1990434 to your computer and use it in GitHub Desktop.
Timezone Dict Merge
#!/usr/bin/python
timezones_account_one= {
'United States of America': {
'Alaska': {
'America/Adak': 'Adak, Alaska, USA',
'America/Anchorage': 'Anchorage, Alaska, USA',
},
'Idaho': {
'America/Los_Angeles': 'Coeur\'d Alene, Idaho, USA',
},
'Hawaii': {
'Pacific/Honolulu': 'Kapolei, Hawaii, USA',
'Pacific/Honolulu': 'Kona, Hawaii, USA',
},
},
'Misc': {
'UTC': {
'UTC': 'UTC',
},
},
}
timezones_account_two = {
'United States of America': {
'Alaska': {
'America/Anchorage': 'Anchorage, Alaska, USA',
},
},
'Asia': {
'Phillipines': {
'Asia/Manila': 'Manila, Metro Manila, Phillipines',
},
},
'Misc': {
'UTC': {
'UTC': 'UTC',
},
},
}
timezones_personal = {
'United States of America': {
'Alaska': {
'America/Anchorage': 'Anchorage, Alaska, USA',
},
},
'Misc': {
'UTC': {
'UTC': 'UTC',
},
},
}
timezonedicts = (timezones_account_one, timezones_account_two, timezones_personal)
zone1set = set()
zone1set.update(*[x.keys() for x in timezonedicts])
zone2set = set()
for zone1 in zone1set:
zone2set.update(*[x.get(zone1, {}).keys() for x in timezonedicts])
timezones = {}
for zone1 in zone1set:
timezones.setdefault(zone1, {})
for zone2 in zone2set:
zone3set = [y for y in [x.get(zone1, {}).get(zone2, {}).items() for x in timezonedicts] if y]
if zone3set:
timezones[zone1].setdefault(zone2, set())
timezones[zone1][zone2].update(*zone3set)
import pprint
pprint.pprint(timezones, width=1, indent=2)
## RESULTS IN
{ 'Asia': { 'Phillipines': set([ ( 'Asia/Manila',
'Manila, Metro Manila, Phillipines')])},
'Misc': { 'UTC': set([ ( 'UTC',
'UTC')])},
'United States of America': { 'Alaska': set([ ( 'America/Adak',
'Adak, Alaska, USA'),
( 'America/Anchorage',
'Anchorage, Alaska, USA')]),
'Hawaii': set([ ( 'Pacific/Honolulu',
'Kona, Hawaii, USA')]),
'Idaho': set([ ( 'America/Los_Angeles',
"Coeur'd Alene, Idaho, USA")])}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment