Skip to content

Commit cfcbc1e

Browse files
committed
add more detail to dict section
1 parent 2e16b61 commit cfcbc1e

File tree

7 files changed

+175
-31
lines changed

7 files changed

+175
-31
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from idom import component, html, run, use_state
2+
3+
4+
@component
5+
def Definitions():
6+
term_to_add, set_term_to_add = use_state(None)
7+
definition_to_add, set_definition_to_add = use_state(None)
8+
all_terms, set_all_terms = use_state({})
9+
10+
def handle_term_to_add_change(event):
11+
set_term_to_add(event["target"]["value"])
12+
13+
def handle_definition_to_add_change(event):
14+
set_definition_to_add(event["target"]["value"])
15+
16+
def handle_add_click(event):
17+
if term_to_add and definition_to_add:
18+
set_all_terms({**all_terms, term_to_add: definition_to_add})
19+
set_term_to_add(None)
20+
set_definition_to_add(None)
21+
22+
def make_delete_click_handler(term_to_delete):
23+
def handle_click(event):
24+
set_all_terms({t: d for t, d in all_terms.items() if t != term_to_delete})
25+
26+
return handle_click
27+
28+
return html.div(
29+
html.button({"onClick": handle_add_click}, "add term"),
30+
html.label(
31+
"Term: ",
32+
html.input({"value": term_to_add, "onChange": handle_term_to_add_change}),
33+
),
34+
html.label(
35+
"Definition: ",
36+
html.input(
37+
{
38+
"value": definition_to_add,
39+
"onChange": handle_definition_to_add_change,
40+
}
41+
),
42+
),
43+
html.hr(),
44+
[
45+
html.div(
46+
html.button(
47+
{"onClick": make_delete_click_handler(term)}, "delete term"
48+
),
49+
html.dt(term),
50+
html.dd(definition),
51+
key=term,
52+
)
53+
for term, definition in all_terms.items()
54+
],
55+
)
56+
57+
58+
run(Definitions)

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

Lines changed: 112 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Thus far, all the values we've been working with have been immutable. These incl
4646
have not had to consider the consiquences of mutations.
4747

4848

49+
.. _Why Avoid Mutation:
50+
4951
Why Avoid Mutation?
5052
-------------------
5153

@@ -123,54 +125,104 @@ you touch or hover over the preview:
123125
Working with Dictionaries
124126
-------------------------
125127

126-
Below are some operations to :bdg-danger:`avoid` and stragies to :bdg-info:`prefer` when
127-
working with dictionaries...
128+
Below are some ways to update dictionaries without mutating them:
129+
130+
.. card:: Updating Items
131+
:link: updating-dictionary-items
132+
:link-type: ref
133+
134+
Avoid using item assignment, ``dict.update``, or ``dict.setdefault``. Instead try
135+
the strategies below:
136+
137+
.. code-block::
138+
139+
{**d, "key": value}
140+
141+
# Python >= 3.9
142+
d | {"key": value}
143+
144+
# Equivalent to setdefault()
145+
{"key": value, **d}
146+
147+
.. card:: Removing Items
148+
:link: removing-dictionary-items
149+
:link-type: ref
150+
151+
Avoid using item deletion or ``dict.pop`` instead try the strategies below:
152+
153+
.. code-block::
154+
155+
{
156+
k: v
157+
for k, v in d.items()
158+
if k != key
159+
}
160+
161+
# Better for removing multiple items
162+
{
163+
k: d[k]
164+
for k in set(d).difference([key])
165+
}
128166
129167
168+
.. _updating-dictionary-items:
169+
130170
Updating Dictionary Items
131171
.........................
132172

133-
.. grid:: 1 1 2 2
173+
.. grid:: 1 1 1 2
134174
:gutter: 1
135175

136176
.. grid-item-card:: :bdg-danger:`Avoid`
137177

138178
.. code-block::
139179
140-
d["key"] = value
180+
d[key] = value
141181
142-
d.update(key=value)
182+
d.update({key: value})
143183
144-
d.setdefault("key", value)
184+
d.setdefault(key, value)
145185
146186
.. grid-item-card:: :bdg-info:`Prefer`
147187

148188
.. code-block::
149189
150-
{**d, "key": value}
190+
{**d, key: value}
151191
152192
# Python >= 3.9
153-
d | {"key": value}
193+
d | {key: value}
154194
155195
# Equivalent to setdefault()
156-
{"key": value, **d}
196+
{key: value, **d}
197+
198+
As we saw in an :ref:`earlier example <why avoid mutation>`, instead of mutating
199+
dictionaries to update their items you should instead create a copy that contains the
200+
desired changes.
201+
202+
However, sometimes you may only want to update some of the information in a dictionary
203+
which is held by a state variable. Consider the case below where we have a form for
204+
updating user information with a preview of the currently entered data. We can
205+
accomplish this using `"unpacking" <https://www.python.org/dev/peps/pep-0448/>`__ with
206+
the ``**`` syntax:
157207

208+
.. idom:: _examples/dict_update
209+
210+
211+
.. _removing-dictionary-items:
158212

159213
Removing Dictionary Items
160214
.........................
161215

162-
.. grid:: 1 1 2 2
216+
.. grid:: 1 1 1 2
163217
:gutter: 1
164218

165219
.. grid-item-card:: :bdg-danger:`Avoid`
166220

167221
.. code-block::
168222
169-
d["key"] = value
170-
171-
d.update(key=value)
223+
del d[key]
172224
173-
d.setdefault("key", value)
225+
d.pop(key)
174226
175227
.. grid-item-card:: :bdg-info:`Prefer`
176228

@@ -179,9 +231,23 @@ Removing Dictionary Items
179231
{
180232
k: v
181233
for k, v in d.items()
182-
if k != "key"
234+
if k != key
235+
}
236+
237+
# Better for removing multiple items
238+
{
239+
k: d[k]
240+
for k in set(d).difference([key])
183241
}
184242
243+
This scenario doesn't come up very frequently. When it does though, the best way to
244+
remove items from dictionaries is to create a copy of the original, but with a filtered
245+
set of keys. One way to do this is with a dictionary comprehension. The example below
246+
shows an interface where you're able to enter a new term and definition. Once added,
247+
you can click a delete button to remove the term and definition:
248+
249+
.. idom:: _examples/dict_remove
250+
185251

186252
Working with Lists
187253
------------------
@@ -193,7 +259,7 @@ working with lists...
193259
Replacing List Items
194260
....................
195261

196-
.. grid:: 1 1 2 2
262+
.. grid:: 1 1 1 2
197263

198264
.. grid-item-card:: :bdg-danger:`Avoid`
199265

@@ -215,7 +281,7 @@ Replacing List Items
215281
Inserting List Items
216282
....................
217283

218-
.. grid:: 1 1 2 2
284+
.. grid:: 1 1 1 2
219285

220286
.. grid-item-card:: :bdg-danger:`Avoid`
221287

@@ -231,6 +297,8 @@ Inserting List Items
231297

232298
.. code-block::
233299
300+
[*l, value]
301+
234302
l + [value]
235303
236304
l + values
@@ -241,7 +309,7 @@ Inserting List Items
241309
Removing List Items
242310
...................
243311

244-
.. grid:: 1 1 2 2
312+
.. grid:: 1 1 1 2
245313

246314
.. grid-item-card:: :bdg-danger:`Avoid`
247315

@@ -251,21 +319,17 @@ Removing List Items
251319
252320
l.pop(index)
253321
254-
l.clear()
255-
256322
.. grid-item-card:: :bdg-info:`Prefer`
257323

258324
.. code-block::
259325
260326
l[:index - 1] + l[index:]
261327
262-
[]
263-
264328
265329
Re-ordering List Items
266330
......................
267331

268-
.. grid:: 1 1 2 2
332+
.. grid:: 1 1 1 2
269333

270334
.. grid-item-card:: :bdg-danger:`Avoid`
271335

@@ -290,10 +354,32 @@ Working with Sets
290354
Below are some operations to :bdg-danger:`avoid` and stragies to :bdg-info:`prefer` when
291355
working with sets...
292356

293-
Ading Set Items
294-
...............
357+
Adding Set Items
358+
................
359+
360+
.. grid:: 1 1 1 2
361+
362+
.. grid-item-card:: :bdg-danger:`Avoid`
363+
364+
.. code-block::
365+
366+
l[index] = value
367+
368+
l[start:end] = values
369+
370+
.. grid-item-card:: :bdg-info:`Prefer`
371+
372+
.. code-block::
373+
374+
l[:index] + [value] + l[index + 1:]
375+
376+
l[:start] + values + l[end + 1:]
377+
378+
379+
Removing Set Items
380+
..................
295381

296-
.. grid:: 1 1 2 2
382+
.. grid:: 1 1 1 2
297383

298384
.. grid-item-card:: :bdg-danger:`Avoid`
299385

docs/source/adding-interactivity/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Adding Interactivity
1616
:animate: fade-in
1717
:open:
1818

19-
.. grid:: 1 1 2 2
19+
.. grid:: 1 2 2 2
2020

2121
.. grid-item-card:: :octicon:`bell` Responding to Events
2222
:link: responding-to-events/index

docs/source/creating-interfaces/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Creating Interfaces
1313
:animate: fade-in
1414
:open:
1515

16-
.. grid:: 1 1 2 2
16+
.. grid:: 1 2 2 2
1717

1818
.. grid-item-card:: :octicon:`code-square` HTML with IDOM
1919
:link: html-with-idom

docs/source/developing-idom/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ or simply sharing your work with your colleagues and friends. We're excited to s
2121
you can help move this project and community forward!
2222

2323

24-
.. grid:: 1 1 2 2
24+
.. grid:: 1 2 2 2
2525
:gutter: 1
2626

2727
.. grid-item-card:: :octicon:`people` Discussion Forum

docs/source/getting-started/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Getting Started
1212
:animate: fade-in
1313
:open:
1414

15-
.. grid:: 1 1 2 2
15+
.. grid:: 1 2 2 2
1616

1717
.. grid-item-card:: :octicon:`tools` Installing IDOM
1818
:link: installing-idom

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ more detailed information about how to :ref:`run IDOM <Running IDOM>` in differe
9393
contexts. For example, if you want to embed IDOM into an existing application, or run
9494
IDOM within a Jupyter Notebook, this is where you can learn how to do those things.
9595

96-
.. grid:: 1 1 2 2
96+
.. grid:: 1 2 2 2
9797

9898
.. grid-item::
9999

0 commit comments

Comments
 (0)