Skip to content

Commit b42cac9

Browse files
committed
Also run string ops code examples (fixes regression). Makes sure example classes that don't directly inherit from Example base class are run. Addressed a few of those examples failing.
1 parent 85a87eb commit b42cac9

7 files changed

Lines changed: 46 additions & 35 deletions

File tree

examples/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def __init__(
2424
self.client = client
2525
self.namespace = namespace
2626
self.set_name = set_name
27-
self.key = (self.namespace, self.set_name, "docreadkey")
27+
self.user_key = "docreadkey"
28+
self.key = (self.namespace, self.set_name, self.user_key)
2829
self.non_existent_key = (self.namespace, self.set_name, "nonexistent")
2930
self.BIN_NAME = "a"
3031

@@ -39,7 +40,7 @@ def __init__(self):
3940
'user_path': os.path.dirname(__file__) + "/client/"
4041
}
4142
}
42-
super().__init__(extra_config)
43+
super().__init__(extra_config=extra_config)
4344

4445
class ExampleWithIndex(Example):
4546
INDEX_NAME = "index_name"

examples/client/aggregate.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
class Aggregate(ExampleWithIndex, UDFExample):
2626
def run(self):
2727
predicates = [
28-
p.equals(self.BIN, 1),
29-
p.between(self.BIN, 1, 3)
28+
p.equals(self.BIN_NAME, 1),
29+
p.between(self.BIN_NAME, 1, 3)
3030
]
3131

3232
for predicate in predicates:
3333
# If predicate is provided, then perform a query
3434
query = self.client.query(self.namespace, self.set_name)
3535
query.where(predicate)
36-
BINS = [self.BIN]
36+
BINS = [self.BIN_NAME]
3737
query.select(*BINS)
3838

3939
MODULE = "stream_example"

examples/client/apply.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
##########################################################################
1717

1818

19-
from .. import ExampleWithRecord
19+
from .. import ExampleWithRecord, UDFExample
2020

2121

22-
class Apply(ExampleWithRecord):
22+
class Apply(ExampleWithRecord, UDFExample):
2323
def run(self):
24-
module = "module"
25-
function = "a"
26-
args = []
24+
self.client.udf_put("./examples/client/simple.lua")
25+
26+
module = "simple"
27+
function = "add"
28+
args = [1, 2]
2729
res = self.client.apply(self.key, module, function, args)
2830

2931
print(res)

examples/client/batch_read.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@
2020

2121

2222
class BatchRead(ExampleWithRecord):
23-
def __init__(self):
24-
pass
25-
2623
def run(self):
2724
# Get records
2825
keys = [self.key, self.non_existent_key]
2926
records = self.client.batch_read(keys)
3027

3128
if records != None:
32-
print(f"{len(records)} records were found")
29+
print(f"{len(records.batch_records)} records were found")
3330
print(records)
3431
else:
3532
print('error: Not Found.')
@@ -38,7 +35,7 @@ def run(self):
3835
records = self.client.batch_read(keys, bins=["a"])
3936

4037
if records != None:
41-
print(f"{len(records)} records were found")
38+
print(f"{len(records.batch_records)} records were found")
4239
print(records)
4340
else:
4441
print('error: Not Found.')
@@ -47,7 +44,7 @@ def run(self):
4744
records = self.client.batch_read(keys, bins=[])
4845

4946
if records != None:
50-
print(f"{len(records)} records were found")
47+
print(f"{len(records.batch_records)} records were found")
5148
print(records)
5249
else:
5350
print('error: Not Found.')

examples/client/get_key_digest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222

2323
class CalcDigest(ExampleWithRecord):
2424
def run(self):
25-
digest = aerospike.calc_digest(self.namespace, self.set_name, self.key)
25+
digest = aerospike.calc_digest(self.namespace, self.set_name, self.user_key)
2626
print(digest)

examples/client/query.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
class Query(UDFExample):
2626
def run(self):
27+
self.client.udf_put("./examples/client/stream_example.lua")
28+
2729
query = self.client.query(self.namespace, self.set_name)
2830

2931
query.select(self.BIN_NAME)
@@ -36,9 +38,11 @@ def run(self):
3638

3739
# callback to be called for each record read
3840
def callback(input_tuple):
39-
(key, meta, rec) = input_tuple
40-
results.append((key, meta, rec))
41-
print(key, meta, rec)
41+
print(input_tuple)
42+
# (key, meta, rec) = input_tuple
43+
# nonlocal results
44+
# results.append((key, meta, rec))
45+
# print(key, meta, rec)
4246

4347
# invoke the operations, and for each record invoke the callback
4448
query.foreach(callback)

examples/run_all_examples.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,33 @@
33
import inspect
44
import os
55
import sys
6-
# from . import Example
76

87

98
example_classes: list[type] = []
109

1110
dir_containing_this_module = os.path.dirname(os.path.abspath(__file__))
12-
all_packages = pkgutil.walk_packages([dir_containing_this_module + "/client"])
13-
for package in all_packages:
14-
print(package)
15-
module = importlib.import_module("." + package.name, ".examples.client")
16-
for name, obj in inspect.getmembers(module, inspect.isclass):
17-
if obj.__module__ != module.__name__:
18-
continue
19-
print("Class found:", obj)
20-
# print(Example is obj.__bases__[0])
21-
# TODO - comparing the same class imported two different ways fails
22-
# There might a better way to do this
23-
if obj.__bases__[0].__name__ != "Example":
24-
continue
25-
example_classes.append(obj)
11+
12+
for folder in ["client", "string_ops"]:
13+
all_packages = pkgutil.walk_packages([
14+
dir_containing_this_module + "/" + folder,
15+
])
16+
for package in all_packages:
17+
print(package)
18+
module = importlib.import_module("." + package.name, ".examples." + folder)
19+
for name, obj in inspect.getmembers(module, inspect.isclass):
20+
if obj.__module__ != module.__name__:
21+
continue
22+
# print(Example is obj.__bases__[0])
23+
# TODO - comparing the same class imported two different ways fails
24+
# There might a better way to do this
25+
print("Found class has these base classes:", obj.__mro__)
26+
if "Example" not in [obj.__name__ for obj in obj.__mro__]:
27+
continue
28+
if not hasattr(obj, "run") or not callable(getattr(obj, "run")):
29+
continue
30+
31+
print("Class found:", obj)
32+
example_classes.append(obj)
2633

2734
if len(sys.argv) == 2:
2835
example_classes = [cls for cls in example_classes if cls.__name__ == sys.argv[1]]

0 commit comments

Comments
 (0)