NNConfig#
- class nextnanopy.NNConfig(fullpath=None)#
Bases:
ConfigThe 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,outputdirectoryand whatever else that simulator takes, such asthreads;nextnanoevo/nextnano_optimizersis not a simulator and holdslicensealone. Useget_optionsto see what a given section actually carries.nextnanopy.configis the instance the whole process shares and the one to reach for; anInputFiletakes a copy of it when it is built, or reads a file of its own when givenconfigpath.- Parameters:
- fullpathstr or pathlib.Path, default=None
Path to the configuration file. If omitted,
.nextnanopy-configin the home directory is used.
- Attributes:
- fullpathstr or pathlib.Path
Path to the configuration file. Passing a path to
saverepoints 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-configin the home directory.- validatorsdict
Callable applied to an option as it is read, keyed like
config. Onlythreadshas 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;
savewrites it to the file, andresetdoes 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_defaultfollowed bysave, 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:
threadscomes back as anint, the rest asstr.
- 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
fullpathis used; if given it becomes the newfullpath.
- 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
threadsis kept as anintwhatever is handed in.
- Raises:
- KeyError
If there is no such section.
Notes
The change reaches this process only.
savewrites it to the file.