Skip to content

Instantly share code, notes, and snippets.

@yee379
Last active February 15, 2023 15:32
Show Gist options
  • Save yee379/7037873 to your computer and use it in GitHub Desktop.
Save yee379/7037873 to your computer and use it in GitHub Desktop.
Determine a phone number's country and region.
import yaml
def locate_phone_number( s, country_code='1', region_code='650', region_digits=7, internal_digits=4, country_digits=10, locations={}, **kwargs ):
s = str(s)
# internal number
if len(s) == internal_digits:
return 'internal', None, s
# regional number
elif len(s) == region_digits:
s = country_code + region_code + s
elif len(s) == country_digits:
s = country_code + s
country = 'unknown'
region = 'unknown'
prev = locations
for i in xrange( len(s) ):
if s[i] in prev:
if 'country' in prev[s[i]]:
country = prev[s[i]]['country']
elif 'region' in prev[s[i]]:
region = prev[s[i]]['region']
prev = prev[s[i]]
else:
break
return country, region
if __name__ == "__main__":
locations = yaml.load( open('phone_number_tree.yaml','r') )
print locate_phone_number( '16501231234', locations=locations ) # ( 'USA', 'CA' )
print locate_phone_number( '85212312345', locations=locations ) # ( 'HKG', 'unknown' )
'1':
'2':
'0':
'1':
region: NJ
'2':
region: DC
'3':
region: CT
'5':
region: AL
'6':
region: WA
'7':
region: ME
'8':
region: ID
'9':
region: CA
'1':
'0':
region: TX
'2':
region: NY
'3':
region: CA
'4':
region: TX
'5':
region: PA
'6':
region: OH
'7':
region: IL
'8':
region: MN
'9':
region: IN
'2':
'4':
region: IL
'5':
region: LA
'8':
region: MS
'9':
region: GA
'3':
'1':
region: MI
'4':
region: OH
'9':
region: FL
'4':
'0':
region: MD
'2':
country: BHS
'6':
country: BRB
'8':
region: MI
'5':
'1':
region: AL
'2':
region: NC
'3':
region: WA
'4':
region: TX
'6':
region: AL
'6':
'0':
region: IN
'2':
region: WI
'4':
country: AIA
'7':
region: PA
'8':
country: ATG
'9':
region: MI
'7':
'0':
region: KY
'6':
region: VA
'8':
'1':
region: TX
'4':
country: VGB
'3':
'0':
'1':
region: MD
'2':
region: DE
'3':
region: CO
'4':
region: WV
'5':
region: FL
'7':
region: WY
'8':
region: NE
'9':
region: IL
'1':
'0':
region: CA
'2':
region: IL
'3':
region: MI
'4':
region: MO
'5':
region: NY
'6':
region: KS
'7':
region: IN
'8':
region: LA
'9':
region: IA
'2':
'0':
region: MN
'1':
region: FL
'3':
region: CA
'5':
region: TX
'3':
'0':
region: OH
'4':
region: AL
'6':
region: NC
'7':
region: LA
'9':
region: MA
'4':
'0':
country: VIR
'5':
country: CYM
'7':
region: NY
'5':
'1':
region: MA
'2':
region: FL
'6':
'0':
region: WA
'1':
region: TX
'8':
'6':
region: FL
'4':
'0':
'1':
region: RI
'2':
region: NE
'4':
region: GA
'5':
region: OK
'6':
region: MT
'7':
region: FL
'8':
region: CA
'9':
region: TX
'1':
'0':
region: MD
'2':
region: PA
'3':
region: MA
'4':
region: WI
'5':
region: CA
'7':
region: MO
'9':
region: OH
'2':
'3':
region: TN
'4':
region: CA
'5':
region: WA
'3':
'0':
region: TX
'2':
region: TX
'4':
region: VA
'5':
region: UT
'4':
'0':
region: OH
'1':
country: BMU
'3':
region: MD
'7':
region: IL
'6':
'9':
region: TX
'7':
'3':
country: GRD
'8':
region: GA
'9':
region: AK
'8':
'0':
region: AZ
'4':
region: PA
'5':
'0':
'1':
region: AK
'2':
region: KY
'3':
region: OR
'4':
region: LA
'5':
region: NM
'7':
region: MN
'8':
region: MA
'9':
region: WA
'1':
'0':
region: CA
'2':
region: TX
'3':
region: OH
'5':
region: IA
'6':
region: NY
'7':
region: MI
'8':
region: NY
'2':
'0':
region: AZ
'3':
'0':
region: CA
'4':
'0':
region: VA
'1':
region: OR
'5':
'1':
region: NJ
'9':
region: CA
'6':
'1':
region: FL
'2':
region: CA
'3':
region: IA
'7':
region: OH
'7':
'0':
region: PA
'1':
region: VA
'3':
region: MO
'4':
region: IN
'5':
region: NM
'8':
'0':
region: OK
'5':
region: NY
'6':
region: MI
'6':
'0':
'1':
region: MS
'2':
region: AZ
'3':
region: NH
'5':
region: SD
'6':
region: KY
'7':
region: NY
'8':
region: WI
'9':
region: NJ
'1':
'0':
region: PA
'2':
region: MN
'4':
region: OH
'5':
region: TN
'6':
region: MI
'7':
region: MA
'8':
region: IL
'9':
region: CA
'2':
'0':
region: KS
'3':
region: AZ
'6':
region: CA
'3':
'0':
region: IL
'1':
region: NY
'6':
region: MO
'4':
'1':
region: IA
'6':
region: NY
'9':
country: TCA
'5':
'0':
region: CA
'1':
region: MN
'6':
'0':
region: MO
'1':
region: CA
'2':
region: MS
'4':
country: MSR
'7':
'0':
country: MNP
'1':
country: GUM
'8':
region: GA
'8':
'1':
region: WV
'2':
region: TX
'4':
country: ASM
'9':
region: FL
'7':
'0':
'1':
region: ND
'2':
region: NV
'3':
region: VA
'4':
region: NC
'6':
region: GA
'7':
region: CA
'8':
region: IL
'1':
'2':
region: IA
'3':
region: TX
'4':
region: CA
'5':
region: WI
'6':
region: NY
'7':
region: PA
'8':
region: NY
'9':
region: CO
'2':
'0':
region: CO
'4':
region: PA
'7':
region: FL
'3':
'0':
region: IL
'1':
region: TN
'2':
region: NJ
'4':
region: MI
'4':
'0':
region: OH
'5':
'4':
region: FL
'7':
region: VA
'8':
country: LCA
'6':
'0':
region: CA
'2':
region: GA
'3':
region: MN
'5':
region: IN
'7':
country: DMA
'9':
region: MO
'7':
'0':
region: GA
'2':
region: FL
'3':
region: IL
'4':
region: MA
'5':
region: NV
'9':
region: IL
'8':
'1':
region: MA
'4':
country: VCT
'5':
region: KS
'6':
region: FL
'8':
'0':
'1':
region: UT
'2':
region: VT
'3':
region: SC
'4':
region: VA
'5':
region: CA
'6':
region: TX
'8':
region: HI
'9':
country: DOM
'1':
'0':
region: MI
'2':
region: IN
'3':
region: FL
'4':
region: PA
'5':
region: IL
'6':
region: MO
'7':
region: TX
'8':
region: CA
'2':
'8':
region: NC
'3':
'0':
region: TX
'1':
region: CA
'2':
region: TX
'4':
'3':
region: SC
'5':
region: NY
'7':
region: IL
'8':
region: NJ
'5':
'0':
region: FL
'6':
region: NJ
'7':
region: MA
'8':
region: CA
'9':
region: KY
'6':
'0':
region: CT
'2':
region: NJ
'3':
region: FL
'4':
region: SC
'5':
region: TN
'8':
country: TTO
'9':
country: KNA
'7':
'0':
region: AK
'6':
country: JAM
'8':
region: PA
'9':
'0':
'1':
region: TN
'3':
region: TX
'4':
region: FL
'6':
region: MI
'7':
region: AK
'8':
region: NJ
'9':
region: CA
'1':
'0':
region: NC
'2':
region: GA
'3':
region: KS
'4':
region: NY
'5':
region: TX
'6':
region: CA
'7':
region: NY
'8':
region: OK
'9':
region: NC
'2':
'0':
region: WI
'5':
region: CA
'8':
region: AZ
'3':
'1':
region: TN
'6':
region: TX
'7':
region: OH
'4':
'0':
region: TX
'1':
region: FL
'7':
region: MI
'9':
region: CA
'5':
'1':
region: CA
'2':
region: MN
'4':
region: FL
'6':
region: TX
'7':
'0':
region: CO
'1':
region: OR
'2':
region: TX
'3':
region: NJ
'8':
region: MA
'9':
region: TX
'8':
'0':
region: NC
'5':
region: LA
'9':
region: MI
country: USA
'2':
'0':
country: EGY
'1':
'1':
country: SSD
'2':
country: ESH
'3':
country: DZA
'6':
country: TUN
'8':
country: LBY
'2':
'0':
country: GMB
'1':
country: SEN
'2':
country: MRT
'3':
country: MLI
'4':
country: GIN
'5':
country: CIV
'6':
country: BFA
'7':
country: NER
'8':
country: TGO
'9':
country: BEN
'3':
'0':
country: MUS
'1':
country: LBR
'2':
country: SLE
'3':
country: GHA
'4':
country: NGA
'5':
country: TCD
'6':
country: CAF
'7':
country: CMR
'8':
country: CPV
'9':
country: STP
'4':
'0':
country: GNQ
'1':
country: GAB
'2':
country: COG
'3':
country: COD
'4':
country: AGO
'5':
country: GNB
'6':
country: IOT
'8':
country: SYC
'9':
country: SDN
'5':
'0':
country: RWA
'1':
country: ETH
'2':
country: SOM
'3':
country: DJI
'4':
country: KEN
'5':
country: TZA
'6':
country: UGA
'7':
country: BDI
'8':
country: MOZ
'6':
'0':
country: ZMB
'1':
country: MDG
'2':
country: REU
'3':
country: ZWE
'4':
country: NAM
'5':
country: MWI
'6':
country: LSO
'7':
country: BWA
'8':
country: SWZ
'9':
country: COM
'7':
country: ZAF
'9':
'0':
country: SHN
'1':
country: ERI
'7':
country: ABW
'8':
country: FRO
'9':
country: GRL
'3':
'0':
country: GRC
'1':
country: NLD
'2':
country: BEL
'3':
country: FRA
'4':
country: ESP
'5':
'0':
country: GIB
'1':
country: PRT
'2':
country: LUX
'3':
country: IRL
'4':
country: ISL
'5':
country: ALB
'6':
country: MLT
'7':
country: CYP
'8':
country: FIN
'9':
country: BGR
'6':
country: HUN
'7':
'0':
country: LTU
'1':
country: LVA
'2':
country: EST
'3':
country: MDA
'4':
country: ARM
'5':
country: BLR
'6':
country: AND
'7':
country: MCO
'8':
country: SMR
'8':
'0':
country: UKR
'5':
country: HRV
'6':
country: SVN
'7':
country: BIH
'9':
country: MKD
'9':
'0':
'6':
country: VAT
country: ITA
'4':
'0':
country: ROU
'1':
country: CHE
'2':
'0':
country: CZE
'1':
country: SVK
'3':
country: LIE
'3':
country: AUT
'4':
country: GBR
'5':
country: DNK
'6':
country: SWE
'7':
country: NOR
'8':
country: POL
'9':
country: DEU
'5':
'0':
'0':
country: SGS
'1':
country: BLZ
'2':
country: GTM
'3':
country: SLV
'4':
country: HND
'5':
country: NIC
'6':
country: CRI
'7':
country: PAN
'8':
country: SPM
'9':
country: HTI
'1':
country: PER
'2':
country: MEX
'3':
country: CUB
'4':
country: ARG
'5':
country: BRA
'6':
country: CHL
'7':
country: COL
'8':
country: VEN
'9':
'0':
country: MAF
'1':
country: BOL
'2':
country: GUY
'3':
country: ECU
'4':
country: GUF
'5':
country: PRY
'6':
country: MTQ
'7':
country: SUR
'8':
country: URY
'9':
country: BES
'6':
'0':
country: MYS
'1':
country: CCK
'2':
country: IDN
'3':
country: PHL
'4':
country: NZL
'5':
country: SGP
'6':
country: THA
'7':
'0':
country: TLS
'2':
country: NFK
'3':
country: BRN
'4':
country: NRU
'5':
country: PNG
'6':
country: TON
'7':
country: SLB
'8':
country: VUT
'9':
country: FJI
'8':
'0':
country: PLW
'1':
country: WLF
'2':
country: COK
'3':
country: NIU
'5':
country: WSM
'6':
country: KIR
'7':
country: NCL
'8':
country: TUV
'9':
country: PYF
'9':
'0':
country: TKL
'1':
country: FSM
'2':
country: MHL
'7':
country: RUS
'8':
'1':
country: JPN
'2':
country: KOR
'4':
country: VNM
'5':
'0':
country: PRK
'2':
country: HKG
'3':
country: MAC
'5':
country: KHM
'6':
country: LAO
'6':
country: CHN
'7':
'0':
country: PCN
'8':
'0':
country: BGD
'6':
country: TWN
'9':
'0':
country: TUR
'1':
country: IND
'2':
country: PAK
'3':
country: AFG
'4':
country: LKA
'5':
country: MMR
'6':
'0':
country: MDV
'1':
country: LBN
'2':
country: JOR
'3':
country: SYR
'4':
country: IRQ
'5':
country: KWT
'6':
country: SAU
'7':
country: YEM
'8':
country: OMN
'7':
'1':
country: ARE
'2':
country: ISR
'3':
country: BHR
'4':
country: QAT
'5':
country: BTN
'6':
country: MNG
'7':
country: NPL
'8':
country: IRN
'9':
'2':
country: TJK
'3':
country: TKM
'4':
country: AZE
'5':
country: GEO
'6':
country: KGZ
'8':
country: UZB
@nickscott-jomablue
Copy link

You have 61 listed as CCK; it is actually the country code for several Australian regions (including the country itself which represents the majority of numbers using 61).
Cocos Islands has a population of 596 versus 23 million in Australia.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment