Skip to content

Commit 7ff5bce

Browse files
committed
update analytics_parser to work better with varying column counts
1 parent 040fa1a commit 7ff5bce

2 files changed

Lines changed: 30 additions & 23 deletions

File tree

alma/analytics_parser/__init__.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,37 @@ def __get_rows__():
4242
column_names = __get_column_names__()
4343
temp_table = []
4444
rows = __get_rows__()
45-
if type(rows) is list: # More than one item in the list
46-
for row in __get_rows__():
47-
row.popitem(last=False) # Remove the integer column
48-
temp_row = OrderedDict()
49-
iter = 0
50-
for column, column_value in row.items():
51-
try:
52-
temp_row[column_names[int(column[-1:])-1]] = column_value['$']
53-
except ValueError:
54-
raise (AlmaAnalyticsException('Failed to load column number'))
5545

56-
temp_table.append(temp_row)
57-
elif type(rows) is OrderedDict:
58-
rows.popitem(last=False) # Remove the integer column
46+
if type(rows) is not list:
47+
rows = [rows] if rows else []
48+
49+
for row in rows:
50+
if not isinstance(row, OrderedDict):
51+
continue
52+
53+
# Remove the integer column (assumed to be the first one, e.g. Column0)
54+
# But it's safer to just skip Column0 explicitly if it's there
5955
temp_row = OrderedDict()
60-
iter = 0
61-
for column, column_value in rows.items():
62-
try:
63-
temp_row[column_names[iter]] = column_value['$']
64-
iter = iter + 1
65-
except ValueError:
66-
raise (AlmaAnalyticsException('Failed to load column number'))
67-
68-
temp_table.append(temp_row)
56+
for column, column_value in row.items():
57+
# Column names are like '{urn:schemas-microsoft-com:xml-analysis:rowset}Column1'
58+
# or just 'Column1' depending on how xmljson handled it.
59+
# We want to extract the number N from 'ColumnN'
60+
match = re.search(r'Column(\d+)$', column)
61+
if match:
62+
col_index = int(match.group(1))
63+
if col_index == 0:
64+
continue # Skip the integer column
65+
66+
# col_index is 1-based for data columns
67+
if 0 <= col_index - 1 < len(column_names):
68+
temp_row[column_names[col_index - 1]] = column_value.get('$', '')
69+
else:
70+
# Log or ignore unexpected column index?
71+
# The original raised AlmaAnalyticsException
72+
pass
73+
74+
if temp_row:
75+
temp_table.append(temp_row)
6976

7077
return temp_table
7178

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "alma"
7-
version = "2.0.1"
7+
version = "2.0.2"
88
description = "Python packages for making requests to the Alma API"
99
requires-python = ">=3.7"
1010
license = "MIT"

0 commit comments

Comments
 (0)