mxutils.IdProvider

Description

Generates int or maxon.Id identifiers when defining them manually is not desirable.

A provider can yield identifiers in multiple scopes, where generation is independent and continuous for each scope.
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

mxutils.IdProvider.DEFAULT_SCOPE

The scope which is used when no scope is being provided.

mxutils.IdProvider.ID_TYPES

Contains all valid identifier types this class can handle.

mxutils.IdProvider.IdType

Returns the type of identifiers managed by the instance.

mxutils.IdProvider.Scopes

Returns all scope identifiers managed by the instance.

Methods Signature

Get([scope, startValue])

Returns the next identifier for the given scope.

GetAll([scope])

Returns all identifiers that have been generated for the given scope.

GetLast([scope])

Returns the last identifier which has been yielded for scope.

__init__([idType])

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. Passing None will use IdProvider.DEFAULT_SCOPE. Defaults to None.

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 use IdProvider.DEFAULT_SCOPE. Defaults to None.

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 use IdProvider.DEFAULT_SCOPE. Defaults to None.

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 to int.

Raises

ValueError – When idType does not lie within IdProvider.ID_TYPES.