Skip to main content

A minimal IOC container inspired by Spring

Project description

PyPI version Build Status

gean

A minimal IOC container inspired by Spring.

Install

python3 -m pip install gean

Example

from gean import Container, includes

class Michael:
  def speak(self):
    return 'what'

@includes(Michael)
class WhateverModule:
  def whatever(self) -> int:
    return 42

  def world(self) -> int:
    return 100

class Application:
  my_dir: str
  whatever: 'int'
  world: int
  m: Michael

  def start(self):
    print(self.my_dir)
    print(self.whatever)
    print(self.world)
    print(self.m.speak())

@includes(
  WhateverModule,
  Application,
  Michael,
)
class ApplicationModule:
  config_dir: str

  def another_dir(self) -> str:
    return self.config_dir + '/another'

  def my_dir(self, another_dir: 'str') -> str:
    return another_dir + '/ñe'

def _main():
  container = Container()
  container.register_instance('/etc/hello/world', name='config_dir')
  container.register_module(ApplicationModule)
  container.resolve(Application).start()

if __name__ == '__main__':
  _main()

Requirements

gean, like Spring, relies on types and signatures to build and resolve the dependency graph.

Required language features:

Design

Dependency registration

Each dependency of type T is registered not only as T but also as all of its implemented interfaces throguh its inheritance hierarchy.

Dependencies may be explicitly named if multiple of the same type are needed.

Dependency kinds

Instances: the provided type is type(instance)

Classes: the provided type is cls itself

Callables: the provided type is get_type_hints(callable)['return']

Modules: for each public method of the module m, the provided type is get_type_hints(m)['return']. Additionally, each dependency is automatically named after the module method that provides it.

History

gean started off as a gist I created to show @alexpizarroj how my team leverages Spring in our projects.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gean-0.0.2.tar.gz (4.4 kB view hashes)

Uploaded Source

Built Distribution

gean-0.0.2-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page