Skip to content

Commit 478a0e6

Browse files
authored
Improve code format & add tests (#246)
* Fix inconsistent indentation in tests * Fix flake8 warnings in tests * Add test for ModelicaSystem.setParameters() This test is currently failing, because getParameters() returns str instead of float. However, the documentation https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/ompython.html#simulation says it should return floats. * Change test_setParameters to match current behavior The documentation is wrong, as discussed in #244 * Add test for ModelicaSystem.setSimulationOptions() * Fix flake8 warnings in __init__.py * Fix flake8 warnings in OMTypedParser.py * Fix flake8 warnings in OMParser/__init__.py * Remove dependency on future I don't think this is needed now that python_requires is >=3.8. * Switch from setup.py to pyproject.toml pyproject.toml is preferred over setup.py for situations like this: https://packaging.python.org/en/latest/guides/modernize-setup-py-project/ * Use dependency list from pyproject.toml in CI * Run flake8 in CI
1 parent 5904e4e commit 478a0e6

17 files changed

+347
-312
lines changed

.github/workflows/FMITest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- name: Install dependencies
3939
run: |
4040
python -m pip install --upgrade pip
41-
pip install future pyparsing numpy psutil pyzmq pytest pytest-md pytest-emoji
41+
pip install . pytest pytest-md pytest-emoji
4242
4343
- name: Set timezone
4444
uses: szenius/set-timezone@v2.0

.github/workflows/Test.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ jobs:
3838
- name: Install dependencies
3939
run: |
4040
python -m pip install --upgrade pip
41-
pip install future pyparsing numpy psutil pyzmq pytest pytest-md pytest-emoji
41+
pip install . pytest pytest-md pytest-emoji flake8
4242
4343
- name: Set timezone
4444
uses: szenius/set-timezone@v2.0
4545
with:
4646
timezoneLinux: 'Europe/Berlin'
4747

48+
- name: Lint with flake8
49+
run: |
50+
flake8 . --count --statistics
51+
4852
- name: Run pytest
4953
uses: pavelzw/pytest-action@v2
5054
with:

OMPython/OMParser/__init__.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
Version: 1.0
3333
"""
3434

35-
from __future__ import absolute_import
36-
from __future__ import division
37-
from __future__ import print_function
38-
from builtins import int, range
39-
4035
import sys
4136

4237
result = dict()
@@ -207,8 +202,6 @@ def delete_elements(strings):
207202

208203

209204
def make_subset_sets(strings, name):
210-
index = 0
211-
anchor = 0
212205
main_set_name = "SET1"
213206
subset_name = "Subset1"
214207
set_name = "Set1"
@@ -285,8 +278,6 @@ def make_subset_sets(strings, name):
285278
def make_sets(strings, name):
286279
if strings == "{}":
287280
return
288-
index = 0
289-
anchor = 0
290281
main_set_name = "SET1"
291282
set_name = "Set1"
292283

@@ -415,7 +406,6 @@ def get_inner_sets(strings, for_this, name):
415406

416407

417408
def make_elements(strings):
418-
original_string = strings
419409
index = 0
420410
main_set_name = "SET1"
421411

@@ -563,7 +553,6 @@ def skip_all_inner_sets(position):
563553
max_count = main_count
564554
last_set = 0
565555
last_subset = 0
566-
last_brace = 0
567556
pos = position
568557

569558
while pos < len(string):
@@ -616,7 +605,6 @@ def skip_all_inner_sets(position):
616605
break
617606
elif ch == "(":
618607
brace_count += 1
619-
brace_start = position
620608
position += 1
621609
while position < end_of_main_set:
622610
s = string[position]
@@ -625,7 +613,6 @@ def skip_all_inner_sets(position):
625613
elif s == ")":
626614
brace_count -= 1
627615
if brace_count == 0:
628-
last_brace = position
629616
break
630617
elif s == "=" and string[position + 1] == "{":
631618
indx = position + 2
@@ -672,7 +659,6 @@ def skip_all_inner_sets(position):
672659
break
673660
elif ch == "(":
674661
brace_count += 1
675-
brace_start = position
676662
position += 1
677663
while position < end_of_main_set:
678664
s = string[position]
@@ -681,13 +667,11 @@ def skip_all_inner_sets(position):
681667
elif s == ")":
682668
brace_count -= 1
683669
if brace_count == 0:
684-
last_brace = position
685670
break
686671
position += 1
687672
position += 1
688673
elif char == "(":
689674
brace_count += 1
690-
brace_start = position
691675
position += 1
692676
while position < end_of_main_set:
693677
s = string[position]
@@ -696,7 +680,6 @@ def skip_all_inner_sets(position):
696680
elif s == ")":
697681
brace_count -= 1
698682
if brace_count == 0:
699-
last_brace = position
700683
break
701684
position += 1
702685

OMPython/OMTypedParser.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
from __future__ import absolute_import
4-
from __future__ import division
5-
from __future__ import print_function
6-
from builtins import int, range
7-
83
__author__ = "Anand Kalaiarasi Ganeson, ganan642@student.liu.se, 2012-03-19, and Martin Sjölund"
94
__license__ = """
105
This file is part of OpenModelica.
@@ -59,6 +54,7 @@
5954

6055
import sys
6156

57+
6258
def convertNumbers(s, l, toks):
6359
n = toks[0]
6460
try:
@@ -75,7 +71,8 @@ def convertString2(s, s2):
7571
tmp = tmp.replace("\n", "\\n")
7672
tmp = tmp.replace("\r", "\\r")
7773
tmp = tmp.replace("\t", "\\t")
78-
return "'"+tmp+"'";
74+
return "'"+tmp+"'"
75+
7976

8077
def convertString(s, s2):
8178
return s2[0].replace("\\\"", '"')
@@ -89,7 +86,6 @@ def convertTuple(t):
8986
return tuple(t[0])
9087

9188

92-
9389
def evaluateExpression(s, loc, toks):
9490
# Convert the tokens (ParseResults) into a string expression
9591
flat_list = [item for sublist in toks[0] for item in sublist]
@@ -100,6 +96,7 @@ def evaluateExpression(s, loc, toks):
10096
except Exception:
10197
return expr
10298

99+
103100
# Number parsing (supports arithmetic expressions in dimensions) (e.g., {1 + 1, 1})
104101
arrayDimension = infixNotation(
105102
Word(alphas + "_", alphanums + "_") | Word(nums),
@@ -123,7 +120,7 @@ def evaluateExpression(s, loc, toks):
123120
Optional('.' + Word(nums)) +
124121
Optional(Word('eE', exact=1) + Word(nums + '+-', nums)))
125122

126-
#ident = Word(alphas + "_", alphanums + "_") | Combine("'" + Word(alphanums + "!#$%&()*+,-./:;<>=?@[]^{}|~ ") + "'")
123+
# ident = Word(alphas + "_", alphanums + "_") | Combine("'" + Word(alphanums + "!#$%&()*+,-./:;<>=?@[]^{}|~ ") + "'")
127124
ident = Word(alphas + "_", alphanums + "_") | QuotedString(quoteChar='\'', escChar='\\').setParseAction(convertString2)
128125
fqident = Forward()
129126
fqident << ((ident + "." + fqident) | ident)
@@ -143,7 +140,7 @@ def evaluateExpression(s, loc, toks):
143140
def parseString(string):
144141
res = omcGrammar.parseString(string)
145142
if len(res) == 0:
146-
return
143+
return
147144
return res[0]
148145

149146

0 commit comments

Comments
 (0)