55import io
66
77def num_type (value ):
8- # Determine if a value is float, int or leave as string
8+
99
10+ # Determine if a value is float, int or leave as string
1011 if '.' in value :
1112 try : # Detect float
1213 value_out = float (value )
1314 return value_out
14-
15+
1516 except ValueError : # Otherwise leave as string
1617 value_out = value
1718 return value_out
18-
19+
1920 else :
2021
2122 try : # Detect int
2223 value_out = int (value )
2324 return value_out
24-
25+
2526 except ValueError : # Otherwise leave as string
2627 value_out = value
2728 return value_out
28-
29+
2930
3031def element_type (element ):
3132 # Determine if an element is a list then pass to num_type()
@@ -35,7 +36,7 @@ def element_type(element):
3536 element_out = []
3637 for val in values : # Determine datatype of each value
3738 element_out .append (num_type (val ))
38-
39+
3940 return element_out
4041
4142 else :
@@ -50,13 +51,13 @@ def parse_panond(fbuf):
5051 ----------
5152 fbuf : File-like object
5253 Buffer of a .pan or .ond file
53-
54+
5455 Returns
5556 -------
5657 comp : Nested Dictionary
57- Contents of the .pan or .ond file following the indentation of the file.
58- The value of datatypes are assumed during reading. The value units are
59- the default used by PVsyst.
58+ Contents of the .pan or .ond file following the indentation of the
59+ file. The value of datatypes are assumed during reading. The value
60+ units are the default used by PVsyst.
6061
6162 Raises
6263 ------
@@ -72,19 +73,27 @@ def parse_panond(fbuf):
7273 """
7374 comp = {} # Component
7475 dict_levels = [comp ]
75-
76+
7677 fbuf .seek (0 )
7778 lines = fbuf .getvalue ().splitlines ()
78- for i in range (0 , len (lines ) - 1 ): # Reading blank lines. Stopping one short to avoid index error. Last line never contains important data.
79- if lines [i ] == '' : # Skipping blank lines
80- continue
8179
82- indent_lvl_1 = (len (lines [i ]) - len (lines [i ].lstrip (' ' ))) // 2
83- indent_lvl_2 = (len (lines [i + 1 ]) - len (lines [i + 1 ].lstrip (' ' ))) // 2
84- line_data = lines [i ].split ('=' )
80+ for i in range (0 , len (lines ) - 1 ):
81+ if lines [i ] == '' : # Skipping blank lines
82+ continue
83+ # Reading blank lines. Stopping one short to avoid index error.
84+ # Last line never contains important data.
85+ indent_lvl_1 = (len (lines [i ]) - len (lines [i ].lstrip (' ' ))) // 2
86+ indent_lvl_2 = (len (lines [i + 1 ]) - len (lines [i + 1 ].lstrip (' ' ))) // 2
87+ line_data = lines [i ].split ('=' )
8588 key = line_data [0 ].strip ()
86- value = element_type (line_data [1 ].strip ()) if len (line_data ) > 1 else None
87- if indent_lvl_2 > indent_lvl_1 : # add a level to the dict. The key here will be ignored. Not vital to file function.
89+ # value = element_type(line_data[1].strip()) if len(line_data) > 1 else None
90+ if len (line_data ) > 1 :
91+ value = element_type (line_data [1 ].strip ())
92+ else :
93+ value = element_type = None
94+ # add a level to the dict. The key here will be ignored.
95+ # Not vital to file function.
96+ if indent_lvl_2 > indent_lvl_1 :
8897 current_level = dict_levels [indent_lvl_1 ]
8998 new_level = {}
9099 current_level [key ] = new_level
@@ -101,19 +110,20 @@ def parse_panond(fbuf):
101110
102111def read_panond (file ):
103112 """
104- Retrieve Module or Inverter data from a .pan or .ond text file, respectively.
113+ Retrieve Module or Inverter data from a .pan or .ond text file,
114+ respectively.
105115
106116 Parameters
107117 ----------
108118 file : string or path object
109119 Name or path of a .pan/.ond file
110-
120+
111121 Returns
112122 -------
113123 content : Nested Dictionary
114- Contents of the .pan or .ond file following the indentation of the file.
115- The value of datatypes are assumed during reading. The value units are
116- the default used by PVsyst.
124+ Contents of the .pan or .ond file following the indentation of the
125+ file. The value of datatypes are assumed during reading. The value
126+ units are the default used by PVsyst.
117127
118128 Raises
119129 ------
@@ -131,7 +141,7 @@ def read_panond(file):
131141 with open (file , "r" , encoding = 'utf-8-sig' ) as file :
132142 f_content = file .read ()
133143 fbuf = io .StringIO (f_content )
134-
144+
135145 content = parse_panond (fbuf )
136146
137147 return content
0 commit comments