Skip to content

Commit 49d4185

Browse files
committed
add last instruction for lists and sets
1 parent 1661444 commit 49d4185

File tree

4 files changed

+148
-5
lines changed

4 files changed

+148
-5
lines changed

docs/source/adding-interactivity/dangers-of-mutability/_examples/list_insert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def handle_change(event):
1010
set_artist_to_add(event["target"]["value"])
1111

1212
def handle_click(event):
13-
if artist_to_add not in artists:
13+
if artist_to_add and artist_to_add not in artists:
1414
set_artists([*artists, artist_to_add])
1515
set_artist_to_add("")
1616

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from idom import component, html, run, use_state
2+
3+
4+
@component
5+
def Grid():
6+
line_size = 5
7+
selected_indices, set_selected_indices = use_state({1, 2, 4})
8+
9+
def make_handle_click(index):
10+
def handle_click(event):
11+
if index in selected_indices:
12+
set_selected_indices(selected_indices - {index})
13+
else:
14+
set_selected_indices(selected_indices | {index})
15+
16+
return handle_click
17+
18+
return html.div(
19+
{"style": {"display": "flex", "flex-direction": "row"}},
20+
[
21+
html.div(
22+
{
23+
"onClick": make_handle_click(index),
24+
"style": {
25+
"height": "30px",
26+
"width": "30px",
27+
"backgroundColor": (
28+
"black" if index in selected_indices else "white"
29+
),
30+
"outline": "1px solid grey",
31+
"cursor": "pointer",
32+
},
33+
},
34+
key=index,
35+
)
36+
for index in range(line_size)
37+
],
38+
)
39+
40+
41+
run(Grid)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from idom import component, html, run, use_state
2+
3+
4+
@component
5+
def Grid():
6+
line_size = 5
7+
selected_indices, set_selected_indices = use_state(set())
8+
9+
def make_handle_click(index):
10+
def handle_click(event):
11+
set_selected_indices(selected_indices | {index})
12+
13+
return handle_click
14+
15+
return html.div(
16+
{"style": {"display": "flex", "flex-direction": "row"}},
17+
[
18+
html.div(
19+
{
20+
"onClick": make_handle_click(index),
21+
"style": {
22+
"height": "30px",
23+
"width": "30px",
24+
"backgroundColor": (
25+
"black" if index in selected_indices else "white"
26+
),
27+
"outline": "1px solid grey",
28+
"cursor": "pointer",
29+
},
30+
},
31+
key=index,
32+
)
33+
for index in range(line_size)
34+
],
35+
)
36+
37+
38+
run(Grid)

docs/source/adding-interactivity/dangers-of-mutability/index.rst

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Below are some ways to update dictionaries without mutating them:
141141
# Python >= 3.9
142142
d | {"key": value}
143143
144-
# Equivalent to setdefault()
144+
# Equivalent to dict.setdefault()
145145
{"key": value, **d}
146146
147147
.. card:: Removing Items
@@ -165,6 +165,9 @@ Below are some ways to update dictionaries without mutating them:
165165
}
166166
167167
168+
----
169+
170+
168171
.. _updating-dictionary-items:
169172

170173
Updating Dictionary Items
@@ -293,11 +296,10 @@ Below are some ways to update lists without mutating them:
293296
294297
l[:start] + values + l[end + 1:]
295298
296-
..card:: Re-ordering Items
299+
.. card:: Re-ordering Items
297300
:link: re-ordering-list-items
298301
:link-type: ref
299302

300-
301303
Avoid using ``list.sort`` or ``list.reverse``. Instead try the strategies below:
302304

303305
.. code-block::
@@ -307,6 +309,9 @@ Below are some ways to update lists without mutating them:
307309
list(reversed(l))
308310
309311
312+
----
313+
314+
310315
.. _inserting-list-items:
311316

312317
Inserting List Items
@@ -324,6 +329,9 @@ Inserting List Items
324329
325330
l.insert(index, value)
326331
332+
# Adding a list "in-place" mutates!
333+
l += [value]
334+
327335
.. grid-item-card:: :bdg-info:`Prefer`
328336

329337
.. code-block::
@@ -336,6 +344,11 @@ Inserting List Items
336344
337345
l[:index] + [value] + l[index:]
338346
347+
Instead of mutating a list to add items to it, we need to create a new list which has
348+
the items we want to append instead. There are several ways to do this for one or more
349+
values however it's often simplest to use `"unpacking"
350+
<https://www.python.org/dev/peps/pep-0448/>`__ with the ``*`` syntax.
351+
339352
.. idom:: _examples/list_insert
340353

341354

@@ -358,7 +371,12 @@ Removing List Items
358371

359372
.. code-block::
360373
361-
l[:index - 1] + l[index:]
374+
l[:index] + l[index + 1:]
375+
376+
Unfortunately, the syntax for creating a copy of a list with one of its items removed is
377+
not quite as clean. You must select the portion the list prior to the item which should
378+
be removed (``l[:index]``) and the portion after the item (``l[index + 1:]``) and add
379+
them together:
362380

363381
.. idom:: _examples/list_remove
364382

@@ -386,6 +404,11 @@ Replacing List Items
386404
387405
l[:start] + values + l[end + 1:]
388406
407+
In a similar manner to :ref:`removing list items`, to replace an item in a list, you
408+
must select the portion before and after the item in question. But this time, instead
409+
of adding those two selections together, you must insert that values you want to replace
410+
between them:
411+
389412
.. idom:: _examples/list_replace
390413

391414

@@ -412,6 +435,12 @@ Re-ordering List Items
412435
413436
list(reversed(l))
414437
438+
There are many different ways that list items could be re-ordered, but two of the most
439+
common are reversing or sorting items. Instead of calling the associated methods on a
440+
list object, you should use the builtin functions :func:`sorted` and :func:`reversed`
441+
and pass the resulting iterator into the :class:`list` constructor to create a sorted
442+
or reversed copy of the given list:
443+
415444
.. idom:: _examples/list_re_order
416445

417446

@@ -448,6 +477,9 @@ Below are ways to update sets without mutating them:
448477
s.intersection(values)
449478
450479
480+
----
481+
482+
451483
.. _adding-set-items:
452484

453485
Adding Set Items
@@ -460,16 +492,27 @@ Adding Set Items
460492
.. code-block::
461493
462494
s.add(value)
495+
s |= {value} # "in-place" operators mutate!
463496
464497
s.update(values)
498+
s |= values # "in-place" operators mutate!
465499
466500
.. grid-item-card:: :bdg-info:`Prefer`
467501

468502
.. code-block::
469503
470504
s.union({value})
505+
s | {value}
471506
472507
s.union(values)
508+
s | values
509+
510+
Sets have some nice ways for evolving them without requiring mutation. The binary
511+
or operator ``|`` serves as a succinct way to compute the union of two sets. However,
512+
you should be careful to not use an in-place assignment with this operator as that will
513+
(counterintuitively) mutate the original set rather than creating a new one.
514+
515+
.. idom:: _examples/set_update
473516

474517

475518
.. _removing-set-items:
@@ -486,8 +529,14 @@ Removing Set Items
486529
s.remove(value)
487530
488531
s.difference_update(values)
532+
s -= values # "in-place" operators mutate!
533+
534+
s.symetric_difference_update(values)
535+
s ^= values # "in-place" operators mutate!
489536
490537
s.intersection_update(values)
538+
s &= values # "in-place" operators mutate!
539+
491540
492541
.. grid-item-card:: :bdg-info:`Prefer`
493542

@@ -496,10 +545,25 @@ Removing Set Items
496545
s.difference({value})
497546
498547
s.difference(values)
548+
s - values
549+
550+
s.symetric_difference(values)
551+
s ^ values
499552
500553
s.intersection(values)
554+
s & values
555+
556+
To remove items from sets you can use the various binary operators or their associated
557+
methods to return new sets without mutating them. As before when :ref:`adding items to
558+
sets` you need to avoid using the inline assignment operators since that will
559+
(counterintuitively) mutate the original set rather than given you a new one:
560+
561+
.. idom:: _examples/set_remove
501562

502563

503564
Useful Packages
504565
---------------
505566

567+
Under construction 🚧
568+
569+
https://pypi.org/project/pyrsistent/

0 commit comments

Comments
 (0)