mxutils.IdProvider¶
Description¶
int
or maxon.Id
identifiers when defining them manually is not desirable.- Example
import maxon
from mxutils import IdProvider
# An IdProvider can be used in any situation where either integer or maxon.Id identifiers
# are required but one does not care too much about the exact makeup of each ID.
# Providers can be initialized for one of the ID types, the default is `int`.
intIds: IdProvider = IdProvider(int)
mxIds: IdProvider = IdProvider(maxon.Id)
# A provider can use scopes/namespaces for generating IDs to provide multiple sets IDs. A
# scope can either be an integer or a string.
scopeA: int = 1000
scopeB: str = 'net.my-company.com.product.ids'
for provider in (intIds, mxIds):
# The property IdType will return the type of IDs yielded by an instance.
print (f'{provider.IdType = }')
# With Get() a new ID is being yielded for the given scope. Providing no scope will
# place the ID in the default scope.
print (f'{provider.Get() = }')
print (f'{provider.Get(scopeA) = }')
# The first time .Get() is called for a new scope, its startValue argument will determine
# where the integer value this scope starts with, its first ID.
print (f'{provider.Get(scopeB, 2000) = }')
print (f'{provider.Get() = }')
print (f'{provider.Get() = }')
print (f'{provider.Get(scopeA) = }')
print (f'{provider.Get(scopeB) = }')
# GetLast() returns the last ID which has been yielded for a scope and GetAll() returns
# all of them.
print (f'{provider.GetLast() = }')
print (f'{provider.GetLast(scopeA) = }')
print (f'{provider.GetLast(scopeB) = }')
print (f'{provider.GetAll() = }')
print (f'{provider.GetAll(scopeA) = }')
print (f'{provider.GetAll(scopeB) = }')
The output will be:
provider.IdType = <class 'int'>
provider.Get() = 1000
provider.Get(scopeA) = 1000
provider.Get(scopeB, 2000) = 2000
provider.Get() = 1001
provider.Get() = 1002
provider.Get(scopeA) = 1001
provider.Get(scopeB) = 2001
provider.GetLast() = 1002
provider.GetLast(scopeA) = 1001
provider.GetLast(scopeB) = 2001
provider.GetAll() = (1000, 1001, 1002)
provider.GetAll(scopeA) = (1000, 1001)
provider.GetAll(scopeB) = (2000, 2001)
provider.IdType = <class 'maxon.data.Id'>
provider.Get() = maxon.Id('net.maxon.user.default.namespace#1000')
provider.Get(scopeA) = maxon.Id('1000#1000')
provider.Get(scopeB, 2000) = maxon.Id('net.my-company.com.product.ids#2000')
provider.Get() = maxon.Id('net.maxon.user.default.namespace#1001')
provider.Get() = maxon.Id('net.maxon.user.default.namespace#1002')
provider.Get(scopeA) = maxon.Id('1000#1001')
provider.Get(scopeB) = maxon.Id('net.my-company.com.product.ids#2001')
provider.GetLast() = maxon.Id('net.maxon.user.default.namespace#1002')
provider.GetLast(scopeA) = maxon.Id('1000#1001')
provider.GetLast(scopeB) = maxon.Id('net.my-company.com.product.ids#2001')
provider.GetAll() = (maxon.Id('net.maxon.user.default.namespace#1000'),
maxon.Id('net.maxon.user.default.namespace#1001'),
maxon.Id('net.maxon.user.default.namespace#1002'))
provider.GetAll(scopeA) = (maxon.Id('1000#1000'),
maxon.Id('1000#1001'))
provider.GetAll(scopeB) = (maxon.Id('net.my-company.com.product.ids#2000'),
maxon.Id('net.my-company.com.product.ids#2001'))
Inheritance diagram¶
Inheritance
Attributes¶
The scope which is used when no scope is being provided. |
|
Contains all valid identifier types this class can handle. |
|
Returns the type of identifiers managed by the instance. |
|
Returns all scope identifiers managed by the instance. |
Methods Signature¶
|
Returns the next identifier for the given |
|
Returns all identifiers that have been generated for the given |
|
Returns the last identifier which has been yielded for |
|
Initializes the provider with an identifier type. |
Methods Definition¶
-
IdProvider.
Get
(scope: int | str | None = None, startValue: int = 1000) → int | maxon.data.Id¶ Returns the next identifier for the given
scope
.- Parameters
scope – The scope symbol to get a new identifier for. When a never before seen scope is passed, a new entry will be created with
startValue
as the starting value. PassingNone
will useIdProvider.DEFAULT_SCOPE
. Defaults toNone
.- Raises
TypeError – On invalid argument types.
- Returns
A newly created identifier which is unique within the given scope.
- Return type
int | maxon.Id
-
IdProvider.
GetAll
(scope: int | str | None = None) → tuple[int] | tuple[maxon.data.Id]¶ Returns all identifiers that have been generated for the given
scope
.- Parameters
scope – The scope symbol to get all identifiers for. Passing
None
will useIdProvider.DEFAULT_SCOPE
. Defaults toNone
.- Raises
TypeError – On invalid argument types.
IndexError – When
scope
does not exist.
- Returns
All identifiers which have been yielded for
scope
. Can be the empty tuple.- Return type
tuple[int] | tuple[maxon.Id]
-
IdProvider.
GetLast
(scope: int | str | None = None) → int | maxon.data.Id¶ Returns the last identifier which has been yielded for
scope
.- Parameters
scope – The scope symbol to get a new identifier for. Passing
None
will useIdProvider.DEFAULT_SCOPE
. Defaults toNone
.- Raises
TypeError – On invalid argument types.
IndexError – When
scope
does not exist or has no elements.
- Returns
The last identifier which has been yielded for
scope
.- Return type
int | maxon.Id
-
IdProvider.
__init__
(idType: Type = <class 'int'>) → None¶ Initializes the provider with an identifier type.
- Parameters
idType (int) – The type of IDs the provider should handle. The value must lie within
IdProvider.ID_TYPES
. Defaults toint
.- Raises
ValueError – When
idType
does not lie withinIdProvider.ID_TYPES
.