NNConfig#

class nextnanopy.NNConfig(fullpath=None)#

Bases: Config

The nextnano configuration: where each product sits and how it is to be run.

One section per nextnano product. A simulator’s section holds exe, license, database, outputdirectory and whatever else that simulator takes, such as threads; nextnanoevo/nextnano_optimizers is not a simulator and holds license alone. Use get_options to see what a given section actually carries.

nextnanopy.config is the instance the whole process shares and the one to reach for; an InputFile takes a copy of it when it is built, or reads a file of its own when given configpath.

Parameters:
fullpathstr or pathlib.Path, default=None

Path to the configuration file. If omitted, .nextnanopy-config in the home directory is used.

Attributes:
fullpathstr or pathlib.Path

Path to the configuration file. Passing a path to save repoints it.

configdict

Validated value of every option, keyed by section and then by option.

sectionsdict_keys

Names of the sections, one per product. Read-only.

defaultsdict

Value every option falls back to, shaped like config.

default_fullpathpathlib.Path

Path used when none is given: .nextnanopy-config in the home directory.

validatorsdict

Callable applied to an option as it is read, keyed like config. Only threads has one, int.

configparserconfigparser.ConfigParser

The raw string values behind config.

Notes

Reading the file is not a read-only act. A file that is missing is written with the defaults.

Examples

>>> import nextnanopy
>>> config = nextnanopy.config
>>> config.get('nextnano++', 'threads')
0

A value that is only set reaches every input file built after it, and is forgotten when the process ends:

>>> config.set('nextnano++', 'threads', 4)
>>> config.get('nextnano++', 'threads')
4

Saving writes it to fullpath, so later processes start from it as well:

>>> config.save()
to_default()#

Put every option back to the value nextnanopy ships.

Reaches this process only; save writes it to the file, and reset does both. A section the defaults do not name, such as a product this version no longer knows, is left as it stands.

reset()#

Put every option back to the value nextnanopy ships and write the file.

to_default followed by save, so the configuration on disk is overwritten and the paths set there are gone.

get(section, option)#

Return the value of an option.

Parameters:
sectionstr

Name of the section, a nextnano product for NNConfig.

optionstr

Name of the option.

Returns:
object

The validated value: threads comes back as an int, the rest as str.

Raises:
KeyError

If there is no such section or option.

get_options(section)#

Return the options of a section together with their values.

Parameters:
sectionstr

Name of the section, a nextnano product for NNConfig.

Returns:
dict

Value of each option, keyed by option name. The stored mapping itself and not a copy, so editing it edits the configuration.

Raises:
KeyError

If there is no such section.

preview()#

Print every section with its options and their values.

save(fullpath=None)#

Write the configuration to a file.

Parameters:
fullpathstr or pathlib.Path, default=None

Where to write. If omitted the current fullpath is used; if given it becomes the new fullpath.

Raises:
ValueError

If any value contains a %. See the class Notes.

Notes

The file is written beside the target and moved into place, so a reader never meets a half-written file and a failure part way leaves the previous one intact.

set(section, option, value)#

Change the value of an option.

Parameters:
sectionstr

Name of the section, a nextnano product for NNConfig.

optionstr

Name of the option. One that the section does not already carry is added rather than refused, so a misspelling passes unnoticed.

valueobject

Value to store. The option’s validator is applied first, so threads is kept as an int whatever is handed in.

Raises:
KeyError

If there is no such section.

Notes

The change reaches this process only. save writes it to the file.