Skip to content

Commit beb026b

Browse files
authored
Merge pull request #7 from JakubBlaha/master
Test + README note on the += operator (list)
2 parents 542a3c3 + 9f79f17 commit beb026b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

README.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ Basics
5959
assert store.a_dictionary['new_value'] == "old value"
6060
assert store.a_dictionary is not store.a_dictionary
6161
62+
# Appending to, extending a list
63+
>>> store.list = [1, 2, 3]
64+
65+
# Because of the fact that .append won't modify the list in the actual file,
66+
# but only a copy...
67+
>>> store.list.append(4)
68+
>>> store.list
69+
[1, 2, 3]
70+
71+
# ... we need to rather use the += operator to append to a list.
72+
>>> store.list += [4]
73+
>>> store.list
74+
[1, 2, 3, 4]
75+
76+
# Similarly, we can extend the list
77+
>>> store.list += [5, 6]
78+
>>> store.list
79+
[1, 2, 3, 4, 5, 6]
80+
6281
Transactions
6382
~~~~~~~~~~~~
6483

test_jsonstore.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ def test_transaction_write(self):
284284
with open(self._store_file) as handle:
285285
self.assertEqual(handle.read(), '{"value1": 1, "value2": 2}')
286286

287+
def test_list_concat_inplace(self):
288+
self.store.list = []
289+
extension = [{"key": "value"}]
290+
291+
# make sure += happens
292+
self.store["list"] += extension
293+
self.store.list += extension
294+
self.assertEqual(self.store.list, extension * 2)
295+
296+
# make sure a deepcopy occurred
297+
self.assertIsNot(self.store.list[0], extension[0])
298+
287299

288300
if __name__ == "__main__":
289301
unittest.main()

0 commit comments

Comments
 (0)