class AppRoleCollection
A specialized collection class for managing AppRole objects, extending ClientValueCollection with custom indexing by role value/name.
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/directory/applications/roles/collection.py
5 - 13
simple
Purpose
AppRoleCollection provides a type-safe container for AppRole objects with enhanced lookup capabilities. It inherits from ClientValueCollection to provide standard collection operations while adding custom indexing that allows retrieval of AppRole objects by their value/name string. This is particularly useful in Office 365 directory applications where roles need to be accessed by their string identifiers rather than numeric indices.
Source Code
class AppRoleCollection(ClientValueCollection[AppRole]):
""""""
def __init__(self, initial_values=None):
super(AppRoleCollection, self).__init__(AppRole, initial_values)
def __getitem__(self, name):
# type: (str) -> AppRole
return next(iter([item for item in self._data if item.value == name]), None)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
bases |
ClientValueCollection[AppRole] | - |
Parameter Details
initial_values: Optional parameter that accepts an initial collection of AppRole objects or compatible data to populate the collection upon instantiation. Can be None (default) to create an empty collection, or an iterable of AppRole objects or data that can be converted to AppRole objects.
Return Value
Instantiation returns an AppRoleCollection object that can hold and manage AppRole instances. The __getitem__ method returns either an AppRole object matching the provided name/value string, or None if no matching role is found in the collection.
Class Interface
Methods
__init__(self, initial_values=None)
Purpose: Initializes a new AppRoleCollection instance with optional initial AppRole objects
Parameters:
initial_values: Optional iterable of AppRole objects or compatible data to populate the collection. Defaults to None for an empty collection.
Returns: None (constructor)
__getitem__(self, name: str) -> AppRole
Purpose: Retrieves an AppRole object from the collection by its value/name string
Parameters:
name: String identifier representing the value property of the AppRole to retrieve
Returns: AppRole object if a role with matching value is found, None otherwise
Attributes
| Name | Type | Description | Scope |
|---|---|---|---|
_data |
list | Internal list storing the AppRole objects in the collection. Inherited from ClientValueCollection base class. | instance |
Dependencies
office365
Required Imports
from office365.directory.applications.roles.role import AppRole
from office365.runtime.client_value_collection import ClientValueCollection
Usage Example
from office365.directory.applications.roles.role import AppRole
from office365.directory.applications.roles.collection import AppRoleCollection
# Create an empty collection
role_collection = AppRoleCollection()
# Create a collection with initial values
role1 = AppRole()
role1.value = 'Admin'
role2 = AppRole()
role2.value = 'User'
role_collection = AppRoleCollection([role1, role2])
# Access role by name/value
admin_role = role_collection['Admin']
if admin_role:
print(f'Found role: {admin_role.value}')
# Access role that doesn't exist returns None
missing_role = role_collection['NonExistent']
if missing_role is None:
print('Role not found')
# Iterate through all roles
for role in role_collection:
print(role.value)
Best Practices
- Use string-based indexing (collection['role_name']) to retrieve roles by their value property rather than numeric indices
- Check for None when using __getitem__ as it returns None if no matching role is found
- Initialize with initial_values parameter when you have a predefined set of roles to avoid multiple add operations
- The collection inherits from ClientValueCollection, so standard collection operations (iteration, length, etc.) are available
- Role lookup is case-sensitive and matches against the 'value' property of AppRole objects
- The __getitem__ implementation uses a linear search, so performance may degrade with very large collections
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class AppRoleAssignmentCollection 82.6% similar
-
class AppCollection 68.5% similar
-
class ApplicationCollection 67.9% similar
-
class AppRole 66.8% similar
-
class RoleAssignmentCollection 64.0% similar