Skip to content

Commit

Permalink
Add error message for cellnames
Browse files Browse the repository at this point in the history
  • Loading branch information
enekomartinmartinez committed Jul 8, 2024
1 parent 00e329d commit 00cc755
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
16 changes: 8 additions & 8 deletions docs/tables/get_functions.tab
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Vensim Vensim example Xmile Xmile example Abstract Syntax Python Translation Vensim comments Xmile comments Python comments
GET XLS DATA "GET XLS DATA('file', 'sheet', 'time_row_or_col', 'cell')" "GetDataStructure('file', 'sheet', 'time_row_or_col', 'cell')" pysd.external.ExtData(...)
GET DIRECT DATA "GET DIRECT DATA('file', 'sheet', 'time_row_or_col', 'cell')" "GetDataStructure('file', 'sheet', 'time_row_or_col', 'cell')" pysd.external.ExtData(...)
GET XLS LOOKUPS "GET XLS LOOKUPS('file', 'sheet', 'x_row_or_col', 'cell')" "GetLookupsStructure('file', 'sheet', 'x_row_or_col', 'cell')" pysd.external.ExtLookup(...)
GET DIRECT LOOKUPS "GET DIRECT LOOKUPS('file', 'sheet', 'x_row_or_col', 'cell')" "GetLookupsStructure('file', 'sheet', 'x_row_or_col', 'cell')" pysd.external.ExtLookup(...)
GET XLS CONSTANTS "GET XLS CONSTANTS('file', 'sheet', 'cell')" "GetConstantsStructure('file', 'sheet', 'cell')" pysd.external.ExtConstant(...)
GET DIRECT CONSTANTS "GET DIRECT CONSTANTS('file', 'sheet', 'cell')" "GetConstantsStructure('file', 'sheet', 'cell')" pysd.external.ExtConstant(...)
GET XLS SUBSCRIPT "GET XLS SUBSCRIPT('file', 'sheet', 'first_cell', 'last_cell', 'prefix')" pysd.external.ExtSubscript(...)
GET DIRECT SUBSCRIPT "GET DIRECT SUBSCRIPT('file', 'sheet', 'first_cell', 'last_cell', 'prefix')" pysd.external.ExtSubscript(...)
GET XLS DATA "GET XLS DATA('file', 'tab', 'time_row_or_col', 'cell')" "GetDataStructure('file', 'tab', 'time_row_or_col', 'cell')" pysd.external.ExtData(...)
GET DIRECT DATA "GET DIRECT DATA('file', 'tab', 'time_row_or_col', 'cell')" "GetDataStructure('file', 'tab', 'time_row_or_col', 'cell')" pysd.external.ExtData(...)
GET XLS LOOKUPS "GET XLS LOOKUPS('file', 'tab', 'x_row_or_col', 'cell')" "GetLookupsStructure('file', 'tab', 'x_row_or_col', 'cell')" pysd.external.ExtLookup(...)
GET DIRECT LOOKUPS "GET DIRECT LOOKUPS('file', 'tab', 'x_row_or_col', 'cell')" "GetLookupsStructure('file', 'tab', 'x_row_or_col', 'cell')" pysd.external.ExtLookup(...)
GET XLS CONSTANTS "GET XLS CONSTANTS('file', 'tab', 'cell')" "GetConstantsStructure('file', 'tab', 'cell')" pysd.external.ExtConstant(...)
GET DIRECT CONSTANTS "GET DIRECT CONSTANTS('file', 'tab', 'cell')" "GetConstantsStructure('file', 'tab', 'cell')" pysd.external.ExtConstant(...)
GET XLS SUBSCRIPT "GET XLS SUBSCRIPT('file', 'tab', 'first_cell', 'last_cell', 'prefix')" pysd.external.ExtSubscript(...)
GET DIRECT SUBSCRIPT "GET DIRECT SUBSCRIPT('file', 'tab', 'first_cell', 'last_cell', 'prefix')" pysd.external.ExtSubscript(...)
41 changes: 37 additions & 4 deletions pysd/py_backend/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

import re
import warnings
import pandas as pd

from openpyxl import load_workbook
from openpyxl.utils.exceptions import InvalidFileException

import numpy as np
import xarray as xr
from openpyxl import load_workbook
import pandas as pd

from . import utils
from .data import Data
from .lookups import Lookups
Expand All @@ -36,12 +40,16 @@ def read(cls, file_name, tab):
read_func = pd.read_excel
read_kwargs['sheet_name'] = tab
elif ext == '.csv':
# TODO test
read_func = pd.read_csv
if tab and not tab[0].isalnum():
# TODO test
read_kwargs['sep'] = tab
else:
# TODO test
read_func = pd.read_table
if tab and not tab[0].isalnum():
# TODO test
read_kwargs['sep'] = tab
# read the data
excel = np.array([
Expand Down Expand Up @@ -166,7 +174,18 @@ def _get_data_from_file_opyxl(self, cellname):
"""
# read data
excel = Excels.read_opyxl(self.file)
try:
excel = Excels.read_opyxl(self.file)
except InvalidFileException:
# TODO test
raise ValueError(
self.py_name + "\n"
f"Cannot read the file '{self.file}'...\n"
f"It could happen that cell='{cellname}' was "
"read as a cell range name due to a wrong "
"definition of cell value"
)

# Get global and local cellrange names
global_cellranges = excel.defined_names
local_cellranges = None
Expand Down Expand Up @@ -1069,12 +1088,16 @@ def get_subscripts_cell(self, row_first, col_first, lastcell):
read_func = pd.read_excel
read_kwargs['sheet_name'] = self.tab
elif ext == '.csv':
# TODO test
read_func = pd.read_csv
if self.tab and not self.tab[0].isalnum():
# TODO test
read_kwargs['sep'] = self.tab
else:
# TODO test
read_func = pd.read_table
if self.tab and not self.tab[0].isalnum():
# TODO test
read_kwargs['sep'] = self.tab

# read the data
Expand All @@ -1094,7 +1117,17 @@ def get_subscripts_cell(self, row_first, col_first, lastcell):

def get_subscripts_name(self, cellname):
"""Get subscripts from cell range name definition"""
excel = load_workbook(self.file, read_only=True, data_only=True)
try:
excel = load_workbook(self.file, read_only=True, data_only=True)
except InvalidFileException:
# TODO test
raise ValueError(
self.py_name + "\n"
f"Cannot read the file '{self.file}'...\n"
f"It could happen that firstcell='{cellname}' was "
"read as a cell range name due to a wrong definition "
"of cell value"
)
global_cellranges = excel.defined_names
local_cellranges = None
# need to lower the sheetnames as Vensim has no case sensitivity
Expand Down

0 comments on commit 00cc755

Please sign in to comment.