Skip to content

Commit 654d1a0

Browse files
committed
Merge branch 'main' into better_direct
2 parents dac39e0 + 73c9d3d commit 654d1a0

File tree

4 files changed

+116
-21
lines changed

4 files changed

+116
-21
lines changed

.github/workflows/deploy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Build wheels
2525
run: python -m cibuildwheel --output-dir wheelhouse
2626
env:
27-
CIBW_PROJECT_REQUIRES_PYTHON: ">=3.7"
27+
CIBW_PROJECT_REQUIRES_PYTHON: ">=3.8"
2828

2929
- uses: actions/upload-artifact@v4
3030
with:

tests/test_table.py

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33

44
from xdeps import Table
55

6-
data = {
7-
"name": np.array(["ip1", "ip2", "ip2", "ip3", "tab$end"]),
8-
"s": np.array([1.0, 2.0, 2.1, 3.0, 4.0]),
9-
"betx": np.array([4.0, 5.0, 5.1, 6.0, 7.0]),
10-
"bety": np.array([2.0, 3.0, 3.1, 4.0, 9.0]),
11-
}
126

7+
def get_a_table():
8+
data = {
9+
"name": np.array(["ip1", "ip2", "ip2", "ip3", "tab$end"]),
10+
"s": np.array([1.0, 2.0, 2.1, 3.0, 4.0]),
11+
"betx": np.array([4.0, 5.0, 5.1, 6.0, 7.0]),
12+
"bety": np.array([2.0, 3.0, 3.1, 4.0, 9.0]),
13+
}
14+
t = Table(data)
15+
return t, data
1316

14-
## Table tests
15-
16-
t = Table(data)
17-
17+
t, data = get_a_table()
1818

1919
def test_table_initialization():
2020
# Valid initialization
@@ -117,6 +117,48 @@ def test_table_getitem_edge_cases():
117117
assert t[("betx",)][2] == t["betx"][2]
118118

119119

120+
def test_table_setitem_col():
121+
t, data = get_a_table()
122+
t["2betx"] = t["betx"] * 2
123+
assert np.array_equal(t["2betx"], data["betx"] * 2)
124+
t["betx"] = 1
125+
assert np.array_equal(t["betx"], np.ones(len(data["betx"])))
126+
t["name"] = t["name"] * 2
127+
assert np.all(t['name'] == [x * 2 for x in data['name']])
128+
129+
130+
def test_table_setitem_col_row():
131+
t, data = get_a_table()
132+
t["betx", 1] = 10
133+
assert t["betx", 1] == 10
134+
t["betx", "ip2"] = 20
135+
assert t["betx", "ip2"] == 20
136+
t["betx", "ip2::1"] = 30
137+
assert t["betx", "ip2::1"] == 30
138+
t["betx", "ip2<<1"] = 40
139+
assert t["betx", "ip2<<1"] == 40
140+
t["betx", "ip2::1>>1"] = 50
141+
assert t["betx", "ip2::1>>1"] == 50
142+
t["betx", "ip2::1>>1"] = 50
143+
assert t["betx", "ip2::1>>1"] == 50
144+
t["betx", "ip2::1>>1"] = 50
145+
assert t["betx", "ip2::1>>1"] == 50
146+
t["betx", "ip2::1>>1"] = 50
147+
assert t["betx", "ip2::1>>1"] == 50
148+
t["betx", "ip2::1>>1"] = 50
149+
assert t["betx", "ip2::1>>1"] == 50
150+
t["betx", "ip2::1>>1"] = 50
151+
assert t["betx", "ip2::1>>1"] == 50
152+
t["betx", "ip2::1>>1"] = 50
153+
assert t["betx", "ip2::1>>1"] == 50
154+
t["betx", "ip2::1>>1"] = 50
155+
assert t["betx", "ip2::1>>1"] == 50
156+
t["betx", "ip2::1>>1"] = 50
157+
assert t["betx", "ip2::1>>1"] == 50
158+
t["betx", "ip2::1>>1"] = 50
159+
assert t["betx", "ip2::1>>1"] == 50
160+
161+
120162
def test_table_numpy_string():
121163
tab = Table(dict(name=np.array(["a", "b$b"]), val=np.array([1, 2])))
122164
assert tab["val", tab.name[1]] == 2
@@ -242,6 +284,29 @@ def test_table_show():
242284
assert table.show(output=str) == "name value\na 1\nb 2\nc 3"
243285

244286

287+
def test_table_show_rows():
288+
data = {"name": np.array(["a", "b", "c"]), "value": np.array([1, 2, 3])}
289+
table = Table(data)
290+
assert table.show(rows=1, output=str) == "name value\nb 2"
291+
292+
293+
def test_table_show_cols():
294+
data = {"name": np.array(["a", "b", "c"]), "value": np.array([1, 2, 3])}
295+
table = Table(data)
296+
assert (
297+
table.show(cols="value", output=str)
298+
== "name value\na 1\nb 2\nc 3"
299+
)
300+
301+
302+
def test_table_show_rows_cols():
303+
t, data = get_a_table()
304+
assert (
305+
t.show(rows="ip2.*", cols="betx", output=str)
306+
== "name betx\nip1 5\nip2::0 5.1"
307+
)
308+
309+
245310
## Table cols tests
246311

247312

xdeps/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.8.2'
1+
__version__ = '0.8.3'

xdeps/table.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ def _get_row_indices(self, row):
443443
return np.array(out, dtype=int)
444444
else:
445445
raise ValueError(f"Invalid row selector {row}")
446+
elif row is None:
447+
return slice(None)
446448
else:
447449
return [self._get_row_index(row)]
448450

@@ -477,7 +479,10 @@ def _select(self, rows, cols):
477479
col_list.insert(0, self._index)
478480
data = {}
479481
for cc in col_list:
480-
data[cc] = eval(cc, gblmath, view)
482+
try:
483+
data[cc] = view[cc]
484+
except KeyError:
485+
data[cc] = eval(cc, gblmath, view)
481486
for kk in self.keys(exclude_columns=True):
482487
data[kk] = self._data[kk]
483488
return self.__class__(
@@ -543,7 +548,7 @@ def __getitem__(self, args):
543548
return self._data[args]
544549
except KeyError:
545550
return eval(args, gblmath, self._data)
546-
if type(args) is tuple: # multiple args
551+
if isinstance(args,tuple): # multiple args
547552
if len(args) == 0:
548553
col = None
549554
row = None
@@ -824,14 +829,39 @@ def __setitem__(self, key, val):
824829
object.__setattr__(self, "_index_cache", None)
825830
object.__setattr__(self, "_count_cache", None)
826831
object.__setattr__(self, "_name_cache", None)
827-
if key in self.__dict__:
828-
object.__setattr__(self, key, val)
829-
elif key in self._col_names:
830-
self._data[key][:] = val
832+
833+
if isinstance(key, str):
834+
if key in self.__dict__:
835+
object.__setattr__(self, key, val)
836+
elif key in self._col_names:
837+
self._data[key][:] = val
838+
else:
839+
self._data[key] = val
840+
if hasattr(val, "__iter__") and len(val) == len(self):
841+
self._col_names.append(key)
842+
elif isinstance(key,tuple):
843+
col,row = key
844+
col = self._data[col]
845+
if isinstance(row, str):
846+
cache, count = self._get_cache()
847+
idx = cache.get((row, 0))
848+
if idx is None:
849+
name, count, offset = self._split_name_count_offset(row)
850+
idx = self._get_row_cache_raise(name, count, offset)
851+
elif isinstance(row, tuple):
852+
cache, count = self._get_cache()
853+
idx = cache.get(row)
854+
if idx is None:
855+
idx = self._get_row_cache_raise(*row)
856+
elif isinstance(row, slice):
857+
idx = self._get_row_indices(row)
858+
elif isinstance(row, list):
859+
idx = self._get_row_indices(row)
860+
else:
861+
idx = row
862+
col[idx]=val
831863
else:
832-
self._data[key] = val
833-
if hasattr(val, "__iter__") and len(val) == len(self):
834-
self._col_names.append(key)
864+
raise ValueError(f"Invalid key {key}")
835865

836866
__setattr__ = __setitem__
837867

0 commit comments

Comments
 (0)