Skip to content

Commit a10198e

Browse files
authored
Merge pull request #1718 from anatoly-scherbakov/issue-1717-defined-namespace-meta-getitem-is-slow
`DefinedNamespaceMeta.__getitem__` is slow
2 parents 273154f + 8d2c1bf commit a10198e

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

rdflib/namespace/__init__.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import warnings
3+
from functools import lru_cache
34
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, Iterable
45
from unicodedata import category
56

@@ -184,17 +185,15 @@ def __repr__(self) -> str:
184185

185186

186187
class DefinedNamespaceMeta(type):
187-
"""
188-
Utility metaclass for generating URIRefs with a common prefix
189-
190-
"""
188+
"""Utility metaclass for generating URIRefs with a common prefix."""
191189

192190
_NS: Namespace
193191
_warn: bool = True
194192
_fail: bool = False # True means mimic ClosedNamespace
195193
_extras: List[str] = [] # List of non-pythonesque items
196194
_underscore_num: bool = False # True means pass "_n" constructs
197195

196+
@lru_cache(maxsize=None)
198197
def __getitem__(cls, name: str, default=None) -> URIRef:
199198
name = str(name)
200199
if str(name).startswith("__"):

0 commit comments

Comments
 (0)