def __getitem__(self, key):
        """Get a component by name or index."""
        if isinstance(key, str):
            for component in self.components:
                if component.name == key:
                    return component
            raise KeyError('Component {} not found.'.format(key))

        if isinstance(key, int):
            return self.components[key]

        raise KeyError('Invalid key {}.'.format(key))
Get a component by name or index.
ipygany/ipygany.py