2727
2828The first 4 digits are the same in all EU countries while additional levels
2929and digits may be vary between countries. This module validates the numbers
30- according to revision 2 and based on the registry as published by the EC.
30+ according to revision 2.1 (or 2.0 if that version is supplied) and based on
31+ the registry as published by the EC.
3132
3233More information:
3334
3435* https://en.wikipedia.org/wiki/Statistical_Classification_of_Economic_Activities_in_the_European_Community
3536* https://ec.europa.eu/eurostat/ramon/nomenclatures/index.cfm?TargetUrl=LST_NOM_DTL&StrNom=NACE_REV2&StrLanguageCode=EN&IntPcKey=&StrLayoutCode=HIERARCHIC
37+ * https://showvoc.op.europa.eu/#/datasets/ESTAT_Statistical_Classification_of_Economic_Activities_in_the_European_Community_Rev._2.1._%28NACE_2.1%29/data
3638
3739>>> validate('A')
3840'A'
39- >>> validate('62.01')
41+ >>> validate('62.01', revision='2.0' )
4042'6201'
41- >>> get_label('62.01 ')
43+ >>> get_label('62.10 ')
4244'Computer programming activities'
4345>>> validate('62.05')
4446Traceback (most recent call last):
6062from stdnum .util import clean , isdigits
6163
6264
65+ # The revision of the NACE definition to use
66+ DEFAULT_REVISION = '2.1'
67+
68+
6369def compact (number : str ) -> str :
6470 """Convert the number to the minimal representation. This strips the
6571 number of any valid separators and removes surrounding whitespace."""
6672 return clean (number , '.' ).strip ()
6773
6874
69- def info (number : str ) -> dict [str , str ]:
75+ def info (number : str , * , revision : str = DEFAULT_REVISION ) -> dict [str , str ]:
7076 """Lookup information about the specified NACE. This returns a dict."""
7177 number = compact (number )
78+ revision = revision .replace ('.' , '' )
7279 from stdnum import numdb
7380 info = dict ()
74- for _n , i in numdb .get ('eu/nace' ).info (number ):
81+ for _n , i in numdb .get (f 'eu/nace{ revision } ' ).info (number ):
7582 if not i :
7683 raise InvalidComponent ()
7784 info .update (i )
7885 return info
7986
8087
81- def get_label (number : str ) -> str :
88+ def get_label (number : str , revision : str = DEFAULT_REVISION ) -> str :
8289 """Lookup the category label for the number."""
83- return info (number )['label' ]
90+ return info (number , revision = revision )['label' ]
8491
8592
8693def label (number : str ) -> str : # pragma: no cover (deprecated function)
8794 """DEPRECATED: use `get_label()` instead.""" # noqa: D40
8895 warnings .warn (
8996 'label() has been to get_label()' ,
9097 DeprecationWarning , stacklevel = 2 )
91- return get_label (number )
98+ return get_label (number , revision = '2.0' )
9299
93100
94- def validate (number : str ) -> str :
101+ def validate (number : str , revision : str = DEFAULT_REVISION ) -> str :
95102 """Check if the number is a valid NACE. This checks the format and
96103 searches the registry to see if it exists."""
97104 number = compact (number )
@@ -103,15 +110,15 @@ def validate(number: str) -> str:
103110 else :
104111 if not isdigits (number ):
105112 raise InvalidFormat ()
106- info (number )
113+ info (number , revision = revision )
107114 return number
108115
109116
110- def is_valid (number : str ) -> bool :
117+ def is_valid (number : str , revision : str = DEFAULT_REVISION ) -> bool :
111118 """Check if the number is a valid NACE. This checks the format and
112119 searches the registry to see if it exists."""
113120 try :
114- return bool (validate (number ))
121+ return bool (validate (number , revision = revision ))
115122 except ValidationError :
116123 return False
117124
0 commit comments