-
Notifications
You must be signed in to change notification settings - Fork 0
/
munch.html
255 lines (207 loc) · 11.3 KB
/
munch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="HandheldFriendly" content="True" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="robots" content="index, follow" />
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:ital,wght@0,400;0,700;1,400&family=Source+Sans+Pro:ital,wght@0,300;0,400;0,700;1,400&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://dotmobo.xyz/theme/stylesheet/style.min.css">
<link id="dark-theme-style" rel="stylesheet" type="text/css"
media="(prefers-color-scheme: dark), (prefers-color-scheme: no-preference)"
href="https://dotmobo.xyz/theme/stylesheet/dark-theme.min.css">
<link id="pygments-dark-theme" rel="stylesheet" type="text/css"
media="(prefers-color-scheme: dark), (prefers-color-scheme: no-preference)"
href="https://dotmobo.xyz/theme/pygments/monokai.min.css">
<link id="pygments-light-theme" rel="stylesheet" type="text/css"
media="(prefers-color-scheme: light)"
href="https://dotmobo.xyz/theme/pygments/emacs.min.css">
<link rel="stylesheet" type="text/css" href="https://dotmobo.xyz/theme/font-awesome/css/fontawesome.css">
<link rel="stylesheet" type="text/css" href="https://dotmobo.xyz/theme/font-awesome/css/brands.css">
<link rel="stylesheet" type="text/css" href="https://dotmobo.xyz/theme/font-awesome/css/solid.css">
<link href="https://dotmobo.xyz/css/custom.css" rel="stylesheet">
<!-- Google Analytics -->
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-68852219-1', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<!-- Chrome, Firefox OS and Opera -->
<meta name="theme-color" content="#333333">
<!-- Windows Phone -->
<meta name="msapplication-navbutton-color" content="#333333">
<!-- iOS Safari -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<!-- Microsoft EDGE -->
<meta name="msapplication-TileColor" content="#333333">
<meta name="author" content="Morgan" />
<meta name="description" content="Munch, un dictionnaire qui supporte les accesseurs" />
<meta name="keywords" content="python, bunch, munch, object, dict">
<meta property="og:site_name" content=".mobo"/>
<meta property="og:title" content="Munch, un dictionnaire qui supporte les accesseurs"/>
<meta property="og:description" content="Munch, un dictionnaire qui supporte les accesseurs"/>
<meta property="og:locale" content="fr_FR"/>
<meta property="og:url" content="https://dotmobo.xyz/munch.html"/>
<meta property="og:type" content="article"/>
<meta property="article:published_time" content="2017-11-09 00:00:00+01:00"/>
<meta property="article:modified_time" content=""/>
<meta property="article:author" content="https://dotmobo.xyz/author/morgan.html">
<meta property="article:section" content="Python"/>
<meta property="article:tag" content="python"/>
<meta property="article:tag" content="bunch"/>
<meta property="article:tag" content="munch"/>
<meta property="article:tag" content="object"/>
<meta property="article:tag" content="dict"/>
<meta property="og:image" content="//dotmobo.xyz/images/avatar700.jpg">
<title>.mobo – Munch, un dictionnaire qui supporte les accesseurs</title>
</head>
<body >
<aside>
<div>
<a href="https://dotmobo.xyz">
<img src="//dotmobo.xyz/images/avatar700.jpg" alt=".mobo" title=".mobo">
</a>
<h1>
<a href="https://dotmobo.xyz">.mobo</a>
</h1>
<p>Explorations d'un développeur</p>
<ul class="social">
<li>
<a class="sc-github" href="https://github.com/dotmobo" target="_blank">
<i class="fab fa-github"></i>
</a>
</li>
<li>
<a class="sc-twitter" href="https://twitter.com/dotmobo" target="_blank">
<i class="fab fa-twitter"></i>
</a>
</li>
<li>
<a class="sc-rss" href="//dotmobo.xyz/feeds/all.atom.xml" target="_blank">
<i class="fas fa-rss"></i>
</a>
</li>
</ul>
</div>
</aside>
<main>
<nav>
<a href="https://dotmobo.xyz">Accueil</a>
<a href="/categories.html">Catégories</a>
<a href="/tags.html">Tags</a>
<a href="/archives.html">Archives</a>
</nav>
<article class="single">
<header>
<h1 id="munch">Munch, un dictionnaire qui supporte les accesseurs</h1>
<p>
Posté le 09/11/2017 dans <a href="https://dotmobo.xyz/category/python.html">Python</a>
</p>
</header>
<div>
<img alt="Python" class="align-right" src="./images/python.png" />
<p><a class="reference external" href="https://github.com/Infinidat/munch">Munch</a> est une petite librairie bien pratique et bien pensé.</p>
<p>En gros, elle permet d'utiliser les accesseurs pour accéder aux éléments de tes dictionnaires, à la manière d'un objet python.</p>
<p>Ça peut être utile notamment dans les tests unitaires, si tu as besoin de mocker rapidement un objet par exemple.</p>
<p>Tu écris un dictionnaire qui correspond à ton objet, tu le <em>munchify</em> et hop !</p>
<p>Si dans une fonction que tu veux tester, tu as utilisé des accesseurs sur un objet, ton <em>munch</em> sera considéré comme l'objet en question.</p>
<p>C'est à priori un fork de <a class="reference external" href="https://github.com/dsc/bunch">Bunch</a>, mais maintenu et qui fonctionne en python 3.</p>
<p>Tu l'installes avec <strong>pip</strong> :</p>
<div class="highlight"><pre><span></span>pip<span class="w"> </span>install<span class="w"> </span>munch
</pre></div>
<p>Tu importes <strong>munchify</strong> et tu convertis ton dictionnaire en <strong>munch</strong> :</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">from</span> <span class="nn">munch</span> <span class="kn">import</span> <span class="n">munchify</span>
<span class="o">>>></span> <span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"last_name"</span><span class="p">:</span> <span class="s2">"Dupont"</span><span class="p">,</span>
<span class="s2">"first_name"</span><span class="p">:</span> <span class="s2">"Jean"</span><span class="p">,</span>
<span class="s2">"age"</span><span class="p">:</span> <span class="mi">30</span><span class="p">,</span>
<span class="s2">"activities"</span><span class="p">:</span> <span class="p">[</span><span class="s2">"football"</span><span class="p">,</span> <span class="s2">"ping-pong"</span><span class="p">],</span>
<span class="s2">"job"</span><span class="p">:</span> <span class="p">{</span>
<span class="s2">"name"</span><span class="p">:</span> <span class="s2">"Developer"</span><span class="p">,</span>
<span class="s2">"enterprise"</span><span class="p">:</span> <span class="s2">"Mozilla"</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="o">>>></span> <span class="n">mymunch</span> <span class="o">=</span> <span class="n">munchify</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">mymunch</span><span class="o">.</span><span class="n">last_name</span>
<span class="s1">'Dupont'</span>
<span class="o">>>></span> <span class="n">mymunch</span><span class="o">.</span><span class="n">first_name</span> <span class="o">=</span> <span class="s2">"Pierre"</span>
<span class="o">>>></span> <span class="n">mymunch</span><span class="o">.</span><span class="n">first_name</span>
<span class="s1">'Pierre'</span>
<span class="o">>>></span> <span class="n">mymunch</span><span class="o">.</span><span class="n">activities</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="s1">'football'</span>
<span class="o">>>></span> <span class="n">mymunch</span><span class="o">.</span><span class="n">job</span><span class="o">.</span><span class="n">enterprise</span>
<span class="s1">'Mozilla'</span>
</pre></div>
<p>Voilà c'est tout !</p>
<p>Tu as accès aux différentes méthodes des dictionnaires, comme <em>keys()</em> et <em>update()</em>, ainsi qu'à l'itération et à l'opérateur <em>splat</em>.
Ça supporte également la sérialization en JSON et en YAML.</p>
</div>
<div class="tag-cloud">
<p>
<a href="https://dotmobo.xyz/tag/python.html">python</a>
<a href="https://dotmobo.xyz/tag/bunch.html">bunch</a>
<a href="https://dotmobo.xyz/tag/munch.html">munch</a>
<a href="https://dotmobo.xyz/tag/object.html">object</a>
<a href="https://dotmobo.xyz/tag/dict.html">dict</a>
</p>
</div>
<!-- Disqus -->
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = 'dotmobo';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>
Veuillez activer le JavaScript pour voir les commentaires.
</noscript>
<!-- End Disqus -->
</article>
<footer>
<p>
© 2024 - This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International License</a>
</p>
<p>
Construit avec <a href="http://getpelican.com" target="_blank">Pelican</a> utilisant le thème <a href="http://bit.ly/flex-pelican" target="_blank">Flex</a>
<span class="footer-separator">|</span>
Switch to the <a href="javascript:void(0)" onclick="theme.switch(`dark`)">dark</a> | <a href="javascript:void(0)" onclick="theme.switch(`light`)">light</a> | <a href="javascript:void(0)" onclick="theme.switch(`browser`)">browser</a> theme
<script id="dark-theme-script"
src="https://dotmobo.xyz/theme/dark-theme/dark-theme.min.js"
data-enable-auto-detect-theme="True"
data-default-theme="dark"
type="text/javascript">
</script>
</p><p>
<a rel="license"
href="http://creativecommons.org/licenses/by-sa/4.0/"
target="_blank">
<img alt="Creative Commons License"
title="Creative Commons License"
style="border-width:0"
src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png"
width="80"
height="15"/>
</a>
</p> </footer>
</main>
<script type="application/ld+json">
{
"@context" : "http://schema.org",
"@type" : "Blog",
"name": " .mobo ",
"url" : "https://dotmobo.xyz",
"image": "//dotmobo.xyz/images/avatar700.jpg",
"description": ".mobo - explorations d'un développeur"
}
</script>
</body>
</html>