Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Is there a way to create List[Provider[X]] from Provider[List[X]]? #872

Copy link
Copy link
Open
@zenlambda

Description

@zenlambda
Issue body actions

Hi, thanks for providing this library, I really like it!

Could you tell me if there is a way to create List[Provider[X]] from Provider[List[X]]?

I call this operation InsideOut.

def InsideOut(Provider[List[X]]) -> List[Provider[X]]: ...


import torch.utils.data
import torchvision.datasets
from dependency_injector import containers, providers

def input_transform():
    return transforms.Compose(
        [transforms.ToTensor(),
        transforms.Normalize((0.5,), (0.5,))])

class AppContainer(containers.DeclarativeContainer):

    transform = providers.Singleton(input_transform)
    base_set = providers.Singleton(
        torchvision.datasets.FashionMNIST,
        './data', train=True,
        transform=transform, download=True)

    train, validate = providers.InsideOut(providers.Singleton(torch.utils.data.random_split, base_set, [0.8, 0.2]))

I've done some experiments with providers.Aggregate where you get a Provider[Dict], which is different from what I want. Also it was very difficult (maybe even impossible) to get pyright to stop throwing errors. Finally the cleanest solution I have a the moment is just keeper the list provider i.e.:

split = providers.Singleton(torch.utils.data.random_split, base_set, [0.8, 0.2]) # Provider[List]`

 train_loader = providers.Singleton(
     torch.utils.data.DataLoader,
     split.provided[0], batch_size=4, shuffle=True)

This works but I feel the first way would be cleaner.

Thanks,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.