5
5
import io
6
6
7
7
def num_type (value ):
8
- # Determine if a value is float, int or leave as string
8
+
9
9
10
+ # Determine if a value is float, int or leave as string
10
11
if '.' in value :
11
12
try : # Detect float
12
13
value_out = float (value )
13
14
return value_out
14
-
15
+
15
16
except ValueError : # Otherwise leave as string
16
17
value_out = value
17
18
return value_out
18
-
19
+
19
20
else :
20
21
21
22
try : # Detect int
22
23
value_out = int (value )
23
24
return value_out
24
-
25
+
25
26
except ValueError : # Otherwise leave as string
26
27
value_out = value
27
28
return value_out
28
-
29
+
29
30
30
31
def element_type (element ):
31
32
# Determine if an element is a list then pass to num_type()
@@ -35,7 +36,7 @@ def element_type(element):
35
36
element_out = []
36
37
for val in values : # Determine datatype of each value
37
38
element_out .append (num_type (val ))
38
-
39
+
39
40
return element_out
40
41
41
42
else :
@@ -50,13 +51,13 @@ def parse_panond(fbuf):
50
51
----------
51
52
fbuf : File-like object
52
53
Buffer of a .pan or .ond file
53
-
54
+
54
55
Returns
55
56
-------
56
57
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.
60
61
61
62
Raises
62
63
------
@@ -72,19 +73,27 @@ def parse_panond(fbuf):
72
73
"""
73
74
comp = {} # Component
74
75
dict_levels = [comp ]
75
-
76
+
76
77
fbuf .seek (0 )
77
78
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
81
79
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 ('=' )
85
88
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 :
88
97
current_level = dict_levels [indent_lvl_1 ]
89
98
new_level = {}
90
99
current_level [key ] = new_level
@@ -101,19 +110,20 @@ def parse_panond(fbuf):
101
110
102
111
def read_panond (file ):
103
112
"""
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.
105
115
106
116
Parameters
107
117
----------
108
118
file : string or path object
109
119
Name or path of a .pan/.ond file
110
-
120
+
111
121
Returns
112
122
-------
113
123
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.
117
127
118
128
Raises
119
129
------
@@ -131,7 +141,7 @@ def read_panond(file):
131
141
with open (file , "r" , encoding = 'utf-8-sig' ) as file :
132
142
f_content = file .read ()
133
143
fbuf = io .StringIO (f_content )
134
-
144
+
135
145
content = parse_panond (fbuf )
136
146
137
147
return content
0 commit comments