scriptconfig.value module

scriptconfig.value.normalize_option_str(s)[source]
class scriptconfig.value.Value(value=None, type=None, help=None, choices=None, position=None, isflag=False, nargs=None, alias=None, required=False, short_alias=None, group=None, mutex_group=None, tags=None)[source]

Bases: NiceRepr

You may set any item in the config’s default to an instance of this class. Using this class allows you to declare the desired default value as well as the type that the value should be (Used when parsing sys.argv).

Variables:
  • value (Any) – A float, int, etc…

  • type (type | None) – the “type” of the value. This is usually used if the value specified is not the type that self.value would usually be set to.

  • parsekw (dict) – kwargs for to argparse add_argument

  • position (None | int) – if an integer, then we allow this value to be a positional argument in the argparse CLI. Note, that values with the same position index will cause conflicts. Also note: positions indexes should start from 1.

  • isflag (bool) – if True, args will be parsed as booleans. Default to False.

  • alias (List[str] | None) – other long names (that will be prefixed with ‘–’) that will be accepted by the argparse CLI.

  • short_alias (List[str] | None) – other short names (that will be prefixed with ‘-’) that will be accepted by the argparse CLI.

  • group (str | None) – Impacts display of underlying argparse object by grouping values with the same type together. There is no other impact.

  • mutex_group (str | None) – Indicates that only one of the values in a group should be given on the command line. This has no impact on python usage.

  • tags (Any) – for external program use

CommandLine

xdoctest -m /home/joncrall/code/scriptconfig/scriptconfig/value.py Value
xdoctest -m scriptconfig.value Value

Example

>>> self = Value(None, type=float)
>>> print('self.value = {!r}'.format(self.value))
self.value = None
>>> self.update('3.3')
>>> print('self.value = {!r}'.format(self.value))
self.value = 3.3
update(value)[source]
cast(value)[source]
copy()[source]
_to_value_kw()[source]

Used in port-to-dataconf and port-to-argparse

classmethod _from_action(action, actionid_to_groupkey, actionid_to_mgroupkey, pos_counter)[source]

Used in port_argparse

Example

import argparse from scriptconfig.value import * # NOQA action = argparse._StoreAction(‘foo’, ‘bar’, default=3) value = Value._from_action(action, {}, {}, 0)

action = argparse._CountAction(‘foo’, ‘bar’) value = Value._from_action(action, {}, {}, 0)

class scriptconfig.value.Flag(value=False, **kwargs)[source]

Bases: Value

Exactly the same as a Value except isflag default to True

class scriptconfig.value.Path(value=None, help=None, alias=None)[source]

Bases: Value

Note this is mean to be used only with scriptconfig.Config. It does NOT represent a pathlib object.

cast(value)[source]
class scriptconfig.value.PathList(value=None, type=None, help=None, choices=None, position=None, isflag=False, nargs=None, alias=None, required=False, short_alias=None, group=None, mutex_group=None, tags=None)[source]

Bases: Value

Can be specified as a list or as a globstr

FIXME:

will fail if there are any commas in the path name

Example

>>> from os.path import join
>>> path = ub.modname_to_modpath('scriptconfig', hide_init=True)
>>> globstr = join(path, '*.py')
>>> # Passing in a globstr is accepted
>>> assert len(PathList(globstr).value) > 0
>>> # Smartcast should separate these
>>> assert len(PathList('/a,/b').value) == 2
>>> # Passing in a list is accepted
>>> assert len(PathList(['/a', '/b']).value) == 2
cast(value=None)[source]
scriptconfig.value._value_add_argument_to_parser(value, _value, self, parser, key, fuzzy_hyphens=0)[source]

POC for a new simplified way for a value to add itself as an argument to a parser.

Parameters:
  • value (Any) – the unwrapped default value

  • _value (Value) – the value metadata

scriptconfig.value._value_add_argument_kw(value, _value, self, key, fuzzy_hyphens=0)[source]

TODO: resolve with _value_add_argument_to_parser(). This just creates one or more kwargs for add_argument. (Depending on how many variants of the argument we want).

Parameters:
  • value (Any) – the unwrapped default value

  • _value (Value) – the value metadata

Returns:

special keys to the method name, args, kwargs invocations.

Return type:

Dict[str, Tuple[str, Tuple, Dict]]

scriptconfig.value._resolve_alias(name, _value, fuzzy_hyphens)[source]
scriptconfig.value.scfg_isinstance(item, cls)[source]

use instead isinstance for scfg types when reloading

Parameters:
  • item (object) – instance to check

  • cls (type) – class to check against

Returns:

bool

scriptconfig.value._maker_smart_parse_action(self)[source]
class scriptconfig.value.CodeRepr[source]

Bases: str