Create Object

yahp.create_object.create_object.create(constructor: Callable[..., TObject], data: Optional[Dict[str, JSON]] = None, f: Union[str, TextIO, pathlib.PurePath, None] = None, cli_args: Union[List[str], bool] = True) TObject[source]

Create a class or invoke a function with arguments coming from a dictionary, YAML string or file, or the CLI.

This function is the main entrypoint to YAHP! It will recurse through the configuration – which can come from CLI args or a JSON dictionary, or YAML file – to invoke the constructor. For example:

import yahp as hp

class Foo:
    '''Foo Docstring

    Args:
        arg (int): Integer variable.
    '''

    def __init__(self, arg: int):
        self.arg = arg
>>> foo_instance = hp.create(Foo, data={'arg': 42})
>>> foo_instance.arg
42

The constructor can also have nested classes:

import yahp as hp

class Bar:
    '''Bar Docstring

    Args:
        foo (Foo): Foo class
    '''

    def __init__(self, foo: Foo):
        self.foo = foo
>>> bar_instance = hp.create(Bar, data={'foo': {'arg': 42}})
>>> bar_instance.foo.arg
42
Parameters
  • constructor (type | callable) –

    Class or function.

    If a class is provided, an instance of the class will be returned. If a function is provided, the resulting value from the function will be returned.

    The arguments used to construct the class or invoke the function come from data, f, and/or cli_args.

  • f (Union[str, None, TextIO, pathlib.PurePath], optional) – If specified, load values from a YAML file. Can be either a filepath or file-like object. Cannot be specified with data.

  • data (Optional[Dict[str, JSON]], optional) –

    Data dictionary.

    If specified, this dictionary will be used for the Hparams. Cannot be specified with f.

  • cli_args (Union[List[str], bool], optional) – CLI argument overrides. Can either be a list of CLI argument, True (the default) to load CLI arguments from sys.argv, or False to not use any CLI arguments.

Returns

The constructed object.

yahp.create_object.create_object.get_argparse(constructor: Union[Type[TObject], Callable[..., TObject]], data: Optional[Dict[str, JSON]] = None, f: Union[str, TextIO, pathlib.PurePath, None] = None, cli_args: Union[List[str], bool] = True) argparse.ArgumentParser[source]

Get an ArgumentParser containing all CLI arguments.

It is usually not necessary to manually parse the CLI args, as create() will do that automatically. However, if you have additional CLI arguments, then it is recommended to use this function to get a ArgumentParser instance to ensure that --help will show all CLI arguments.

For example:

import yahp as hp

class MyClass:
    '''MyClass Docstring

    Args:
        foo (int): Foo
    '''

    def __init__(self, foo: int):
        self.foo = foo

# Get the parser
parser = hp.get_argparse(MyClass)

# Add additional arguments
parser.add_argument(
    '--my_argument',
    type=str,
    help='Additional, non-YAHP argument',
)

The --my_argument is accessible like normal:

>>> cli_args = ['--foo', '42', '--my_argument', 'Hello, world!']
>>> args = parser.parse_args(cli_args)
>>> args.my_argument
'Hello, world!'

And create() would still work, ignoring the custom --my_argument:

>>> my_instance = hp.create(MyClass, cli_args=['--foo', '42'])
>>> my_instance.foo
42
Parameters
  • constructor (type | callable) –

    Class or function.

    If a subclass of Hparams is provided, then the CLI arguments will match the fields of the hyperameter class.

    Otherwise, if a generic class or function is provided, then the arguments, default values, and help text come from docstring and constructor (or function) signature.

  • f (str | TextIO | pathlib.Path, optional) – If specified, load values from a YAML file. Can be either a filepath or file-like object. Cannot be specified with data.

  • data (Optional[Dict[str, JSON]], optional) – f specified, uses this dictionary for instantiating the Hparams. Cannot be specified with f.

  • cli_args (Union[List[str], bool], optional) – CLI argument overrides. Can either be a list of CLI argument, true (the default) to load CLI arguments from sys.argv, or false to not use any CLI arguments.

Returns

An argparser with all CLI arguments, but without any help.

Return type

argparse.ArgumentParser