Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ Available formats
eu.ecnumber
eu.eic
eu.nace
eu.nace.v2_0
eu.nace.v2_1
eu.oss
eu.vat
fi.alv
Expand Down
6 changes: 6 additions & 0 deletions docs/stdnum.eu.nace.v2_0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
stdnum.eu.nace.v2_0
===================

.. automodule:: stdnum.eu.nace.v2_0
:members:
:no-index: InvalidChecksum, InvalidFormat, InvalidLength, InvalidComponent
6 changes: 6 additions & 0 deletions docs/stdnum.eu.nace.v2_1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
stdnum.eu.nace.v2_1
===================

.. automodule:: stdnum.eu.nace.v2_1
:members:
:no-index: InvalidChecksum, InvalidFormat, InvalidLength, InvalidComponent
6 changes: 6 additions & 0 deletions stdnum/eu/nace/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from . import v2_0, v2_1 # makes subpackages accessible
from .v2_1 import * # re-export v2_1 as the default version


__all__ = (list(getattr(v2_1, "__all__", [])))
__doc__ = v2_1.__doc__
10 changes: 10 additions & 0 deletions stdnum/eu/nace/v2_0/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .nace import *


__all__ = (list(getattr(nace, "__all__", []))or [n for n in dir(nace) if not n.startswith("_")])
__doc__ = nace.__doc__
# remove nace as submodule reference
try:
del nace
except NameError:
pass
File renamed without changes.
4 changes: 2 additions & 2 deletions stdnum/eu/nace.py → stdnum/eu/nace/v2_0/nace.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""NACE (classification for businesses in the European Union).
"""NACE Rev2 (classification for businesses in the European Union).

The NACE (nomenclature statistique des activités économiques dans la
Communauté européenne) is a 4-level (and up to 4 digit) code for classifying
Expand Down Expand Up @@ -71,7 +71,7 @@ def info(number: str) -> dict[str, str]:
number = compact(number)
from stdnum import numdb
info = dict()
for _n, i in numdb.get('eu/nace').info(number):
for _n, i in numdb.get('eu/nace/v2_0/nace').info(number):
if not i:
raise InvalidComponent()
info.update(i)
Expand Down
10 changes: 10 additions & 0 deletions stdnum/eu/nace/v2_1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .nace import *


__all__ = (list(getattr(nace, "__all__", []))or [n for n in dir(nace) if not n.startswith("_")])
__doc__ = nace.__doc__
# remove nace as submodule reference
try:
del nace
except NameError:
pass
1,050 changes: 1,050 additions & 0 deletions stdnum/eu/nace/v2_1/nace.dat

Large diffs are not rendered by default.

113 changes: 113 additions & 0 deletions stdnum/eu/nace/v2_1/nace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# nace.py - functions for handling EU NACE classification
# coding: utf-8
#
# Copyright (C) 2017-2025 Arthur de Jong
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA

"""NACE Rev2.1 (classification for businesses in the European Union).

The NACE (nomenclature statistique des activités économiques dans la
Communauté européenne) is a 4-level (and up to 4 digit) code for classifying
economic activities. It is the European implementation of the UN
classification ISIC.

The first 4 digits are the same in all EU countries while additional levels
and digits may be vary between countries. This module validates the numbers
according to revision 2.1 and based on the registry as published by the EC.

More information:

* https://en.wikipedia.org/wiki/Statistical_Classification_of_Economic_Activities_in_the_European_Community
* https://showvoc.op.europa.eu/#/datasets/ESTAT_Statistical_Classification_of_Economic_Activities_in_the_European_Community_Rev._2.1._%28NACE_2.1%29/downloads

>>> validate('A')
'A'
>>> validate('62.01')
'6201'
>>> get_label('62.01')
'Computer programming activities'
>>> validate('62.05')
Traceback (most recent call last):
...
InvalidComponent: ...
>>> validate('62059') # does not validate country-specific numbers
Traceback (most recent call last):
...
InvalidLength: ...
>>> format('6201')
'62.01'
""" # noqa: E501

from __future__ import annotations

import warnings

from stdnum.exceptions import *
from stdnum.util import clean, isdigits


def compact(number: str) -> str:
"""Convert the number to the minimal representation. This strips the
number of any valid separators and removes surrounding whitespace."""
return clean(number, '.').strip()


def info(number: str) -> dict[str, str]:
"""Lookup information about the specified NACE. This returns a dict."""
number = compact(number)
from stdnum import numdb
info = dict()
for _n, i in numdb.get('eu/nace/v2_1/nace').info(number):
if not i:
raise InvalidComponent()
info.update(i)
return info


def get_label(number: str) -> str:
"""Lookup the category label for the number."""
return info(number)['label']


def validate(number: str) -> str:
"""Check if the number is a valid NACE. This checks the format and
searches the registry to see if it exists."""
number = compact(number)
if len(number) > 4:
raise InvalidLength()
elif len(number) == 1:
if not number.isalpha():
raise InvalidFormat()
else:
if not isdigits(number):
raise InvalidFormat()
info(number)
return number


def is_valid(number: str) -> bool:
"""Check if the number is a valid NACE. This checks the format and
searches the registry to see if it exists."""
try:
return bool(validate(number))
except ValidationError:
return False


def format(number: str) -> str:
"""Reformat the number to the standard presentation format."""
return '.'.join((number[:2], number[2:])).strip('.')