PyFrag
Public Member Functions | Public Attributes | List of all members
qmworks.plams.tools.kftools.KFFile Class Reference
Inheritance diagram for qmworks.plams.tools.kftools.KFFile:
Inheritance graph
[legend]
Collaboration diagram for qmworks.plams.tools.kftools.KFFile:
Collaboration graph
[legend]

Public Member Functions

def __init__ (self, path, autosave=True)
 
def read (self, section, variable)
 
def write (self, section, variable, value)
 
def save (self)
 
def delete_section (self, section)
 
def __getitem__ (self, name)
 
def __setitem__ (self, name, value)
 
def __iter__ (self)
 

Public Attributes

 autosave
 
 path
 
 tmpdata
 
 reader
 

Detailed Description

A class for reading and writing binary files in KF format.

This class acts as a wrapper around |KFReader| collecting all the data written by user in some "temporary zone" and using Fortran binaries ``udmpkf`` and ``cpkf`` to write this data to the physical file when needed.

The constructor argument *path* should be a string with a path to an existing KF file or a new KF file that you wish to create. If a path to existing file is passed, new |KFReader| instance is created allowing to read all the data from this file.

When :meth:`~KFFile.write` method is used, the new data is not immediately written to a disk. Instead of that, it is temporarily stored in ``tmpdata`` dictionary. When method :meth:`~KFFile.save` is invoked, contents of that dictionary are written to a physical file and ``tmpdata`` is emptied.

Other methods like :meth:`~KFFile.read` or :meth:`~KFFile.delete_section` are aware of ``tmpdata`` and work flawlessly, regardless if :meth:`~KFFile.save` was called or not.

By default, :meth:`~KFFile.save` is automatically invoked after each :meth:`~KFFile.write`, so physical file on a disk is always "actual". This behavior can be adjusted with *autosave* constructor parameter. Having autosave enabled is usually a good idea, however, if you need to write a lot of small pieces of data to your file, the overhead of calling ``udmpkf`` and ``cpkf`` after *every* :meth:`~KFFile.write` can lead to significant delays. In such a case it is advised to disable autosave and call :meth:`~KFFile.save` manually, when needed.

Dictionary-like bracket notation can be used as a shortcut to read and write variables::

    mykf = KFFile('someexistingkffile.kf')
    #all three below are equivalent
    x = mykf['General%Termination Status']
    x = mykf[('General','Termination Status')]
    x = mykf.read('General','Termination Status')

    #all three below are equivalent
    mykf['Geometry%xyz'] = somevariable
    mykf[('Geometry','xyz')] = somevariable
    mykf.write('Geometry','xyz', somevariable)

Member Function Documentation

◆ __getitem__()

def qmworks.plams.tools.kftools.KFFile.__getitem__ (   self,
  name 
)
Allow to use ``x = mykf['section%variable']`` or ``x = mykf[('section','variable')]`` instead of ``x = kf.read('section', 'variable')``.
Here is the call graph for this function:

◆ __iter__()

def qmworks.plams.tools.kftools.KFFile.__iter__ (   self)
Iteration yields pairs of section name and variable name.

◆ __setitem__()

def qmworks.plams.tools.kftools.KFFile.__setitem__ (   self,
  name,
  value 
)
Allow to use ``mykf['section%variable'] = value`` or ``mykf[('section','variable')] = value`` instead of ``kf.write('section', 'variable', value)``.
Here is the call graph for this function:

◆ delete_section()

def qmworks.plams.tools.kftools.KFFile.delete_section (   self,
  section 
)
Delete the entire *section* from this KF file.

◆ read()

def qmworks.plams.tools.kftools.KFFile.read (   self,
  section,
  variable 
)
Extract and return data for a *variable* located in a *section*.

For single-value numerical or boolean variables returned value is a single number or bool. For longer variables this method returns a list of values. For string variables a single string is returned.
Here is the caller graph for this function:

◆ save()

def qmworks.plams.tools.kftools.KFFile.save (   self)
Save all changes stored in ``tmpdata`` to physical file on a disk.
Here is the caller graph for this function:

◆ write()

def qmworks.plams.tools.kftools.KFFile.write (   self,
  section,
  variable,
  value 
)
Write a *variable* with a *value* in a *section* . If such a variable already exists in this section, the old value is overwritten.
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: