Skip to content

What is the best way to have a "factory method" style? #358

@melezhik

Description

@melezhik

Right now I ended up with a following approach ( not sure if it the best):

import fire
import foo
import bar

def foo(*args):
  Foo(*args).configure()

def bar(*args):
  Bar(*args).configure()

def dispatch(**args):
  print("arguments passed", args)
  """Factory Method"""
  localizers = {
    "policy": foo,
    "repo": bar,
  }
  return localizers[args["obj_type"]](args)

if __name__ == '__main__':
  fire.Fire(dispatch)

Ideally I would want that Fire method allow me to pass classnames as variable which I define through a command line, something like that:

from ado import * # Repo and Policy classes are defined at libs/ado.py 

if __name__ == '__main__':
  fire.Fire(obj_type)
python script.py --obj_type=Foo

Not sure if it's possible 🤔 ...

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