Skip to content

Commit

Permalink
Make excels2vensim work with latest PySD and openpyxl versions
Browse files Browse the repository at this point in the history
  • Loading branch information
enekomartinmartinez committed Oct 9, 2023
1 parent fac9d1c commit 1e9c164
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ['3.7', '3.8', '3.11']
python-version: ['3.9', '3.11']

steps:
- uses: actions/checkout@v3
Expand All @@ -39,7 +39,7 @@ jobs:
uses: coverallsapp/github-action@v2
with:
file: coverage.xml
if: ${{ matrix.python-version == 3.7 && matrix.os == 'ubuntu-latest' }}
if: ${{ matrix.python-version == 3.9 && matrix.os == 'ubuntu-latest' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

4 changes: 2 additions & 2 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name: Upload Python Package
on:
release:
types: [created]

jobs:
deploy:

Expand All @@ -17,7 +17,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: '3.7'
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ This library is able to automate the generation of the following type of equatio

## Requirements

- Python 3.7+
- PySD 3.0+
- Python 3.9+
- PySD 3.12+

## Resources

Expand Down
4 changes: 2 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ The `original code for excels2vensim is available at GitHub <https://github.com/

Requirements
------------
* Python 3.7+
* PySD 3.0+
* Python 3.9+
* PySD 3.12+

Contents
--------
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ From the project's main directory, use the following command to install it:
Required Dependencies
---------------------
*excels2vensim* uses `PySD <https://pysd.readthedocs.io>`_ library for reading subscripts from Vensims model file. It requires at least **Python 3.7** and **PySD 3.0**.
*excels2vensim* uses `PySD <https://pysd.readthedocs.io>`_ library for reading subscripts from Vensims model file. It requires at least **Python 3.9** and **PySD 3.12**.

If not installed, *PySD* should be built automatically if you are installing via `pip`, using `conda`, or from source.
2 changes: 1 addition & 1 deletion excels2vensim/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.13.1"
__version__ = "0.14.0"
17 changes: 9 additions & 8 deletions excels2vensim/excels2vensim.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,26 +376,27 @@ def _write_cellrange(name, file, sheet, cellrange, force):
"""
wb = Excels.read(file)
sheetId = [sheetname_wb.lower() for sheetname_wb
in wb.sheetnames].index(sheet.lower())
for sheetId, sheet1 in enumerate(wb.sheetnames):
if sheet1.lower() == sheet.lower():
local_cellranges = wb[sheet1].defined_names
break

existing_names = wb.defined_names.localnames(sheetId)
if name in existing_names:
if wb.defined_names.get(name, sheetId).attr_text == cellrange:
if name in local_cellranges:
if local_cellranges.get(name).attr_text == cellrange:
# cellrange already defined with same name and coordinates
return
elif force:
wb.defined_names.delete(name, sheetId)
del local_cellranges[name]
else:
raise ValueError(
f"\nTrying to write a cellrange with name '{name}' at "
+ f"'{cellrange}'. However, '{name}' already exist in "
+ f"'{wb.defined_names.get(name, sheetId).attr_text}'\n"
+ f"'{local_cellranges.get(name).attr_text}'\n"
+ "Use force=True to overwrite it.")

new_range = DefinedName(
name, attr_text=cellrange, localSheetId=sheetId)
wb.defined_names.append(new_range)
local_cellranges.add(new_range)

@staticmethod
def _col_to_num(col):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pysd>=3.0
pysd>=3.12
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
setup(
name='excels2vensim',
version=__version__,
python_requires='>=3.7',
python_requires='>=3.9',
author='Eneko Martin Martinez',
author_email='[email protected]',
packages=find_packages(exclude=['docs', 'tests', 'dist', 'build']),
Expand All @@ -23,8 +23,6 @@

'License :: OSI Approved :: MIT License',

'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
Expand Down
2 changes: 0 additions & 2 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@
python_files = tests_*.py
filterwarnings =
error
ignore:distutils Version classes are deprecated. Use packaging.version instead
ignore:\n\nPython 3.7 will no longer be supported by PySD in future releases.:FutureWarning

10 changes: 6 additions & 4 deletions tests/tests_excels2vensim.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,9 @@ def test_force(tmp_path, _root):
e2v.execute(element_dict)

wb = load_workbook("inputs_force.xlsx")
assert "my_var" in wb.defined_names.localnames(0)
assert wb.defined_names.get("my_var", 0).attr_text == 'Region1!$A$10:$A$10'
ws = wb["Region1"]
assert "my_var" in ws.defined_names
assert ws.defined_names.get("my_var").attr_text == 'Region1!$A$10:$A$10'
wb.close()

element_dict["my_var"]["cell"] = "B34"
Expand All @@ -376,6 +377,7 @@ def test_force(tmp_path, _root):
e2v.execute(element_dict)

wb = load_workbook("inputs_force.xlsx")
assert "my_var" in wb.defined_names.localnames(0)
assert wb.defined_names.get("my_var", 0).attr_text == 'Region1!$B$34:$B$34'
ws = wb["Region1"]
assert "my_var" in ws.defined_names
assert ws.defined_names.get("my_var").attr_text == 'Region1!$B$34:$B$34'
wb.close()
20 changes: 9 additions & 11 deletions tests/tests_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,17 @@ def test_write_cell_range(tmp_path, _root):

assert file in e2v.Excels._Excels
wb = e2v.Excels._Excels[file]
ws = wb['Sheet1']

sheetId = [sheetname_wb.lower() for sheetname_wb
in wb.sheetnames].index(sheet.lower())

assert name in wb.defined_names.localnames(sheetId)
assert wb.defined_names.get(name, sheetId).attr_text == 'Sheet1!$A$1:$B$2'
assert name in ws.defined_names
assert ws.defined_names.get(name).attr_text == 'Sheet1!$A$1:$B$2'

# write same cellrange (pass)
write_cellrange(name, file, sheet,
sheet+'!$A$1:$B$2', False)

assert name in wb.defined_names.localnames(sheetId)
assert wb.defined_names.get(name, sheetId).attr_text == 'Sheet1!$A$1:$B$2'
assert name in ws.defined_names
assert ws.defined_names.get(name).attr_text == 'Sheet1!$A$1:$B$2'

# write cellrange with different values (error)
expected = f"\nTrying to write a cellrange with name '{name}'"\
Expand All @@ -219,15 +217,15 @@ def test_write_cell_range(tmp_path, _root):
write_cellrange(name, file, sheet,
sheet+'!$A$3:$B$4', False)

assert name in wb.defined_names.localnames(sheetId)
assert wb.defined_names.get(name, sheetId).attr_text == 'Sheet1!$A$1:$B$2'
assert name in ws.defined_names
assert ws.defined_names.get(name).attr_text == 'Sheet1!$A$1:$B$2'

# write cellrange with different values (force to remove old)
write_cellrange(name, file, sheet,
sheet+'!$A$3:$B$4', True)

assert name in wb.defined_names.localnames(sheetId)
assert wb.defined_names.get(name, sheetId).attr_text == 'Sheet1!$A$3:$B$4'
assert name in ws.defined_names
assert ws.defined_names.get(name).attr_text == 'Sheet1!$A$3:$B$4'

# close file
e2v.Excels.save_and_close()
Expand Down

0 comments on commit 1e9c164

Please sign in to comment.