Esempio n. 1
0
    def test_factory_dict(self) -> None:
        def make_default_value(x: int) -> List[int]:
            return [x**3]

        cubes = factory_dict(make_default_value, ((x, x**2) for x in range(3)),
                             three=42)

        # Check kwargs passed to the constructor
        self.assertEqual(42, cubes["three"])

        # Check args passed to the constructor
        self.assertEqual(0, cubes[0])
        self.assertEqual(1, cubes[1])
        self.assertEqual(4, cubes[2])

        # Check value factory for missing values
        self.assertEqual([27], cubes[3])

        # Check value factory only used for __getitem__, not other methods like get()
        self.assertIsNone(cubes.get(4))
        self.assertEqual("jake", cubes.get(5, "jake"))

        # This time we access directly via __getitem__
        self.assertEqual([64], cubes[4])
        cubes.get(4).append(8)  # type: ignore[union-attr]
        self.assertEqual([64, 8], cubes[4])
Esempio n. 2
0
 def plugin_subsystems(cls):
     subsystem_type_by_plugin_type = factory_dict(
         default_subsystem_for_plugin)
     subsystem_type_by_plugin_type.update(
         (subsystem_type.plugin_type(), subsystem_type)
         for subsystem_type in cls._CUSTOM_PLUGIN_SUBSYSTEMS)
     return tuple(subsystem_type_by_plugin_type[plugin_type]
                  for plugin_type in checker.plugins())
Esempio n. 3
0
  def __init__(self):
    # TODO(John Sirois): Kill products and simply have users register ProductMapping subtypes
    # as data products.  Will require a class factory, like `ProductMapping.named(typename)`.
    self.products = factory_dict(Products.ProductMapping)  # type -> ProductMapping instance.
    self.required_products = set()

    self.data_products = {}  # type -> arbitrary object.
    self.required_data_products = set()
Esempio n. 4
0
    def __init__(self):
        # TODO(John Sirois): Kill products and simply have users register ProductMapping subtypes
        # as data products.  Will require a class factory, like `ProductMapping.named(typename)`.
        self.products = factory_dict(
            Products.ProductMapping)  # type -> ProductMapping instance.
        self.required_products = set()

        self.data_products = {}  # type -> arbitrary object.
        self.required_data_products = set()
Esempio n. 5
0
  def test_factory_dict(self):
    cubes = factory_dict(lambda x: [x ** 3], ((x, x ** 2) for x in range(3)), three=42)
    self.assertEqual(0, cubes[0])
    self.assertEqual(1, cubes[1])
    self.assertEqual(4, cubes[2])

    self.assertEqual([27], cubes[3])

    self.assertIsNone(cubes.get(4))
    self.assertEqual([64], cubes[4])

    cubes.get(4).append(8)
    self.assertEqual([64, 8], cubes[4])

    self.assertEqual(42, cubes['three'])

    self.assertEqual('jake', cubes.get(5, 'jake'))
Esempio n. 6
0
    def test_factory_dict(self):
        cubes = factory_dict(lambda x: [x**3], ((x, x**2) for x in range(3)),
                             three=42)
        self.assertEqual(0, cubes[0])
        self.assertEqual(1, cubes[1])
        self.assertEqual(4, cubes[2])

        self.assertEqual([27], cubes[3])

        self.assertIsNone(cubes.get(4))
        self.assertEqual([64], cubes[4])

        cubes.get(4).append(8)
        self.assertEqual([64, 8], cubes[4])

        self.assertEqual(42, cubes['three'])

        self.assertEqual('jake', cubes.get(5, 'jake'))
Esempio n. 7
0
 def __init__(self):
     """
     :API: public
     """
     self._rooted_products_by_root = factory_dict(RootedProducts)
Esempio n. 8
0
 def plugin_subsystems(cls):
   subsystem_type_by_plugin_type = factory_dict(default_subsystem_for_plugin)
   subsystem_type_by_plugin_type.update((subsystem_type.plugin_type(), subsystem_type)
                                        for subsystem_type in cls._CUSTOM_PLUGIN_SUBSYSTEMS)
   return tuple(subsystem_type_by_plugin_type[plugin_type] for plugin_type in checker.plugins())
Esempio n. 9
0
 def __init__(self):
   """
   :API: public
   """
   self._rooted_products_by_root = factory_dict(RootedProducts)