Skip to content
Snippets Groups Projects
Commit 8256c8d9 authored by Mads M. Pedersen's avatar Mads M. Pedersen
Browse files

htcfile now inserts new sections before "exit;" + more examples in documentation

parent 04684b91
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -35,6 +35,37 @@ class HTCFile(HTCContents, HTCDefaults):
>>> htcfile = HTCFile('htc/test.htc')
>>> htcfile.wind.wsp = 10
>>> htcfile.save()
#---------------------------------------------
>>> htc = HTCFile(filename=None, modelpath=None) # create minimal htcfile
#Add section
>>> htc.add_section('hydro')
#Add subsection
>>> htc.hydro.add_section("hydro_element")
#Set values
>>> htc.hydro.hydro_element.wave_breaking = [2, 6.28, 1] # or
>>> htc.hydro.hydro_element.wave_breaking = 2, 6.28, 1
#Set comments
>>> htc.hydro.hydro_element.wave_breaking.comments = "This is a comment"
#Access section
>>> hydro_element = htc.hydro.hydro_element #or
>>> hydro_element = htc['hydro.hydro_element'] # or
>>> hydro_element = htc['hydro/hydro_element'] # or
>>> print (hydro_element.wave_breaking) #string represenation
wave_breaking 2 6.28 1; This is a comment
>>> print (hydro_element.wave_breaking.name_) # command
wave_breaking
>>> print (hydro_element.wave_breaking.values) # values
[2, 6.28, 1
>>> print (hydro_element.wave_breaking.comments) # comments
This is a comment
>>> print (hydro_element.wave_breaking[0]) # first value
2
"""
filename = None
......@@ -96,9 +127,9 @@ class HTCFile(HTCContents, HTCDefaults):
self._add_contents(HTCSection.from_lines(lines))
else:
line = HTCLine.from_lines(lines)
self._add_contents(line)
if line.name_ == "exit":
break
self._add_contents(line)
def reset(self):
......@@ -146,7 +177,7 @@ class HTCFile(HTCContents, HTCDefaults):
def __str__(self):
self.contents #load
return "".join(self.initial_comments + [c.__str__(1) for c in self])
return "".join(self.initial_comments + [c.__str__(1) for c in self]+ ["exit;"])
def save(self, filename=None):
self.contents #load if not loaded
......
......@@ -34,7 +34,7 @@ class TestHtcFile(unittest.TestCase):
def check_htc_file(self, f):
with open(f) as fid:
orglines = fid.readlines()
orglines = fid.read().strip().split("\n")
htcfile = HTCFile(f,"../")
newlines = str(htcfile).split("\n")
......@@ -131,6 +131,17 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(htcfile.wind.mann.create_turb_parameters[0], 29.4)
self.assertEqual(htcfile.wind.mann.create_turb_parameters[3], 1004)
self.assertEqual(htcfile.wind.mann.create_turb_parameters.comments, "L, alfaeps, gamma, seed, highfrq compensation")
def test_add_section2(self):
htcfile = HTCFile()
htcfile.add_section('hydro')
#self.assertEqual(str(htcfile).strip()[-5:], "exit;")
htcfile = HTCFile(self.testfilepath + "test.htc")
htcfile.add_section('hydro')
self.assertEqual(str(htcfile).strip()[-5:], "exit;")
def test_add_mann(self):
htcfile = HTCFile()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment