Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
[swig-python] fix examples after e0c07d
Browse files Browse the repository at this point in the history
KeySet.__getitem__ throws KeyError if key is missing
(see https://docs.python.org/3/reference/datamodel.html#object.__getitem__)

This fixes #1
  • Loading branch information
manuelm authored and Markus Raab committed Jul 11, 2014
1 parent 4952600 commit bac604f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/bindings/swig/python/examples/example_kdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
db.get(ks, "user/MyApp")

# check if key exists
key = ks.lookup("user/MyApp/mykey")
if not key:
try:
key = ks["user/MyApp/mykey"]
except KeyError:
# create a new key + append to keyset
key = kdb.Key("user/MyApp/mykey")
ks.append(key)

# change keys value
key.value = "new_value"

Expand Down
6 changes: 5 additions & 1 deletion src/bindings/swig/python/examples/example_keyset.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
print(" KeySet1[1]={0}".format(ks1[1]))
print(" KeySet1[-1]={0}".format(ks1[-1]))
print(" KeySet1['user/key1']={0}".format(ks1["user/key1"]))
print(" KeySet1['doesnt_exist']={0}".format(ks1["doesnt_exist"]))
try:
print(" KeySet1['doesnt_exist']={0}".format(ks1["doesnt_exist"]))
except KeyError:
print(" KeySet1['doesnt_exist'] throws KeyError")
print(" KeySet1.lookup('doesnt_exist')={0}".format(ks1.lookup("doesnt_exist")))
print("")

print("You asked for slices? You get slices:")
Expand Down

0 comments on commit bac604f

Please sign in to comment.