Usage

Initialization

from chzip import ChZip, Locality
z = ChZip()

Download directory

The resources have been downloaded to a “resource directory” on your disk. It contains all the files needed for this library to work.

It can be set manually by calling the constructor with the absolute path as argument.

It defaults to the res/ directory inside the installation path. If changed, you will need to call chzip.download_and_unpack_all() once, and chzip.upgrade_all() after that.

Get all localities matching a ZIP code

Example for ZIP code 1696, giving one result, i.e. one chzip.common.Locality, with a different short (18 chars) and long (27 chars) name:

> results = z.find(1696)
[<Locality[short_name='Vuisternens-Ogoz',
          long_name='Vuisternens-en-Ogoz',
          canton='FR']>]

> results[0].long_name
"Vuisternens-en-Ogoz"

Note

If possible, always use the long name.

Get all locality names matching a ZIP code

If all you want from the section “Get all localities matching a ZIP code” is the long name of localities, you can use this little snippet of code:

z.long_names( zip=1696 )

See also

chzip.common.Locality.short_name and chzip.common.Locality.long_name to understand the difference between these two formats.

Find localities matching the exact name

> z.find( long_name='Vuisternens-en-Ogoz' )

[<Locality[short_name='Vuisternens-Ogoz',
long_name='Vuisternens-en-Ogoz',
canton='FR']>]

Find localities containing a search term

The following method is very handy when you need to match the user input with a locality of the database. It will returns a list of matching localities as previously.

> z.find( long_name_like='Vuisternens-en-Ogoz' )
[<Locality[short_name='Vuisternens-Ogoz',
           long_name='Vuisternens-en-Ogoz',
           canton='FR']>]

Warning

You are responsible to take care of the query string as this may return thousands of results with dumb queries, which could use a lot of memory. You may be served better with the all method as explained here: Get all localities (no search criteria).

Get all localities (no search criteria)

chzip.ChZip.all() may be used to fetch all the data:

for locality in z.all():
    do_something_with( locality )

If you are looking for a list, then:

l = list( z.all() )