Skip to main content

Python version of Google's common library for parsing, formatting, storing and validating international phone numbers.

Project description

phonenumbers Python Library

This is a Python port of libphonenumber, originally from:
http://code.google.com/p/libphonenumber/.

Original Java code is Copyright (C) 2009-2011 The Libphonenumber Authors

===========================
phonenumbers Installation
===========================

Install using setup.py:
$ tar xfz phonenumbers-<version>.tar.gz
$ cd phonenumbers-<version>
$ python setup.py build
$ sudo python setup.py install # or su first


===========================
Running Tests
===========================

With phonenumbers on the Python path, run:
$ python -m testwrapper


===========================
Auto-Generating Python Code
===========================

The code within the phonenumbers/data subdirectory is automatically
generated from the master XML metadata file (../resources/PhoneNumberMetadata.xml).
Similarly, the code in the phonenumbers/geodata subdirectory is automatically
generated from the files in ../resources/geocoding/

The script buildmetadatafromxml.py performs the first autogeneration.
Run it with:
python buildmetadatafromxml.py ../resources/PhoneNumberMetaData.xml phonenumbers/data .

This will:
- Create the phonenumbers/data/ directory if not present.
- Create a collection of files, one for each region code:
phonenumbers/data/region_<code>.py
Each file contains the Python constructors for the metadata for that
region.
- Create a file phonenumbers/data/__init__.py which
accumulates all of the per-region files.

The script buildgeocodingdata.py performs the second autogeneration.
python buildgeocodingdata.py ../resources/geocoding phonenumbers/geodata/__init__.py

This approach results in a large set of Python files, but makes it easy
to apply local fixes to the formatting metadata.


===========================
Library Developer Internals
===========================

The Python code is derived from the original Java code, and
mostly sticks to the structure of that code to make it easier
to include future changes to the upstream code.

However, there are a number of differences:
- Naming conventions are converted to Python standards; in
particular, method names are connected_with_underscores
rather than beingInCamelCase.
- The PhoneNumber and PhoneMetadata classes are written by hand
based on the Java code and the protocol buffer definitions,
rather than by using the the Python protocol buffer library.
This makes the mapping to the Java code easier to follow, and
allows for the custom modifications that have been made to
the base protocol buffer. Attribute values of None are used
to indicate that a particular (optional) attribute is not
present (instead of hasAttribute() methods).
- The Java PhoneNumberUtil class was a singleton, and so its
contents are included at the top level in phonenumberutil.py.
Static methods from the PhoneNumberUtil class thus become
functions in phonenumberutil.py; private and package methods
get a leading underscore in their name.
- Accessor functions (setAttribute() and getAttribute() are
avoided, and direct access to attributes is used instead.
- Methods named get_something_from(object) are typically renamed
to something_from(object).
- The format() methods in PhoneNumberUtil were renamed to
format_number() to avoid clashing with the Python built-in
format().
- The internals of phonenumberutil.py do not have logging.
- The Python version is less concerned with speed and size
optimization than the Java version (as Python code is more likely
to run on a server platform and less likely to run on an
embedded/smartphone platform).

Much of the functionality of this library depends on regular
expressions, so it's worth highlighting the translation between
Java and Python regexps:
- Java replacement group references are "$1 $2" etc, Python's are
"\1 \2" etc.
- Java Matcher(x).lookingAt() translates to Python re_obj.match(x)
- Java Matcher(x).find() translates to Python m = re.search(x).
- Java Matcher(x).matches() translates to Python m = re_obj.match(x)
together with a check that m.end() == len(x).
The last of these is encapsulated in the fullmatch() function in
re_util.py.


===========================
Release Procedure
===========================

Standard Python v2.x version:
- Ensure that HISTORY file is up-to-date.
- Set the __version__ field in phonenumbers/__init__.py
- Check that the list of symbols in phonenumbers/__init__.py __all__ is
up to date. The allcheck.py script helps with this.
- Check that the unit tests all run successfully:
cd python && make test
- Check that Python 2.5 is still supported:
cd python && make PYTHON=python2.5 test
- Create a release-<version> branch and shift to it with:
git checkout -b release-<version>
- Push the branch to Github with:
git push <github-remote> release-<version>
- Create a setup-tools packaged tarball dist/phonenumbers-<version>.tar.gz
with:
make sdist # runs setup.py sdist
- Create a Debian package deb_dist/python-phonenumbers-<version>-1_all.deb
on a Linux machine with:
make deb
- Upload the tarball and Debian package to Github downloads page at:
https://github.com/daviddrysdale/python-phonenumbers/downloads
- Push the package to PyPI with:
setup.py sdist upload

Experimental Python v3.x version:
- Perform the v2.x release procedure above.
- Run the do3 script, which re-creates the python3/ directory from scratch,
based on the contents of the python/ directory.
- Check that the unit tests run:
cd python3 && make test
- Create a py3k-release-<version> branch and shift to it with:
git checkout -b py3k-release-<version>
- Push the branch to Github with:
git push origin py3k-release-<version>
- Create a setup-tools packaged tarball dist/phonenumbers3k-<version>.tar.gz
with:
make sdist # runs python3 setup.py sdist

Project details


Release history Release notifications

History Node

8.9.6

History Node

8.9.5

History Node

8.9.4

History Node

8.9.3

History Node

8.9.2

History Node

8.9.1

History Node

8.9.0

History Node

8.8.11

History Node

8.8.10

History Node

8.8.9

History Node

8.8.8

History Node

8.8.7

History Node

8.8.6

History Node

8.8.5

History Node

8.8.4

History Node

8.8.3

History Node

8.8.2

History Node

8.8.1

History Node

8.8.0

History Node

8.7.1

History Node

8.7.0

History Node

8.6.0

History Node

8.5.2

History Node

8.5.1

History Node

8.5.0

History Node

8.4.3

History Node

8.4.2

History Node

8.4.1

History Node

8.4.0

History Node

8.3.3

History Node

8.3.2

History Node

8.3.1

History Node

8.3.0

History Node

8.2.0

History Node

8.1.0

History Node

8.0.1

History Node

8.0.0

History Node

7.7.5

History Node

7.7.4

History Node

7.7.3

History Node

7.7.2

History Node

7.7.1

History Node

7.7.0

History Node

7.6.1

History Node

7.6.0

History Node

7.5.2

History Node

7.5.1

History Node

7.5.0

History Node

7.4.5

History Node

7.4.4

History Node

7.4.3

History Node

7.4.2

History Node

7.4.1

History Node

7.4.0

History Node

7.3.2

History Node

7.3.1

History Node

7.3.0

History Node

7.2.8

History Node

7.2.7

History Node

7.2.6

History Node

7.2.5

History Node

7.2.4

History Node

7.2.3

History Node

7.2.2

History Node

7.1.1

History Node

7.1.0

History Node

7.0.11

History Node

7.0.9

History Node

7.0.8

History Node

7.0.7

History Node

7.0.6

History Node

7.0.5

History Node

7.0.4

History Node

7.0.2

History Node

7.0.1

History Node

7.0.0

History Node

6.3.0

History Node

6.2.0

History Node

6.1.0

History Node

6.0.0

History Node

6.0.0a

History Node

5.9.2

History Node

5.9.1

History Node

5.9b1

History Node

5.8b1

History Node

5.7b2

History Node

5.7b1

History Node

5.6b1

History Node

5.5b1

History Node

5.4b1

History Node

5.3b1

History Node

5.2b1

History Node

5.1b1

This version
History Node

5.0b2

History Node

5.0b1

History Node

4.9b1

History Node

4.8b1

History Node

4.7b1

History Node

4.6b1

History Node

4.5b1

History Node

4.4b1

History Node

4.3b1

History Node

4.2b1

History Node

4.1b1

History Node

4.0b1

History Node

3.9b1

History Node

3.8b1

History Node

3.7b1

History Node

3.5b2

History Node

3.5b1

History Node

3.3a1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
phonenumbers-5.0b2.tar.gz (565.7 kB) Copy SHA256 hash SHA256 Source None Jul 18, 2012

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page