template_class.py
Go to the documentation of this file.
1 # Author: Moritz Reichert
2 # Date : 26.09.2024
3 import numpy as np
4 
5 
6 class template(object):
7  """
8  Class to read a WinNet template file.
9  """
10 
11  def __init__(self, path):
12  """
13  Initialize the template class.
14  """
15  self.path = path
16 
17 
18  def read_data(self):
19  """
20  Read the data from the template file and store it in a dictionary.
21  """
22  # Create an empty dictionary to store the entries
23  self.__entries = {}
24 
25  # Read the data from the file
26  with open(self.path, 'r') as f:
27  self.data = f.readlines()
28  for line in self.data:
29  if line.strip().startswith('#'):
30  continue
31 
32  if line.strip() =="":
33  continue
34 
35  key = line.split("=")[0].strip()
36  value = line.split("=")[1].strip().replace('"', '').replace("'", "")
37  self.__entries[key] = value
38 
39  @property
40  def entries(self):
41  """
42  Get the entries of the template file.
43  """
44  # Check if entry exists.
45  #print all attributes of the object
46  if not hasattr(self, '_template__entries'):
47  self.read_data()
48  return self.__entries
49 
50 
51  def __getitem__(self, key):
52  """
53  Get the value of a specific key.
54  """
55  if not hasattr(self, '_template__entries'):
56  self.read_data()
57  return self.entries[key]
58 
59  def __setitem__(self, key, value):
60  """
61  Set the value of a specific key.
62  """
63  if not hasattr(self, '_template__entries'):
64  self.read_data()
65  self.__entries[key] = value
66 
67 
68  def save_template(self, path, winnet_path=None):
69  """
70  Save the template file.
71  """
72  with open(path, 'w') as f:
73  for key, value in self.entries.items():
74  if winnet_path is None:
75  f.write(f"{key} = {value}\n")
76  else:
77  entry = str(value).replace("@WINNET@",winnet_path)
78  f.write(f"{key} = {entry}\n")
79 
80 
81 if __name__ == '__main__':
82  # Example:
83  path = '/home/mreichert/data/Networks/comparison_winNet/WinNet-dev/par/NSE_comp.par'
84  t = template(path)
85  print(t["isotopes_file"])
86  t.save_template('test.par', winnet_path='../../runs/winnet')
src_files.template_class.template.__init__
def __init__(self, path)
Definition: template_class.py:11
src_files.template_class.template.save_template
def save_template(self, path, winnet_path=None)
Definition: template_class.py:68
src_files.template_class.template.__setitem__
def __setitem__(self, key, value)
Definition: template_class.py:59
src_files.template_class.template.path
path
Definition: template_class.py:15
src_files.template_class.template
Definition: template_class.py:6
src_files.template_class.template.read_data
def read_data(self)
Definition: template_class.py:18
src_files.template_class.template.__entries
__entries
Definition: template_class.py:23
src_files.template_class.template.data
data
Definition: template_class.py:27
src_files.template_class.template.__getitem__
def __getitem__(self, key)
Definition: template_class.py:51
src_files.template_class.template.entries
def entries(self)
Definition: template_class.py:40