Added pybind11::make_key_iterator for map iteration
This allows exposing a dict-like interface to python code, allowing
iteration over keys via:
for k in custommapping:
...
while still allowing iteration over pairs, so that you can also
implement 'dict.items()' functionality which returns a pair iterator,
allowing:
for k, v in custommapping.items():
...
example-sequences-and-iterators is updated with a custom class providing
both types of iteration.
diff --git a/example/example-sequences-and-iterators.py b/example/example-sequences-and-iterators.py
index 69ec84e..764a527 100755
--- a/example/example-sequences-and-iterators.py
+++ b/example/example-sequences-and-iterators.py
@@ -3,7 +3,7 @@
import sys
sys.path.append('.')
-from example import Sequence
+from example import Sequence, StringMap
s = Sequence(5)
print("s = " + str(s))
@@ -29,6 +29,24 @@
print(i, end=' ')
print('')
+m = StringMap({ 'hi': 'bye', 'black': 'white' })
+print(m['hi'])
+print(len(m))
+print(m['black'])
+try:
+ print(m['orange'])
+ print('Error: should have thrown exception')
+except KeyError:
+ pass
+m['orange'] = 'banana'
+print(m['orange'])
+
+for k in m:
+ print("key = %s, value = %s" % (k, m[k]))
+
+for k,v in m.items():
+ print("item: (%s, %s)" % (k,v))
+
from example import ConstructorStats
cstats = ConstructorStats.get(Sequence)
print("Instances not destroyed:", cstats.alive())