2018-09-20 13:24:42 +02:00
# The ISO registry
2018-08-30 11:56:36 +02:00
[Home ](index.md ) / [Basic usage ](basic.md ) / [Advanced usage ](advanced.md )
2018-09-20 13:45:12 +02:00
As of version 3.0 (August/September 2018), we have introduced a global calendar registry for calendars related to a country or a region that belongs to the [ISO 3166-1 ](https://en.wikipedia.org/wiki/ISO_3166-1 ) or the [ISO 3166-2 ](https://en.wikipedia.org/wiki/ISO_3166-2 ) for sub-regions (such as USA states, Australian territories or Canadian provinces and such).
2018-08-30 11:56:36 +02:00
2018-09-20 13:24:42 +02:00
## Iterate over the whole registry
2018-08-30 11:56:36 +02:00
```python
>>> from workalendar.registry import registry
2020-04-24 11:44:29 +02:00
>>> calendars = registry.get_calendars() # This returns a dictionary
2019-11-15 15:29:27 +01:00
>>> for code, calendar_class in calendars.items():
2019-06-24 16:28:45 +02:00
... print("`{}` is code for '{}'".format(code, calendar_class.name))
2018-08-30 11:56:36 +02:00
`AT` is code for 'Austria'
`BE` is code for 'Belgium'
`BG` is code for 'Bulgaria'
`HR` is code for 'Croatia'
`CY` is code for 'Cyprus'
`CZ` is code for 'Czech Republic'
`EE` is code for 'Estonia'
`DK` is code for 'Denmark'
`FI` is code for 'Finland'
`FR` is code for 'France'
... continued
```
2019-11-15 15:29:27 +01:00
The "private property" `registry.region_registry` is a `dict` object, with the ISO code as a key, and the calendar class as the value. As a "workalendar standard", **every** calendar in the registry has a `name` property (derived from the docstring), so you'd probably be able to build a user-friendly list of available calendars, for a dropdown list, for example.
2019-05-24 15:59:38 +02:00
2020-04-24 11:44:29 +02:00
**DEPRECATION WARNING**: As of version 9.0.0, the ``IsoRegistry.items()`` has been renamed into ``IsoRegistry.get_calendars()`` for all your queries in the registry.
2019-11-15 16:33:14 +01:00
2018-08-30 11:56:36 +02:00
## Retrieve a collection of regions
2019-11-15 16:33:14 +01:00
If you want the full dictionary of **countries** , you can use the ``get_calendars()`` method.
2019-11-15 15:29:27 +01:00
```python
2019-11-15 16:33:14 +01:00
>>> registry.get_calendars()
2019-11-15 15:29:27 +01:00
{'AT': < class ' workalendar . europe . austria . Austria ' > ,
'BE': < class ' workalendar . europe . belgium . Belgium ' > ,
'BG': < class ' workalendar . europe . bulgaria . Bulgaria ' > ,
'KY': < class ' workalendar . europe . cayman_islands . CaymanIslands ' > ,
# ... continued
}
```
2019-11-15 16:33:14 +01:00
Let's say you'd need only a subset of the ISO registry. For example, France, Switzerland and Canada calendars. You can use the method `get_calendars()` to filter only the calendars you want.
2018-08-30 11:56:36 +02:00
```python
2019-11-15 16:33:14 +01:00
>>> registry.get_calendars(['FR', 'CH', 'CA'])
2019-06-24 16:28:45 +02:00
{'FR': < class ' workalendar . europe . france . France ' > ,
'CH': < class ' workalendar . europe . switzerland . Switzerland ' > ,
'CA': < class ' workalendar . america . canada . Canada ' > }
2018-08-30 11:56:36 +02:00
```
Also, if you want those regions **and** their subregions, you can use the `include_subregions` flag:
```python
2019-11-15 16:33:14 +01:00
>>> registry.get_calendars(['FR', 'CH', 'CA'], include_subregions=True)
2019-06-24 16:28:45 +02:00
{'CA': < class ' workalendar . america . canada . Canada ' > ,
'CA-AB': < class ' workalendar . america . canada . Alberta ' > ,
'CA-BC': < class ' workalendar . america . canada . BritishColumbia ' > ,
'CA-MB': < class ' workalendar . america . canada . Manitoba ' > ,
'CA-NB': < class ' workalendar . america . canada . NewBrunswick ' > ,
'CA-NL': < class ' workalendar . america . canada . Newfoundland ' > ,
'CA-NS': < class ' workalendar . america . canada . NovaScotia ' > ,
'CA-NT': < class ' workalendar . america . canada . NorthwestTerritories ' > ,
'CA-NU': < class ' workalendar . america . canada . Nunavut ' > ,
'CA-ON': < class ' workalendar . america . canada . Ontario ' > ,
'CA-PE': < class ' workalendar . america . canada . PrinceEdwardIsland ' > ,
'CA-QC': < class ' workalendar . america . canada . Quebec ' > ,
'CA-SK': < class ' workalendar . america . canada . Saskatchewan ' > ,
'CA-YT': < class ' workalendar . america . canada . Yukon ' > ,
'CH': < class ' workalendar . europe . switzerland . Switzerland ' > ,
'CH-VD': < class ' workalendar . europe . switzerland . Vaud ' > ,
2019-11-18 16:38:07 +01:00
'CH-GE': < class ' workalendar . europe . switzerland . Geneva ' > ,
2019-06-24 16:28:45 +02:00
'FR': < class ' workalendar . europe . france . France ' > }
2018-08-30 11:56:36 +02:00
```
*Note*: if any of the codes is unknown, this function won't raise an error.
2019-11-15 15:29:27 +01:00
You can also get the full dict of all calendars registered in the ISO Registry with all the subregions using the following function call:
```python
2019-11-15 16:33:14 +01:00
>>> registry.get_calendars(include_subregions=True)
2019-11-15 15:29:27 +01:00
```
2018-08-30 11:56:36 +02:00
## Select only one calendar
Let's say that we only know the ISO code for Switzerland (`CH`). If we want to compute holidays for Switzerland in 2018, we can do as follows:
```python
>>> registry.get_calendar_class('CH')
>>> CalendarClass = registry.get_calendar_class('CH')
>>> calendar = CalendarClass()
>>> calendar.holidays(2018)
[(datetime.date(2018, 1, 1), 'New year'),
2019-06-24 16:28:45 +02:00
(datetime.date(2018, 1, 2), "Berchtold's Day"),
2018-08-30 11:56:36 +02:00
(datetime.date(2018, 3, 30), 'Good Friday'),
(datetime.date(2018, 4, 1), 'Easter Sunday'),
(datetime.date(2018, 4, 2), 'Easter Monday'),
2019-06-24 16:28:45 +02:00
(datetime.date(2018, 5, 1), 'Labour Day'),
2018-08-30 11:56:36 +02:00
(datetime.date(2018, 5, 10), 'Ascension Thursday'),
(datetime.date(2018, 5, 20), 'Whit Sunday'),
(datetime.date(2018, 5, 21), 'Whit Monday'),
2019-06-24 16:28:45 +02:00
(datetime.date(2018, 8, 1), 'National Holiday'),
2018-08-30 11:56:36 +02:00
(datetime.date(2018, 12, 25), 'Christmas Day'),
(datetime.date(2018, 12, 26), 'Boxing Day')]
```
*Note*: this function would return `None` if the code is unknown.
## Select only sub-regions
As an example, the United States of America, or Australia and others are divided into "sub-regions" (states, provinces, territories, etc). There are usually Federal or State holidays, and variants depending on the region you're targeting.
```python
>>> registry.get_subregions('AU')
2019-06-24 16:28:45 +02:00
{'AU-ACT': < class ' workalendar . oceania . australia . AustralianCapitalTerritory ' > ,
'AU-NSW': < class ' workalendar . oceania . australia . NewSouthWales ' > ,
'AU-NT': < class ' workalendar . oceania . australia . NorthernTerritory ' > ,
'AU-QLD': < class ' workalendar . oceania . australia . Queensland ' > ,
'AU-SA': < class ' workalendar . oceania . australia . SouthAustralia ' > ,
'AU-TAS': < class ' workalendar . oceania . australia . Tasmania ' > ,
'AU-VIC': < class ' workalendar . oceania . australia . Victoria ' > ,
'AU-WA': < class ' workalendar . oceania . australia . WesternAustralia ' > }
2018-08-30 11:56:36 +02:00
```
2019-06-24 16:28:45 +02:00
*Note*: this function will return an empty `dict` if the code is unknown.
2018-08-30 11:56:36 +02:00
[Home ](index.md ) / [Basic usage ](basic.md ) / [Advanced usage ](advanced.md )