-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
675 lines (323 loc) · 577 KB
/
search.xml
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>安装遇到的各种问题</title>
<link href="/posts/59a3d58d/"/>
<url>/posts/59a3d58d/</url>
<content type="html"><![CDATA[<h1 id="acunetix(AWVS)"><a href="#acunetix(AWVS)" class="headerlink" title="acunetix(AWVS)"></a>acunetix(AWVS)</h1><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">sudo systemctl stop acunetix</span><br><span class="line">sudo vm wvcsc <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>v_221007170<span class="regexp">/scanner/</span>wvsc</span><br><span class="line">sudo chown acunetix:acunetix <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>v_221007170<span class="regexp">/scanner/</span>wvsc</span><br><span class="line">sudo chmod +x <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>v_221007170<span class="regexp">/scanner/</span>wvsc</span><br><span class="line">sudo rm <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>data<span class="regexp">/license/</span>*</span><br><span class="line">sudo mv license_info.json <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>data<span class="regexp">/license/</span></span><br><span class="line">sudo mv wa_data.dat <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>data<span class="regexp">/license/</span></span><br><span class="line">sudo chown acunetix:acunetix <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>data<span class="regexp">/license/</span>wa_data.dat</span><br><span class="line">sudo chown acunetix:acunetix <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>data<span class="regexp">/license/</span>license_info.json</span><br><span class="line">sudo chmod <span class="number">764</span> <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>data<span class="regexp">/license/</span>license_info.json</span><br><span class="line">sudo chmod <span class="number">764</span> <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>data<span class="regexp">/license/</span>wa_data.dat</span><br><span class="line">sudo chattr +i <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>data<span class="regexp">/license/</span>wa_data.dat</span><br><span class="line">sudo chattr +i <span class="regexp">/home/</span>acunetix<span class="regexp">/.acunetix/</span>data<span class="regexp">/license/</span>license_info.json</span><br></pre></td></tr></table></figure><p>遇到的问题(缺少lib库)</p><figure class="highlight smali"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">Checking os...</span><br><span class="line">Checking for dependencies...</span><br><span class="line"> - dependency libcups.so.2<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libcairo.so.2<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libpango-1.0.so.0<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libnssutil3.so<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libatk-bridge-2.0.so.0<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libnss3.so<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libXdamage.so.1<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libXfixes.so.3<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libasound.so.2<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libXrandr.so.2<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libatk-1.0.so.0<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libgbm.so.1<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libsmime3.so<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libXcomposite.so.1<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libnspr4.so<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libxkbcommon.so.0<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line"> - dependency libatspi.so.0<span class="built_in"> not </span>found on the<span class="keyword"> system</span></span><br><span class="line">Some dependencies are<span class="built_in"> not </span>found on the<span class="keyword"> system</span>. Aborting installation.</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>解决办法:</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install libxdamage1 libgtk-3-0 libasound2 libnss3 libxss1 libx11-xcb-dev libgbm-dev libasound2-dev</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>平时积累</title>
<link href="/posts/730b8fb7/"/>
<url>/posts/730b8fb7/</url>
<content type="html"><![CDATA[<h1 id="1、重装系统"><a href="#1、重装系统" class="headerlink" title="1、重装系统"></a>1、重装系统</h1><h2 id="1-1、备份环境变量的注册表"><a href="#1-1、备份环境变量的注册表" class="headerlink" title="1.1、备份环境变量的注册表"></a>1.1、备份环境变量的注册表</h2><blockquote><p>备份环境变量的目的是让你操作系统之后不需要再去配置环境变量</p></blockquote><p><code>Win+R</code>输入<code>regedit</code></p><p>打开下面这个地址</p><p><strong>系统环境变量</strong>位置</p><p><code>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment</code></p><p><strong>用户环境变量</strong>位置</p><p><code>HKEY_CURRENT_USER\Environment</code></p><p>分别<code>用户环境变量</code>和<code>系统环境变量</code>在<code>Environment</code>上右击点击<code>导出</code>就好了</p><p><strong>记得备份好,不要重装完系统备份的东西没了,重装系统之后双击就行了</strong></p><p><strong>建议将软件都装在D盘,如果你只有一个盘当我没说</strong></p><h2 id="1-2、高级玩法"><a href="#1-2、高级玩法" class="headerlink" title="1.2、高级玩法"></a>1.2、高级玩法</h2><p>不想写</p><h1 id="2、Windows安装"><a href="#2、Windows安装" class="headerlink" title="2、Windows安装"></a>2、Windows安装</h1><h2 id="2-1、Windows镜像地址"><a href="#2-1、Windows镜像地址" class="headerlink" title="2.1、Windows镜像地址"></a>2.1、Windows镜像地址</h2><p>Windows10:<a href="https://www.microsoft.com/zh-cn/software-download/windows10">https://www.microsoft.com/zh-cn/software-download/windows10</a></p><p>Windows11:<a href="https://www.microsoft.com/zh-cn/software-download/windows11">https://www.microsoft.com/zh-cn/software-download/windows11</a></p><p>也可以通过<code>Rufus</code>下载windows镜像</p><h2 id="2-2、下载和制作USB启动盘"><a href="#2-2、下载和制作USB启动盘" class="headerlink" title="2.2、下载和制作USB启动盘"></a>2.2、下载和制作USB启动盘</h2><p>Rufus - 轻松创建USB启动盘:<a href="http://rufus.ie/zh/">http://rufus.ie/zh/</a></p><h2 id="2-3、重启用U盘启动"><a href="#2-3、重启用U盘启动" class="headerlink" title="2.3、重启用U盘启动"></a>2.3、重启用U盘启动</h2><p>分区直接将c盘,esp,mbr分区删除,然后直接选中未分配的区域点下一步就行了</p><h2 id="2-4、进入系统"><a href="#2-4、进入系统" class="headerlink" title="2.4、进入系统"></a>2.4、进入系统</h2><h3 id="可选操作"><a href="#可选操作" class="headerlink" title="可选操作"></a>可选操作</h3><p>将桌面路径修改至D盘,操作过程</p><p><code>此电脑</code>—–><code>桌面</code>—–><code>属性</code>—–><code>位置</code>—–>直接将C改为D就好了</p><p>其它依次这样操作</p><h2 id="2-5、激活windows、office"><a href="#2-5、激活windows、office" class="headerlink" title="2.5、激活windows、office"></a>2.5、激活windows、office</h2><p>云萌window激活、版本转换:<a href="https://cmwtat.cloudmoe.com/cn.html">https://cmwtat.cloudmoe.com/cn.html</a></p><p>HEU_KMS_Acticator:<a href="https://github.com/zbezj/HEU_KMS_Activator">https://github.com/zbezj/HEU_KMS_Activator</a></p><p>Office Tools <a href="https://otp.landian.vip/zh-cn/">https://otp.landian.vip/zh-cn/</a></p><h1 id="3、必备软件"><a href="#3、必备软件" class="headerlink" title="3、必备软件"></a>3、必备软件</h1><h2 id="3-1、装驱动"><a href="#3-1、装驱动" class="headerlink" title="3.1、装驱动"></a>3.1、装驱动</h2><p>如果没有无线网卡,去你电脑品牌的官网下载驱动就行了,</p><h2 id="3-2、浏览器"><a href="#3-2、浏览器" class="headerlink" title="3.2、浏览器"></a>3.2、浏览器</h2><p>Edge(自带,推荐,因为可以同步)</p><p>Google Chrome(谷歌浏览器): <a href="https://www.google.cn/chrome/index.html">https://www.google.cn/chrome/index.html</a></p><h2 id="3-3、Bandizip-解压软件"><a href="#3-3、Bandizip-解压软件" class="headerlink" title="3.3、Bandizip(解压软件)"></a>3.3、Bandizip(解压软件)</h2><p>Bandizip:<a href="http://www.bandisoft.com/">http://www.bandisoft.com/</a></p><h2 id="3-4、安全软件"><a href="#3-4、安全软件" class="headerlink" title="3.4、安全软件"></a>3.4、安全软件</h2><p>火绒安全:<a href="https://www.huorong.cn/">https://www.huorong.cn/</a></p><h2 id="3-5、截图软件"><a href="#3-5、截图软件" class="headerlink" title="3.5、截图软件"></a>3.5、截图软件</h2><p>Snipaste:<a href="https://zh.snipaste.com/">https://zh.snipaste.com/</a></p><p>Snipaste微软商店地址:<a href="https://www.microsoft.com/zh-cn/p/snipaste/9p1wxpkb68kx">https://www.microsoft.com/zh-cn/p/snipaste/9p1wxpkb68kx</a></p><h2 id="3-6、文本编辑软件"><a href="#3-6、文本编辑软件" class="headerlink" title="3.6、文本编辑软件"></a>3.6、文本编辑软件</h2><p>Sublime Text:<a href="http://www.sublimetext.com/">http://www.sublimetext.com/</a></p><p>notepad++:<a href="https://github.com/notepad-plus-plus/notepad-plus-plus/releases">https://github.com/notepad-plus-plus/notepad-plus-plus/releases</a></p><h2 id="3-7、Office破解版安装"><a href="#3-7、Office破解版安装" class="headerlink" title="3.7、Office破解版安装"></a>3.7、Office破解版安装</h2><p>Office Tools <a href="https://otp.landian.vip/zh-cn/">https://otp.landian.vip/zh-cn/</a></p><h1 id="4、其他软件"><a href="#4、其他软件" class="headerlink" title="4、其他软件"></a>4、其他软件</h1><h2 id="4-1、jetbrains全家桶补丁"><a href="#4-1、jetbrains全家桶补丁" class="headerlink" title="4.1、jetbrains全家桶补丁"></a>4.1、jetbrains全家桶补丁</h2><p>配合:<a href="https://jetbra.in/s">https://jetbra.in/s</a></p><p>补丁地址:<a href="https://github.com/ja-netfilter/ja-netfilter">https://github.com/ja-netfilter/ja-netfilter</a></p><p>知了大神: <a href="https://zhile.io/2021/11/29/ja-netfilter-javaagent-lib.html">https://zhile.io/2021/11/29/ja-netfilter-javaagent-lib.html</a></p><h2 id="4-2、流氓软件删除"><a href="#4-2、流氓软件删除" class="headerlink" title="4.2、流氓软件删除"></a>4.2、流氓软件删除</h2><p>SoftCnKiller高速下载器捆绑软件杀手:<a href="https://softcnkiller.blog.csdn.net/article/details/104799162?spm=1001.2014.3001.5502">https://softcnkiller.blog.csdn.net/article/details/104799162?spm=1001.2014.3001.5502</a></p><h2 id="4-3、idm补丁"><a href="#4-3、idm补丁" class="headerlink" title="4.3、idm补丁"></a>4.3、idm补丁</h2><p>补丁作者:<a href="https://ckk.ir/863/%d8%af%d8%a7%d9%86%d9%84%d9%88%d8%af-%d8%a8%d9%87%d8%aa%d8%b1%db%8c%d9%86-%da%a9%d8%b1%da%a9-idm-internet-download-manager/">https://ckk.ir/863/%d8%af%d8%a7%d9%86%d9%84%d9%88%d8%af-%d8%a8%d9%87%d8%aa%d8%b1%db%8c%d9%86-%da%a9%d8%b1%da%a9-idm-internet-download-manager/</a></p><p>放在idm根目录就好</p><h2 id="4-4、Clash(包括中文地址)"><a href="#4-4、Clash(包括中文地址)" class="headerlink" title="4.4、Clash(包括中文地址)"></a>4.4、Clash(包括中文地址)</h2><p>Clash_for_windows_pkg:<a href="https://archive.org/details/clash_for_windows_pkg">https://archive.org/details/clash_for_windows_pkg</a></p><p>Clash Verge <a href="https://github.com/zzzgydi/clash-verge/releases">https://github.com/zzzgydi/clash-verge/releases</a></p><p>Clash For Windows 汉化补丁和汉化脚本:<a href="https://github.com/Z-Siqi/Clash-for-Windows_Chinese">https://github.com/Z-Siqi/Clash-for-Windows_Chinese</a><br><a href="https://github.com/ender-zhao/Clash-for-Windows_Chinese">https://github.com/ender-zhao/Clash-for-Windows_Chinese</a></p><p>ClashForAndroid:<a href="https://web.archive.org/web/20231103071433/https://github.com/Kr328/ClashForAndroid">https://web.archive.org/web/20231103071433/https://github.com/Kr328/ClashForAndroid</a></p><pre><code> ClashMetaForAndroid:https://github.com/MetaCubeX/ClashMetaForAndroid</code></pre><h2 id="4-5、Burpsuite-pro"><a href="#4-5、Burpsuite-pro" class="headerlink" title="4.5、Burpsuite_pro"></a>4.5、Burpsuite_pro</h2><p>BurpLoaderKeygen: Burp Suite Pro Loader & Keygen:<a href="https://github.com/h3110w0r1d-y/BurpLoaderKeygen">https://github.com/h3110w0r1d-y/BurpLoaderKeygen</a></p><h2 id="4-6、M3U8视频下载"><a href="#4-6、M3U8视频下载" class="headerlink" title="4.6、M3U8视频下载"></a>4.6、M3U8视频下载</h2><p>M3U8批量下载器:<a href="https://www.52pojie.cn/thread-1374045-1-1.html">https://www.52pojie.cn/thread-1374045-1-1.html</a></p><h1 id="5、网站一键安装脚本"><a href="#5、网站一键安装脚本" class="headerlink" title="5、网站一键安装脚本"></a>5、网站一键安装脚本</h1><p>自动安装 - OneinStack:<a href="https://oneinstack.com/auto/">https://oneinstack.com/auto/</a></p><p>LNMP一键安装包:<a href="https://lnmp.org/">https://lnmp.org/</a></p><p>宝塔:<a href="https://www.bt.cn/">https://www.bt.cn</a></p><h1 id="6、Linux"><a href="#6、Linux" class="headerlink" title="6、Linux"></a>6、Linux</h1><p>一键更换国内软件源 :<a href="https://github.com/SuperManito/LinuxMirrors">https://github.com/SuperManito/LinuxMirrors</a></p><h1 id="7、配置wls2-与windows主机的连通性"><a href="#7、配置wls2-与windows主机的连通性" class="headerlink" title="7、配置wls2 与windows主机的连通性"></a>7、配置wls2 与windows主机的连通性</h1><p>powershell 管理员运行</p><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">New-NetFirewallRule</span> <span class="literal">-DisplayName</span> <span class="string">"WSL"</span> <span class="literal">-Direction</span> Inbound <span class="literal">-InterfaceAlias</span> <span class="string">"vEthernet (WSL)"</span> <span class="literal">-Action</span> Allow</span><br></pre></td></tr></table></figure><p>如果还不能ping通说明windows 主机拒绝了icmp报文</p><h3 id="配置防火墙规则"><a href="#配置防火墙规则" class="headerlink" title="配置防火墙规则"></a>配置防火墙规则</h3><p>入站规则—><code>核心网络诊断-ICMP回显请求(ICMPv4-In)</code>勾选启用就行了</p><h3 id="clash-for-windows-设置"><a href="#clash-for-windows-设置" class="headerlink" title="clash for windows 设置"></a>clash for windows 设置</h3><p>打开 Allow LAN,</p><p>在wsl中环境变量,统计下面代码,添加到<code>.bashrc</code>这个文件最后,</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">port=25648</span><br><span class="line">hostip=$(<span class="built_in">cat</span> /etc/resolv.conf | grep nameserver | awk <span class="string">'{ print $2 }'</span>)</span><br><span class="line"><span class="comment"># setp是设置代理的命令,可以自定义</span></span><br><span class="line"><span class="built_in">alias</span> setp=<span class="string">'export https_proxy="http://${hostip}:${port}";export http_proxy="http://${hostip}:${port}";export all_proxy="socks5://${hostip}:${port}";export ALL_PROXY="socks5://${hostip}:${port}";echo "set proxy:";env|grep -i proxy'</span></span><br><span class="line"><span class="comment"># unsetp是取消代理的命令,可以自定义</span></span><br><span class="line"><span class="built_in">alias</span> unsetp=<span class="string">'unset https_proxy; unset http_proxy; unset all_proxy; unset ALL_PROXY;echo "clear proxy:";env|grep -i proxy'</span></span><br></pre></td></tr></table></figure><h1 id="8、小工具"><a href="#8、小工具" class="headerlink" title="8、小工具"></a>8、小工具</h1><p>查看自己代理ip</p><p><a href="http://23.80.5.90/ip.php">http://23.80.5.90/ip.php</a></p><p>查看电脑出口公网ip</p><p><a href="http://www.cip.cc/">http://www.cip.cc/</a></p><p><a href="https://tool.lu/netcard/">https://tool.lu/netcard/</a></p><h1 id="9、直播录屏软件"><a href="#9、直播录屏软件" class="headerlink" title="9、直播录屏软件"></a>9、直播录屏软件</h1><p><code>Open Broadcaster Software | OBS</code></p><figure class="highlight dts"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="symbol">https:</span><span class="comment">//obsproject.com</span></span><br></pre></td></tr></table></figure><h1 id="10、流氓垃圾软件清理工具"><a href="#10、流氓垃圾软件清理工具" class="headerlink" title="10、流氓垃圾软件清理工具"></a>10、流氓垃圾软件清理工具</h1><p>清理工具<code>SoftCnKiller高速下载器捆绑软件杀手</code></p><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="regexp">//</span>blog.csdn.net<span class="regexp">/hfhbutn/</span>article<span class="regexp">/details/</span><span class="number">104799162</span></span><br></pre></td></tr></table></figure><p>免疫工具<code>小鱼儿yr系统封装优化设置辅助工具</code></p><figure class="highlight dts"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="symbol">https:</span><span class="comment">//www.yrxitong.com/h-nd-100.html</span></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 分享 </category>
</categories>
<tags>
<tag> 工具 </tag>
<tag> 教程 </tag>
</tags>
</entry>
<entry>
<title>proxy_pool搭建过程</title>
<link href="/posts/62eac934/"/>
<url>/posts/62eac934/</url>
<content type="html"><![CDATA[<p>一、使用docker搭建一个redis</p><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker pull redis</span><br><span class="line"></span><br><span class="line">docker <span class="keyword">run</span><span class="language-bash"> -p 6379:6379 --name myredis -v D:/Docker/redis/:/etc/redis/ -d redis redis-server /etc/redis/redis.conf --appendonly no</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><p>二、配置proxy_pool中的setting</p><p><img src="/proxy-pool%E6%90%AD%E5%BB%BA%E8%BF%87%E7%A8%8B%5Cimage-20211225212641539.webp" alt="a"></p><p>三、拉取proxy_pool</p><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">docker pull jhao104/proxy_pool</span><br><span class="line"><span class="comment"># 命令报错,docker是一个虚拟环境,127.0.0.1和localhost指的是虚拟环境内部,而不是外部宿主机,所以无法这样访问.</span></span><br><span class="line">docker <span class="keyword">run</span><span class="language-bash"> --<span class="built_in">env</span> DB_CONN=redis://:password@ip:port/0 -p 5010:5010 jhao104/proxy_pool</span></span><br><span class="line"><span class="comment"># 使用下面命令</span></span><br><span class="line">docker <span class="keyword">run</span><span class="language-bash"> --<span class="built_in">env</span> DB_CONN=redis://:@host.docker.internal:6379/0 -p 5010:5010 jhao104/proxy_pool</span></span><br><span class="line"></span><br><span class="line">对于mac和windows,可以使用host.docker.internal替换<span class="number">127.0</span>.<span class="number">0.1</span></span><br><span class="line">如 mongodb://host.docker.internal:<span class="number">27017</span></span><br><span class="line">对于Linux可以采用如下方案(后续应该也可以用上面的方案,但是当前docker还没有修改此问题):</span><br><span class="line">创建一个桥接网络</span><br><span class="line">下面的localNet是网络名字,可自行修改;</span><br><span class="line">关于<span class="number">192.168</span>.<span class="number">0.0</span>这个子网,也可以自行定义.</span><br><span class="line">默认按照下面的命令,执行后将可以通过<span class="number">192.168</span>.<span class="number">0.1</span>访问宿主机.</span><br><span class="line">docker network create -d bridge --subnet <span class="number">192.168</span>.<span class="number">0.0</span>/<span class="number">24</span> --gateway <span class="number">192.168</span>.<span class="number">0.1</span> localNet</span><br><span class="line">使用<span class="number">192.168</span>.<span class="number">0.1</span>替换<span class="number">127.0</span>.<span class="number">0.1</span>,如mongodb://<span class="number">192.168</span>.<span class="number">0.1</span>:<span class="number">27017</span></span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>安装PicGo中的picgo-plugin-compress插件</title>
<link href="/posts/4c3fd5bc/"/>
<url>/posts/4c3fd5bc/</url>
<content type="html"><![CDATA[<p>下载PigGo</p><p><img src="/%E5%AE%89%E8%A3%85PicGo%E4%B8%AD%E7%9A%84picgo-plugin-compress%E6%8F%92%E4%BB%B6/image-20211217233444390.webp" alt="image-20211217233444390"></p><p>在PigGo的更目录执行下面命令</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install picgo-plugin-compress --save --registry=https://registry.npm.taobao.org</span><br></pre></td></tr></table></figure><p>如果报错下面信息那就这些这个命令</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">npm install -g cnpm --registry=https://registry.npmmirror.com</span><br><span class="line"></span><br><span class="line">cnpm install picgo-plugin-compress --save</span><br></pre></td></tr></table></figure><p><img src="/%E5%AE%89%E8%A3%85PicGo%E4%B8%AD%E7%9A%84picgo-plugin-compress%E6%8F%92%E4%BB%B6/image-20211217234115720.webp"></p><p>然后在PicGo里面配置就好了,点击启用transformer-compress</p><p><img src="/%E5%AE%89%E8%A3%85PicGo%E4%B8%AD%E7%9A%84picgo-plugin-compress%E6%8F%92%E4%BB%B6/image-20211217234439905.webp" alt="image-20211217234439905"></p>]]></content>
</entry>
<entry>
<title>python多态</title>
<link href="/posts/d2b9d54/"/>
<url>/posts/d2b9d54/</url>
<content type="html"><![CDATA[<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- Codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021-11-27 下午 1:43</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : object09.py</span></span><br><span class="line"><span class="comment"># @Software : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># class Person:</span></span><br><span class="line"><span class="comment"># def __init__(self, name):</span></span><br><span class="line"><span class="comment"># self.name = name</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def eat(self):</span></span><br><span class="line"><span class="comment"># print('---------->eat1')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def eat(self, food):</span></span><br><span class="line"><span class="comment"># print('----------->eat:', food)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># p = Person('jack')</span></span><br><span class="line"><span class="comment"># p.eat('肉丸子')</span></span><br><span class="line"><span class="keyword">import</span> inspect</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Base</span>:</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">test</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'------base'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">A</span>(<span class="title class_ inherited__">Base</span>):</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">test</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'AAAAAAAAAA'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">B</span>(<span class="title class_ inherited__">Base</span>):</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">test</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'BBBBBBBBBB'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">C</span>(<span class="title class_ inherited__">Base</span>):</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">test</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'CCCCCCCCCC'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">D</span>(A, B, C):</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># c = C()</span></span><br><span class="line"><span class="comment"># c.test()</span></span><br><span class="line"><span class="comment"># c.test1()</span></span><br><span class="line"><span class="comment"># c.test2()</span></span><br><span class="line">d = D()</span><br><span class="line">d.test()</span><br><span class="line"><span class="built_in">print</span>(inspect.getmro(D))</span><br><span class="line"><span class="built_in">print</span>(D.__mro__)</span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">python允许多继承</span></span><br><span class="line"><span class="string">def 子类(父类1,父类2)</span></span><br><span class="line"><span class="string"> pas</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">如果父类有相同方法名称,搜索顺序是,</span></span><br><span class="line"><span class="string">自己-父类1-父类2。。。。-父类中的父类</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- Codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021-11-27 下午 2:17</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : object10.py</span></span><br><span class="line"><span class="comment"># @Software : PyCharm</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 多继承的搜索顺序,经典类,新式类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">P1</span>():</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">foo</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'p1====foo'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">bar</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'p1------bar'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">P2</span>():</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">foo</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'p2------.foo'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">C1</span>(P1, P2):</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">C2</span>(P1, P2):</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">bar</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'C2====bar'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">D1</span>(C1, C2):</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line">d = D1()</span><br><span class="line">d.foo()</span><br><span class="line">d.bar()</span><br><span class="line"><span class="built_in">print</span>(D1.__mro__)</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- Codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021-11-27 下午 2:40</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : object11.py</span></span><br><span class="line"><span class="comment"># @Software : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">私有化:</span></span><br><span class="line"><span class="string"> __age</span></span><br><span class="line"><span class="string"> def __show(self):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> --->__类名__属性</span></span><br><span class="line"><span class="string"> 私有化:封装 将属性私有化,定义公有set和get方法</span></span><br><span class="line"><span class="string"> def setAge(self, age):</span></span><br><span class="line"><span class="string"> 判断</span></span><br><span class="line"><span class="string"> def getAge(self):</span></span><br><span class="line"><span class="string"> return self.__age</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> s.setAge(20)</span></span><br><span class="line"><span class="string"> s.getAge()</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> class Studnt:</span></span><br><span class="line"><span class="string"> def __init__(self,age):</span></span><br><span class="line"><span class="string"> self.__age=age</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> @property</span></span><br><span class="line"><span class="string"> def age(self):</span></span><br><span class="line"><span class="string"> return ...</span></span><br><span class="line"><span class="string"> @age.setter</span></span><br><span class="line"><span class="string"> def age(self,age):</span></span><br><span class="line"><span class="string"> self.__age=age</span></span><br><span class="line"><span class="string"> s = student()</span></span><br><span class="line"><span class="string"> s.age = 10</span></span><br><span class="line"><span class="string"> print(s.age)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> 继承:</span></span><br><span class="line"><span class="string"> has a</span></span><br><span class="line"><span class="string"> class Student:</span></span><br><span class="line"><span class="string"> def __init__(self,name,book):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> is a # 真正的继承</span></span><br><span class="line"><span class="string"> 父类 子类</span></span><br><span class="line"><span class="string"> class Person:</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> class Student(Person):</span></span><br><span class="line"><span class="string"> ...</span></span><br><span class="line"><span class="string"> def study(self):</span></span><br><span class="line"><span class="string"> ...</span></span><br><span class="line"><span class="string"> def</span></span><br><span class="line"><span class="string"> s = Student()</span></span><br><span class="line"><span class="string"> s.study()</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> 1.__init__</span></span><br><span class="line"><span class="string"> 2.重写方法</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> 多继承:</span></span><br><span class="line"><span class="string"> class A:</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> class B:</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> class C(A,B):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> python3使用新式类:广度优先</span></span><br><span class="line"><span class="string"> python2使用经典类:深度优先</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> 查看搜索顺序</span></span><br><span class="line"><span class="string"> C.__mor__</span></span><br><span class="line"><span class="string"> import inspect</span></span><br><span class="line"><span class="string"> priny(inspect.getmro(C))</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 面向对象的特点:多态 封装 继承</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>:</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, name</span>):</span><br><span class="line"> <span class="variable language_">self</span>.name = name</span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">feed_pet</span>(<span class="params">self, pet</span>): <span class="comment"># pet既可以接受cat,也可以接受dog,还可以接收tiger</span></span><br><span class="line"> <span class="comment"># 其他语言的多态只能传一个类的子类</span></span><br><span class="line"> <span class="comment"># 如果传入的pet参数是Pet类型为真</span></span><br><span class="line"> <span class="comment"># isinstance(obj,类) ----->判断obj是不是类的对象或者判断obj是不是该子类的对象</span></span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">isinstance</span>(pet, Pet):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}喜欢养宠物{},昵称是{}'</span>.<span class="built_in">format</span>(<span class="variable language_">self</span>.name, pet.role, pet.nickname))</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'不是宠物类型。。。。'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Pet</span>:</span><br><span class="line"> role = <span class="string">''</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, nickname, age</span>):</span><br><span class="line"> <span class="variable language_">self</span>.nickname = nickname</span><br><span class="line"> <span class="variable language_">self</span>.age = age</span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">show</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'昵称:{},年龄:{}'</span>.<span class="built_in">format</span>(<span class="variable language_">self</span>.nickname, <span class="variable language_">self</span>.age))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Cat</span>(<span class="title class_ inherited__">Pet</span>):</span><br><span class="line"> role = <span class="string">'猫'</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">catch_mouse</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'抓老鼠....'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Dog</span>(<span class="title class_ inherited__">Pet</span>):</span><br><span class="line"> role = <span class="string">'狗'</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">whach_house</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'看家高手....'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Tiger</span>:</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">eat</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'太可怕了,能吃人'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建对象</span></span><br><span class="line">cat = Cat(<span class="string">'花花'</span>, <span class="number">2</span>)</span><br><span class="line">dog = Dog(<span class="string">'大黄'</span>, <span class="number">4</span>)</span><br><span class="line"></span><br><span class="line">person = Person(<span class="string">'小明'</span>)</span><br><span class="line">person.feed_pet(cat)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'-------------'</span>)</span><br><span class="line">person = Person(<span class="string">'小花'</span>)</span><br><span class="line">person.feed_pet(Tiger)</span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">pet 父类 cat dog 子类</span></span><br><span class="line"><span class="string">pet 大类型 cat </span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">回顾:</span><br><span class="line">面向对象的特点:</span><br><span class="line">封装,继承,多态</span><br><span class="line">封装:</span><br><span class="line">私有化属性:定义公有的set和get方法</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Student</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params"><span class="variable language_">self</span></span>):</span><br><span class="line"> <span class="variable language_">self</span>.__age=age</span><br><span class="line"></span><br><span class="line"> <span class="variable">@property</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">age</span>(<span class="params"><span class="variable language_">self</span></span>):</span><br><span class="line"> <span class="keyword">return</span> <span class="variable language_">self</span>.__age</span><br><span class="line"> <span class="variable">@age</span>.setter 装饰器,防止下面将上面覆盖</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">age</span>(<span class="params"><span class="variable language_">self</span>,age</span>):</span><br><span class="line"> <span class="variable language_">self</span>.__age=age</span><br><span class="line">s = <span class="title class_">Student</span>()</span><br><span class="line">s.age = <span class="number">10</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">继承:</span><br><span class="line">父类中私有的,子类无法继承</span><br><span class="line">is <span class="symbol">a:</span></span><br><span class="line"> 父类 子类</span><br><span class="line"> <span class="keyword">class</span> <span class="title class_">Student</span>(<span class="title class_">Person</span>):</span><br><span class="line"> pass</span><br><span class="line">has <span class="symbol">a:</span></span><br><span class="line"> <span class="keyword">class</span> <span class="title class_">Student</span>:</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params"><span class="variable language_">self</span>,book,computer</span>):</span><br><span class="line"> book是自定义类型 --- 系统类型</span><br><span class="line"> s = <span class="title class_">Student</span>()</span><br><span class="line"></span><br><span class="line">多继承:</span><br><span class="line"><span class="keyword">class</span> C(A,B):</span><br><span class="line"> pass</span><br><span class="line">广度优先 从上到下</span><br><span class="line">查看搜索顺序</span><br><span class="line">C.__mro__</span><br><span class="line"></span><br><span class="line">多态:</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>:</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">feed_pet</span>(<span class="params"><span class="variable language_">self</span>,cat</span>):</span><br><span class="line"> isinstance(pet,<span class="title class_">Pet</span>)判断参数是否是哪个类</span><br><span class="line"> pass</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- Codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021-12-17 下午 11:58</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : test.py</span></span><br><span class="line"><span class="comment"># @Software : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>:</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="variable language_">self</span>.__money = <span class="number">200</span></span><br><span class="line"> <span class="variable language_">self</span>.name = <span class="string">'匿名'</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">show1</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="variable language_">self</span>.name, <span class="variable language_">self</span>.__money)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Studen</span>(<span class="title class_ inherited__">Person</span>):</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">super</span>().__init__()</span><br><span class="line"> <span class="built_in">super</span>(Studen, <span class="variable language_">self</span>).__init__()</span><br><span class="line"> Person().__init__()<span class="comment"># 用的少</span></span><br><span class="line"> <span class="variable language_">self</span>.__money = <span class="number">100</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">show</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'money:'</span>, <span class="variable language_">self</span>.__money)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">s = Studen()</span><br><span class="line">s.show()</span><br><span class="line">s.show1()</span><br></pre></td></tr></table></figure>]]></content>
</entry>
<entry>
<title>IntelliJ IDEA 2021.3、PyCharm永久激活</title>
<link href="/posts/366baac0/"/>
<url>/posts/366baac0/</url>
<content type="html"><![CDATA[<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><blockquote><p>今天我安装PyCharm和IDEA的时候专业版的时候发现之前的激活方法已经失效了,于是,我花了两个小时在网上找到了。两种插件激活方法,这两种都是比较新的,</p><p>第一个插件<code>FineAgent</code>(作者好像把这项目停了,但是还可以使用)</p><p>第二种<code>ja-netfilter</code>Z大开发的(2021.11.30号发布) </p><p>接下来我就以Z大的插件来写一个教程</p></blockquote><h2 id="插件地址:热心大佬,随便选择一个进入"><a href="#插件地址:热心大佬,随便选择一个进入" class="headerlink" title="插件地址:热心大佬,随便选择一个进入"></a><a href="https://jetbra.in/s">插件地址:热心大佬</a>,随便选择一个进入</h2><p> <img src="/jetbrains/image-20220727183739894.png" alt="image-20220727183739894"></p><h2 id="如果不能进入,按照下面操作进入"><a href="#如果不能进入,按照下面操作进入" class="headerlink" title="如果不能进入,按照下面操作进入"></a>如果不能进入,按照下面操作进入</h2><p> <img src="/jetbrains/image-20220727183848825.png" alt="image-20220727183848825"></p><h2 id="最后下载左上角这个文件,解压到你想安装的目录,记住这个目录"><a href="#最后下载左上角这个文件,解压到你想安装的目录,记住这个目录" class="headerlink" title="最后下载左上角这个文件,解压到你想安装的目录,记住这个目录"></a>最后下载左上角这个文件,解压到你想安装的目录,记住这个目录</h2><p> <img src="/jetbrains/image-20220727183720681.png" alt="image-20220727183720681"></p><h1 id="1、在jetbrains的官网下载并安装PyCharm或idea"><a href="#1、在jetbrains的官网下载并安装PyCharm或idea" class="headerlink" title="1、在jetbrains的官网下载并安装PyCharm或idea"></a>1、在jetbrains的官网下载并安装PyCharm或idea</h1><blockquote><p><a href="https://www.jetbrains.com/">https://www.jetbrains.com/</a></p></blockquote><h1 id="2、安装应用程序"><a href="#2、安装应用程序" class="headerlink" title="2、安装应用程序"></a>2、安装应用程序</h1><p>1)安装之后找到<code>bin</code>目录下以<code>*.exe.vmoptions</code>结尾的文件(例如<code>idea64.exe.vmoptions</code>),以记事本打开,在最后一行添加</p><p>2)如果之前已经安装好那么在<code>C:\Users\[用户名]\AppData\Roaming\JetBrains</code>目录下找以<code>*.exe.vmoptions</code>结尾的文件(例如<code>idea64.exe.vmoptions</code>)</p><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">--add-opens</span>=java.base/jdk<span class="selector-class">.internal</span><span class="selector-class">.org</span><span class="selector-class">.objectweb</span>.asm=ALL-UNNAMED</span><br><span class="line"><span class="attr">--add-opens</span>=java.base/jdk<span class="selector-class">.internal</span><span class="selector-class">.org</span><span class="selector-class">.objectweb</span><span class="selector-class">.asm</span>.tree=ALL-UNNAMED</span><br><span class="line"></span><br><span class="line">#注意下面路径填写你之前下载文件的解压路径,加载ja-netfilter<span class="selector-class">.jar</span></span><br><span class="line">-javaagent:D:/Program Files/JetBrains/ja-netfilter/ja-netfilter.jar=jetbrains</span><br></pre></td></tr></table></figure><h1 id="3、然后在上面下载插件的那个地址复制对应软件的激活码就好了"><a href="#3、然后在上面下载插件的那个地址复制对应软件的激活码就好了" class="headerlink" title="3、然后在上面下载插件的那个地址复制对应软件的激活码就好了"></a>3、然后在上面下载插件的那个地址复制对应软件的激活码就好了</h1><p><img src="/jetbrains/image-20220727184341208.png" alt="image-20220727184341208"></p><h1 id="开源地址"><a href="#开源地址" class="headerlink" title="开源地址"></a>开源地址</h1><h2 id="ja-netfilter"><a href="#ja-netfilter" class="headerlink" title="ja-netfilter"></a><a href="https://gitee.com/ja-netfilter/ja-netfilter">ja-netfilter</a></h2><h2 id="插件作者:Z大-知了"><a href="#插件作者:Z大-知了" class="headerlink" title="插件作者:Z大(知了)"></a><a href="https://zhile.io/">插件作者:Z大(知了)</a></h2>]]></content>
<categories>
<category> 教程 </category>
</categories>
<tags>
<tag> 激活 </tag>
<tag> ider </tag>
<tag> Pycharm </tag>
<tag> jetbrains </tag>
</tags>
</entry>
<entry>
<title>base64解码</title>
<link href="/posts/c539f933/"/>
<url>/posts/c539f933/</url>
<content type="html"><![CDATA[<p>base64解码</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> base64</span><br><span class="line"></span><br><span class="line">container = <span class="built_in">open</span>(<span class="string">r'base.txt'</span>, <span class="string">'rt'</span>, encoding=<span class="string">'utf-8'</span>).read()</span><br><span class="line"><span class="keyword">while</span> <span class="number">1</span>:</span><br><span class="line"> container = base64.b64decode(container)</span><br><span class="line"> <span class="keyword">if</span> <span class="string">' '</span> <span class="keyword">in</span> <span class="built_in">str</span>(container):</span><br><span class="line"> <span class="built_in">print</span>(<span class="built_in">str</span>(container))</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">if</span> <span class="string">'{'</span> <span class="keyword">in</span> <span class="built_in">str</span>(container):</span><br><span class="line"> <span class="built_in">print</span>(<span class="built_in">str</span>(container))</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">if</span> <span class="string">'flag'</span> <span class="keyword">in</span> <span class="built_in">str</span>(container):</span><br><span class="line"> <span class="built_in">print</span>(<span class="built_in">str</span>(container))</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">if</span> <span class="string">'ctf'</span> <span class="keyword">in</span> <span class="built_in">str</span>(container):</span><br><span class="line"> <span class="built_in">print</span>(<span class="built_in">str</span>(container))</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> 工具 </tag>
</tags>
</entry>
<entry>
<title>邮件测试</title>
<link href="/posts/b57f2192/"/>
<url>/posts/b57f2192/</url>
<content type="html"><![CDATA[<p>邮件测试</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> smtplib</span><br><span class="line"><span class="keyword">from</span> email.mime.text <span class="keyword">import</span> MIMEText</span><br><span class="line"><span class="keyword">from</span> email.utils <span class="keyword">import</span> formataddr</span><br><span class="line"></span><br><span class="line">my_sender = <span class="string">'wa.ns@qq.com'</span> <span class="comment"># 发件人邮箱账号</span></span><br><span class="line">my_pass = <span class="string">'afgbznymvzbkjhee'</span> <span class="comment"># 发件人邮箱密码</span></span><br><span class="line">my_user = <span class="string">'wa.ns@qq.com'</span> <span class="comment"># 收件人邮箱账号,我这边发送给自己</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_status</span>(<span class="params">**kwargs</span>):</span><br><span class="line"> <span class="comment"># 调用一言一句话</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">YiYan</span>():</span><br><span class="line"> data = {</span><br><span class="line"> <span class="string">'c'</span>: <span class="string">'j'</span>,</span><br><span class="line"> <span class="string">'encode'</span>: <span class="string">'text'</span>,</span><br><span class="line"> <span class="string">'charset'</span>: <span class="string">'utf-8'</span>,</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> HitokotoUrl = <span class="string">'https://v1.hitokoto.cn/'</span></span><br><span class="line"> YiJvHua = requests.get(url=HitokotoUrl, params=(data)).text</span><br><span class="line"> url_google = <span class="string">'http://xy1314.xyz/API/Google/api.php?msg={}&b=en'</span>.<span class="built_in">format</span>(YiJvHua)</span><br><span class="line"> translation = requests.get(url_google).text</span><br><span class="line"> <span class="keyword">return</span> YiJvHua + <span class="string">'<br>'</span> + translation</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 调用PushPlus</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">PushPlus</span>():</span><br><span class="line"> translation = YiYan()</span><br><span class="line"> send = translation</span><br><span class="line"> text = <span class="string">"""</span></span><br><span class="line"><span class="string"> <p>哥哥帮你打卡成功了呦!!送哥哥一句话:</p></span></span><br><span class="line"><span class="string"> <p align="right">{}</p></span></span><br><span class="line"><span class="string"> """</span>.<span class="built_in">format</span>(send)</span><br><span class="line"> data = {</span><br><span class="line"> <span class="string">"title"</span>: <span class="string">"我在校园"</span>,</span><br><span class="line"> <span class="string">"content"</span>: <span class="string">"{}"</span>.<span class="built_in">format</span>(text),</span><br><span class="line"> <span class="string">"template"</span>: <span class="string">"json"</span></span><br><span class="line"> }</span><br><span class="line"> PushPlus_Url = <span class="string">"http://pushplus.hxtrip.com/send?token=35f8b520a9be4e2883d2681870b0fe37"</span></span><br><span class="line"> result = requests.get(PushPlus_Url, params=(data)).text</span><br><span class="line"> <span class="keyword">return</span> result + text</span><br><span class="line"> text = <span class="string">"""</span></span><br><span class="line"><span class="string"> <p>哥哥帮你打卡成功了呦!!送哥哥一句话:</p></span></span><br><span class="line"><span class="string"> <p align="right">{}</p></span></span><br><span class="line"><span class="string"> """</span>.<span class="built_in">format</span>(YiYan())</span><br><span class="line"> PushPlus()</span><br><span class="line"> <span class="keyword">return</span> text</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">mail</span>():</span><br><span class="line"> ret = <span class="literal">True</span></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> msg = MIMEText(get_status())</span><br><span class="line"> msg[<span class="string">'From'</span>] = formataddr([<span class="string">"神秘人"</span>, my_sender]) <span class="comment"># 括号里的对应发件人邮箱昵称、发件人邮箱账号</span></span><br><span class="line"> msg[<span class="string">'To'</span>] = formataddr([<span class="string">"FK"</span>, my_user]) <span class="comment"># 括号里的对应收件人邮箱昵称、收件人邮箱账号</span></span><br><span class="line"> msg[<span class="string">'Subject'</span>] = <span class="string">"神秘邮件"</span> <span class="comment"># 邮件的主题,也可以说是标题</span></span><br><span class="line"></span><br><span class="line"> server = smtplib.SMTP_SSL(<span class="string">"smtp.qq.com"</span>, <span class="number">465</span>) <span class="comment"># 发件人邮箱中的SMTP服务器,端口是25</span></span><br><span class="line"> server.login(my_sender, my_pass) <span class="comment"># 括号中对应的是发件人邮箱账号、邮箱密码</span></span><br><span class="line"> server.sendmail(my_sender, [my_user, ], msg.as_string()) <span class="comment"># 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件</span></span><br><span class="line"> server.quit() <span class="comment"># 关闭连接</span></span><br><span class="line"> <span class="keyword">except</span> Exception: <span class="comment"># 如果 try 中的语句没有执行,则会执行下面的 ret=False</span></span><br><span class="line"> ret = <span class="literal">False</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">ret = mail()</span><br><span class="line"><span class="keyword">if</span> ret:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"邮件发送成功"</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"邮件发送失败"</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> 工具 </tag>
</tags>
</entry>
<entry>
<title>大小写转换脚本</title>
<link href="/posts/8401e43a/"/>
<url>/posts/8401e43a/</url>
<content type="html"><![CDATA[<h1 id="大小写转换脚本"><a href="#大小写转换脚本" class="headerlink" title="大小写转换脚本"></a>大小写转换脚本</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(<span class="string">'大小写转换脚本'</span>)</span><br><span class="line">str1 = <span class="built_in">input</span>(<span class="string">'请输入你要转换的字符:'</span>)</span><br><span class="line">re = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">'将字符串全部转换为大写请按1,反之按2:'</span>))</span><br><span class="line"><span class="keyword">if</span> re == <span class="number">1</span>:</span><br><span class="line"> <span class="built_in">print</span>(str1.upper())</span><br><span class="line"><span class="keyword">if</span> re == <span class="number">2</span>:</span><br><span class="line"> <span class="built_in">print</span>(str1.lower())</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> ctf工具 </tag>
</tags>
</entry>
<entry>
<title>埃特巴什码</title>
<link href="/posts/e37c0862/"/>
<url>/posts/e37c0862/</url>
<content type="html"><![CDATA[<h1 id="埃特巴什码"><a href="#埃特巴什码" class="headerlink" title="埃特巴什码"></a>埃特巴什码</h1><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">DIC_LOWER = "abcdefghijklmnopqrstuvwxyz"</span><br><span class="line">DIC_UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"</span><br><span class="line">DIC_LOWER_RE = DIC_LOWER[::-1]</span><br><span class="line">DIC_UPPER_RE = DIC_UPPER[::-1]</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">加密算法</span></span><br><span class="line">def encrypt_atbash(string):</span><br><span class="line"> ciphertext = ""</span><br><span class="line"> for i in string:</span><br><span class="line"> if i in DIC_LOWER:</span><br><span class="line"> ciphertext += DIC_LOWER_RE[DIC_LOWER.index(i)]</span><br><span class="line"> elif i in DIC_UPPER:</span><br><span class="line"> ciphertext += DIC_UPPER_RE[DIC_UPPER.index(i)]</span><br><span class="line"> else:</span><br><span class="line"> ciphertext += i</span><br><span class="line"> return ciphertext</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">解密算法</span></span><br><span class="line">def decrypt_atbash(string):</span><br><span class="line"> plaintext = ""</span><br><span class="line"> for i in string:</span><br><span class="line"> if i in DIC_LOWER_RE:</span><br><span class="line"> plaintext += DIC_LOWER[DIC_LOWER_RE.index(i)]</span><br><span class="line"> elif i in DIC_UPPER:</span><br><span class="line"> plaintext += DIC_UPPER[DIC_UPPER_RE.index(i)]</span><br><span class="line"> else:</span><br><span class="line"> plaintext += i</span><br><span class="line"> return plaintext</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ == '__main__':</span><br><span class="line"> Key_Word = input("请输入要解埃特巴什码编码:")</span><br><span class="line"> ciphertext_ = encrypt_atbash(Key_Word)</span><br><span class="line"> plaintext_ = decrypt_atbash(ciphertext_)</span><br><span class="line"> print(ciphertext_)</span><br><span class="line"> print(plaintext_)</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> ctf工具 </tag>
</tags>
</entry>
<entry>
<title>我在校园2.5python</title>
<link href="/posts/387075cf/"/>
<url>/posts/387075cf/</url>
<content type="html"><![CDATA[<h1 id="📲-我在校园打卡-Python"><a href="#📲-我在校园打卡-Python" class="headerlink" title="📲 我在校园打卡(Python)"></a>📲 我在校园打卡(Python)</h1><p>新版本我在校园取消了原来的token鉴权机制,改为JWSESSION进行鉴权。</p><p>我结合两个位大神的程序更改了一下源代码实现了以下功能</p><h2 id="😊目前实现"><a href="#😊目前实现" class="headerlink" title="😊目前实现"></a>😊目前实现</h2><ul><li><input checked="" disabled="" type="checkbox"> 使用自动登录的方式获取jwsession值,将jwsession储存在leanCloud </li><li><input checked="" disabled="" type="checkbox"> 增加了一言一句话,调用google翻译的api来翻译一言一句话(心灵鸡汤😂)</li><li><input checked="" disabled="" type="checkbox"> 使用PushPlus每天自动发送告诉你是否打卡成功邮件</li></ul><h2 id="🚩准备工作"><a href="#🚩准备工作" class="headerlink" title="🚩准备工作"></a>🚩准备工作</h2><p>纯python编写,需要安装第三方库<code>requests</code> <code>leancloud</code></p><h2 id="🌕依赖安装(本地测试,也可以不测试,之间上传)"><a href="#🌕依赖安装(本地测试,也可以不测试,之间上传)" class="headerlink" title="🌕依赖安装(本地测试,也可以不测试,之间上传)"></a>🌕依赖安装(本地测试,也可以不测试,之间上传)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests</span><br><span class="line">pip install -i https://pypi.tuna.tsinghua.edu.cn/simple leancloud</span><br></pre></td></tr></table></figure><h2 id="👌开始"><a href="#👌开始" class="headerlink" title="👌开始"></a>👌开始</h2><h3 id="1-获取-PushPlus-的-token"><a href="#1-获取-PushPlus-的-token" class="headerlink" title="1. 获取 PushPlus 的 token"></a>1. 获取 PushPlus 的 token</h3><p>微信搜索公众号“pushplus 推送加”,关注后即可生成属于自己的 token,后面需要用到</p><h3 id="2-注册leanCloud"><a href="#2-注册leanCloud" class="headerlink" title="2.注册leanCloud"></a>2.注册leanCloud</h3><p>1)<a href="https://console.leancloud.cn/apps">注册 leanCloud 账号</a></p><p>2)到控制台新建应用,应用名字随意,应用版本选择 <strong>开发版</strong></p><p>3)进入应用,点击左侧的“数据存储 ➡ 结构化数据”,新建 Class:名称为“Info”,Class 访问权限为“所有用户”,下面的 ACL 权限选择“限制写入”</p><p>4)进入刚才创建的 Class,添加新列,值为“jwsession”,之后添加新行,注意这时候 objectId 会有一个值。如果你知道如何抓包获取自己的 jwsession,则可以填入 jwsession 值,否则可以随便填。</p><p>5)点击左侧的“设置 ➡ 应用凭证”,记住 appId 和 masterKey(<strong>请务必自己保管好,不要泄露</strong>) 的值,待会需要用到</p><h4 id="3-创建云函数"><a href="#3-创建云函数" class="headerlink" title="3. 创建云函数"></a>3. 创建云函数</h4><p>1)注册腾讯云账号并登录,进行实名认证</p><p>2)到 <a href="https://console.cloud.tencent.com/scf/list?rid=1&ns=default">https://console.cloud.tencent.com/scf/list?rid=1&ns=default</a> ,选择 “新建云函数” ➔ “自定义创建”,提交方法,可以直接在线编辑然后将代码复制里面,然后点击完成就创建好了</p><h4 id="4-修改配置文件"><a href="#4-修改配置文件" class="headerlink" title="4. 修改配置文件"></a>4. 修改配置文件</h4><p>到刚才新创建的云函数中,打开代码配置文件进行修改。</p><p>1)“我在校园”账号配置项说明:</p><ul><li><code>username</code>:“我在校园”的账号,一般是你的手机号码</li><li><code>password</code>:“我在校园”的密码,忘记了打开小程序重新设置就行</li><li><code>temperature</code>:默认上报的体温为 36°C,如果你想随机上报体温,请以“36~38”的形式填写</li><li><code>city</code>:你学校所在的城市</li><li><code>school</code>:你学校的名字</li></ul><p>2)“pushPlus” 账号配置项说明:</p><ul><li><code>isEnable</code>:默认 <code>false</code>,表示不开启消息提醒功能,若要开启请修改为 <code>true</code></li><li><code>notifyToken</code>:之前你从 pushPlus 公众号那里获取的 token</li></ul><p>3)“leanCloud” 账号配置项说明:</p><ul><li><code>appId</code>:之前在 leanCloud 获取的 appId</li><li><code>masterKey</code>:之前在 leanCloud 获取的 masterKey</li></ul><h4 id="5-安装-leanCloud-库"><a href="#5-安装-leanCloud-库" class="headerlink" title="5. 安装 leanCloud 库"></a>5. 安装 leanCloud 库</h4><p>到刚才新创建的云函数中,<code>ctrl + shift + ~</code> 新建终端,cd 进入 <code>index.py</code> 所在的文件夹中,通过如下命令安装 leanCloud 库:</p><figure class="highlight cmake"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip3 <span class="keyword">install</span> leancloud -t .</span><br></pre></td></tr></table></figure><p>可能会报错,主要是因为相关库版本不匹配的问题,不影响正常使用。看到 <code>successfully installed</code> 就说明安装成功了。</p><h2 id="代码段-云函数"><a href="#代码段-云函数" class="headerlink" title="代码段(云函数)"></a>代码段(云函数)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- Codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/11/5 8:12</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : 健康打卡2.5.py</span></span><br><span class="line"><span class="comment"># @Software : PyCharm</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">import</span> logging</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line"><span class="keyword">import</span> leancloud</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将你的账号和密码写在下面引号内</span></span><br><span class="line">username = <span class="string">""</span></span><br><span class="line">password = <span class="string">""</span></span><br><span class="line"><span class="comment"># 修改此处token值,打开官网扫码点击一对一就可以看见token值:http://www.pushplus.plus/ 将获取到的token值填写在下面引号内</span></span><br><span class="line">token = <span class="string">''</span></span><br><span class="line"><span class="comment"># leancloud的appId和key值</span></span><br><span class="line">appId = <span class="string">""</span></span><br><span class="line">masterKey = <span class="string">""</span></span><br><span class="line"></span><br><span class="line">logger = logging.getLogger()</span><br><span class="line">logger.setLevel(logging.INFO)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">leanCloudss</span>:</span><br><span class="line"> <span class="comment"># 初始化 leanCloud 对象</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line"> leancloud.init(appId, master_key=masterKey)</span><br><span class="line"> <span class="variable language_">self</span>.obj = leancloud.Query(<span class="string">'Info'</span>).first()</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 获取 jwsession</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">getJwsession</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="keyword">return</span> <span class="variable language_">self</span>.obj.get(<span class="string">'jwsession'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 设置 jwsession</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">setJwsession</span>(<span class="params">self, jwsession</span>):</span><br><span class="line"> <span class="variable language_">self</span>.obj.<span class="built_in">set</span>(<span class="string">'jwsession'</span>, jwsession)</span><br><span class="line"> <span class="variable language_">self</span>.obj.save()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 自动登录获取jwsession值</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">WoZaiXiaoYuanPuncher</span>:</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="comment"># JWSESSION</span></span><br><span class="line"> <span class="variable language_">self</span>.jwsession = <span class="literal">None</span></span><br><span class="line"> <span class="comment"># 打卡结果</span></span><br><span class="line"> <span class="variable language_">self</span>.status_code = <span class="number">0</span></span><br><span class="line"> <span class="comment"># 登陆接口</span></span><br><span class="line"> <span class="variable language_">self</span>.loginUrl = <span class="string">"https://gw.wozaixiaoyuan.com/basicinfo/mobile/login/username"</span></span><br><span class="line"> <span class="comment"># 请求头</span></span><br><span class="line"> <span class="variable language_">self</span>.header = {</span><br><span class="line"> <span class="string">"Accept-Encoding"</span>: <span class="string">"gzip, deflate, br"</span>,</span><br><span class="line"> <span class="string">"Connection"</span>: <span class="string">"keep-alive"</span>,</span><br><span class="line"> <span class="string">"User-Agent"</span>: <span class="string">"Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.13(0x18000d32) NetType/WIFI Language/zh_CN miniProgram"</span>,</span><br><span class="line"> <span class="string">"Content-Type"</span>: <span class="string">"application/json;charset=UTF-8"</span>,</span><br><span class="line"> <span class="string">"Content-Length"</span>: <span class="string">"2"</span>,</span><br><span class="line"> <span class="string">"Host"</span>: <span class="string">"gw.wozaixiaoyuan.com"</span>,</span><br><span class="line"> <span class="string">"Accept-Language"</span>: <span class="string">"en-us,en"</span>,</span><br><span class="line"> <span class="string">"Accept"</span>: <span class="string">"application/json, text/plain, */*"</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment"># 请求体(必须有)</span></span><br><span class="line"> <span class="variable language_">self</span>.body = <span class="string">"{}"</span></span><br><span class="line"></span><br><span class="line"> <span class="comment"># 登录</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">login</span>(<span class="params">self</span>):</span><br><span class="line"></span><br><span class="line"> url = <span class="string">f'<span class="subst">{self.loginUrl}</span>?username=<span class="subst">{username}</span>&password=<span class="subst">{password}</span>'</span></span><br><span class="line"> <span class="variable language_">self</span>.session = requests.session()</span><br><span class="line"> <span class="comment"># 登录</span></span><br><span class="line"> response = <span class="variable language_">self</span>.session.post(url=url, data=<span class="variable language_">self</span>.body, headers=<span class="variable language_">self</span>.header)</span><br><span class="line"> res = json.loads(response.text)</span><br><span class="line"> <span class="keyword">if</span> res[<span class="string">"code"</span>] == <span class="number">0</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"使用账号信息登录成功"</span>)</span><br><span class="line"> jwsession = response.headers[<span class="string">'JWSESSION'</span>]</span><br><span class="line"> <span class="keyword">return</span> jwsession</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(res)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"登录失败,请检查账号信息"</span>)</span><br><span class="line"> <span class="variable language_">self</span>.status_code = <span class="number">5</span></span><br><span class="line"> <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Custom</span>:</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"> <span class="comment"># 调用一言一句话</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">YiYan</span>(<span class="params">self</span>):</span><br><span class="line"> data = {</span><br><span class="line"> <span class="string">'c'</span>: <span class="string">'j'</span>,</span><br><span class="line"> <span class="string">'encode'</span>: <span class="string">'text'</span>,</span><br><span class="line"> <span class="string">'charset'</span>: <span class="string">'utf-8'</span>,</span><br><span class="line"> }</span><br><span class="line"> HitokotoUrl = <span class="string">'https://v1.hitokoto.cn/'</span></span><br><span class="line"> YiJvHua = requests.get(url=HitokotoUrl, params=(data)).text</span><br><span class="line"> <span class="comment"># url_google = 'http://xy1314.xyz/API/Google/api.php?msg={}&b=en'.format(YiJvHua)</span></span><br><span class="line"> <span class="comment"># translation = requests.get(YiJvHua).text</span></span><br><span class="line"> <span class="keyword">return</span> YiJvHua</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 调用PushPlus</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">PushPlus</span>(<span class="params">self, text</span>):</span><br><span class="line"> data = {</span><br><span class="line"> <span class="string">"title"</span>: <span class="string">"我在校园"</span>,</span><br><span class="line"> <span class="string">"content"</span>: <span class="string">"{}"</span>.<span class="built_in">format</span>(text),</span><br><span class="line"> <span class="string">"template"</span>: <span class="string">"json"</span></span><br><span class="line"> }</span><br><span class="line"> PushPlus_Url = <span class="string">"http://www.pushplus.plus/send?token={}"</span>.<span class="built_in">format</span>(token)</span><br><span class="line"> result = requests.get(PushPlus_Url, params=(data)).text</span><br><span class="line"> <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 判度是否返回的code来推送消息</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">get_status</span>(<span class="params">self</span>):</span><br><span class="line"> Send_News = Custom()</span><br><span class="line"> <span class="keyword">if</span> <span class="variable language_">self</span>[<span class="string">'code'</span>] == <span class="number">0</span>:</span><br><span class="line"> text = <span class="string">"哥哥帮你打卡成功了呦!!送哥哥一句话:<br><br>{}"</span>.<span class="built_in">format</span>(Send_News.YiYan())</span><br><span class="line"> <span class="keyword">return</span> Send_News.PushPlus(text)</span><br><span class="line"> <span class="keyword">elif</span> <span class="variable language_">self</span>[<span class="string">'code'</span>] == <span class="number">1</span>:</span><br><span class="line"> text = <span class="string">"健康打卡已经结束了哦!!!"</span></span><br><span class="line"> <span class="keyword">return</span> Send_News.PushPlus(text + <span class="string">'<br>'</span> + Send_News.YiYan())</span><br><span class="line"> <span class="keyword">elif</span> <span class="variable language_">self</span>[<span class="string">'code'</span>] == -<span class="number">10</span>:</span><br><span class="line"> text = <span class="string">"···jwsession已失效"</span></span><br><span class="line"> <span class="keyword">return</span> Send_News.PushPlus(text + <span class="string">'<br>'</span> + Send_News.YiYan())</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> text = <span class="string">"!!!发生未知错误"</span></span><br><span class="line"> <span class="keyword">return</span> Send_News.PushPlus(text + <span class="string">'<br>'</span> + Send_News.YiYan())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">answer</span>:</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="variable language_">self</span>.api = <span class="string">"https://student.wozaixiaoyuan.com/health/save.json"</span></span><br><span class="line"> <span class="variable language_">self</span>.headers = {</span><br><span class="line"> <span class="string">"Host"</span>: <span class="string">"student.wozaixiaoyuan.com"</span>,</span><br><span class="line"> <span class="string">"Content-Type"</span>: <span class="string">"application/x-www-form-urlencoded"</span>,</span><br><span class="line"> <span class="string">"Accept-Encoding"</span>: <span class="string">"gzip, deflate, br"</span>,</span><br><span class="line"> <span class="string">"Connection"</span>: <span class="string">"keep-alive"</span>,</span><br><span class="line"> <span class="comment"># 修改5:User-Agent</span></span><br><span class="line"> <span class="string">"User-Agent"</span>: <span class="string">"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat"</span>,</span><br><span class="line"> <span class="comment"># 修改6:Referer</span></span><br><span class="line"> <span class="string">"Referer"</span>: <span class="string">"https://servicewechat.com/wxce6d08f781975d91/181/page-frame.html"</span>,</span><br><span class="line"> <span class="string">"Content-Length"</span>: <span class="string">"360"</span>,</span><br><span class="line"> <span class="string">"JWSESSION"</span>: <span class="string">"{}"</span>.<span class="built_in">format</span>(leanCloudss().getJwsession())</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="variable language_">self</span>.data = {</span><br><span class="line"> <span class="string">"answers"</span>: <span class="string">'["0","1","36.5","无"]'</span>,</span><br><span class="line"> <span class="comment"># 修改8:打卡地址</span></span><br><span class="line"> <span class="string">"longitude"</span>: <span class="string">"109.02697"</span>, <span class="comment"># 经度</span></span><br><span class="line"> <span class="string">"latitude"</span>: <span class="string">"32.6955"</span>, <span class="comment"># 纬度</span></span><br><span class="line"> <span class="string">"country"</span>: <span class="string">"中国"</span>,</span><br><span class="line"> <span class="string">"province"</span>: <span class="string">"陕西省"</span>,</span><br><span class="line"> <span class="string">"city"</span>: <span class="string">"西安市"</span>,</span><br><span class="line"> <span class="string">"district"</span>: <span class="string">"**区"</span>,</span><br><span class="line"> <span class="string">"township"</span>: <span class="string">"**街道"</span>,</span><br><span class="line"> <span class="string">"street"</span>: <span class="string">"***路"</span>,</span><br><span class="line"> <span class="string">"areacode"</span>: <span class="string">"1111111"</span>,</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">get_seq</span>(<span class="params">self</span>):</span><br><span class="line"> current_hour = datetime.datetime.now()</span><br><span class="line"> current_hour = current_hour.hour + <span class="number">8</span></span><br><span class="line"> <span class="keyword">if</span> <span class="number">0</span> <= current_hour <= <span class="number">18</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">run</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"JWSESSION:"</span> + <span class="variable language_">self</span>.headers[<span class="string">"JWSESSION"</span>])</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'当前时间为:'</span>, datetime.datetime.now())</span><br><span class="line"> <span class="variable language_">self</span>.res = requests.post(<span class="variable language_">self</span>.api, headers=<span class="variable language_">self</span>.headers, data=<span class="variable language_">self</span>.data, ).json() <span class="comment"># 打卡提交</span></span><br><span class="line"> time.sleep(<span class="number">1</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="variable language_">self</span>.res)</span><br><span class="line"> <span class="keyword">if</span> <span class="variable language_">self</span>.res[<span class="string">'code'</span>] != <span class="number">0</span> <span class="keyword">and</span> <span class="variable language_">self</span>.res[<span class="string">'code'</span>] != <span class="number">1</span>:</span><br><span class="line"> leanClouds().setJwsession(WoZaiXiaoYuanPuncher().login())</span><br><span class="line"> get_status(<span class="variable language_">self</span>.res)</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> get_status(<span class="variable language_">self</span>.res)</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line"> <span class="keyword">if</span> answer().run():</span><br><span class="line"> <span class="keyword">pass</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> answer().run()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">main_handler</span>(<span class="params">event, context</span>):</span><br><span class="line"> logger.info(<span class="string">'got event{}'</span>.<span class="built_in">format</span>(event))</span><br><span class="line"> <span class="keyword">return</span> answer().run()</span><br><span class="line"></span><br></pre></td></tr></table></figure><h2 id="📢-声明"><a href="#📢-声明" class="headerlink" title="📢 声明"></a>📢 声明</h2><ol><li>本项目仅供编程学习/个人使用,请遵守Apache-2.0 License开源项目授权协议。</li><li>请在国家法律法规和校方相关原则下使用。</li><li>开发者不对任何下载者和使用者的任何行为负责。</li><li>本程序无任何后门,所有数据仅存留于使用者机器上。 </li><li>请不要轻易将自己的账号信息告诉他人。</li></ol>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> 工具 </tag>
<tag> 校园 </tag>
</tags>
</entry>
<entry>
<title>phpstudy和windows_mysql_冲突解决办法</title>
<link href="/posts/d114782d/"/>
<url>/posts/d114782d/</url>
<content type="html"><![CDATA[<h1 id="1、删除自己在电脑上安装的mysql服务"><a href="#1、删除自己在电脑上安装的mysql服务" class="headerlink" title="1、删除自己在电脑上安装的mysql服务"></a>1、删除自己在电脑上安装的mysql服务</h1><p>Win+X,管理员打开windows终端然后输入命令,<code>PS:如果你没有添加mysql环境变量,那么你需要进入mysql中的bin目录里面打开终端</code></p><p><img src="/phpstudy%E5%92%8Cwindows-mysql-%E5%86%B2%E7%AA%81%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/image-20211115174131632.webp" alt="image-20211115174131632"></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">先停止你的mysql服务</span></span><br><span class="line">net stop mysql</span><br><span class="line">mysqld -remove</span><br></pre></td></tr></table></figure><h1 id="2、打开phpstudy启动mysql"><a href="#2、打开phpstudy启动mysql" class="headerlink" title="2、打开phpstudy启动mysql"></a>2、打开phpstudy启动mysql</h1><p>点击启动就可以启动了</p><p><img src="/phpstudy%E5%92%8Cwindows-mysql-%E5%86%B2%E7%AA%81%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/image-20211115174500274.webp" alt="image-20211115174500274"></p><h1 id="3、创建新的mysql服务,服务名称重新命名"><a href="#3、创建新的mysql服务,服务名称重新命名" class="headerlink" title="3、创建新的mysql服务,服务名称重新命名"></a>3、创建新的mysql服务,服务名称重新命名</h1><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">新建myql服务名称</span></span><br><span class="line">mysqld install sql</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动mysql服务,你就可以打开你原来的mysql数据库了</span></span><br><span class="line">net start slq</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 环境问题 </category>
</categories>
<tags>
<tag> 教程 </tag>
</tags>
</entry>
<entry>
<title>python魔术方法</title>
<link href="/posts/b87b8b00/"/>
<url>/posts/b87b8b00/</url>
<content type="html"><![CDATA[<h1 id="python-魔术方法"><a href="#python-魔术方法" class="headerlink" title="python 魔术方法"></a>python 魔术方法</h1><h1 id="常用魔术方法"><a href="#常用魔术方法" class="headerlink" title="常用魔术方法"></a>常用魔术方法</h1><hr><blockquote><p>魔术方法就是一个类/对象中的方法,和普通方法唯一的不同时,普通方法需要调用!而魔术方法是在特定时刻自动触发。</p></blockquote><h4 id="1-init"><a href="#1-init" class="headerlink" title="1.init"></a>1.<strong>init</strong></h4><figure class="highlight crystal"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">初始化魔术方法</span><br><span class="line">触发时机:初始化对象时触发(不是实例化触发,但是和实例化在一个操作中)</span><br><span class="line">参数:至少有一个<span class="keyword">self</span>,接收对象</span><br><span class="line">返回值:无</span><br><span class="line">作用:初始化对象的成员</span><br><span class="line">注意:使用该方式初始化的成员都是直接写入对象当中,类中无法具有</span><br></pre></td></tr></table></figure><h4 id="2-new"><a href="#2-new" class="headerlink" title="2.new"></a>2.<strong>new</strong></h4><figure class="highlight asciidoc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">实例化魔术方法</span><br><span class="line">触发时机: 在实例化对时触发</span><br><span class="line">参数:至少一个cls 接收当前类</span><br><span class="line">返回值:必须返回一个对象实例</span><br><span class="line">作用:实例化对象</span><br><span class="line">注意:实例化对象是Object类底层实现,其他类继承了Object的<span class="emphasis">__new__</span>才能够实现实例化对象。</span><br><span class="line">没事别碰这个魔术方法,先触发<span class="emphasis">__new__</span>才会触发<span class="emphasis">__init__</span> </span><br></pre></td></tr></table></figure><h4 id="3-del"><a href="#3-del" class="headerlink" title="3.del"></a>3.<strong>del</strong></h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">析构魔术方法</span><br><span class="line">触发时机:当对象没有用(没有任何变量引用)的时候被触发</span><br><span class="line">参数:一个<span class="variable language_">self</span> 结婚搜对象</span><br><span class="line">返回值:无</span><br><span class="line">作用:使用完对象是回收资源</span><br><span class="line">注意:<span class="keyword">del</span>不一定会触发当前方法,只有当前对象没有任何变量接收时才会触发</span><br></pre></td></tr></table></figure><h4 id="4-call"><a href="#4-call" class="headerlink" title="4.call"></a>4.<strong>call</strong></h4><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">调用对象的魔术方法</span><br><span class="line"><span class="section">触发时机:将对象当作函数调用时触发 对象()</span></span><br><span class="line"><span class="section">参数:至少一个self接收对象,其余根据调用时参数决定</span></span><br><span class="line">返回值:根据情况而定</span><br><span class="line">作用:可以将复杂的步骤进行合并操作,减少调用的步骤,方便使用</span><br><span class="line">注意:无</span><br></pre></td></tr></table></figure><h4 id="5-len"><a href="#5-len" class="headerlink" title="5.len"></a>5.<strong>len</strong></h4><figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">触发时机:使用<span class="built_in">len</span>(对象) 的时候触发</span><br><span class="line">参数:一个参数<span class="built_in">self</span></span><br><span class="line">返回值:必须是一个整型</span><br><span class="line">作用:可以设置为检测对象成员个数,但是也可以进行其他任意操作</span><br><span class="line">注意:返回值必须必须是整数,否则语法报错,另外该要求是格式要求。</span><br></pre></td></tr></table></figure><h4 id="6-str"><a href="#6-str" class="headerlink" title="6.str"></a>6.<strong>str</strong></h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">触发时机:使用<span class="built_in">print</span>(对象)或者<span class="built_in">str</span>(对象)的时候触发</span><br><span class="line">参数:一个<span class="variable language_">self</span>接收对象</span><br><span class="line">返回值:必须是字符串类型</span><br><span class="line">作用:<span class="built_in">print</span>(对象时)进行操作,得到字符串,通常用于快捷操作</span><br><span class="line">注意:无</span><br></pre></td></tr></table></figure><h4 id="7-repr"><a href="#7-repr" class="headerlink" title="7.repr"></a>7.<strong>repr</strong></h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">触发时机:在使用<span class="built_in">repr</span>(对象)的时候触发</span><br><span class="line">参数:一个<span class="variable language_">self</span>接收对象</span><br><span class="line">返回值:必须是字符串</span><br><span class="line">作用:将对象转使用<span class="built_in">repr</span>化为字符串时使用,也可以用于快捷操作</span><br></pre></td></tr></table></figure><blockquote><p>repr函数和str函数处理字符串只有一个区别:</p><p>str的结果 字符串本身 (结果可以被eval执行)</p><p>如:x = ‘无图言Diao’ str() ->无图言Diao</p><p>rerpr的结果 字符串定义结构 (eavl不会执行repr结果)</p><p>如:x = ‘无图言Diao’ repr() ->’无图言Diao’</p><p>备注: 在类中通常情况下__str__和__repr__ 设置相同即可</p><p>eval()</p><p>函数 将字符串当作python代码执行</p><p>格式:eval(字符串)</p><p>返回值:可以有返回值</p></blockquote><h4 id="8-bool"><a href="#8-bool" class="headerlink" title="8.bool"></a>8.<strong>bool</strong></h4><figure class="highlight gml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">触发时机: 使用<span class="built_in">bool</span>(对象)的时候触发</span><br><span class="line">参数:一个<span class="symbol">self</span>接收对象</span><br><span class="line">返回值:必须是布尔值</span><br><span class="line">作用:根据实际情况决定,可以作为快捷方式使用</span><br><span class="line">注意:仅适合于返回布尔值的操作</span><br></pre></td></tr></table></figure><h4 id="9-format"><a href="#9-format" class="headerlink" title="9.format"></a>9.<strong>format</strong></h4><figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">触发时机:使用字符串.<span class="built_in">format</span>(对象)时候触发</span><br><span class="line">参数:一个<span class="built_in">self</span>接收对象,一个参数接收<span class="built_in">format</span>的{}中的格式,例如:><span class="number">5</span></span><br><span class="line">返回值:必须是字符串</span><br><span class="line">作用:设置对象可以作为<span class="built_in">format</span>的参数,并且自定义对象格式化的规则</span><br><span class="line">注意:无</span><br></pre></td></tr></table></figure><h1 id="描述符相关的魔术方法"><a href="#描述符相关的魔术方法" class="headerlink" title="描述符相关的魔术方法"></a>描述符相关的魔术方法</h1><hr><h4 id="1-get"><a href="#1-get" class="headerlink" title="1.get()"></a>1.<strong>get</strong>()</h4><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">触发时机:在获取指定描述符操作的成员属性的值的时候触发</span><br><span class="line">参数:1描述符对象本身,2描述符描述的属性所在的对象,描述符描述的对象的类</span><br><span class="line">返回值:必须有,不然无法获取相应属性值</span><br><span class="line">注意:仅在描述符中使用</span><br></pre></td></tr></table></figure><h4 id="2-set"><a href="#2-set" class="headerlink" title="2.set()"></a>2.<strong>set</strong>()</h4><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">触发时机:在设置或者添加指定描述符操作的成员属性的时候触发</span><br><span class="line">参数:1描述符对象本身,2描述符描述的属性所在的对象,3要设置的值</span><br><span class="line">返回值:无</span><br><span class="line">注意:仅在描述符中使用</span><br></pre></td></tr></table></figure><h4 id="3-delete"><a href="#3-delete" class="headerlink" title="3.delete()"></a>3.<strong>delete</strong>()</h4><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">触发时机:在删除指定描述符操作的成员属性的时候触发</span><br><span class="line">参数:1描述符对象本身,2描述符描述的属性所在的对象</span><br><span class="line">返回值:无</span><br><span class="line">注意:仅在描述符中使用</span><br></pre></td></tr></table></figure><h1 id="与属性操作相关的魔术方法"><a href="#与属性操作相关的魔术方法" class="headerlink" title="与属性操作相关的魔术方法"></a>与属性操作相关的魔术方法</h1><hr><h4 id="1-getattr-()"><a href="#1-getattr-()" class="headerlink" title="1.__getattr__()"></a>1.__getattr__()</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">触发时机:获取不存在的对象成员时触发</span><br><span class="line">参数:<span class="number">1</span>接收当前对象的<span class="variable language_">self</span>,一个是获取成员名称的字符串</span><br><span class="line">返回值:必须有值</span><br><span class="line">作用:为访问不存在的属性设置值</span><br><span class="line">注意:getattribute无论何时都会在<span class="built_in">getattr</span>之前触发,触发了getattribute就不会在触发<span class="built_in">getattr</span>了</span><br></pre></td></tr></table></figure><h4 id="2-setattr-()"><a href="#2-setattr-()" class="headerlink" title="2.__setattr__()"></a>2.__setattr__()</h4><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">触发时机:设置对象成员值的时候触发</span></span><br><span class="line"><span class="section">参数:1个当前对象的self,一个是要设置的成员名称字符串,一个是要设置的值</span></span><br><span class="line"><span class="section">返回值:无 过程操作</span></span><br><span class="line"><span class="section">作用:接管设置操作,可以在设置前之前进行判断验证等行为</span></span><br><span class="line"><span class="section">注意:在当前方法中无法使用成员=值的方式直接设置成员,否则会无限递归,必须借助object的设置方法来完成</span></span><br><span class="line"></span><br><span class="line">object.__setattr__(参数1,参数2,参数3)</span><br></pre></td></tr></table></figure><h4 id="3-delattr-()"><a href="#3-delattr-()" class="headerlink" title="3.__delattr__()"></a>3.__delattr__()</h4><figure class="highlight crystal"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">触发时机:删除对象成员时触发</span><br><span class="line">参数:一个当前对象的<span class="keyword">self</span></span><br><span class="line">返回值:无</span><br><span class="line">作用:可以在删除成员时进行验证。</span><br></pre></td></tr></table></figure><h4 id="4-getattribute-()"><a href="#4-getattribute-()" class="headerlink" title="4.__getattribute__()"></a>4.__getattribute__()</h4><figure class="highlight crystal"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">触发时机:使用对象成员时触发,无论成员是否存在</span><br><span class="line">参数:<span class="number">1</span>个接收当前对象<span class="keyword">self</span>,一个是获取的成员的名称字符串</span><br><span class="line">返回值:必须有</span><br><span class="line">作用:在具有封装操作(私有化时),为程序开部分访问权限使用</span><br></pre></td></tr></table></figure><h4 id="5-dir-()"><a href="#5-dir-()" class="headerlink" title="5.__dir__()"></a>5.__dir__()</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">触发时机:<span class="built_in">dir</span>(对象)的时候触发</span><br><span class="line">参数:<span class="number">1</span>个接收当前对象<span class="variable language_">self</span></span><br><span class="line">返回值:必须为序列类型(列表,元组,集合等,)</span><br><span class="line">作用:可以自定义成员列表的返回值</span><br></pre></td></tr></table></figure><h1 id="运算相关魔术方法-上帝模式"><a href="#运算相关魔术方法-上帝模式" class="headerlink" title="运算相关魔术方法(上帝模式)"></a>运算相关魔术方法(上帝模式)</h1><hr><h2 id="比较运算相关魔术方法"><a href="#比较运算相关魔术方法" class="headerlink" title="比较运算相关魔术方法"></a>比较运算相关魔术方法</h2><hr><h4 id="1-lt"><a href="#1-lt" class="headerlink" title="1.lt()"></a>1.<strong>lt</strong>()</h4><figure class="highlight gml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">格式:</span><br><span class="line"> def __lt__(<span class="symbol">self</span>,<span class="symbol">other</span>):</span><br><span class="line"> <span class="keyword">return</span> 数据</span><br><span class="line"></span><br><span class="line">特征:</span><br><span class="line"> 触发时机:进行小于判断时自动触发</span><br><span class="line"> 参数:<span class="number">2</span>个参数第一个是<span class="symbol">self</span>,第二个判断的第二个对象</span><br><span class="line"> 返回值:返回值可以任意类型,推荐布尔值</span><br><span class="line"> 作用:定义小于号的行为:<span class="variable language_">x</span> < <span class="variable language_">y</span> 调用 <span class="variable language_">x</span>.lt(<span class="variable language_">y</span>)</span><br></pre></td></tr></table></figure><h4 id="2-le"><a href="#2-le" class="headerlink" title="2.le()"></a>2.<strong>le</strong>()</h4><figure class="highlight gml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">格式:</span><br><span class="line"> def __le__(<span class="symbol">self</span>):</span><br><span class="line"> <span class="keyword">return</span> str</span><br><span class="line"></span><br><span class="line">特征:</span><br><span class="line"> 触发时机:进行小于等于判断时自动触发</span><br><span class="line"> 参数:<span class="number">2</span>个参数第一个是<span class="symbol">self</span>,第二个判断的第二个对象</span><br><span class="line"> 返回值:返回值可以任意类型,推荐布尔值</span><br><span class="line"> 作用:定义小于等于号的行为:<span class="variable language_">x</span> <= <span class="variable language_">y</span> 调用 <span class="variable language_">x</span>.le(<span class="variable language_">y</span>)</span><br></pre></td></tr></table></figure><h4 id="3-gt"><a href="#3-gt" class="headerlink" title="3.gt()"></a>3.<strong>gt</strong>()</h4><figure class="highlight gml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">格式:</span><br><span class="line"> def __gt__(<span class="symbol">self</span>):</span><br><span class="line"> <span class="keyword">return</span> str</span><br><span class="line"></span><br><span class="line">特征:</span><br><span class="line"> 触发时机:进行大于判断时自动触发</span><br><span class="line"> 参数:<span class="number">2</span>个参数第一个是<span class="symbol">self</span>,第二个判断的第二个对象</span><br><span class="line"> 返回值:返回值可以任意类型,推荐布尔值</span><br><span class="line"> 作用:定义大于号的行为:<span class="variable language_">x</span> > <span class="variable language_">y</span> 调用 <span class="variable language_">x</span>.gt(<span class="variable language_">y</span>)</span><br></pre></td></tr></table></figure><h4 id="4-ge"><a href="#4-ge" class="headerlink" title="4.ge()"></a>4.<strong>ge</strong>()</h4><figure class="highlight gml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">格式:</span><br><span class="line"> def __ge__(<span class="symbol">self</span>):</span><br><span class="line"> <span class="keyword">return</span> str</span><br><span class="line"></span><br><span class="line">特征:</span><br><span class="line"> 触发时机:进行大于等于判断时自动触发</span><br><span class="line"> 参数:<span class="number">2</span>个参数第一个是<span class="symbol">self</span>,第二个判断的第二个对象</span><br><span class="line"> 返回值:返回值可以任意类型,推荐布尔值</span><br><span class="line"> 作用:定义大于等于号的行为:<span class="variable language_">x</span> >= <span class="variable language_">y</span> 调用 <span class="variable language_">x</span>.ge(<span class="variable language_">y</span>)</span><br></pre></td></tr></table></figure><h4 id="5-eq"><a href="#5-eq" class="headerlink" title="5.eq()"></a>5.<strong>eq</strong>()</h4><figure class="highlight gml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">格式:</span><br><span class="line"> def __eq__(<span class="symbol">self</span>):</span><br><span class="line"> <span class="keyword">return</span> str</span><br><span class="line"></span><br><span class="line">特征:</span><br><span class="line"> 触发时机:进行等于判断时自动触发</span><br><span class="line"> 参数:<span class="number">2</span>个参数第一个是<span class="symbol">self</span>,第二个判断的第二个对象</span><br><span class="line"> 返回值:返回值可以任意类型,推荐布尔值</span><br><span class="line"> 作用:定义大于等于号的行为:<span class="variable language_">x</span> == <span class="variable language_">y</span> 调用 <span class="variable language_">x</span>.eq(<span class="variable language_">y</span>)</span><br></pre></td></tr></table></figure><h4 id="6-ne"><a href="#6-ne" class="headerlink" title="6.ne()"></a>6.<strong>ne</strong>()</h4><figure class="highlight gml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">格式:</span><br><span class="line"> def __ne__(<span class="symbol">self</span>):</span><br><span class="line"> <span class="keyword">return</span> str</span><br><span class="line"></span><br><span class="line">特征:</span><br><span class="line"> 触发时机:进行不等于判断时自动触发</span><br><span class="line"> 参数:<span class="number">2</span>个参数第一个是<span class="symbol">self</span>,第二个判断的第二个对象</span><br><span class="line"> 返回值:返回值可以任意类型,推荐布尔值</span><br><span class="line"> 作用:定义不等号的行为:<span class="variable language_">x</span> != <span class="variable language_">y</span> 调用 <span class="variable language_">x</span>.ne(<span class="variable language_">y</span>)</span><br></pre></td></tr></table></figure><h2 id="算术运算相关魔术方法"><a href="#算术运算相关魔术方法" class="headerlink" title="算术运算相关魔术方法"></a>算术运算相关魔术方法</h2><hr><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">__add__(<span class="variable language_">self</span>, other) 定义加法的行为:+</span><br><span class="line">__sub__(<span class="variable language_">self</span>, other) 定义减法的行为:-</span><br><span class="line">__mul__(<span class="variable language_">self</span>, other) 定义乘法的行为:</span><br><span class="line">__truediv__(<span class="variable language_">self</span>, other) 定义真除法的行为:/</span><br><span class="line">__floordiv__(<span class="variable language_">self</span>, other) 定义整数除法的行为://</span><br><span class="line">__mod__(<span class="variable language_">self</span>, other) 定义取模算法的行为:%</span><br><span class="line">__divmod__(<span class="variable language_">self</span>, other) 定义当被 <span class="built_in">divmod</span>() 调用时的行为</span><br><span class="line">__pow__(<span class="variable language_">self</span>, other[, modulo]) 定义当被 power() 调用或 ** 运算时的行为</span><br><span class="line">__lshift__(<span class="variable language_">self</span>, other) 定义按位左移位的行为:<<</span><br><span class="line">__rshift__(<span class="variable language_">self</span>, other) 定义按位右移位的行为:>></span><br><span class="line">__and__(<span class="variable language_">self</span>, other) 定义按位与操作的行为:&</span><br><span class="line">__xor__(<span class="variable language_">self</span>, other) 定义按位异或操作的行为:^</span><br><span class="line">__or__(<span class="variable language_">self</span>, other) 定义按位或操作的行为:|</span><br></pre></td></tr></table></figure><h2 id="反运算相关魔术方法"><a href="#反运算相关魔术方法" class="headerlink" title="反运算相关魔术方法"></a>反运算相关魔术方法</h2><hr><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">__radd__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rsub__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rmul__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rtruediv__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rfloordiv__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rmod__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rdivmod__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rpow__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rlshift__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rrshift__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rand__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__rxor__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br><span class="line">__ror__(<span class="variable language_">self</span>, other) 与上方相同,当左操作数不支持相应的操作时被调用</span><br></pre></td></tr></table></figure><h2 id="赋值运算相关魔术方法"><a href="#赋值运算相关魔术方法" class="headerlink" title="赋值运算相关魔术方法"></a>赋值运算相关魔术方法</h2><hr><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">__iadd__(<span class="variable language_">self</span>, other) 定义赋值加法的行为:+=</span><br><span class="line">__isub__(<span class="variable language_">self</span>, other) 定义赋值减法的行为:-=</span><br><span class="line">__imul__(<span class="variable language_">self</span>, other) 定义赋值乘法的行为:=</span><br><span class="line">__itruediv__(<span class="variable language_">self</span>, other) 定义赋值真除法的行为:/=</span><br><span class="line">__ifloordiv__(<span class="variable language_">self</span>, other) 定义赋值整数除法的行为://=</span><br><span class="line">__imod__(<span class="variable language_">self</span>, other) 定义赋值取模算法的行为:%=</span><br><span class="line">__ipow__(<span class="variable language_">self</span>, other[, modulo]) 定义赋值幂运算的行为:**=</span><br><span class="line">__ilshift__(<span class="variable language_">self</span>, other) 定义赋值按位左移位的行为:<<=</span><br><span class="line">__irshift__(<span class="variable language_">self</span>, other) 定义赋值按位右移位的行为:>>=</span><br><span class="line">__iand__(<span class="variable language_">self</span>, other) 定义赋值按位与操作的行为:&=</span><br><span class="line">__ixor__(<span class="variable language_">self</span>, other) 定义赋值按位异或操作的行为:^=</span><br><span class="line">__ior__(<span class="variable language_">self</span>, other) 定义赋值按位或操作的行为:|=</span><br></pre></td></tr></table></figure><h2 id="一元运算相关魔术方法"><a href="#一元运算相关魔术方法" class="headerlink" title="一元运算相关魔术方法"></a>一元运算相关魔术方法</h2><hr><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">__pos__(<span class="variable language_">self</span>) 定义正号的行为:+x</span><br><span class="line">__neg__(<span class="variable language_">self</span>) 定义负号的行为:-x</span><br><span class="line">__abs__(<span class="variable language_">self</span>) 定义当被 <span class="built_in">abs</span>() 调用时的行为</span><br><span class="line">__invert__(<span class="variable language_">self</span>) 定义按位求反的行为:~x</span><br></pre></td></tr></table></figure><h2 id="类型转换相关魔术方法-JIANG"><a href="#类型转换相关魔术方法-JIANG" class="headerlink" title="类型转换相关魔术方法 JIANG"></a>类型转换相关魔术方法 JIANG</h2><hr><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">__complex__(<span class="variable language_">self</span>) 定义当被 <span class="built_in">complex</span>() 调用时的行为(需要返回恰当的值)</span><br><span class="line">__int__(<span class="variable language_">self</span>) 定义当被 <span class="built_in">int</span>() 调用时的行为(需要返回恰当的值)</span><br><span class="line">__float__(<span class="variable language_">self</span>) 定义当被 <span class="built_in">float</span>() 调用时的行为(需要返回恰当的值)</span><br><span class="line">__round__(<span class="variable language_">self</span>[, n]) 定义当被 <span class="built_in">round</span>() 调用时的行为(需要返回恰当的值)</span><br><span class="line">__index(<span class="variable language_">self</span>)__ <span class="number">1.</span> 当对象是被应用在切片表达式中时,实现整形强制转换</span><br><span class="line"> <span class="number">2.</span> 如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 index</span><br><span class="line"> <span class="number">3.</span> 如果 index 被定义,则 <span class="built_in">int</span> 也需要被定义,且返回相同的值</span><br></pre></td></tr></table></figure><h2 id="上下文管理相关魔术方法"><a href="#上下文管理相关魔术方法" class="headerlink" title="上下文管理相关魔术方法"></a>上下文管理相关魔术方法</h2><hr><p><strong>enter</strong>() 和 <strong>exit</strong>()</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">enter(<span class="variable language_">self</span>)</span><br><span class="line"> <span class="number">1.</span> 定义当使用 <span class="keyword">with</span> 语句时的初始化行为</span><br><span class="line"> <span class="number">2.</span> enter 的返回值被 <span class="keyword">with</span> 语句的目标或者 <span class="keyword">as</span> 后的名字绑定</span><br><span class="line"></span><br><span class="line">exit(<span class="variable language_">self</span>, exctype, excvalue, traceback)</span><br><span class="line"> <span class="number">1.</span> 定义当一个代码块被执行或者终止后上下文管理器应该做什么</span><br><span class="line"> <span class="number">2.</span> 一般被用来处理异常,清除工作或者做一些代码块执行完毕之后的日常工作</span><br></pre></td></tr></table></figure><h2 id="容器类型相关魔术方法"><a href="#容器类型相关魔术方法" class="headerlink" title="容器类型相关魔术方法"></a>容器类型相关魔术方法</h2><hr><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">__len__(<span class="variable language_">self</span>) 定义当被 <span class="built_in">len</span>() 调用时的行为(返回容器中元素的个数)</span><br><span class="line">__getitem__(<span class="variable language_">self</span>, key) 定义获取容器中指定元素的行为,相当于 <span class="variable language_">self</span>[key]</span><br><span class="line">__setitem__(<span class="variable language_">self</span>, key, value) 定义设置容器中指定元素的行为,相当于 <span class="variable language_">self</span>[key] = value</span><br><span class="line">__delitem__(<span class="variable language_">self</span>, key) 定义删除容器中指定元素的行为,相当于 <span class="keyword">del</span> <span class="variable language_">self</span>[key]</span><br><span class="line">__iter__(<span class="variable language_">self</span>) 定义当迭代容器中的元素的行为</span><br><span class="line">__reversed__(<span class="variable language_">self</span>) 定义当被 <span class="built_in">reversed</span>() 调用时的行为</span><br><span class="line">__contains__(<span class="variable language_">self</span>, item) 定义当使用成员测试运算符(<span class="keyword">in</span> 或 <span class="keyword">not</span> <span class="keyword">in</span>)时的行为</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> 自学 </tag>
</tags>
</entry>
<entry>
<title>python_django</title>
<link href="/posts/650b5471/"/>
<url>/posts/650b5471/</url>
<content type="html"><![CDATA[<h1 id="我的笔记"><a href="#我的笔记" class="headerlink" title="我的笔记"></a>我的笔记</h1><p><a href="https://docs.python.org/zh-cn/3/library/venv.html">官方文档</a></p><table><thead><tr><th align="left">平台</th><th align="left">Shell</th><th align="left">用于激活虚拟环境的命令</th></tr></thead><tbody><tr><td align="left">POSIX</td><td align="left">bash/zsh</td><td align="left">$ source <venv>/bin/activate</td></tr><tr><td align="left"></td><td align="left">fish</td><td align="left">$ source <venv>/bin/activate.fish</td></tr><tr><td align="left"></td><td align="left">csh/tcsh</td><td align="left">$ source <venv>/bin/activate.csh</td></tr><tr><td align="left"></td><td align="left">PowerShell Core</td><td align="left">$ <venv>/bin/Activate.ps1</td></tr><tr><td align="left">Windows</td><td align="left">cmd.exe</td><td align="left">C:> <venv>\Scripts\activate.bat</td></tr><tr><td align="left"></td><td align="left">PowerShell</td><td align="left">PS C:> <venv>\Scripts\Activate.ps1</td></tr></tbody></table><p>创建网站框架</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.\django-admin.exe startproject mysite</span><br></pre></td></tr></table></figure><p>启动django框架</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">python manage.py runserver</span><br><span class="line">python manage.py runserver [端口号]</span><br></pre></td></tr></table></figure><p>网站中的一个功能创建应用</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python .\manage.py startapp index</span><br></pre></td></tr></table></figure><p>数据库迁移</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python manage.py migate</span><br></pre></td></tr></table></figure><p>列出django子命令</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python manage .py</span><br></pre></td></tr></table></figure><h1 id="目录解读"><a href="#目录解读" class="headerlink" title="目录解读"></a>目录解读</h1><p><img src="/python-django/image-20211129095301686.webp" alt="image-20211129095301686"></p><p><img src="/python-django/image-20211129104235294.webp" alt="image-20211129104235294"></p><p><img src="/python-django/image-20211129104304785.webp" alt="image-20211129104304785"></p><p><img src="/python-django/image-20211129104323422.webp" alt="image-20211129104323422"></p><p><img src="/python-django/image-20211129104800457.webp" alt="image-20211129104800457"></p><p><img src="/python-django/image-20211129104820337.webp" alt="image-20211129104820337"></p><p><img src="/python-django/image-20211129104940840.webp" alt="image-20211129104940840"></p><p><img src="/python-django/image-20211129105214895.webp" alt="image-20211129105214895"></p><p><img src="/python-django/image-20211129105351007.webp" alt="image-20211129105351007"></p><p><img src="/python-django/image-20211129105747983.webp" alt="image-20211129105747983"></p><p>我的理解</p><p>目前在我看来视图(view)就是用来写html的文件,urls就是将这个文件的路径导入,使得可以在浏览器显示</p><p><img src="/python-django/image-20211129110849895.webp" alt="image-20211129110849895"></p><p>浏览器访问page/2003/djgano匹配到视图(views)中有page_2003中有函数,然后输出函数的内容</p><p>这是路由吗??我感觉是,因为urls将浏览器请求的url地址指向views</p><p><img src="/python-django/image-20211129111001293.webp" alt="image-20211129111001293"><img src="/python-django/image-20211129111029113.webp" alt="image-20211129111029113"></p><h1 id="路由配置"><a href="#路由配置" class="headerlink" title="路由配置"></a>路由配置</h1><p><img src="/python-django/image-20211129111214428.webp" alt="image-20211129111214428"></p><h2 id="路由配置-path"><a href="#路由配置-path" class="headerlink" title="路由配置 - path"></a>路由配置 - path</h2><p><img src="/python-django/image-20211129111418584.webp" alt="image-20211129111418584"></p><h2 id="路由配置-path-转换器"><a href="#路由配置-path-转换器" class="headerlink" title="路由配置 - path - 转换器"></a>路由配置 - path - 转换器</h2><p><img src="/python-django/image-20211129112600885.webp" alt="image-20211129112600885"></p><h2 id="路由配置-path-转换器-1"><a href="#路由配置-path-转换器-1" class="headerlink" title="路由配置 - path - 转换器"></a>路由配置 - path - 转换器</h2><p><img src="/python-django/image-20211129112814502.webp" alt="image-20211129112814502"></p><h3 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h3><p><img src="/python-django/image-20211129113915653.webp" alt="image-20211129113915653"><img src="/python-django/image-20211129113939729.webp" alt="image-20211129113939729"></p><p><img src="/python-django/image-20211129113926093.webp" alt="image-20211129113926093"><img src="/python-django/image-20211129114053987.webp" alt="image-20211129114053987"></p><p>可以看出来,执行的时候是由上而下执行</p><h3 id="练习:计算器"><a href="#练习:计算器" class="headerlink" title="练习:计算器"></a>练习:计算器</h3><p><img src="/python-django/image-20211129123645517.webp" alt="image-20211129123645517"></p><h2 id="路由配置-re-path"><a href="#路由配置-re-path" class="headerlink" title="路由配置 - re_path()"></a>路由配置 - re_path()</h2><p><img src="/python-django/image-20211129141056822.webp" alt="image-20211129141056822"></p><h3 id="样例"><a href="#样例" class="headerlink" title="样例"></a>样例</h3><p><img src="/python-django/image-20211129141712298.webp" alt="image-20211129141712298"></p><h3 id="生日正则:"><a href="#生日正则:" class="headerlink" title="生日正则:"></a>生日正则:</h3><p><img src="/python-django/image-20211129145314266.webp" alt="image-20211129145314266"></p><p><img src="/python-django/image-20211129145335743.webp" alt="image-20211129145335743"></p><p><img src="/python-django/image-20211129145352623.webp" alt="image-20211129145352623"></p><h1 id="Django中的请求"><a href="#Django中的请求" class="headerlink" title="Django中的请求"></a>Django中的请求</h1><p><img src="/python-django/image-20211129170039257.webp" alt="image-20211129170039257"></p><p><img src="/python-django/image-20211129171430226.webp" alt="image-20211129171430226"></p><h2 id="Django中的响应对象"><a href="#Django中的响应对象" class="headerlink" title="Django中的响应对象"></a>Django中的响应对象</h2><p><img src="/python-django/image-20211129172858756.webp" alt="image-20211129172858756"></p><p>Django中的响应对象</p><p><img src="/python-django/image-20211129173135044.webp" alt="image-20211129173135044"></p><p><img src="/python-django/image-20211129173257111.webp" alt="image-20211129173257111"></p><h2 id="GET处理"><a href="#GET处理" class="headerlink" title="GET处理"></a>GET处理</h2><p><img src="/python-django/image-20211129174221824.webp" alt="image-20211129174221824"></p><p><img src="/python-django/image-20211129174751910.webp" alt="image-20211129174751910"></p><p><img src="/python-django/image-20211129175033023.webp" alt="image-20211129175033023"></p><h2 id="POST处理"><a href="#POST处理" class="headerlink" title="POST处理"></a>POST处理</h2><p><img src="/python-django/image-20211129201247287.webp" alt="image-20211129201247287"></p><p><img src="/python-django/image-20211129201306505.webp" alt="image-20211129201306505"></p><p><img src="/python-django/image-20211129201327000.webp" alt="image-20211129201327000"></p><h2 id="传统的mvc"><a href="#传统的mvc" class="headerlink" title="传统的mvc"></a>传统的mvc</h2><p><img src="/python-django/image-20211129203627800.webp" alt="image-20211129203627800"></p><p>Django是MTV结构</p><h1 id="模板配置"><a href="#模板配置" class="headerlink" title="模板配置"></a>模板配置</h1><p><img src="/python-django/image-20211130185916088.webp" alt="image-20211130185916088"></p><h2 id="模板加载方式"><a href="#模板加载方式" class="headerlink" title="模板加载方式"></a>模板加载方式</h2><p><img src="/python-django/image-20211130190516341.webp" alt="image-20211130190516341"></p><p><img src="/python-django/image-20211130191454722.webp" alt="image-20211130191454722"></p><h2 id="视图层与模板层之间的交互"><a href="#视图层与模板层之间的交互" class="headerlink" title="视图层与模板层之间的交互"></a>视图层与模板层之间的交互</h2><p><img src="/python-django/image-20211130192009564.webp" alt="image-20211130192009564"></p><h3 id="例子:"><a href="#例子:" class="headerlink" title="例子:"></a>例子:</h3><p><img src="/python-django/image-20211201150602148.webp" alt="image-20211201150602148"></p><p><img src="/python-django/image-20211201150617963.webp" alt="image-20211201150617963"></p><h2 id="模板的变量"><a href="#模板的变量" class="headerlink" title="模板的变量"></a>模板的变量</h2><p><img src="/python-django/image-20211201151027473.webp" alt="image-20211201151027473"></p><h3 id="调用方法"><a href="#调用方法" class="headerlink" title="调用方法"></a>调用方法</h3><p><img src="/python-django/image-20211201151055547.webp" alt="image-20211201151055547"></p><p>都是用双大括号加变量名</p><p>测试各种变量的传值</p><p><img src="/python-django/image-20211201152336107.webp" alt="image-20211201152336107"></p><p><img src="/python-django/image-20211201152350065.webp" alt="image-20211201152350065"></p><p><img src="/python-django/image-20211201152402659.webp" alt="image-20211201152402659"></p><h2 id="模板标签"><a href="#模板标签" class="headerlink" title="模板标签"></a>模板标签</h2><p><img src="/python-django/image-20211201152701477.webp" alt="image-20211201152701477"></p><p>基本都需要结束标签,个别特殊的不需要结束标签,</p><h3 id="if标签"><a href="#if标签" class="headerlink" title="if标签"></a>if标签</h3><p><img src="/python-django/image-20211201153028048.webp" alt="image-20211201153028048"></p><p><code>if</code>标签的结束标签是<code>endif</code></p><p><img src="/python-django/image-20211201153132882.webp" alt="image-20211201153132882"></p><h3 id="测试例子"><a href="#测试例子" class="headerlink" title="测试例子"></a>测试例子</h3><p><img src="/python-django/image-20211201153747378.webp" alt="image-20211201153747378"></p><p><img src="/python-django/image-20211203101238114.webp" alt="image-20211203101238114"></p><p><img src="/python-django/image-20211203101258139.webp" alt="image-20211203101258139"></p><h3 id="for标签"><a href="#for标签" class="headerlink" title="for标签"></a>for标签</h3><p><img src="/python-django/image-20211204160943943.webp" alt="image-20211204160943943"></p><h2 id="模板过滤器"><a href="#模板过滤器" class="headerlink" title="模板过滤器"></a>模板过滤器</h2><p><img src="/python-django/image-20211204162513617.webp" alt="image-20211204162513617"></p><h2 id="常用过滤器"><a href="#常用过滤器" class="headerlink" title="常用过滤器"></a>常用过滤器</h2><p><img src="/python-django/image-20211204162620250.webp" alt="image-20211204162620250"></p><h2 id="模板继承"><a href="#模板继承" class="headerlink" title="模板继承"></a>模板继承</h2><p><img src="/python-django/image-20211204164816564.webp" alt="image-20211204164816564"></p><h3 id="父页面传入的参数无法继承给子页面"><a href="#父页面传入的参数无法继承给子页面" class="headerlink" title="父页面传入的参数无法继承给子页面"></a>父页面传入的参数无法继承给子页面</h3><p><img src="/python-django/image-20211204185525663.webp" alt="image-20211204185525663"></p><h1 id="url书写规范"><a href="#url书写规范" class="headerlink" title="url书写规范"></a>url书写规范</h1><p><img src="/python-django/image-20211207203612783.webp" alt="image-20211207203612783"></p><p><code>相对地址使用较多</code></p><p><img src="/python-django/image-20211207203845679.webp" alt="image-20211207203845679"></p><h2 id="url反向解析"><a href="#url反向解析" class="headerlink" title="url反向解析"></a>url反向解析</h2><p><img src="/python-django/image-20211208153642369.webp" alt="image-20211208153642369"></p><p>代码:</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">html</span><br><span class="line"><span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"{% url 'tr' '100' %}"</span>></span>url反向解析版本<span class="tag"></<span class="name">a</span>></span></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">路由配置</span><br><span class="line">path(<span class="string">'test_urls_result/<int:age>'</span>, views.test_url_result, name=<span class="string">'tr'</span>)</span><br><span class="line"></span><br><span class="line">视图配置</span><br><span class="line"><span class="keyword">def</span> <span class="title function_">test_url_result</span>(<span class="params">request, age</span>):</span><br><span class="line"> <span class="keyword">return</span> HttpResponse(<span class="string">"---test_url_result is ok----"</span>)</span><br></pre></td></tr></table></figure><p>优点:可以动态获取url,因为是通过别名来</p><h1 id="静态文件"><a href="#静态文件" class="headerlink" title="静态文件"></a>静态文件</h1><p><img src="/python-django/image-20211212164924682.webp" alt="image-20211212164924682"></p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">第一种办法</span><br><span class="line"><span class="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>Title<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">img</span> <span class="attr">src</span>=<span class="string">"/staticpython-django/404.webp"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">img</span> <span class="attr">src</span>=<span class="string">"http://127.0.0.1:8000/staticpython-django/right_top.webp"</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">第二种方法</span><br></pre></td></tr></table></figure><h1 id="应用及分布式路由"><a href="#应用及分布式路由" class="headerlink" title="应用及分布式路由"></a>应用及分布式路由</h1><p>应用就是在django项目中是一个独立的业务模块,可以包含自己的路由,视图,模板,模型,</p><blockquote><p>我的理解就是将分模块创建路由,不至于将这个项目所有的文件都在一个路由里面,这样会使代码不易阅读,</p></blockquote><p>创建路由的命令</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python manage.py startapp [项目名]</span><br></pre></td></tr></table></figure><h1 id="数据(mysql)"><a href="#数据(mysql)" class="headerlink" title="数据(mysql)"></a>数据(mysql)</h1><p>python中的django连接mysql需要mysqlclient包,</p><p>创建数据库必须指定字符集</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">create database mysite3 default charset utf8;</span><br><span class="line">setting.py设置</span><br><span class="line"> # mysql配置</span><br><span class="line"> 'ENGINE': 'django.db.backends.mysql',</span><br><span class="line"> 'NAME': 'mysite3',</span><br><span class="line"> 'USER': 'root',</span><br><span class="line"> 'PASSWORD': '1',</span><br><span class="line"> 'HOST': '127.0.0.1',</span><br><span class="line"> 'PORT': '3306',</span><br></pre></td></tr></table></figure><figure class="highlight mipsasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ORM框架</span></span><br><span class="line"><span class="keyword"></span><span class="keyword">ORM</span></span><br><span class="line">类数据表</span><br><span class="line">对象数据行</span><br><span class="line">属性字段</span><br></pre></td></tr></table></figure><h2 id="连接过程"><a href="#连接过程" class="headerlink" title="连接过程"></a>连接过程</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">添加数据表</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Book</span>(models.Model):</span><br><span class="line"> title = models.CharField(<span class="string">'书名'</span>, max_length=<span class="number">7</span>, default=<span class="string">''</span>)</span><br><span class="line"> price = models.DecimalField(<span class="string">'价格'</span>, max_digits=<span class="number">7</span>, decimal_places=<span class="number">2</span>)</span><br><span class="line"> info = models.CharField(<span class="string">'关于'</span>, max_length=<span class="number">100</span>, default=<span class="string">''</span>)</span><br><span class="line">生成迁移文件</span><br><span class="line">python manage.py makmigrations</span><br><span class="line">执行迁移脚本程序</span><br><span class="line">python3 manage.py migrate</span><br></pre></td></tr></table></figure><h2 id="模型类-字段类型"><a href="#模型类-字段类型" class="headerlink" title="模型类-字段类型"></a>模型类-字段类型</h2><p>BooleanField()</p><p>数据库类型:tinyint(1)</p><p>CharField()</p><p>数据库类型varchar</p><p>必须要指定max_length参数值</p><p>DateField()</p><p>数据库类型:date</p><p>参数:</p><p>auto_now:每次保存对象时,自动设置该字段为当前时间取值True</p><p>auto_now_add:设置第一次创建时的当前时间</p><p>default:设置当前时间(取值:字符串格式时间</p><p>DateTimeField()</p><p>数据库类型:datetime(6)</p><p>作用:表示日期和时间</p><p>参数同上</p><p>FloatField()</p><p>数据库类型:double</p><p>编程语言中和数据库中都使用小数表示值</p><p>DecimalField()</p><p>数据库类型:decimal(x,y)</p><p>参数:max_digits:位数总数,包括小数点后的位数,该值必须大于等于decimal_places</p><p>decimal_places:小数点后的数字数量</p><p>EmailField()</p><p>数据库类型:varchar</p><p>编程语言和数据库中使用字符串</p><p>IntegerField()</p><p>数据库类型:int</p><p>ImageField()</p><p>数据库类型:varchar(100)</p><p>作用在数据库中为了保存图片的路径</p><p>TextField()</p><p>数据库类型:longtext</p><h2 id="模型类-字段选项"><a href="#模型类-字段选项" class="headerlink" title="模型类-字段选项"></a>模型类-字段选项</h2><p>db_index</p><p>索引</p><p>unique</p><p>为True时必须时唯一索引</p><p>db_column</p><p>指定列的名称,如果不指定,采用属性名作为列名</p><p>verbose_name</p><p>设置此字段的admin界面上的显示名称</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 字段选项样例</span></span><br><span class="line"><span class="comment"># 创建一个属性,表示用户名称,长度30字符,必须时唯一的,不能为空,添加索引</span></span><br><span class="line">name = models.CharField(max_length=<span class="number">30</span>,unique=<span class="literal">True</span>, null=<span class="literal">False</span>,db_index=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> 毕设 </tag>
<tag> 自学 </tag>
</tags>
</entry>
<entry>
<title>生成器与迭代器</title>
<link href="/posts/76a3894a/"/>
<url>/posts/76a3894a/</url>
<content type="html"><![CDATA[<p><img src="/%E7%94%9F%E6%88%90%E5%99%A8%E4%B8%8E%E8%BF%AD%E4%BB%A3%E5%99%A8/image-20211031232546648.webp" alt="image-20211031232546648"></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/10/31 11:11</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : generator01.py</span></span><br><span class="line"><span class="comment"># @Sofware : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">通过列表生成式(列表推导式),我们可以直接创建一个列表</span></span><br><span class="line"><span class="string">但是,受内存限制,列表容量肯定是有限的。</span></span><br><span class="line"><span class="string">而且,创建一个包含100万个元素的列表,不仅占用很大的存储看见,</span></span><br><span class="line"><span class="string">如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了</span></span><br><span class="line"><span class="string">所以如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢!</span></span><br><span class="line"><span class="string">这样就不必创建完整的list,从而节省大量的空间,在python中,这种一边循环一边计算的机制称为生成器</span></span><br><span class="line"><span class="string">generator</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">得到生成器的方式:</span></span><br><span class="line"><span class="string">1.通过列表推导式得到生成器</span></span><br><span class="line"><span class="string">2.</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># [x for x in range(100000000)]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># [0,2,3,7,6,3,6,8,9,6,5]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># newlist = [x*3 for x in range(10)]</span></span><br><span class="line"><span class="comment"># print(newlist)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 得到生成器</span></span><br><span class="line">g = (x*<span class="number">3</span> <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(g)) <span class="comment"># <class 'generator'></span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过调用__next__()方式得到元素</span></span><br><span class="line"><span class="built_in">print</span>(g.__next__()) <span class="comment"># 0</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方式2:next(生成器对象) builtins 系统内置函数</span></span><br><span class="line"><span class="comment"># 每调用一次next则会产生一个元素</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g)) <span class="comment"># 3</span></span><br><span class="line"><span class="comment"># StopIteration 生成器本来就可以产生10个,得到10个,在调用next会抛出异常</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/10/31 11:33</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : generator02.py</span></span><br><span class="line"><span class="comment"># @Sofware : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># g = (x * 3 for x in range(10))</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># while True:</span></span><br><span class="line"><span class="comment"># try:</span></span><br><span class="line"><span class="comment"># e = next(g)</span></span><br><span class="line"><span class="comment"># print(e)</span></span><br><span class="line"><span class="comment"># except StopIteration:</span></span><br><span class="line"><span class="comment"># print('没有更多元素了!')</span></span><br><span class="line"><span class="comment"># break</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义生成器的方式二:借助函数完成</span></span><br><span class="line"><span class="comment"># 只要函数中出现yield关键字,说明函数就不是函数,变成生成器了</span></span><br><span class="line"><span class="comment"># 斐波那契数列</span></span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">步骤:</span></span><br><span class="line"><span class="string">1.定义一个函数,函数中使用yield关键字</span></span><br><span class="line"><span class="string">2.调用函数,接收调用的结果</span></span><br><span class="line"><span class="string">3.得到的结果就是生成器</span></span><br><span class="line"><span class="string">4.借助与next(),__next__得到元素</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># def func():</span></span><br><span class="line"><span class="comment"># n = 0</span></span><br><span class="line"><span class="comment"># while True:</span></span><br><span class="line"><span class="comment"># n += 1</span></span><br><span class="line"><span class="comment"># print(n)</span></span><br><span class="line"><span class="comment"># yield n # return n + 暂停</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># a = func()</span></span><br><span class="line"><span class="comment"># next(a)</span></span><br><span class="line"><span class="comment"># next(a)</span></span><br><span class="line"><span class="comment"># next(a)</span></span><br><span class="line"><span class="comment"># a.__next__()</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">fib</span>(<span class="params">length</span>):</span><br><span class="line"> a, b = <span class="number">0</span>, <span class="number">1</span></span><br><span class="line"> n = <span class="number">0</span></span><br><span class="line"> <span class="keyword">while</span> n < length:</span><br><span class="line"> <span class="comment"># print(b)</span></span><br><span class="line"> <span class="keyword">yield</span> b</span><br><span class="line"> a, b = b, a + b</span><br><span class="line"> n += <span class="number">1</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">'没有更多元素了!!!'</span> <span class="comment"># return就是在得到StopIteration后的提示</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">g = fib(<span class="number">10</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">next</span>(g))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/10/31 13:46</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : generator03.py</span></span><br><span class="line"><span class="comment"># @Sofware : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">生成器方法:</span></span><br><span class="line"><span class="string"> __next__(): 获取下一个元素</span></span><br><span class="line"><span class="string"> send(value): 向每次生成器调用中传值 注意:第一次调用send(None)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">gen</span>():</span><br><span class="line"> i = <span class="number">0</span></span><br><span class="line"> <span class="keyword">while</span> i < <span class="number">5</span>:</span><br><span class="line"> temp = <span class="keyword">yield</span> i</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'temp'</span>, temp)</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(temp):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'---------'</span>, x)</span><br><span class="line"> i += <span class="number">1</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">'没有更多的数据了'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">g = gen()</span><br><span class="line"><span class="comment"># print(next(g))</span></span><br><span class="line"><span class="comment"># print(next(g))</span></span><br><span class="line">g.send(<span class="literal">None</span>)</span><br><span class="line">n1 = g.send(<span class="number">5</span>)</span><br><span class="line">n2 = g.send(<span class="number">5</span>)</span><br><span class="line"><span class="built_in">print</span>(n1)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/10/31 16:48</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : generator04.py</span></span><br><span class="line"><span class="comment"># @Software : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 进程>线程>协程</span></span><br><span class="line"><span class="comment"># 迅雷:</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">task1</span>(<span class="params">n</span>):</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'正在搬第{}块砖'</span>.<span class="built_in">format</span>(i + <span class="number">1</span>))</span><br><span class="line"> <span class="keyword">yield</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">task2</span>(<span class="params">i</span>):</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(i):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'正在听第{}首歌'</span>.<span class="built_in">format</span>(i + <span class="number">1</span>))</span><br><span class="line"> <span class="keyword">yield</span> <span class="literal">None</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">g1 = task1(<span class="number">5</span>)</span><br><span class="line">g2 = task2(<span class="number">5</span>)</span><br><span class="line"><span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> <span class="comment"># next(g1)</span></span><br><span class="line"> <span class="comment"># next(g2)</span></span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> g1.__next__()</span><br><span class="line"> g2.__next__()</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">生成器:generator</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">定义生成器方式:</span></span><br><span class="line"><span class="string">1.通过列表推导式的方式</span></span><br><span class="line"><span class="string"> g = (x+1 for x in rang(6))</span></span><br><span class="line"><span class="string">2.复杂+yield</span></span><br><span class="line"><span class="string"> def func():</span></span><br><span class="line"><span class="string"> ...</span></span><br><span class="line"><span class="string"> yield</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string"> g = func()</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">产生元素:</span></span><br><span class="line"><span class="string"> 1.next(generator) ----》每次调用都会产生一个新的元素,如果元素产生完毕,再次调用的话就会产生异常</span></span><br><span class="line"><span class="string"> 2.生成器自己的方法:</span></span><br><span class="line"><span class="string"> g.__next__()</span></span><br><span class="line"><span class="string"> g.send(value)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> 应用:协程</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/10/31 0:28</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : list01.py</span></span><br><span class="line"><span class="comment"># @Sofware : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 列表推导式</span></span><br><span class="line"><span class="comment"># 旧的列表-----》新的列表</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 1、列表推导式: 格式:[表达式 for 变量 in 旧列表] 或者 [表达式 for 变量 in 旧列表 if 条件]</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 过滤掉长度小于或者等于3的人名</span></span><br><span class="line"></span><br><span class="line">names = [<span class="string">'tome'</span>, <span class="string">'lily'</span>, <span class="string">'abc'</span>, <span class="string">'jack'</span>, <span class="string">'steven'</span>, <span class="string">'bob'</span>]</span><br><span class="line"></span><br><span class="line">result = [name.capitalize() <span class="keyword">for</span> name <span class="keyword">in</span> names <span class="keyword">if</span> <span class="built_in">len</span>(name) > <span class="number">3</span>]</span><br><span class="line"><span class="built_in">print</span>(result)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 将1-100之间能被3整除,组成一个新列表</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">nwelist = [i <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>, <span class="number">101</span>) <span class="keyword">if</span> i % <span class="number">3</span> == <span class="number">0</span> <span class="keyword">and</span> i % <span class="number">5</span> == <span class="number">0</span>]</span><br><span class="line"><span class="built_in">print</span>(nwelist)</span><br><span class="line"></span><br><span class="line"><span class="comment"># [(偶数,奇数),(),(),()] [(0,1),(0,3),(0,5)]</span></span><br><span class="line">nwelist1 = [(i, j) <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">5</span>) <span class="keyword">if</span> i % <span class="number">2</span> == <span class="number">0</span> <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>) <span class="keyword">if</span> j % <span class="number">2</span> != <span class="number">0</span>]</span><br><span class="line"><span class="built_in">print</span>(nwelist1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 3, 5]] ---->[3 ,6 , 9, 5]</span></span><br><span class="line">list1 = [[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>], [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>], [<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>], [<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>]]</span><br><span class="line">nwelist2 = [i[-<span class="number">1</span>] <span class="keyword">for</span> i <span class="keyword">in</span> list1]</span><br><span class="line"><span class="built_in">print</span>(nwelist2)</span><br><span class="line"></span><br><span class="line">dict1 = {<span class="string">'name'</span>: <span class="string">'tom'</span>, <span class="string">'salart'</span>: <span class="number">5000</span>}</span><br><span class="line">dict2 = {<span class="string">'name'</span>: <span class="string">'lucy'</span>, <span class="string">'salart'</span>: <span class="number">8000</span>}</span><br><span class="line">dict3 = {<span class="string">'name'</span>: <span class="string">'jack'</span>, <span class="string">'salart'</span>: <span class="number">4500</span>}</span><br><span class="line">dict4 = {<span class="string">'name'</span>: <span class="string">'lily'</span>, <span class="string">'salart'</span>: <span class="number">3000</span>}</span><br><span class="line"></span><br><span class="line">list1 = [dict1, dict2, dict3, dict4]</span><br><span class="line"><span class="comment"># 如果薪资大于5000加200,低于等于5000加500</span></span><br><span class="line">nwelist1 = [employee[<span class="string">'salart'</span>] + <span class="number">300</span> <span class="keyword">if</span> employee[<span class="string">'salart'</span>] > <span class="number">5000</span> <span class="keyword">else</span> employee[<span class="string">'salart'</span>] + <span class="number">500</span> <span class="keyword">for</span> employee <span class="keyword">in</span> list1]</span><br><span class="line"><span class="built_in">print</span>(nwelist1)</span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">def func(names)</span></span><br><span class="line"><span class="string"> newlist=[]</span></span><br><span class="line"><span class="string"> for name in names:</span></span><br><span class="line"><span class="string"> if len(name)>3:</span></span><br><span class="line"><span class="string"> newlist.append(name)</span></span><br><span class="line"><span class="string"> return newlist</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">print(func())</span></span><br><span class="line"><span class="string">def func()</span></span><br><span class="line"><span class="string"> newlist = []</span></span><br><span class="line"><span class="string"> for i in range(5): # 偶数</span></span><br><span class="line"><span class="string"> if i % 2 == 0:</span></span><br><span class="line"><span class="string"> for j in range(10):</span></span><br><span class="line"><span class="string"> if j % 2 != 0:</span></span><br><span class="line"><span class="string"> newlist.append((i,j))</span></span><br><span class="line"><span class="string">print(func())</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 集合推导式</span></span><br><span class="line">list1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">9</span>]</span><br><span class="line">set1 = {x <span class="keyword">for</span> x <span class="keyword">in</span> list1 <span class="keyword">if</span> x > <span class="number">5</span>}</span><br><span class="line"><span class="built_in">print</span>(set1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 字典推导式</span></span><br><span class="line">dict1 = {<span class="string">'a'</span>: <span class="string">'A'</span>, <span class="string">'b'</span>: <span class="string">'B'</span>, <span class="string">'c'</span>: <span class="string">'C'</span>, <span class="string">'d'</span>: <span class="string">'D'</span>}</span><br><span class="line">newdict = {value: key <span class="keyword">for</span> key, value <span class="keyword">in</span> dict1.items()}</span><br><span class="line"><span class="built_in">print</span>(newdict)</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> 自学 </tag>
</tags>
</entry>
<entry>
<title>windows装mysql解压版教程</title>
<link href="/posts/5880561a/"/>
<url>/posts/5880561a/</url>
<content type="html"><![CDATA[<h1 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h1><ul><li><p>用<strong>管理员</strong>打开CMD,切换到MySql的解压目录下的<code>bin</code>目录:</p></li><li><p>执行以下命令</p></li></ul><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">执行这里会生成,记住这个密码</span></span><br><span class="line">mysqld --initialize --console</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装mysql服务</span></span><br><span class="line">mysqld -install</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">windows</span></span><br><span class="line">mysqld.exe --initialize --console</span><br><span class="line">mysqld.exe -install</span><br></pre></td></tr></table></figure><ul><li><p>输入命令<code>net start mysql</code>或<code>sc start mysql</code>启动mysql服务</p></li><li><p>输入<code>mysql -u root -p</code>来登陆数据库,并输入前面记录的临时密码:</p></li><li><p>登陆成功后输入以下命令<code>a</code>将原来复杂的密码修改为自己的密码,并输入<code>commit;</code>提交:</p></li></ul> <figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">/*1、修改密码*/</span><br><span class="line">alter user 'root'@'localhost' identified by '想要设置的密码';</span><br><span class="line"></span><br><span class="line">/*2、由于mysql 8.0*修改加密方式,使用下面命令可以解决大部分无法连接数据库命令*/</span><br><span class="line">ALTER USER 'root'@'localhost' IDENTIFIED BY '1' PASSWORD EXPIRE NEVER;</span><br><span class="line">/*3、MySQL8.0的caching_sha2_password问题*/</span><br><span class="line">ALTER USER root@localhost IDENTIFIED WITH mysql_native_password BY '1';</span><br><span class="line">FLUSH PRIVILEGES;</span><br></pre></td></tr></table></figure><ul><li>在mysql的目录下设置配置文件my.ini</li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">[mysqld] </span><br><span class="line"># 设置mysql的安装目录,也就是刚才我们解压的目录</span><br><span class="line">basedir=D:/Program Files/mysql/mysql-8.0.27-winx64</span><br><span class="line"># 设置mysql数据库的数据的存放目录</span><br><span class="line"># datadir=D:/Program Files/mysql/mysql-8.0.27-winx64/data</span><br><span class="line"># 设置默认使用的端口</span><br><span class="line">port=3306</span><br><span class="line"># 允许最大连接数</span><br><span class="line">max_connections=200</span><br><span class="line"># 允许连接失败的次数。这是为了防止有人试图攻击数据库</span><br><span class="line">max_connect_errors=10</span><br><span class="line"># 服务端使用的字符集</span><br><span class="line">character-set-server=utf8mb4</span><br><span class="line"># 数据库字符集对应一些排序等规则使用的字符集</span><br><span class="line">collation-server=utf8mb4_general_ci</span><br><span class="line"># 创建新表时将使用的默认存储引擎</span><br><span class="line">default-storage-engine=INNODB</span><br><span class="line"># 默认使用“mysql_native_password”插件作为认证加密方式</span><br><span class="line"># MySQL8.0默认认证加密方式为caching_sha2_password</span><br><span class="line">default_authentication_plugin=mysql_native_password</span><br><span class="line"> </span><br><span class="line">[mysql]</span><br><span class="line"># 设置mysql客户端默认字符集</span><br><span class="line">default-character-set=utf8mb4</span><br><span class="line"> </span><br><span class="line">[client]</span><br><span class="line">default-character-set=utf8mb4</span><br><span class="line">port=3306</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure><h1 id="卸载"><a href="#卸载" class="headerlink" title="卸载"></a>卸载</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">/*删除mysql*/</span><br><span class="line">taskkill /f /t /im mysqld.exe</span><br><span class="line">sc delete mysql</span><br><span class="line">/*停止MySQL服务*/</span><br><span class="line">net stop mysqld 或者 sc stop mysqld</span><br><span class="line">/*删除MySQL服务*/</span><br><span class="line">sc delete mysqld 或 mysqld -remove(需先停止服务)</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 教程 </category>
</categories>
<tags>
<tag> 环境搭建 </tag>
</tags>
</entry>
<entry>
<title>青龙面板安装</title>
<link href="/posts/b9ec591f/"/>
<url>/posts/b9ec591f/</url>
<content type="html"><![CDATA[<h1 id="安装说明"><a href="#安装说明" class="headerlink" title="安装说明"></a>安装说明</h1><h2 id="前置要求"><a href="#前置要求" class="headerlink" title="前置要求"></a>前置要求</h2><figure class="highlight vim"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>.已安装docker-<span class="keyword">ce</span></span><br><span class="line"><span class="number">2</span>.选装docker-compose</span><br></pre></td></tr></table></figure><h2 id="安装方式1"><a href="#安装方式1" class="headerlink" title="安装方式1"></a>安装方式1</h2><figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">1.</span> 新建一个文件夹,用于存放相关数据</span><br><span class="line"><span class="bullet">2.</span> 下载本仓库中的<span class="code">`docker-compose.yml`</span>至本地,或是复制文件内容后在本地自行建立并粘贴内容</span><br><span class="line"><span class="bullet">3.</span> 使用docker-compose启动</span><br><span class="line"><span class="bullet">4.</span> 浏览器输入ip:5700即可进入面板</span><br><span class="line"></span><br><span class="line"><span class="section"># 新建数据文件夹</span></span><br><span class="line">mkdir qinglong</span><br><span class="line">cd qinglong</span><br><span class="line"><span class="section"># 下载docker-compose.yml文件</span></span><br><span class="line">wget https://raw.githubusercontent.com/whyour/qinglong/develop/docker-compose.yml</span><br><span class="line"><span class="section"># 启动</span></span><br><span class="line">docker-compose up -d</span><br></pre></td></tr></table></figure><h2 id="安装方式2"><a href="#安装方式2" class="headerlink" title="安装方式2"></a>安装方式2</h2><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 复制下列命令在ssh执行(先安装docker)</span></span><br><span class="line"><span class="comment"># 注:$PWD请修改为实际你想安装的路径</span></span><br><span class="line"> docker run -dit \</span><br><span class="line"> -v <span class="variable">$PWD</span><span class="regexp">/ql/</span>config:<span class="regexp">/ql/</span>config \</span><br><span class="line"> -v <span class="variable">$PWD</span><span class="regexp">/ql/</span>log:<span class="regexp">/ql/</span>log \</span><br><span class="line"> -v <span class="variable">$PWD</span><span class="regexp">/ql/</span>db:<span class="regexp">/ql/</span>db \</span><br><span class="line"> -v <span class="variable">$PWD</span><span class="regexp">/ql/</span>repo:<span class="regexp">/ql/</span>repo \</span><br><span class="line"> -v <span class="variable">$PWD</span><span class="regexp">/ql/</span>raw:<span class="regexp">/ql/</span>raw \</span><br><span class="line"> -v <span class="variable">$PWD</span><span class="regexp">/ql/</span>scripts:<span class="regexp">/ql/</span>scripts \</span><br><span class="line"> -v <span class="variable">$PWD</span><span class="regexp">/ql/</span>jbot:<span class="regexp">/ql/</span>jbot \</span><br><span class="line"> -p <span class="number">5702</span>:<span class="number">5702</span> \</span><br><span class="line"> -p <span class="number">5701</span>:<span class="number">5701</span> \</span><br><span class="line"> --name qinglong \</span><br><span class="line"> --hostname qinglong \</span><br><span class="line"> --restart unless-stopped \</span><br><span class="line"> registry.cn-hangzhou.aliyuncs.com<span class="regexp">/wanswu/</span>qinglong:<span class="number">1.2</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># 我的配置</span></span><br><span class="line">docker run -dit -v D:<span class="regexp">/JDscirpt/</span>qinglong<span class="regexp">/ql/</span>config:<span class="regexp">/ql/</span>config -v D:<span class="regexp">/JDscirpt/</span>qinglong<span class="regexp">/ql/</span>log:<span class="regexp">/ql/</span>log -v D:<span class="regexp">/JDscirpt/</span>qinglong<span class="regexp">/ql/</span>db:<span class="regexp">/ql/</span>db -v D:<span class="regexp">/JDscirpt/</span>qinglong<span class="regexp">/ql/</span>repo:<span class="regexp">/ql/</span>repo -v D:<span class="regexp">/JDscirpt/</span>qinglong<span class="regexp">/ql/</span>raw:<span class="regexp">/ql/</span>raw -v D:<span class="regexp">/JDscirpt/</span>qinglong<span class="regexp">/ql/</span>scripts:<span class="regexp">/ql/</span>scripts -v D:<span class="regexp">/JDscirpt/</span>qinglong<span class="regexp">/ql/</span>jbot:<span class="regexp">/ql/</span>jbot -p <span class="number">5700</span>:<span class="number">5700</span> -p <span class="number">5701</span>:<span class="number">5701</span> --name qinglong --hostname qinglong --restart unless-stopped whyour/qinglong:latest</span><br></pre></td></tr></table></figure><h2 id="登录"><a href="#登录" class="headerlink" title="登录"></a>登录</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">打开浏览器访问宿主机ip的5700端口即可</span><br><span class="line">例如http://192.168.100.123:5700即ip:5700</span><br><span class="line"></span><br><span class="line">首次登录</span><br><span class="line">账号:admin 密码:admin</span><br><span class="line">会生成`auth.json`</span><br><span class="line"></span><br><span class="line">在ssh输入 </span><br><span class="line">1.docker <span class="built_in">exec</span> it qinglong bash</span><br><span class="line">2.<span class="built_in">cat</span> /ql/config/auth.json</span><br><span class="line"></span><br><span class="line"><span class="built_in">cat</span>查看之后返回的结果类似如下字段</span><br><span class="line"></span><br><span class="line">{<span class="string">"username"</span>:<span class="string">"admin"</span>,<span class="string">"password"</span>:<span class="string">"Xb-ZYP526wmg4_h6q1WqIO"</span>}</span><br><span class="line"><span class="comment"># admin即为登录名;Xb-ZYP526wmg4_h6q1WqIO为登录密码</span></span><br></pre></td></tr></table></figure><p>输入此处记录的<code>username</code>及<code>password</code>,即可成功登陆qinglong面板,登陆后即可正常使用</p><h2 id="拉取脚本"><a href="#拉取脚本" class="headerlink" title="拉取脚本"></a>拉取脚本</h2><figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">示例</span><br><span class="line">ql repo https:<span class="regexp">//gi</span>thub.com/xxx.git <span class="comment">#拉取仓库</span></span><br><span class="line">ql raw https:<span class="regexp">//</span>raw.githubusercontent.com/xxx <span class="comment">#拉取单个脚本</span></span><br></pre></td></tr></table></figure><h2 id="备份"><a href="#备份" class="headerlink" title="备份"></a>备份</h2><p>所有数据都将保存在<code>docker-compose.yml</code>所在的同级目录的<code>data</code>文件夹中,如需要备份,请直接备份<code>docker-compose.yml</code>及<code>data</code>文件夹即可</p><figure class="highlight tap"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">root@debian:/opt/qinglong<span class="comment"># ls -lah</span></span><br><span class="line">总用量 8.0K</span><br><span class="line">drwxr-xr-x<span class="number"> 3 </span>root root 4.0K 8月<span class="number"> 30 </span>01:29 .</span><br><span class="line">drwxr-xr-x<span class="number"> 4 </span>root root 4.0K 8月<span class="number"> 30 </span>00:51 ..</span><br><span class="line">drwxr-xr-x<span class="number"> 8 </span>root root 4.0K 8月<span class="number"> 30 </span>01:30 data</span><br><span class="line">-rw-r--r--<span class="number"> 1 </span>root root <span class="number"> 386 </span> 8月<span class="number"> 30 </span>01:29 docker-compose.yml</span><br></pre></td></tr></table></figure><h2 id="更新"><a href="#更新" class="headerlink" title="更新"></a>更新</h2><p>在面板执行”更新面板”任务即可</p><p>或者</p><figure class="highlight vim"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">cd</span> qinglong</span><br><span class="line">docker-compose down</span><br><span class="line">docker pull whyour/qinglon<span class="variable">g:latest</span></span><br><span class="line">docker-compose <span class="keyword">up</span> -d</span><br></pre></td></tr></table></figure><h2 id="依赖"><a href="#依赖" class="headerlink" title="依赖"></a>依赖</h2><p>python</p><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">requests</span><br><span class="line"><span class="selector-tag">canvas</span></span><br><span class="line">ping3</span><br><span class="line">jieba</span><br></pre></td></tr></table></figure><p>nodejs</p><figure class="highlight x86asm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">png-<span class="keyword">js</span></span><br><span class="line"><span class="built_in">ds</span></span><br><span class="line">@types/node</span><br><span class="line">require</span><br><span class="line">typescript</span><br><span class="line">crypto-<span class="keyword">js</span></span><br><span class="line">prettytable</span><br><span class="line">dotenv</span><br><span class="line">jsdom</span><br><span class="line">date-fns</span><br><span class="line">tough-cookie</span><br><span class="line">tslib</span><br><span class="line">ws@<span class="number">7.4</span><span class="number">.3</span></span><br><span class="line">ts-md5</span><br><span class="line">jsdom -g</span><br><span class="line">jieba</span><br><span class="line"><span class="built_in">fs</span></span><br><span class="line">form-data</span><br><span class="line">json5</span><br><span class="line"><span class="meta">global</span>-agent</span><br><span class="line"><span class="keyword">js</span>-base64</span><br><span class="line">axios</span><br></pre></td></tr></table></figure><p>Linux</p><figure class="highlight mipsasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">bizCode</span></span><br><span class="line"><span class="keyword"></span><span class="keyword">bizMsg</span></span><br><span class="line"><span class="keyword"></span>lxml</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> 工具 </category>
</categories>
<tags>
<tag> 瞎学 </tag>
<tag> qinglong </tag>
</tags>
</entry>
<entry>
<title>Windows10网络感叹号修复</title>
<link href="/posts/d947b397/"/>
<url>/posts/d947b397/</url>
<content type="html"><![CDATA[<h1 id="Windows10-修复网络感叹号bug"><a href="#Windows10-修复网络感叹号bug" class="headerlink" title="Windows10 修复网络感叹号bug"></a>Windows10 修复网络感叹号bug</h1><blockquote><p>修复连接显示无internet但是有网的bug,链接在下方</p></blockquote><p><img src="/Windows10%E7%BD%91%E7%BB%9C%E6%84%9F%E5%8F%B9%E5%8F%B7%E4%BF%AE%E5%A4%8D/image-20211011162902502.webp" alt="image-20211011162902502"></p><blockquote><p>打开这个工具,点击一键修复,然后重启就好了</p></blockquote><p><img src="/Windows10%E7%BD%91%E7%BB%9C%E6%84%9F%E5%8F%B9%E5%8F%B7%E4%BF%AE%E5%A4%8D/image-20211011163154911.webp" alt="image-20211011163154911"></p><p><img src="/Windows10%E7%BD%91%E7%BB%9C%E6%84%9F%E5%8F%B9%E5%8F%B7%E4%BF%AE%E5%A4%8D/image-20211011163441976.webp" alt="image-20211011163441976"></p><h2 id="windows10网络修复工具下载链接(阿里云)"><a href="#windows10网络修复工具下载链接(阿里云)" class="headerlink" title="windows10网络修复工具下载链接(阿里云)"></a><a href="https://www.aliyundrive.com/s/BcyXmzrxPWo">windows10网络修复工具下载链接(阿里云)</a></h2><h2 id="蓝奏云链接"><a href="#蓝奏云链接" class="headerlink" title="蓝奏云链接"></a><a href="https://syakj.lanzoui.com/irCHJv7sfxe">蓝奏云链接</a></h2>]]></content>
<categories>
<category> 教程 </category>
</categories>
<tags>
<tag> windows10 </tag>
</tags>
</entry>
<entry>
<title>Windows远程桌面docker容器</title>
<link href="/posts/354df3ef/"/>
<url>/posts/354df3ef/</url>
<content type="html"><![CDATA[<p>编写Dockerfile文件(可选择dokcer文件,我没测试)</p><p>(Windows无法连通docker容器所以这是Linux中做的实验)</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">FROM centos</span><br><span class="line">MAINTAINER Wans<wa.ns@qq.com></span><br><span class="line">ENV MYPATH /sbin/init</span><br><span class="line">WORKDIR $MYPATH</span><br><span class="line"></span><br><span class="line">RUN yum -y install vim</span><br><span class="line">RUN yum -y install net-tools</span><br><span class="line">RUN rpm -Uvh https://mirrors.tuna.tsinghua.edu.cn/epel/epel-release-latest-7.noarch.rpm</span><br><span class="line">RUN yum -y install xrdp</span><br><span class="line">RUN systemctl start xrdp</span><br><span class="line">RUN systemctl enable xrdp</span><br><span class="line"></span><br><span class="line">CMD echo $MYPATH</span><br><span class="line">CMD echo "----end----"</span><br></pre></td></tr></table></figure><p>1、放行3389端口,重启防火墙</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --permanent --zone=public --add-port=3389/tcp</span><br><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure><p>2、运行docker centos容器</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -it -p 3389:3389 -p 3350:3350 --privileged true --name mycentos centos /sbin/init</span><br></pre></td></tr></table></figure><p>3、安装xrdp、vim、net-tools</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">yum -y install vim</span><br><span class="line">yum -y install net-tools</span><br><span class="line">rpm -Uvh https://mirrors.tuna.tsinghua.edu.cn/epel/epel-release-latest-7.noarch.rpm</span><br><span class="line">yum -y install xrdp</span><br></pre></td></tr></table></figure><p>启动xrdp</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">systemctl start xrdp</span><br><span class="line">systemctl enable xrdp</span><br></pre></td></tr></table></figure><p>打开Windows远程连接就好</p><p><img src="/Windows%E8%BF%9C%E7%A8%8B%E6%A1%8C%E9%9D%A2Linux/image-20211010160758852.webp" alt="image-20211010160758852"></p>]]></content>
<categories>
<category> 教程 </category>
</categories>
<tags>
<tag> Linux </tag>
</tags>
</entry>
<entry>
<title>Docker自学</title>
<link href="/posts/34285/"/>
<url>/posts/34285/</url>
<content type="html"><![CDATA[<h1 id="Docker安装(linux)"><a href="#Docker安装(linux)" class="headerlink" title="Docker安装(linux)"></a>Docker安装(linux)</h1><p>安装docker</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">卸载干净</span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">sudo</span> yum remove docker \</span></span><br><span class="line"><span class="language-bash"> docker-client \</span></span><br><span class="line"><span class="language-bash"> docker-client-latest \</span></span><br><span class="line"><span class="language-bash"> docker-common \</span></span><br><span class="line"><span class="language-bash"> docker-latest \</span></span><br><span class="line"><span class="language-bash"> docker-latest-logrotate \</span></span><br><span class="line"><span class="language-bash"> docker-logrotate \</span></span><br><span class="line"><span class="language-bash"> docker-engine</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装包</span></span><br><span class="line">yum install -y yum-utils</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">设置镜像仓库</span></span><br><span class="line">yum-config-manager \</span><br><span class="line"> --add-repo \</span><br><span class="line"> http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">更新yum软件包索引</span></span><br><span class="line">yum makecache fast</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装docker相关的包</span></span><br><span class="line">yum install docker-ce docker-ce-cli containerd.io</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动docker</span></span><br><span class="line">systemctl start docker</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">运行docker容器</span></span><br><span class="line">docker run hello-world</span><br></pre></td></tr></table></figure><h2 id="Docker卸载(linux)"><a href="#Docker卸载(linux)" class="headerlink" title="Docker卸载(linux)"></a>Docker卸载(linux)</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1、卸载依赖</span><br><span class="line">yum remove docker-ce docker-ce-cli containerd.io</span><br><span class="line">2、删除资源</span><br><span class="line">rm -rf /var/lib/docker</span><br></pre></td></tr></table></figure><h2 id="配置Docker加速"><a href="#配置Docker加速" class="headerlink" title="配置Docker加速"></a>配置Docker加速</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">sudo mkdir -p /etc/docker</span><br><span class="line">sudo tee /etc/docker/daemon.json <<-'EOF'</span><br><span class="line">{</span><br><span class="line"> "registry-mirrors":["https://阿里云节点"]</span><br><span class="line"> </span><br><span class="line">}</span><br></pre></td></tr></table></figure><h2 id="修改Docker默认镜像储存位置"><a href="#修改Docker默认镜像储存位置" class="headerlink" title="修改Docker默认镜像储存位置"></a>修改Docker默认镜像储存位置</h2><p><strong>暂停docker</strong></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wsl --shutdown</span><br></pre></td></tr></table></figure><p><strong>导出原有镜像</strong></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wsl --export docker-desktop "D:\Docker\wsl\data\docker-desktop.tar"</span><br><span class="line">wsl --export docker-desktop-data "D:\Docker\wsl\data\docker-desktop-data.tar"</span><br></pre></td></tr></table></figure><p><strong>取消原有镜像</strong></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wsl --unregister docker-desktop</span><br><span class="line">wsl --unregister docker-desktop-data</span><br></pre></td></tr></table></figure><p><strong>导入镜像</strong></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">wsl --import docker-desktop D:\Docker\wsl\distro D:\Docker\wsl\data\docker-desktop.tar --version 2</span><br><span class="line">wsl --import docker-desktop-data D:\Docker\wsl\data D:\Docker\wsl\data\docker-desktop-data.tar --version 2</span><br></pre></td></tr></table></figure><h1 id="Docker常用命令"><a href="#Docker常用命令" class="headerlink" title="Docker常用命令"></a>Docker常用命令</h1><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker version # 显示docker的版本</span><br><span class="line">docker info # 显示docker系统级信息,包括镜像和容器的数量</span><br><span class="line">docker 命令 --help # 帮助命令</span><br></pre></td></tr></table></figure><h2 id="帮助文档(中文)"><a href="#帮助文档(中文)" class="headerlink" title="帮助文档(中文)"></a><a href="http://www.dockerinfo.net/document">帮助文档(中文)</a></h2><h2 id="镜像命令"><a href="#镜像命令" class="headerlink" title="镜像命令"></a>镜像命令</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">docker images # 查看镜像</span><br><span class="line">-a # 显示所有的镜像</span><br><span class="line">-q # 只显示容器的id</span><br><span class="line"></span><br><span class="line">docker search # 搜索镜像</span><br><span class="line">--fileter=STARS=3000 # 搜索出来的镜像就是stars大于3000的</span><br><span class="line"></span><br><span class="line">docker pull # 下载镜像</span><br><span class="line">docker pull mysql:5.7</span><br></pre></td></tr></table></figure><h2 id="删除镜像"><a href="#删除镜像" class="headerlink" title="删除镜像"></a>删除镜像</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker rmi -f images id</span><br><span class="line">docker rmi -f $(docker images -aq) # 删除所有的images</span><br></pre></td></tr></table></figure><h2 id="运行容器命令"><a href="#运行容器命令" class="headerlink" title="运行容器命令"></a>运行容器命令</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">docker run [可选参数] image</span><br><span class="line">参数说明</span><br><span class="line">--name=“name” # 容器命令</span><br><span class="line">-d # 后台运行</span><br><span class="line">-it # 使用交互方式运行,进入容器查看内容</span><br><span class="line">-p # 指定容器的端口 -p 80:80</span><br><span class="line"> -p ip:主机端口:容器端口</span><br><span class="line"> -p 主机端口容器端口(常用)</span><br><span class="line"> -p 容器端口</span><br><span class="line"> </span><br><span class="line">-P(大写) # 随机指定端口</span><br><span class="line">docker run -itd -p 80:80 [imgaes]</span><br></pre></td></tr></table></figure><h2 id="列出所有的运行容器"><a href="#列出所有的运行容器" class="headerlink" title="列出所有的运行容器"></a>列出所有的运行容器</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker ps</span><br><span class="line">-a # 显示所有的容器+历史运行的容器</span><br><span class="line">-n=? # 列出最近创建的容器</span><br><span class="line">-q # 只显示容器的编号</span><br></pre></td></tr></table></figure><h2 id="退出容器"><a href="#退出容器" class="headerlink" title="退出容器"></a>退出容器</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">exit # 退出并通知容器</span><br><span class="line">Ctrl+p+q # 容器不停止退出</span><br></pre></td></tr></table></figure><h2 id="删除容器"><a href="#删除容器" class="headerlink" title="删除容器"></a>删除容器</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">docker rmi -f 镜像id # 删除指定镜像</span><br><span class="line">docker rmi -f 镜像id 镜像id 镜像id # 删除多个镜像 </span><br><span class="line">docker rmi -f $(docker images aq) # 删除全部镜像</span><br><span class="line"></span><br><span class="line">docker rm -f 容器id # 删除指定容器</span><br><span class="line">docker rm -f 容器 容器 镜像id # 删除多个容器 </span><br><span class="line">docker rm -f $(docker ps -aq) # 删除全部容器</span><br><span class="line">docker ps -a -q|xargs docker rm # 删除全部容器</span><br></pre></td></tr></table></figure><h2 id="启动和停止容器的操作"><a href="#启动和停止容器的操作" class="headerlink" title="启动和停止容器的操作"></a>启动和停止容器的操作</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">docker start</span><br><span class="line">docker restart</span><br><span class="line">docker stop</span><br><span class="line">docker kill </span><br></pre></td></tr></table></figure><h2 id="常用其他命令"><a href="#常用其他命令" class="headerlink" title="常用其他命令"></a>常用其他命令</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">后台启动容器</span><br><span class="line">docker run -d 镜像</span><br><span class="line">问题 docker ps 发现容器停止</span><br><span class="line">docker容器使用后台运行,就必须有应该前台进程,docker发现没有应用就会停止</span><br></pre></td></tr></table></figure><h2 id="查看日志"><a href="#查看日志" class="headerlink" title="查看日志"></a>查看日志</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker log -f -t --tail 容器,没有日志</span><br></pre></td></tr></table></figure><h2 id="查看容器的信息"><a href="#查看容器的信息" class="headerlink" title="查看容器的信息"></a>查看容器的信息</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">docker inspect 容器</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">将本地的镜像导出</span></span><br><span class="line">docker save -o 导出的路径 镜像id</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">加载本地的镜像文件</span></span><br><span class="line">docker load -i 镜像文件</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">修改镜像文件</span></span><br><span class="line">docker tag 镜像id 新镜像名称:版本</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">保存当前容器的状态</span></span><br><span class="line">docker commit [CONTAINER ID] demo:v1.3</span><br></pre></td></tr></table></figure><h2 id="进入当前正在运行的容器"><a href="#进入当前正在运行的容器" class="headerlink" title="进入当前正在运行的容器"></a>进入当前正在运行的容器</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">方法1</span><br><span class="line">docker exec -it 容器id /bin/bash</span><br><span class="line">方法2</span><br><span class="line">docker attach 容器id </span><br><span class="line">区别:</span><br><span class="line">exec命令,进入容器后开启一个新的终端,可以操作(常用)</span><br><span class="line">attach命令,进入容器正在执行的终端,不会启动新的进程</span><br></pre></td></tr></table></figure><h2 id="从容器拷贝文件到主机上"><a href="#从容器拷贝文件到主机上" class="headerlink" title="从容器拷贝文件到主机上"></a>从容器拷贝文件到主机上</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker cp 容器:容器内的路径 主机的路径</span><br><span class="line">同理的主机到容器</span><br></pre></td></tr></table></figure><h2 id="部署es-kubana"><a href="#部署es-kubana" class="headerlink" title="部署es+kubana"></a>部署es+kubana</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">es暴露的端口多,耗内存,数据放安全目录,挂载</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">--net somenetwork 网络配置</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动elasticsearch</span></span><br><span class="line">docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">如果是云服务 linux就卡住</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker stats 查看cpu状态</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">es 耗内存</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">测试es是否成功</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">访问 http://127.0.0.1:9200</span></span><br><span class="line">{</span><br><span class="line"> "name" : "1043572a13e5",</span><br><span class="line"> "cluster_name" : "docker-cluster",</span><br><span class="line"> "cluster_uuid" : "X5qHk_JRRhK9HPKWfaMvYg",</span><br><span class="line"> "version" : {</span><br><span class="line"> "number" : "7.6.2",</span><br><span class="line"> "build_flavor" : "default",</span><br><span class="line"> "build_type" : "docker",</span><br><span class="line"> "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",</span><br><span class="line"> "build_date" : "2020-03-26T06:34:37.794943Z",</span><br><span class="line"> "build_snapshot" : false,</span><br><span class="line"> "lucene_version" : "8.4.0",</span><br><span class="line"> "minimum_wire_compatibility_version" : "6.8.0",</span><br><span class="line"> "minimum_index_compatibility_version" : "6.0.0-beta1"</span><br><span class="line"> },</span><br><span class="line"> "tagline" : "You Know, for Search"</span><br><span class="line">}</span><br></pre></td></tr></table></figure><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 增加内存的限制,修改配置文件 -e 环境配置修改</span><br><span class="line">docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2</span><br></pre></td></tr></table></figure><p>作业:使用kibana连接ES</p><h2 id="可视化"><a href="#可视化" class="headerlink" title="可视化"></a>可视化</h2><ul><li>portainer(先用这个)</li></ul><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 8088:9000 \ --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer</span><br></pre></td></tr></table></figure><ul><li>Rancher(CI/CD再用)</li></ul><p><strong>什么是portainer?</strong></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 8088:9000 \ --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer</span><br></pre></td></tr></table></figure><p>docker图形化管理工具!提供一个后台面板供我们操作!</p><h1 id="Docker镜像讲解"><a href="#Docker镜像讲解" class="headerlink" title="Docker镜像讲解"></a>Docker镜像讲解</h1><h2 id="镜像是什么"><a href="#镜像是什么" class="headerlink" title="镜像是什么"></a>镜像是什么</h2><p>镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含<br>运行某个软件所需的所有内容,包括代码、运行时、库、环境变量和配置文件。</p><p>所有应用,直接打包docker镜像,就可以直接跑起来!</p><ul><li><p>从远程仓库下载</p></li><li><p>朋友拷贝给你</p></li><li><p>自己制作镜像Dockerfile</p></li></ul><h2 id="Docker镜像加载原理"><a href="#Docker镜像加载原理" class="headerlink" title="Docker镜像加载原理"></a>Docker镜像加载原理</h2><blockquote><p>UnionFS(联合文件系统)</p></blockquote><p>我们下载的时候看到的一层层就是这个</p><p>UnionFS(联合文件系统):Union文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,<br>它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系<br>统下(unite several directories into a single virtual filesystem)。Union 文件系统是 Docker 镜像的基<br>础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。<br>特性:一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统,联合加载会把各层文件<br>系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录</p><blockquote><p>Docker镜像加载原理 </p></blockquote><p>docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS。<br>bootfs(boot file system)主要包含bootloader和kernel, bootloader主要是引导加载kernel, Linux刚启<br>动时会加载bootfs文件系统,在Docker镜像的最底层是bootfs。这一层与我们典型的Linux/Unix系统是<br>一样的,包含boot加载器和内核。当boot加载完成之后整个内核就都在内存中了,此时内存的使用权已<br>由bootfs转交给内核,此时系统也会卸载bootfs。</p><p>rootfs (root file system) ,在bootfs之上。包含的就是典型 Linux 系统中的 /dev, /proc, /bin, /etc 等标<br>准目录和文件。rootfs就是各种不同的操作系统发行版,比如Ubuntu,Centos等等。.</p><p>如何提交一个自己的镜像</p><h2 id="commit镜像"><a href="#commit镜像" class="headerlink" title="commit镜像"></a>commit镜像</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker commit 提交容器成为一个新容器</span><br><span class="line"></span><br><span class="line">docker commit -m="提交描述信息" -a="作者" 容器id 目标镜像名:[tag]</span><br></pre></td></tr></table></figure><p>实战测试</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">1、启动一个默认的tomcat</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">2、发现这个tomcat没有webapps应用,官方镜像webapps没有文件!</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">3、自己拷贝进去基本的文件</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">4、将我们操作过的容器通过commit提交为一个镜像,我们以后就是用我们修改过的镜像</span></span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006180546891.webp" alt="image-20211006180546891"></p><p>学习方式说明:理解概念,但是一定要实践</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">如果你想要保存当前容器的状态,就可以通过commit来提交,获得一个镜像</span><br><span class="line">相当于快照</span><br></pre></td></tr></table></figure><p>到这里才是入门!!!学习不需要坚持,学习是爱好!!!</p><h1 id="容器数据卷"><a href="#容器数据卷" class="headerlink" title="容器数据卷"></a>容器数据卷</h1><h2 id="什么是容器数据卷"><a href="#什么是容器数据卷" class="headerlink" title="什么是容器数据卷"></a>什么是容器数据卷</h2><blockquote><p>docker的理念和回顾</p></blockquote><p>将应用和环境打包成一个镜像!</p><p>数据?如果数据都在容器中,那么我们容器删除,数据就会丢失==需求:数据可以持久化==</p><p>MySQL,容器删除,删库跑路!==需求:MySQL数据可以储存在本地==!</p><p>容器之间可以有一个共享的技术!Docker容器中产生的数据,同步到本地!</p><p>这就是卷技术!目录的挂载,将我们的容器内的目录,挂在到Linux什么!</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006195711247.webp" alt="image-20211006195711247"></p><p><strong>总结一句话:容器的持久化和同步操作!容器间也是可以数据共享的!</strong></p><h2 id="使用数据卷"><a href="#使用数据卷" class="headerlink" title="使用数据卷"></a>使用数据卷</h2><blockquote><p>方式一:直接使用命令来挂载</p></blockquote><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">docker run -it -v 主机目录:容器目录</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">测试</span></span><br><span class="line">docker run -it -v D:/dockertest/tomcat:/home tomcat /bin/bash</span><br></pre></td></tr></table></figure><p>这里可以看到成功挂载了</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006201204091.webp" alt="挂载成功"></p><p>测试同步</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006201602762.webp" alt="image-20211006201602762"></p><p>再来测试!</p><p>1、停止容器</p><p>2、宿主机上修改文件</p><p>3、启动容器</p><p>4、容器内的数据依旧是同步的!</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006202110667.webp" alt="image-20211006202110667"></p><p>好处:我们以后修改只需要在本地修改即可,容器内会自动同步</p><h2 id="实战:安装MySQL"><a href="#实战:安装MySQL" class="headerlink" title="实战:安装MySQL"></a>实战:安装MySQL</h2><p>思考:mysql的数据持久化</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">获取镜像</span></span><br><span class="line">docker pull mysql:5.7</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">运行容器,需要做数据挂载!</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">安装MySQL,需要配置密码的,</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">官方测试:docker run -name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动我们的mysql</span></span><br><span class="line">-d 后台运行</span><br><span class="line">-p 端口映射</span><br><span class="line">-v 卷挂载</span><br><span class="line">-e 环境配置</span><br><span class="line">--name 容器名字</span><br><span class="line">docker run -d -p 3310:3306 -v D:/dockertest/mysql/conf:/etc/mysql/conf.d -v D:/dockertest/mysqldata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7</span><br><span class="line"></span><br><span class="line">启动成功</span><br></pre></td></tr></table></figure><p>假设我们将容器删除</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006204840217.webp" alt="image-20211006204840217"></p><p>发现我们挂载到本地的数据卷依旧没有丢失,这就实现了容器数据的持久性功能</p><h2 id="具名和匿名挂载"><a href="#具名和匿名挂载" class="headerlink" title="具名和匿名挂载"></a>具名和匿名挂载</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">匿名挂载(直接写容器捏路径,不去写主机内路径,可以自动生成主机路径)</span></span><br><span class="line">-v 容器内路径</span><br><span class="line">docker run -d -P --name nginx01 -v /etc/nginx nginx</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看volume情况</span></span><br><span class="line">PS C:\Users\Wans> docker volume ls</span><br><span class="line">DRIVER VOLUME NAME</span><br><span class="line">local 2de53d078ecf017bc509e723f27f6b561f665c0cccf7946d62882e89493ce68a</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">这里发现,这种没有名字就是匿名挂载,我们在-v只写了容器内的路径,没写容器外的路径</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">具名挂载</span></span><br><span class="line">PS C:\Users\Wans> docker run -d -P --name nginx02 -v jvming:/etc/nginx nginx</span><br><span class="line">4e3ecbe8f7ab5c25b2a00cb882f0c744bb5c849070c7a7772fb304e7a1a4df2c</span><br><span class="line">PS C:\Users\Wans> docker volume ls</span><br><span class="line">DRIVER VOLUME NAME</span><br><span class="line">local jvming</span><br><span class="line">PS C:\Users\Wans></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">通过-v 卷名:容器内路径</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看一下这个卷</span></span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006210349955.webp" alt="image-20211006210349955"><br>所有的docker容器内的卷,没有指定目录的情况下都在<code>/var/lib/docker/volumes/xxx/_data</code></p><p>我们通过具名挂载可以方便的找到我们的一个卷,大多数情况在使用的<code>具名挂载</code></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">如何确定是具名挂载还是匿名挂载,还是指定路径挂载!</span></span><br><span class="line">-v 容器内路径#匿名挂载</span><br><span class="line">-v 卷名:容器内路径#具名挂载</span><br><span class="line">-v /宿主机路径:容器内路径# 指定路径挂载!</span><br></pre></td></tr></table></figure><p>拓展</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">通过-v容器内路径: ro rw 改变读写权限</span></span><br><span class="line">ro readonly # 只读</span><br><span class="line">rw readwrite # 可读可写</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">一旦这个设置了容器权限,容器对我们挂载出来的内容就有限了</span></span><br><span class="line">docker run -d -P --name nginx02 -v jvming:/etc/nginx:ro nginx</span><br><span class="line">docker run -d -P --name nginx02 -v jvming:/etc/nginx:rw nginx</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">ro只要看到ro说明这个路径只能通过宿主机来操作,容器内部无法操作</span></span><br></pre></td></tr></table></figure><h2 id="初始Dockerfile"><a href="#初始Dockerfile" class="headerlink" title="初始Dockerfile"></a>初始Dockerfile</h2><p>Dockerfile就是用来构建dockers镜像的构建文件!命令脚本、先体验一下!</p><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建Dockerfile文件,名字可以随意,建议dockerfile</span></span><br><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">VOLUME</span><span class="language-bash"> [<span class="string">"volume01"</span>,<span class="string">"volume02"</span>]</span></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> <span class="built_in">echo</span> <span class="string">"----end----"</span></span></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> /bin/bash</span></span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006221917614.webp" alt="image-20211006221917614"></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动自己写的容器</span></span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006222316149.webp" alt="image-20211006222316149"></p><p>这个卷和外部一定是同步的</p><p>测试一下刚才的文件是否同步出去了!</p><p>这种方式我们未来使用的十分多,因为我们通常会构建自己的镜像!</p><p>假设构建镜像时候没有挂载卷,要手动镜像挂载-v卷名:容器内路径!</p><h2 id="数据卷容器"><a href="#数据卷容器" class="headerlink" title="数据卷容器"></a>数据卷容器</h2><p>多个mysql同步数据!</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006223333496.webp" alt="image-20211006223333496"></p><figure class="highlight 1c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># 启动3个容器,通过我们刚才自己的写镜像启动</span></span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006224547475.webp" alt="image-20211006224547475"></p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006224352364.webp" alt="image-20211006224352364"></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">测试,删除docker01,查看docker02和docker03依旧可以访问volume</span></span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211006225002861.webp" alt="image-20211006225002861"></p><p>多个mysql实现数据共享</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 3310:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7</span><br><span class="line"></span><br><span class="line">docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volome-from mysql01 mysql:5.7</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">这时候可以实现两个容器数据同步</span></span><br></pre></td></tr></table></figure><p>结论:</p><p>容器之间的配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止!!</p><p>但是一旦你持久化到本地这个时候,本地的数据不会删除</p><h1 id="Dockerfile"><a href="#Dockerfile" class="headerlink" title="Dockerfile"></a>Dockerfile</h1><h2 id="Dockerfile介绍"><a href="#Dockerfile介绍" class="headerlink" title="Dockerfile介绍"></a>Dockerfile介绍</h2><p>dockerfile是用来构建docker镜像的文件!命令参数脚本!</p><p>构建步骤:</p><p>1、编写dockerfile文件</p><p>2、docker build 构建成为一个镜像</p><p>3、docker run 运行镜像</p><p>4、docker push 发布镜像(dockerhub、阿里云镜像仓库)</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211007061934942.webp" alt="image-20211007061934942"></p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211007062001424.webp" alt="image-20211007062001424"></p><p>很多官方镜像都是基础包好的功能没有,我们提出会搭建自己的镜像</p><h2 id="Dockerfile构建过程"><a href="#Dockerfile构建过程" class="headerlink" title="Dockerfile构建过程"></a>Dockerfile构建过程</h2><p><strong>基础知识</strong></p><p>1、每个保留关键字(指令)都是必须是大写字母</p><p>2、执行是从上到下顺序执行</p><p>3、# 表示注释</p><p>4、每个指令都会创建提交一个新的镜像层,并提交</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211007062549193.webp" alt="image-20211007062549193"></p><p>步骤:开发、部署、运维、缺一不可</p><p>dockerfile:构建文件,定义一切的步骤,源代码</p><p>dockerimages:通过dockerfile构建生成的镜像,最终发布和运行的产品</p><p>docker容器:容器就是镜像运行起来提供服务器</p><h2 id="Dockerfile的指令"><a href="#Dockerfile的指令" class="headerlink" title="Dockerfile的指令"></a>Dockerfile的指令</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">FROM# 基础镜像,一切从这里开始构建</span><br><span class="line">MAINTAINER# 镜像构建者信息:姓名+邮箱</span><br><span class="line">RUN# 镜像构建的时候要运行的命令</span><br><span class="line">ADD# 步骤:tomecat镜像,这个tomcat压缩包!添加内容</span><br><span class="line">WORKDIR# 镜像的工作目录</span><br><span class="line">VOLUME# 挂载的目录</span><br><span class="line">EXPOST# 保留端口配置</span><br><span class="line">CMD# 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代</span><br><span class="line">ENTRYPOINT# 指定这个容器启动的时候要运行的命令</span><br><span class="line">ONBUILD# 当构建一个被继承Dockerfile这个时候就会运行ONBUILD的指令,出发指令</span><br><span class="line">COPY# 类似ADD,将我们文件拷贝到镜像中</span><br><span class="line">ENV# 构建的时候设置环境变量 -e</span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211007064730968.webp" alt="image-20211007064730968"></p><h2 id="实战测试"><a href="#实战测试" class="headerlink" title="实战测试"></a>实战测试</h2><p>Docker hub 中99%都是从FROM scratch,然后配置需要的软件和配置来进行的构建</p><blockquote><p>创建自己的centos镜像</p></blockquote><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1、编写Dockerfiel文件</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># FROM centos</span></span><br><span class="line"><span class="comment"># VOLUME ["volume01","volume02"]</span></span><br><span class="line"><span class="comment"># CMD echo "----end----"</span></span><br><span class="line"><span class="comment"># CMD /bin/bash</span></span><br><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">MAINTAINER</span> Wans<wa.ns@qq.com></span><br><span class="line"><span class="comment"># 环境变量</span></span><br><span class="line"><span class="keyword">ENV</span> MYPATH /usr/local</span><br><span class="line"><span class="keyword">WORKDIR</span><span class="language-bash"> <span class="variable">$MYPATH</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">RUN</span><span class="language-bash"> yum -y install vim</span></span><br><span class="line"><span class="keyword">RUN</span><span class="language-bash"> yum -y install net-tools</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">EXPOSE</span> <span class="number">80</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> <span class="built_in">echo</span> <span class="variable">$MYPATH</span></span></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> <span class="built_in">echo</span> <span class="string">"----end----"</span></span></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> /bin/bash</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2、通过这个文件构建镜像</span></span><br><span class="line"><span class="comment"># 命令 docker build -f <dockerfile文件路径> -t 镜像名:[tag]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 3、测试运行,添加了vim和ifconfig</span></span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211007074651332.webp" alt="image-20211007074651332"></p><p>我们可以看看镜像的历史记录,就可以永久它是怎么做的</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211007075255351.webp" alt="image-20211007075255351"></p><blockquote><p>CMD和ENTRYPOINT区别</p></blockquote><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">编写dokcerfile文件</span></span><br><span class="line">FROM centos</span><br><span class="line">CMD ["ls","-a"]</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">构建镜像</span></span><br><span class="line">PS D:\dockertest> docker build -f Dockerfile -t cmdtest:0.1 .</span><br><span class="line">[+] Building 22.3s (5/5) FINISHED</span><br><span class="line"> => [internal] load build definition from Dockerfile 2.5s</span><br><span class="line"> => => transferring dockerfile: 381B 0.0s</span><br><span class="line"> => [internal] load .dockerignore 3.8s</span><br><span class="line"> => => transferring context: 2B 0.0s</span><br><span class="line"> => [internal] load metadata for docker.io/library/centos:latest 16.4s</span><br><span class="line"> => CACHED [1/1] FROM docker.io/library/centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 0.0s</span><br><span class="line"> => exporting to image 0.7s</span><br><span class="line"> => => exporting layers 0.0s</span><br><span class="line"> => => writing image sha256:7d202bdf002be182b794b7f2b4c90c4fe3560c3ac4f8cebc27f1c8a94ab10a9a 0.3s</span><br><span class="line"> => => naming to docker.io/library/cmdtest:0.1 0.4s</span><br><span class="line"></span><br><span class="line">Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">run运行,发现<span class="built_in">ls</span> -a命令生效</span></span><br><span class="line">PS D:\dockertest> docker run cmdtest:0.1</span><br><span class="line">.</span><br><span class="line">..</span><br><span class="line">.dockerenv</span><br><span class="line">bin</span><br><span class="line">dev</span><br><span class="line">etc</span><br><span class="line">home</span><br><span class="line">lib</span><br><span class="line">lib64</span><br><span class="line">lost+found</span><br><span class="line">media</span><br><span class="line">mnt</span><br><span class="line">opt</span><br><span class="line">proc</span><br><span class="line">root</span><br><span class="line">run</span><br><span class="line">sbin</span><br><span class="line">srv</span><br><span class="line">sys</span><br><span class="line">tmp</span><br><span class="line">usr</span><br><span class="line">var</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">无法追加-l命令</span></span><br><span class="line">PS D:\dockertest> docker run cmdtest:0.1 -l</span><br><span class="line">docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">cmd的情况下 -l 替换 CMD [<span class="string">"ls"</span>,<span class="string">"-a"</span>]命令,-l不是命令所以报错!</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">使用ENTRYPOINT 则可以在run后面加-l</span></span><br></pre></td></tr></table></figure><p>Dockerfile中很多命令都十分相似,我们需要了解他们的区别。</p><h2 id="实战:Tomcat镜像"><a href="#实战:Tomcat镜像" class="headerlink" title="实战:Tomcat镜像"></a>实战:Tomcat镜像</h2><h3 id="1、准备镜像文件tomcat压缩包,jdk的压缩包"><a href="#1、准备镜像文件tomcat压缩包,jdk的压缩包" class="headerlink" title="1、准备镜像文件tomcat压缩包,jdk的压缩包"></a>1、准备镜像文件tomcat压缩包,jdk的压缩包</h3><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211007115140033.webp" alt="image-20211007115140033"></p><h3 id="2、-编写dockerfile文件,"><a href="#2、-编写dockerfile文件," class="headerlink" title="2、 编写dockerfile文件,"></a>2、 编写dockerfile文件,</h3><p> 官方命名<code>Dockerfile</code>,build会自动寻找这个文件,就不需要-f指定了</p><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># vim Dockerfile</span></span><br><span class="line"><span class="keyword">FROM</span> centos</span><br><span class="line"><span class="keyword">MAINTAINER</span> Wans<wa.ns@qq.com></span><br><span class="line"></span><br><span class="line"><span class="comment"># 把宿主机当前上下文的read.txt拷贝到容器/usr/local/路径下</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> Readme.md /usr/local/Readme.md</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 把java与tomcat添加到容器中</span></span><br><span class="line"><span class="keyword">ADD</span><span class="language-bash"> jdk-8u11-linux-x64.tar.gz /usr/local/</span></span><br><span class="line"><span class="keyword">ADD</span><span class="language-bash"> apache-tomcat-9.0.54.tar.gz /usr/local/</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 安装vim编辑器</span></span><br><span class="line"><span class="keyword">RUN</span><span class="language-bash"> yum -y install vim</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置工作访问时候的WORKDIR路径,登录落脚点</span></span><br><span class="line"><span class="keyword">ENV</span> MYPATH /usr/local</span><br><span class="line"><span class="keyword">WORKDIR</span><span class="language-bash"> <span class="variable">$MYPATH</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 配置java与tomcat环境变量</span></span><br><span class="line"><span class="keyword">ENV</span> JAVA_HOME /usr/local/jdk-<span class="number">8</span>u11-linux-x64</span><br><span class="line"><span class="keyword">ENV</span> CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar</span><br><span class="line"><span class="keyword">ENV</span>CATALINA_HOME /usr/local/apache-tomcat-<span class="number">9.0</span>.<span class="number">54</span></span><br><span class="line"><span class="keyword">ENV</span>CATALINA_BASH /usr/local/apache-tomcat-<span class="number">9.0</span>.<span class="number">54</span></span><br><span class="line"><span class="keyword">ENV</span>PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin</span><br><span class="line"></span><br><span class="line"><span class="keyword">EXPOSE</span> <span class="number">8080</span></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> /usr/local/apache-tomcat-9.0.54/bin/startup.sh && <span class="built_in">tail</span> -F /url/local/apache-tomcat-9.0.54/bin/logs/catalina.out</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><h3 id="3、构建镜像"><a href="#3、构建镜像" class="headerlink" title="3、构建镜像"></a>3、构建镜像</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker build -t wanstomcat .</span><br></pre></td></tr></table></figure><p>4、启动镜像</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -d -p 9090:8080 --name wanstomcat -v D:/dockertest/tomcat/test:/usr/local/apache-tomcat-9.0.54/webapps/test -v D:/dockertest/tomcat/logs/:/url/local/apache-tomcat-9.0.54/logs wanstomcat</span><br></pre></td></tr></table></figure><p>5、 访问测试</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211008062543095.webp" alt="image-20211008062543095"></p><p>6、发布项目(由于做了卷挂载,我们直接本地编写项目就可以发布了)</p><h1 id="Dockers网络"><a href="#Dockers网络" class="headerlink" title="Dockers网络"></a>Dockers网络</h1><h2 id="理解Docker0"><a href="#理解Docker0" class="headerlink" title="理解Docker0"></a>理解Docker0</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">问题Docker怎么处理容器网络访问的?</span></span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211008204301074.webp" alt="image-20211008204301074"></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker run -d -P --name tomcat01 tomcat</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看容器的内部网络地址 ip addr 因为我下载的tomcat 没有ip addr 命令</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">所以我就使用了<span class="built_in">ls</span> -la测试了一下</span></span><br><span class="line"></span><br><span class="line">docker exec -it tomcat01 ls -la</span><br><span class="line"></span><br><span class="line">PS C:\Users\Wans> docker exec -it 7c2708f0dd7a ls -la</span><br><span class="line">total 176</span><br><span class="line">drwxr-xr-x 1 root root 4096 Oct 6 19:10 .</span><br><span class="line">drwxr-xr-x 1 root root 4096 Sep 29 11:13 ..</span><br><span class="line">-rw-r--r-- 1 root root 18994 Sep 28 13:34 BUILDING.txt</span><br><span class="line">-rw-r--r-- 1 root root 6210 Sep 28 13:34 CONTRIBUTING.md</span><br><span class="line">-rw-r--r-- 1 root root 60269 Sep 28 13:34 LICENSE</span><br><span class="line">-rw-r--r-- 1 root root 2333 Sep 28 13:34 NOTICE</span><br><span class="line">-rw-r--r-- 1 root root 3372 Sep 28 13:34 README.md</span><br><span class="line">-rw-r--r-- 1 root root 6905 Sep 28 13:34 RELEASE-NOTES</span><br><span class="line">-rw-r--r-- 1 root root 16517 Sep 28 13:34 RUNNING.txt</span><br><span class="line">drwxr-xr-x 2 root root 4096 Oct 6 19:10 bin</span><br><span class="line">drwxr-xr-x 1 root root 4096 Oct 8 13:05 conf</span><br><span class="line">drwxr-xr-x 2 root root 4096 Oct 6 19:10 lib</span><br><span class="line">drwxrwxrwx 1 root root 4096 Oct 8 13:05 logs</span><br><span class="line">drwxr-xr-x 2 root root 4096 Oct 6 19:10 native-jni-lib</span><br><span class="line">drwxrwxrwx 2 root root 4096 Oct 6 19:10 temp</span><br><span class="line">drwxr-xr-x 2 root root 4096 Oct 6 19:10 webapps</span><br><span class="line">drwxr-xr-x 7 root root 4096 Sep 28 13:34 webapps.dist</span><br><span class="line">drwxrwxrwx 2 root root 4096 Sep 28 13:34 work</span><br><span class="line">PS C:\Users\Wans></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">linux 可以 ping 通 docker 容器内部</span></span><br></pre></td></tr></table></figure><blockquote><p>原理 </p></blockquote><p>1、我们没安装一个docker容器,docker就会给docker容器分配一个ip,我们只要安装了docker就会有一个网卡Docker0桥接模式,使用的技术是evth-pair技术!</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">evth-pair 就是一对的虚拟设备接口,他们都是成对出现的,一段连着协议,一段彼此相连</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">evth-pair充当桥梁,连接各种虚拟网络设备的</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">openStac,docker容器之间的连接,ovs的连接,都是evth-pair技术</span></span><br></pre></td></tr></table></figure><p>2、tomcat01和tomcat02直接可以ping通</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211008223737900.webp" alt="image-20211008223737900"></p><p>结论:tomcat01和tomcat02是共用一个路由器,docker0</p><p>所有的容器不指定网络的情况下,都是docker0路由器的,docker会给我们的容器分配一个默认的可用ip</p><blockquote><p>小结</p></blockquote><p>Docker使用的是Linux的桥接,宿主机中是一个docker容器的网桥 docker0</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211008224414385.webp" alt="image-20211008224414385"></p><p>Docker中的所有的网络接口都是虚拟的,虚拟转发效率高</p><p>只要容器删除,对应的一对网桥就删除</p><h2 id="–link"><a href="#–link" class="headerlink" title="–link"></a>–link</h2><blockquote><p>思考一个场景,我们就编写了一个微服务,database url = ip,项目部重启,数据库ip换掉了,我们希望可以处理这个问题,可以用名字来访问容器</p></blockquote><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">不能ping通</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">PS D:\Users\Wans\Desktop> docker <span class="built_in">exec</span> -it tomcat02 ping tomcat01</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">通过 --<span class="built_in">link</span>可以解决名字ping</span></span><br><span class="line">docker run -d -P --name tomcat03 --link tomcat02 tomcat</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">反向不可以ping通</span></span><br><span class="line">docker exec -it tomcat02 ping tomcat03</span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211008230158994.webp" alt="image-20211008230158994"></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">这里看到--<span class="built_in">link</span>将tomcat02的地址写在hosts中</span></span><br></pre></td></tr></table></figure><p>现在已经不推荐–linkl</p><p>自定义网络!!不适合docker0!</p><p>docker0问题:不支持容器名连接访问</p><h2 id="自定义网络"><a href="#自定义网络" class="headerlink" title="自定义网络"></a>自定义网络</h2><blockquote><p>查看所有的docker网络</p></blockquote><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211008230613841.webp" alt="image-20211008230613841"></p><p><strong>网络模式</strong></p><p>bridge:桥接模式(莫仍,自己创建也使用桥接模式)</p><p>host:主机模式(和宿主机共享网络)</p><p>none:不配网络</p><p>container:容器网络连通(用的少,局限大)</p><p><strong>测试</strong></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">我们直接启动的命令 --net bridge,而这个就是我们的docker0</span></span><br><span class="line">docker run -d -P --name tomcat01 tomcat</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">--net bridge(这个是默认的)</span></span><br><span class="line">docker run -d -P --name tomcat01 --net bridge tomcat</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">docker0特点:默认,域名不能访问, --<span class="built_in">link</span>可以打通连接!</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看创建网卡的命令</span></span><br><span class="line">PS D:\Users\Wans\Desktop> docker network create --help</span><br><span class="line"></span><br><span class="line">Usage: docker network create [OPTIONS] NETWORK</span><br><span class="line"></span><br><span class="line">Create a network</span><br><span class="line"></span><br><span class="line">Options:</span><br><span class="line"> --attachable Enable manual container attachment</span><br><span class="line"> --aux-address map Auxiliary IPv4 or IPv6 addresses used by</span><br><span class="line"> Network driver (default map[])</span><br><span class="line"> --config-from string The network from which to copy the configuration</span><br><span class="line"> --config-only Create a configuration only network</span><br><span class="line"> -d, --driver string Driver to manage the Network (default "bridge")</span><br><span class="line"> --gateway strings IPv4 or IPv6 Gateway for the master subnet</span><br><span class="line"> --ingress Create swarm routing-mesh network</span><br><span class="line"> --internal Restrict external access to the network</span><br><span class="line"> --ip-range strings Allocate container ip from a sub-range</span><br><span class="line"> --ipam-driver string IP Address Management Driver (default "default")</span><br><span class="line"> --ipam-opt map Set IPAM driver specific options (default map[])</span><br><span class="line"> --ipv6 Enable IPv6 networking</span><br><span class="line"> --label list Set metadata on a network</span><br><span class="line"> -o, --opt map Set driver specific options (default map[])</span><br><span class="line"> --scope string Control the network's scope</span><br><span class="line"> --subnet strings Subnet in CIDR format that represents a</span><br><span class="line"> network segment</span><br><span class="line">PS D:\Users\Wans\Desktop></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">成功创建网卡</span></span><br><span class="line">PS D:\Users\Wans\Desktop> docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet</span><br><span class="line">108130a48496e1f9e2b864cc3fb324dbdd4ccc1822985307773789b7c8ae6e3e</span><br><span class="line">PS D:\Users\Wans\Desktop> docker network ls</span><br><span class="line">NETWORK ID NAME DRIVER SCOPE</span><br><span class="line">e02c3a9814c2 bridge bridge local</span><br><span class="line">ac450d1a3661 host host local</span><br><span class="line">108130a48496 mynet bridge local</span><br><span class="line">1e32ceb0b028 none null local</span><br><span class="line">PS D:\Users\Wans\Desktop></span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211010212207280.webp" alt="image-20211010212207280"></p><h2 id="网络连通"><a href="#网络连通" class="headerlink" title="网络连通"></a>网络连通</h2><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211010212416674.webp" alt="image-20211010212416674"></p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211010212444973.webp" alt="image-20211010212444973"></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">测试打通 tomcat -mynet</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">连通之后就是将tomcat01放到了mynet网络下</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">一个容器两个ip地址 !</span> </span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">相当于阿里云服务,内网ip和公网ip</span></span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211010212709223.webp" alt="image-20211010212709223"></p><p>结论:假设要跨网络操作别人,就需要docker network connet连通</p><h2 id="实战:Redis集群"><a href="#实战:Redis集群" class="headerlink" title="实战:Redis集群"></a>实战:Redis集群</h2><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211010213235048.webp" alt="image-20211010213235048"></p><p>shell脚本!</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">创建网卡</span></span><br><span class="line">docker network create redis --subnet 172.38.0.0/16</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">通过脚本创建六个redis配置</span></span><br><span class="line">for port in $(seq 1 6); \</span><br><span class="line">do \</span><br><span class="line">mkdir -p /mydata/redis/node-${port}/conf</span><br><span class="line">touch /mydata/redis/node-${port}/conf/redis.conf</span><br><span class="line">cat << EOF >/mydata/redis/node-${port}/conf/redis.conf</span><br><span class="line">port 6379</span><br><span class="line">bind 0.0.0.0</span><br><span class="line">cluster-enabled yes</span><br><span class="line">cluster-config-file nodes.conf</span><br><span class="line">cluster-node-timeout 5000</span><br><span class="line">cluster-announce-ip 172.38.0.1${port}</span><br><span class="line">cluster-announce-port 6379</span><br><span class="line">cluster-announce-bus-port 16379</span><br><span class="line">appendonly yes</span><br><span class="line">EOF</span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \</span><br><span class="line">-v /mydata/redis/node-${port}/data:/data \</span><br><span class="line">-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \</span><br><span class="line">-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server</span><br><span class="line">/etc/redis/redis.conf; \</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">启动6个容器</span></span><br><span class="line">docker run -p 6371:6379 -p 16371:16379 --name redis-1 \</span><br><span class="line">-v /mydata/redis/node-1/data:/data \</span><br><span class="line">-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \</span><br><span class="line">-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf</span><br></pre></td></tr></table></figure><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211010220516620.webp" alt="image-20211010220516620"></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 进入redis</span></span><br><span class="line">docker <span class="built_in">exec</span> -it redis-1 /bin/sh</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建集群的配置</span></span><br><span class="line">/data <span class="comment"># redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.</span></span><br><span class="line">13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas </span><br><span class="line">1</span><br><span class="line">>>> Performing <span class="built_in">hash</span> slots allocation on 6 nodes...</span><br><span class="line">Master[0] -> Slots 0 - 5460</span><br><span class="line">Master[1] -> Slots 5461 - 10922</span><br><span class="line">Master[2] -> Slots 10923 - 16383</span><br><span class="line">Adding replica 172.38.0.15:6379 to 172.38.0.11:6379</span><br><span class="line">Adding replica 172.38.0.16:6379 to 172.38.0.12:6379</span><br><span class="line">Adding replica 172.38.0.14:6379 to 172.38.0.13:6379</span><br><span class="line">M: 008bae73c70dc85404d37bd5a2e50803d63c01db 172.38.0.11:6379</span><br><span class="line"> slots:[0-5460] (5461 slots) master</span><br><span class="line">M: 08a33c605873c8dc30fd0b883310e09647c22ef8 172.38.0.12:6379</span><br><span class="line"> slots:[5461-10922] (5462 slots) master</span><br><span class="line">M: 08da98ed12ca231a64fb18b168df106bcdbc1132 172.38.0.13:6379</span><br><span class="line"> slots:[10923-16383] (5461 slots) master</span><br><span class="line">S: 6c466d74c7e2b505c1732280fea7f13177467e2d 172.38.0.14:6379</span><br><span class="line"> replicates 08da98ed12ca231a64fb18b168df106bcdbc1132</span><br><span class="line">S: e24369c4771b8948325dd5ce2eb4bd99dab415d9 172.38.0.15:6379</span><br><span class="line"> replicates 008bae73c70dc85404d37bd5a2e50803d63c01db</span><br><span class="line">S: 32fc9e78674f24cefcc070052f1de39fbf54e4f2 172.38.0.16:6379</span><br><span class="line"> replicates 08a33c605873c8dc30fd0b883310e09647c22ef8</span><br><span class="line">Can I <span class="built_in">set</span> the above configuration? (<span class="built_in">type</span> <span class="string">'yes'</span> to accept): <span class="built_in">yes</span></span><br><span class="line">>>> Nodes configuration updated</span><br><span class="line">>>> Assign a different config epoch to each node</span><br><span class="line">>>> Sending CLUSTER MEET messages to <span class="built_in">join</span> the cluster</span><br><span class="line">Waiting <span class="keyword">for</span> the cluster to <span class="built_in">join</span></span><br><span class="line">...</span><br><span class="line">>>> Performing Cluster Check (using node 172.38.0.11:6379)</span><br><span class="line">M: 008bae73c70dc85404d37bd5a2e50803d63c01db 172.38.0.11:6379</span><br><span class="line"> slots:[0-5460] (5461 slots) master</span><br><span class="line"> 1 additional replica(s)</span><br><span class="line">M: 08a33c605873c8dc30fd0b883310e09647c22ef8 172.38.0.12:6379</span><br><span class="line"> slots:[5461-10922] (5462 slots) master</span><br><span class="line"> 1 additional replica(s)</span><br><span class="line">S: 32fc9e78674f24cefcc070052f1de39fbf54e4f2 172.38.0.16:6379</span><br><span class="line"> slots: (0 slots) slave</span><br><span class="line"> replicates 08a33c605873c8dc30fd0b883310e09647c22ef8</span><br><span class="line">S: 6c466d74c7e2b505c1732280fea7f13177467e2d 172.38.0.14:6379</span><br><span class="line"> slots: (0 slots) slave</span><br><span class="line"> replicates 08da98ed12ca231a64fb18b168df106bcdbc1132</span><br><span class="line">S: e24369c4771b8948325dd5ce2eb4bd99dab415d9 172.38.0.15:6379</span><br><span class="line"> slots: (0 slots) slave</span><br><span class="line"> replicates 008bae73c70dc85404d37bd5a2e50803d63c01db</span><br><span class="line">M: 08da98ed12ca231a64fb18b168df106bcdbc1132 172.38.0.13:6379</span><br><span class="line"> slots:[10923-16383] (5461 slots) master</span><br><span class="line"> 1 additional replica(s)</span><br><span class="line">[OK] All nodes agree about slots configuration.</span><br><span class="line">>>> Check <span class="keyword">for</span> open slots...</span><br><span class="line">>>> Check slots coverage...</span><br><span class="line">[OK] All 16384 slots covered.</span><br></pre></td></tr></table></figure><p>Docker搭建redis集群完成</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211010221506757.webp" alt="image-20211010221506757"></p><p>我们使用了docker之后,所有的技术都会简单</p><h1 id="SpringBoot微服务打包Docker镜像"><a href="#SpringBoot微服务打包Docker镜像" class="headerlink" title="SpringBoot微服务打包Docker镜像"></a>SpringBoot微服务打包Docker镜像</h1><p>1、使用 IDEA 构建一个 SpringBoot 项目</p><p>2、编写一个helloController</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">HelloController</span> {</span><br><span class="line"><span class="meta">@GetMapping("/hello")</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">hello</span><span class="params">()</span>{</span><br><span class="line"><span class="keyword">return</span> <span class="string">"hello,kuangshen"</span>;</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>3、启动测试下,端口修改下,避免8080冲突!本地访问没问题就可以!</p><p>4、打jar包</p><p><img src="/Docker%E5%AD%A6%E4%B9%A0/image-20211010223916639.webp" alt="image-20211010223916639"></p><p>有了 jar 包,我们就可以作镜像了! 记得测试一下jar包可以使用吗!</p><h2 id="打包镜像"><a href="#打包镜像" class="headerlink" title="打包镜像"></a>打包镜像</h2><p>1、在项目下编写 Dockerfile 文件,将打包好的jar包拷贝到Dockerfile同级目录</p><figure class="highlight dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">FROM</span> java:<span class="number">8</span></span><br><span class="line"><span class="comment"># 服务器只有dockerfile和jar在同级目录</span></span><br><span class="line"><span class="keyword">COPY</span><span class="language-bash"> *.jar /app.jar</span></span><br><span class="line"><span class="keyword">CMD</span><span class="language-bash"> [<span class="string">"--server.port=8080"</span>]</span></span><br><span class="line"><span class="comment"># 指定容器内要暴露的端口</span></span><br><span class="line"><span class="keyword">EXPOSE</span> <span class="number">8080</span></span><br><span class="line"><span class="keyword">ENTRYPOINT</span><span class="language-bash"> [<span class="string">"java"</span>,<span class="string">"-jar"</span>,<span class="string">"/app.jar"</span>]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><p>2、将Dockerfile 和 项目的 jar 包上传到linux服务器上,构建运行</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">[root@kuangshen idea]# pwd</span><br><span class="line">/home/idea</span><br><span class="line">[root@kuangshen idea]# ll</span><br><span class="line">total 17228</span><br><span class="line">-rw-r--r-- 1 root root 17634294 May 14 12:33 demo-0.0.1-SNAPSHOT.jar</span><br><span class="line">-rw-r--r-- 1 root root 207 May 14 12:32 Dockerfile</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">构建镜像</span></span><br><span class="line">docker build -t idea-ks .</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">查看镜像</span></span><br><span class="line">docker images</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">运行</span></span><br><span class="line">docker run -d -P --name idea-ks idea-ks</span><br><span class="line">[root@kuangshen ~]# docker ps</span><br><span class="line">CONTAINER ID IMAGE PORTS NAMES</span><br><span class="line">2366c960ba99 idea-ks 0.0.0.0:32779->8080/tcp idea-ks1</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">测试访问</span></span><br><span class="line">[root@kuangshen ~]# curl localhost:32779</span><br><span class="line">[root@kuangshen ~]# curl localhost:32779/hello</span><br><span class="line"></span><br></pre></td></tr></table></figure><h1 id="Docker-Compose"><a href="#Docker-Compose" class="headerlink" title="Docker Compose"></a>Docker Compose</h1><h2 id="安装Docker-compose"><a href="#安装Docker-compose" class="headerlink" title="安装Docker-compose"></a>安装Docker-compose</h2><p>1、下载安装</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">这个可能快点!</span></span><br><span class="line">curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">添加执行权限</span></span><br><span class="line">sudo chmod +x /usr/local/bin/docker-compose</span><br></pre></td></tr></table></figure><p>2、体验</p><h2 id="博客WordPress"><a href="#博客WordPress" class="headerlink" title="博客WordPress"></a>博客WordPress</h2><p>1、下载,安装数据库、配置、、</p><p>compse应用—》一键启动</p><p>1、下载项目(docker-compose.yam)</p><p>2、如果需要文件,dockerfile</p><p>3、文件准备齐全一键启动</p><p>创建docker-compose.yml</p><p>将下面脚本写入</p><p>然后执行docker-compose up</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">version: "3.9"</span><br><span class="line"> </span><br><span class="line">services:</span><br><span class="line"> db:</span><br><span class="line"> image: mysql:5.7</span><br><span class="line"> volumes:</span><br><span class="line"> - db_data:/var/lib/mysql</span><br><span class="line"> restart: always</span><br><span class="line"> environment:</span><br><span class="line"> MYSQL_ROOT_PASSWORD: somewordpress</span><br><span class="line"> MYSQL_DATABASE: wordpress</span><br><span class="line"> MYSQL_USER: wordpress</span><br><span class="line"> MYSQL_PASSWORD: wordpress</span><br><span class="line"> </span><br><span class="line"> wordpress:</span><br><span class="line"> depends_on:</span><br><span class="line"> - db</span><br><span class="line"> image: wordpress:latest</span><br><span class="line"> volumes:</span><br><span class="line"> - wordpress_data:/var/www/html</span><br><span class="line"> ports:</span><br><span class="line"> - "8000:80"</span><br><span class="line"> restart: always</span><br><span class="line"> environment:</span><br><span class="line"> WORDPRESS_DB_HOST: db:3306</span><br><span class="line"> WORDPRESS_DB_USER: wordpress</span><br><span class="line"> WORDPRESS_DB_PASSWORD: wordpress</span><br><span class="line"> WORDPRESS_DB_NAME: wordpress</span><br><span class="line">volumes:</span><br><span class="line"> db_data: {}</span><br><span class="line"> wordpress_data: {}</span><br></pre></td></tr></table></figure><p>前台启动</p><p>docker -d</p><p>docker-compose up -d</p>]]></content>
<categories>
<category> Docker </category>
</categories>
<tags>
<tag> Docker </tag>
<tag> 瞎学 </tag>
</tags>
</entry>
<entry>
<title>Python列表</title>
<link href="/posts/19235/"/>
<url>/posts/19235/</url>
<content type="html"><![CDATA[<p><img src="/Python%E5%88%97%E8%A1%A8/image-20211006000059728.webp" alt="image-20211006000059728"></p><p>pycharm Code 配置</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : ${DATE} ${TIME}</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : ${NAME}.py</span></span><br><span class="line"><span class="comment"># @Sofware : ${PRODUCT_NAME}</span></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/3/8 10:46</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : list01.py</span></span><br><span class="line"><span class="comment"># @Sofware : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">翻转:reverse()</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">pop 弹栈 移除列表最后一个,返回值是删除的那个元素</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line">hotpot_list = [<span class="string">'海底捞'</span>, <span class="string">'呷哺'</span>, <span class="string">'张亮麻辣烫'</span>, <span class="string">'热辣一号'</span>, <span class="string">'宽板凳'</span>]</span><br><span class="line">hotpot_list.append(<span class="string">'张亮麻辣烫'</span>)</span><br><span class="line"><span class="built_in">print</span>(hotpot_list)</span><br><span class="line"><span class="built_in">print</span>(hotpot_list.remove(<span class="string">'张亮麻辣烫'</span>))</span><br><span class="line"><span class="built_in">print</span>(hotpot_list)</span><br><span class="line"><span class="built_in">print</span>(hotpot_list.pop())</span><br><span class="line"><span class="built_in">print</span>(hotpot_list)</span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">sorted(lits)</span></span><br><span class="line"><span class="string">list.sort()</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line">l = [<span class="number">4</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">68</span>, <span class="number">9</span>]</span><br><span class="line">l.sort(reverse=<span class="literal">False</span>) <span class="comment"># 升序</span></span><br><span class="line"><span class="built_in">print</span>(l)</span><br><span class="line"></span><br><span class="line"><span class="comment"># # 声明</span></span><br><span class="line">names = [<span class="string">'jack'</span>, <span class="string">'tom'</span>, <span class="string">'lucy'</span>, <span class="string">'superman'</span>, <span class="string">'ironman'</span>]</span><br><span class="line">computer_brands = []</span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 地址</span></span><br><span class="line"><span class="comment"># print(id(names))</span></span><br><span class="line"><span class="comment"># print(id(computer_brands))</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 元素的获取: 下标 索引</span></span><br><span class="line"><span class="comment"># print(names[0])</span></span><br><span class="line"><span class="comment"># print(names[1])</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 获取最后一个元素</span></span><br><span class="line"><span class="comment"># print(names[-1])</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 结合循环</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'___________________'</span>)</span><br><span class="line"><span class="keyword">for</span> name <span class="keyword">in</span> names:</span><br><span class="line"> <span class="built_in">print</span>(name)</span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 查询names里面有没有保存超人</span></span><br><span class="line"><span class="comment"># print('________________')</span></span><br><span class="line"><span class="comment"># for name in names:</span></span><br><span class="line"><span class="comment"># if name == 'superman':</span></span><br><span class="line"><span class="comment"># print(name)</span></span><br><span class="line"><span class="comment"># break</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># print('没有超人在里面')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 简便</span></span><br><span class="line"><span class="comment"># if 'superman' in names: # 判断有没有</span></span><br><span class="line"><span class="comment"># print('有超人在里面')</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># print('没有超人在里面')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/3/8 11:07</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : list02.py</span></span><br><span class="line"><span class="comment"># @Sofware : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 增删改</span></span><br><span class="line">brands = [<span class="string">'hp'</span>, <span class="string">'dell'</span>, <span class="string">'thinkpad'</span>, <span class="string">'支持华为'</span>, <span class="string">'lenovo'</span>, <span class="string">'mac'</span>, <span class="string">'神舟'</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 改</span></span><br><span class="line"><span class="built_in">print</span>(brands)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(brands[-<span class="number">1</span>])</span><br><span class="line">brands[-<span class="number">1</span>] = <span class="string">'HASEE'</span> <span class="comment"># 赋值 步骤:1.找到(使用下标)</span></span><br><span class="line"><span class="built_in">print</span>(brands)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'+++++++++++'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># HUAWEI</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># for brand in brands:</span></span><br><span class="line"><span class="comment"># if '华为' in brand:</span></span><br><span class="line"><span class="comment"># brands[brands.index('华为')] = 'HUAWEI'</span></span><br><span class="line"><span class="comment"># print(brands)</span></span><br><span class="line"><span class="comment"># break</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(brands)):</span><br><span class="line"> <span class="keyword">if</span> <span class="string">'华为'</span> <span class="keyword">in</span> brands[i]:</span><br><span class="line"> brands[i] = <span class="string">'HUAWEI'</span></span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"><span class="built_in">print</span>(brands)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除 del</span></span><br><span class="line"><span class="comment"># del brands[2]</span></span><br><span class="line"><span class="comment"># print(brands)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除 只要是hp,mac 都要删除</span></span><br><span class="line"><span class="comment"># for i in range(len(brands)):</span></span><br><span class="line"><span class="comment"># if 'hp' in brands[i]:</span></span><br><span class="line"><span class="comment"># del brands[i]</span></span><br><span class="line"><span class="comment"># if 'mac' in brands[i]:</span></span><br><span class="line"><span class="comment"># del brands[i]</span></span><br><span class="line"><span class="comment"># break</span></span><br><span class="line"><span class="comment"># print(brands)</span></span><br><span class="line"></span><br><span class="line">l = <span class="built_in">len</span>(brands)</span><br><span class="line">i = <span class="number">0</span></span><br><span class="line"><span class="keyword">while</span> i < l:</span><br><span class="line"> <span class="keyword">if</span> <span class="string">'hp'</span> <span class="keyword">in</span> brands[i] <span class="keyword">or</span> <span class="string">'mac'</span> <span class="keyword">in</span> brands[i]:</span><br><span class="line"> <span class="keyword">del</span> brands[i]</span><br><span class="line"> l -= <span class="number">1</span></span><br><span class="line"> <span class="keyword">continue</span></span><br><span class="line"> i += <span class="number">1</span></span><br><span class="line"><span class="built_in">print</span>(brands)</span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">they are students</span></span><br><span class="line"><span class="string">yews</span></span><br><span class="line"><span class="string">['hello', 'good', 'apple', 'world', 'digit', 'alpha']</span></span><br><span class="line"><span class="string">hello</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line">name_list = [<span class="string">'hello'</span>, <span class="string">'good'</span>, <span class="string">'apple'</span>, <span class="string">'world'</span>, <span class="string">'digit'</span>, <span class="string">'alpha'</span>]</span><br><span class="line"></span><br><span class="line">result = <span class="built_in">input</span>(<span class="string">'请输入要删除的元素:'</span>)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(name_list)):</span><br><span class="line"> <span class="keyword">if</span> result <span class="keyword">in</span> name_list[i]:</span><br><span class="line"> <span class="keyword">del</span> name_list[i]</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"><span class="built_in">print</span>(name_list)</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/3/8 12:50</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : list04.py</span></span><br><span class="line"><span class="comment"># @Sofware : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># s = 'abcdefg' s[2:5]</span></span><br><span class="line"></span><br><span class="line">list1 = [<span class="string">'杨超越'</span>, <span class="string">'迪丽热巴'</span>, <span class="string">'佟丽娅'</span>, <span class="string">'杨幂'</span>, <span class="string">'赵丽颖'</span>, <span class="string">'刘亦菲'</span>, <span class="string">'黑嘉嘉'</span>, <span class="number">100</span>, <span class="number">99.99</span>]</span><br><span class="line"><span class="built_in">print</span>(list1)</span><br><span class="line"><span class="built_in">print</span>(list1[<span class="number">3</span>])</span><br><span class="line"><span class="built_in">print</span>(list1.index(<span class="string">'杨幂'</span>))</span><br><span class="line"><span class="comment"># 切片</span></span><br><span class="line"><span class="built_in">print</span>(list1[<span class="number">3</span>:<span class="number">6</span>])</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(list1[-<span class="number">3</span>:-<span class="number">1</span>])</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(list1[-<span class="number">1</span>::-<span class="number">1</span>])</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/3/8 13:15</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : list05.py</span></span><br><span class="line"><span class="comment"># @Sofware : PyCharm</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # list列表添加</span></span><br><span class="line"><span class="comment"># # 临时小数据库</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 创建一个空列表</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># girls = []</span></span><br><span class="line"><span class="comment"># # quit 表示退出</span></span><br><span class="line"><span class="comment"># # while True:</span></span><br><span class="line"><span class="comment"># # name = input('请输入你心目中的美女的名字:')</span></span><br><span class="line"><span class="comment"># # if name == 'quit':</span></span><br><span class="line"><span class="comment"># # break</span></span><br><span class="line"><span class="comment"># # # 列表的函数使用:append extends insert</span></span><br><span class="line"><span class="comment"># # # append() 末尾追加</span></span><br><span class="line"><span class="comment"># # girls.append(name)</span></span><br><span class="line"><span class="comment"># #</span></span><br><span class="line"><span class="comment"># # print(girls)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # extend 类似列表的合并</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># names = ['黑嘉嘉', '孙俪', '巩俐', '王丽坤']</span></span><br><span class="line"><span class="comment"># # name = input('请输入你心目中的美女的名字:')</span></span><br><span class="line"><span class="comment"># girls.extend(names)</span></span><br><span class="line"><span class="comment"># print(girls)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 符号 + 也可以用于列表的合并</span></span><br><span class="line"><span class="comment"># girls = girls+names</span></span><br><span class="line"><span class="comment"># print(girls)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # insert 插入</span></span><br><span class="line"><span class="comment"># # ['杨幂', '黑嘉嘉', '孙俪', '巩俐', '王丽坤']</span></span><br><span class="line"><span class="comment"># # 0 1 2 3 4</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # append 末尾添加</span></span><br><span class="line"><span class="comment"># # insert 指定位置添加</span></span><br><span class="line"><span class="comment"># # extend 一次添加多个元素</span></span><br><span class="line"><span class="comment"># girls.insert(1,'刘涛')</span></span><br><span class="line"><span class="comment"># print(girls)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># print('--------------')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 产生10个随机数,将其保存在列表</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># import random</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># random_list = []</span></span><br><span class="line"><span class="comment"># # for i in range(10):</span></span><br><span class="line"><span class="comment"># # ran = random.randint(1, 99)</span></span><br><span class="line"><span class="comment"># # random_list.append(ran)</span></span><br><span class="line"><span class="comment"># # print(random_list)</span></span><br><span class="line"><span class="comment"># # random_list = []</span></span><br><span class="line"><span class="comment"># # for i in range(10):</span></span><br><span class="line"><span class="comment"># # ran = random.randint(1, 10)</span></span><br><span class="line"><span class="comment"># # # if ran in random_list:</span></span><br><span class="line"><span class="comment"># # # pass</span></span><br><span class="line"><span class="comment"># # # else:</span></span><br><span class="line"><span class="comment"># # # random_list.append(ran)</span></span><br><span class="line"><span class="comment"># # if ran not in random_list:</span></span><br><span class="line"><span class="comment"># # random_list.append(ran)</span></span><br><span class="line"><span class="comment"># # print(random_list)</span></span><br><span class="line"><span class="comment"># i = 0</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># while i < 10:</span></span><br><span class="line"><span class="comment"># ran = random.randint(0, 99)</span></span><br><span class="line"><span class="comment"># if ran not in random_list:</span></span><br><span class="line"><span class="comment"># random_list.append(ran)</span></span><br><span class="line"><span class="comment"># i += 1</span></span><br><span class="line"><span class="comment"># print(random_list)</span></span><br><span class="line"><span class="comment"># # 找出列表最大值</span></span><br><span class="line"><span class="comment"># print(max(random_list))</span></span><br><span class="line"><span class="comment"># print(min(random_list))</span></span><br><span class="line"><span class="comment"># print('--------------求最大值-------------')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># mvalue = random_list[0]</span></span><br><span class="line"><span class="comment"># minvalue = random_list[0]</span></span><br><span class="line"><span class="comment"># for i in random_list:</span></span><br><span class="line"><span class="comment"># if i > mvalue:</span></span><br><span class="line"><span class="comment"># mvalue = i</span></span><br><span class="line"><span class="comment"># if i < minvalue:</span></span><br><span class="line"><span class="comment"># minvalue = i</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># print('最大值:%s,最小值:%s' % (mvalue, minvalue))</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># print('求和')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># print('计算机求和', sum(random_list))</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 声明累加的变量和</span></span><br><span class="line"><span class="comment"># sum = 0</span></span><br><span class="line"><span class="comment"># for i in random_list:</span></span><br><span class="line"><span class="comment"># sum *= i</span></span><br><span class="line"><span class="comment"># print(sum)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 排序:sort排序</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># print(sorted(random_list, reverse=True))</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'-------自己写排序------'</span>)</span><br><span class="line"><span class="comment"># 自己写排序</span></span><br><span class="line">num_list1 = [<span class="number">1</span>, <span class="number">16</span>, <span class="number">3</span>, <span class="number">18</span>, <span class="number">9</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>]</span><br><span class="line">num_list2 = []</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> num_list1:</span><br><span class="line"> <span class="keyword">for</span> b <span class="keyword">in</span> num_list1[i + <span class="number">1</span>]:</span><br><span class="line"> <span class="keyword">if</span> i > b:</span><br><span class="line"> num_list2.append(num_list1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># while i < len(num_list1):</span></span><br><span class="line"><span class="comment"># if num_list1[i] < num_list1[i+1]:</span></span><br><span class="line"><span class="comment"># num_list2.append(num_list1[i])</span></span><br><span class="line"><span class="comment"># i += 1</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># num_list2.append(num_list1[i])</span></span><br><span class="line"><span class="comment"># i += 1</span></span><br><span class="line"><span class="comment"># print(num_list2)</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/3/14 12:29</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : list_算法.py</span></span><br><span class="line"><span class="comment"># @Sofware : PyCharm</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">str()</span></span><br><span class="line"><span class="string">int()</span></span><br><span class="line"><span class="string">len()</span></span><br><span class="line"><span class="string">list()</span></span><br><span class="line"><span class="string">sorted()</span></span><br><span class="line"><span class="string">print()</span></span><br><span class="line"><span class="string">input()</span></span><br><span class="line"><span class="string">enumerate():函数用于将一个可遍历的数据对象(如列表,元组或字符串)组合微一个案例</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="comment"># l1 = ['a', 'abc', 'jk', 'opop']</span></span><br><span class="line"><span class="comment"># for index, value in enumerate(l1):</span></span><br><span class="line"><span class="comment"># print(index, value)</span></span><br><span class="line">numbers = [<span class="number">2</span>, <span class="number">6</span>, <span class="number">9</span>, <span class="number">8</span>, <span class="number">7</span>, <span class="number">96</span>, <span class="number">3</span>, <span class="number">5</span>]</span><br><span class="line"><span class="comment"># print(numbers.sort(reverse=True))</span></span><br><span class="line"><span class="comment"># print(numbers)</span></span><br><span class="line"><span class="comment"># print(sorted(numbers))</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 自定义排序方法</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(numbers)):</span><br><span class="line"> <span class="comment"># number[i] = 5</span></span><br><span class="line"> <span class="keyword">for</span> j <span class="keyword">in</span> <span class="built_in">range</span>(i + <span class="number">1</span>, <span class="built_in">len</span>(numbers)):</span><br><span class="line"> <span class="keyword">if</span> numbers[i] > numbers[j]:</span><br><span class="line"> <span class="comment"># 快速交换位置</span></span><br><span class="line"> numbers[i], numbers[j] = numbers[j], numbers[i]</span><br><span class="line"><span class="built_in">print</span>(numbers)</span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> Python </tag>
</tags>
</entry>
<entry>
<title>python函数调用</title>
<link href="/posts/28879/"/>
<url>/posts/28879/</url>
<content type="html"><![CDATA[<h1 id="函数嵌套调用"><a href="#函数嵌套调用" class="headerlink" title="函数嵌套调用"></a>函数嵌套调用</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">a</span>():</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'AAAAAAA'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">b</span>():</span><br><span class="line"> <span class="comment"># 调用函数a</span></span><br><span class="line"> a()</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'BBBBBBB'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">c</span>():</span><br><span class="line"> b()</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'CCCCCCC'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">c()</span><br></pre></td></tr></table></figure><h1 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义函数:随机数产生</span></span><br><span class="line"><span class="comment"># 自动格式化Ctrl+Alt+L</span></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">generate_random</span>():</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">20</span>):</span><br><span class="line"> ran = random.randint(<span class="number">1</span>, <span class="number">20</span>)</span><br><span class="line"> <span class="built_in">print</span>(ran)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(generate_random)</span><br><span class="line"><span class="comment"># <function generate_random at 0x000002755D17E040></span></span><br><span class="line"><span class="comment"># 函数名 函数 在 内存地址</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用:函数名() 找到函数并执行函数体</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'------1------'</span>)</span><br><span class="line">generate_random()</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'------2------'</span>)</span><br><span class="line">generate_random()</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 函数:带参数的</span></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">generate_random</span>(<span class="params">ran, ranmin, ranmax</span>): <span class="comment"># 形参,用来占位的参数,这里是ran,ranmin,ranmax</span></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(ran):</span><br><span class="line"> ran1 = random.randint(ranmin, ranmax)</span><br><span class="line"> <span class="built_in">print</span>(ran1)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">num = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">'请输入要产生的随机数个数:'</span>))</span><br><span class="line">num2 = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">'请输入要产生的随机数范围最小值:'</span>))</span><br><span class="line">num3 = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">'请输入要产生的随机数范围最大值:'</span>))</span><br><span class="line">generate_random(num, num2, num3) <span class="comment"># 实参 实际参数,具体的值</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 求加法的函数</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>(<span class="params">a, b</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'加起来等于'</span>, a + b)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">a = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">'输入第一个数:'</span>))</span><br><span class="line">b = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">'输入第二个数:'</span>))</span><br><span class="line">add(a, b)</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">定义一个登录的函数,参数是:username,password</span></span><br><span class="line"><span class="string">函数体:</span></span><br><span class="line"><span class="string">判断参数传过来的是username,password是否正确,如果正确则登录成功,否则打印失败</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 函数的定义</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">login</span>(<span class="params">username, password</span>):</span><br><span class="line"> uname = <span class="string">'admin123'</span></span><br><span class="line"> pwd = <span class="string">'112233'</span></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">3</span>):</span><br><span class="line"> <span class="keyword">if</span> username == uname <span class="keyword">and</span> password == pwd:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'输入正确'</span>)</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'输入错误'</span>)</span><br><span class="line"> username = <span class="built_in">str</span>(<span class="built_in">input</span>(<span class="string">'输入用户名:'</span>))</span><br><span class="line"> password = <span class="built_in">str</span>(<span class="built_in">input</span>(<span class="string">'输入密码:'</span>))</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'账户锁定!'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">username = <span class="built_in">str</span>(<span class="built_in">input</span>(<span class="string">'输入用户名:'</span>))</span><br><span class="line">password = <span class="built_in">str</span>(<span class="built_in">input</span>(<span class="string">'输入密码:'</span>))</span><br><span class="line">login(username, password)</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 找出列表的最大值</span></span><br><span class="line"><span class="comment"># 自己封装一个求最大值函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">max</span>(<span class="params">iterable</span>):</span><br><span class="line"> <span class="built_in">max</span> = iterable[<span class="number">0</span>]</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> iterable:</span><br><span class="line"> <span class="keyword">if</span> i > <span class="built_in">max</span>:</span><br><span class="line"> <span class="built_in">max</span> = i</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'最大值是:'</span>, <span class="built_in">max</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用:测试是否能找出最大值</span></span><br><span class="line">list1 = [<span class="number">4</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line"><span class="built_in">max</span>(list1)</span><br><span class="line"></span><br><span class="line">tuple1 = (<span class="number">3</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">5</span>)</span><br><span class="line"><span class="built_in">max</span>(tuple1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># sort min reverse</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">type</span>(tuple1)) <span class="comment"># 查看是什么类型</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 判断是不是什么类型:isinstance</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">isinstance</span>(<span class="number">2</span>, <span class="built_in">int</span>)) <span class="comment"># 返回值是False,True</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">isinstance</span>(tuple1, <span class="built_in">tuple</span>))</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 可变参数</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># def add(*args):</span></span><br><span class="line"><span class="comment"># print(args)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># add(1)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义方式</span></span><br><span class="line"><span class="comment"># def add(*args):</span></span><br><span class="line"><span class="comment"># # *args的用法:当传入的参数个数未知,且不需要知道参数名称时。</span></span><br><span class="line"><span class="comment"># # print(args)</span></span><br><span class="line"><span class="comment"># sum = 0</span></span><br><span class="line"><span class="comment"># if len(args) > 0:</span></span><br><span class="line"><span class="comment"># for i in args:</span></span><br><span class="line"><span class="comment"># sum += i</span></span><br><span class="line"><span class="comment"># print('累加的和是sum:', sum)</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># print('没有元素可计算,sum:', sum)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># add()</span></span><br><span class="line"><span class="comment"># add(1)</span></span><br><span class="line"><span class="comment"># add(1, 2)</span></span><br><span class="line"><span class="comment"># add(1, 2, 3, 4)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># xxx计算出来的累加和是:xxx</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>(<span class="params">name, age, *args</span>):</span><br><span class="line"> <span class="comment"># **args的用法:当传入的参数个数未知,但需要知道参数的名称时(立马想到了字典,即键值对)</span></span><br><span class="line"> <span class="built_in">print</span>(args, name)</span><br><span class="line"> <span class="built_in">sum</span> = <span class="number">0</span></span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">len</span>(args) > <span class="number">0</span>:</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> args:</span><br><span class="line"> <span class="built_in">sum</span> += i</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}累加的和:sum:{}'</span>.<span class="built_in">format</span>(name, <span class="built_in">sum</span>))</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'没有元素可计算:sum:'</span>, <span class="built_in">sum</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用</span></span><br><span class="line">add(<span class="string">'菲菲'</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>)</span><br><span class="line"><span class="comment"># 注意:可变参数必须放在后面:name,*args</span></span><br><span class="line">add(<span class="string">'我'</span>, <span class="number">10</span>)</span><br><span class="line"><span class="comment"># name,age, *args='我',10</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># # 可变参数 + 关键字参数</span></span><br><span class="line"><span class="comment"># # 关键字参数:key=value 键值对</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def add(a, b=10): # 关键字参数</span></span><br><span class="line"><span class="comment"># result = a + b</span></span><br><span class="line"><span class="comment"># print(result)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 调用</span></span><br><span class="line"><span class="comment"># add(5)</span></span><br><span class="line"><span class="comment"># add(4, 7) # a=4,b=7 覆盖默认值</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def add1(a, b=10, c=4):</span></span><br><span class="line"><span class="comment"># result = a + b + c</span></span><br><span class="line"><span class="comment"># print(result)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># add1(1)</span></span><br><span class="line"><span class="comment"># add1(1, 5) # 给b赋值</span></span><br><span class="line"><span class="comment"># add1(1, c=6) # 直接将6赋值给c</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 函数:打印每位同学姓名和年龄</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line">studens = {</span><br><span class="line"> <span class="string">'001'</span>: (<span class="string">'蔡徐坤'</span>, <span class="number">20</span>),</span><br><span class="line"> <span class="string">'002'</span>: (<span class="string">'王源'</span>, <span class="number">18</span>),</span><br><span class="line"> <span class="string">'003'</span>: (<span class="string">'王俊凯'</span>, <span class="number">21</span>),</span><br><span class="line"> <span class="string">'004'</span>: (<span class="string">'易烊千玺'</span>, <span class="number">19</span>)</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">print_boy</span>(<span class="params">name, **persons</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}喜欢吃肉肉'</span>.<span class="built_in">format</span>(name))</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">isinstance</span>(persons, <span class="built_in">dict</span>): <span class="comment"># 判断是不是什么类型:isinstance,这里判断是不是person是不是字典类型</span></span><br><span class="line"> values = persons.values() <span class="comment"># 如果是字典,那么使用values接受字典里面的键值对</span></span><br><span class="line"> <span class="comment"># print(values) # 输出字典</span></span><br><span class="line"> <span class="keyword">for</span> name, age <span class="keyword">in</span> values: <span class="comment"># 使用name和age接受values中的键值对</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}的年龄是{}'</span>.<span class="built_in">format</span>(name, age)) <span class="comment"># 循环输出字典中的元素</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">print_boy(<span class="string">'健健'</span>, **studens)</span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def func(**key):</span></span><br><span class="line"><span class="comment"># print(key)</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># func(a=1, b=1, c=1)</span></span><br><span class="line"><span class="comment"># dict1 = {'001': 'python', '002': 'java', '004': 'c++'}</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># func(**dict1)</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"""</span></span><br><span class="line"><span class="string">使用函数的时候:</span></span><br><span class="line"><span class="string">def aa(*kwargs):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string">"""</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">aa</span>(<span class="params">**kwargs</span>): <span class="comment"># 只要在定义函数的时候加入**就是装包</span></span><br><span class="line"> <span class="built_in">print</span>(kwargs)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">aa(a=<span class="number">1</span>, b=<span class="string">'hello'</span>, c=<span class="string">'tom'</span>) <span class="comment"># 装包成字典</span></span><br><span class="line"><span class="comment"># 如果在开发的时候,已知一个字典</span></span><br><span class="line">dict1 = {<span class="string">'a'</span>: <span class="number">1</span>, <span class="string">'b'</span>: <span class="string">'hello'</span>, <span class="string">'c'</span>: <span class="string">'tom'</span>}</span><br><span class="line">aa(**dict1) <span class="comment"># 只要在调用函数的时候加入**就是拆包</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">bb</span>(<span class="params">a, b, *c, **d</span>):</span><br><span class="line"> <span class="built_in">print</span>(a, b, c, d)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">bb(<span class="number">1</span>, <span class="number">2</span>) <span class="comment"># 打印两个参数 1 2 一个元组,一个字典</span></span><br><span class="line">bb(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>) <span class="comment"># 打印 ,1 2 (3,4) {}</span></span><br><span class="line">bb(<span class="number">1</span>, <span class="number">2</span>, x=<span class="number">100</span>, y=<span class="number">200</span>) <span class="comment"># 1 2 () {'x':100,'y':200}</span></span><br><span class="line">bb(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, x=<span class="number">100</span>) <span class="comment"># 1 2 (3,) {'x':100}</span></span><br><span class="line">bb(<span class="number">1</span>, <span class="number">2</span>, x=<span class="number">100</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>(<span class="params">a, *args</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}{}'</span>.<span class="built_in">format</span>(a, args))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func(<span class="number">2</span>, [<span class="number">1</span>, <span class="number">23</span>, <span class="number">4</span>], <span class="string">'fad'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func2</span>(<span class="params">a, b=<span class="number">10</span>, c=<span class="number">3</span>, **kwargs</span>):</span><br><span class="line"> <span class="built_in">print</span>(a, b, c, kwargs)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func2(<span class="number">1</span>) <span class="comment"># 1 10 3 {} 给默认值所以可传入一个参数</span></span><br><span class="line">func2(<span class="number">2</span>, b=<span class="number">11</span>) <span class="comment"># 2 11 3 {}</span></span><br><span class="line">func2(<span class="number">3</span>, c=<span class="number">5</span>, b=<span class="number">7</span>, x=<span class="number">1</span>, y=<span class="number">2</span>) <span class="comment"># 3 7 5 {'x': 1, 'y': 2},传值的时候可以打乱位置</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func3</span>(<span class="params">a, *args, **kwargs</span>):</span><br><span class="line"> <span class="built_in">print</span>(a, args, kwargs)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">t = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br><span class="line">func3(<span class="number">1</span>, t) <span class="comment"># 1 ((1, 2, 3, 4),) {}</span></span><br><span class="line"></span><br><span class="line">l = [<span class="number">2</span>, <span class="number">5</span>, <span class="number">8</span>]</span><br><span class="line">func3(<span class="number">1</span>, l, c=<span class="number">9</span>, b=<span class="number">6</span>) <span class="comment"># 1 ([2, 5, 8],) {'c': 9, 'b': 6}</span></span><br><span class="line"></span><br><span class="line">dict1 = {<span class="string">'1'</span>: <span class="string">'a'</span>, <span class="string">'2'</span>: <span class="string">'b'</span>, <span class="string">'3'</span>: <span class="string">'c'</span>}</span><br><span class="line">func3(<span class="number">1</span>, *l, **dict1) <span class="comment"># 1 (2, 5, 8) {'1': 'a', '2': 'b', '3': 'c'} # *拆列表 **拆字典</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func1</span>(<span class="params">name, *args</span>):</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">len</span>(args) > <span class="number">0</span>:</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> args: <span class="comment"># 这里args是元组</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}学过了{}'</span>.<span class="built_in">format</span>(name, i))</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'没有学过任何语言'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">courses = [<span class="string">'html'</span>, <span class="string">'mysql'</span>, <span class="string">'c++'</span>, <span class="string">'python'</span>]</span><br><span class="line"><span class="comment"># 调用函数</span></span><br><span class="line">func1(<span class="string">'健健'</span>, *courses) <span class="comment"># *拆列表(拆包)</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">无参数函数:</span></span><br><span class="line"><span class="string"> def func():</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func() ---调用</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string"> 有参数函数</span></span><br><span class="line"><span class="string">1.普通参数</span></span><br><span class="line"><span class="string"> def func(name,age):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func('aa',19) --->形参与实参的个数要一致</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">2.可变参数:</span></span><br><span class="line"><span class="string"> A.</span></span><br><span class="line"><span class="string"> def func(*args):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func() ----> 函数调用时,实参的个数可以没有,也可以多个,*不能是关键字参数</span></span><br><span class="line"><span class="string"> func(4) </span></span><br><span class="line"><span class="string"> func(5,'h')</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> B.</span></span><br><span class="line"><span class="string"> def func(**kwargs):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func(a=1,b=2)----> 函数调用时,实参的个数可以没有,也可以多个,**必须是关键字参数</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string"> C.</span></span><br><span class="line"><span class="string"> def func(*args,**kwargs):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> list1 = [1,2,3,4,5]</span></span><br><span class="line"><span class="string"> dict1 = {'1':'a','2':'b','3':'c'}</span></span><br><span class="line"><span class="string"> func(*list1,**dict1) 拆包func(1,2,3,4,5,'1':'a','2':'b','3':'c')</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string"> D.混用</span></span><br><span class="line"><span class="string"> def func(name,*args,*8kwargs):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func('tom') ---->必须赋值</span></span><br><span class="line"><span class="string">3.默认值</span></span><br><span class="line"><span class="string"> def func(name,age=18):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func('tom') ----> tom 18</span></span><br><span class="line"><span class="string"> func('tom',age = 20) ---->关键字赋值</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 返回值:将函数中运算的结果通过return关键字‘扔’出来</span></span><br><span class="line"><span class="comment"># 如果使用print这个值就不会出来只会在def内部打印,外面是接受不到的</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># def add(a, b): # 加载</span></span><br><span class="line"><span class="comment"># result = a + b</span></span><br><span class="line"><span class="comment"># # print(result)</span></span><br><span class="line"><span class="comment"># return '这都不会', result</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 调用函数</span></span><br><span class="line"><span class="comment"># # add(2, 4) 这里只能打印函数中的print内容不会接收return</span></span><br><span class="line"><span class="comment"># x, y = add(2, 4)</span></span><br><span class="line"><span class="comment"># print(x, y)</span></span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">return 返回值</span></span><br><span class="line"><span class="string">1.return后面可以是一个参数 接收的时候x=add(1,2)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">2.return后面也可以是多个参数,如果是多个参数则底层会将多个参数先放在一个元组中,</span></span><br><span class="line"><span class="string"> 将元组作为整体返回 x=add(1,2) x--> (1,2,3)</span></span><br><span class="line"><span class="string">3.接收的时候也可以是多个:return 'hello','world' x,y = ('hello','world') --->x = 'hello' y = 'world'</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">加入购物车</span></span><br><span class="line"><span class="string">判断用户是否登录,如果登录,成功加入购物车,否则提示请登录之后添加</span></span><br><span class="line"><span class="string">成功 True 不成功 False</span></span><br><span class="line"><span class="string">def add_shoppingcart(goodsName):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string">def login(username,password):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">调用</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="comment"># isLogin = False</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def add_shoppingcart(goodsName):</span></span><br><span class="line"><span class="comment"># if isLogin and goodsName:</span></span><br><span class="line"><span class="comment"># print('成功将{}加入到购物车!!!'.format(goodsName))</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># print('用户没有登录!或者没有添加如何商品请登录之后添加')</span></span><br><span class="line"><span class="comment"># return isLogin</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def login(username, password):</span></span><br><span class="line"><span class="comment"># if username == 'zhao' and password == '123456':</span></span><br><span class="line"><span class="comment"># return True</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># print('登录失败用户名或者密码错误')</span></span><br><span class="line"><span class="comment"># return False</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 调用函数</span></span><br><span class="line"><span class="comment"># while True:</span></span><br><span class="line"><span class="comment"># username = input('输入用户名:')</span></span><br><span class="line"><span class="comment"># password = input('输入密码:')</span></span><br><span class="line"><span class="comment"># isLogin = login(username, password)</span></span><br><span class="line"><span class="comment"># y = add_shoppingcart('榴莲')</span></span><br><span class="line"><span class="comment"># if isLogin:</span></span><br><span class="line"><span class="comment"># break</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">isLogin = <span class="literal">False</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add_shoppingcart</span>(<span class="params">goodsName</span>):</span><br><span class="line"> <span class="keyword">global</span> isLogin</span><br><span class="line"> <span class="keyword">if</span> isLogin:</span><br><span class="line"> <span class="keyword">if</span> goodsName:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'成功将{}加入到购物车!!!'</span>.<span class="built_in">format</span>(goodsName))</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'没有选中任何商品'</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> answer = <span class="built_in">input</span>(<span class="string">'没有登录!是否登录(yes/no)'</span>)</span><br><span class="line"> <span class="keyword">if</span> answer == <span class="string">'yes'</span>:</span><br><span class="line"> <span class="comment"># 登录</span></span><br><span class="line"> username = <span class="built_in">input</span>(<span class="string">'输入用户名:'</span>)</span><br><span class="line"> password = <span class="built_in">input</span>(<span class="string">'输入密码:'</span>)</span><br><span class="line"> <span class="comment"># 调用login</span></span><br><span class="line"> isLogin = login(username, password) <span class="comment"># 在一个函数中调用另外一个函数 将login函数中return的值赋值给islogin</span></span><br><span class="line"> <span class="built_in">print</span>(isLogin)</span><br><span class="line"> <span class="keyword">return</span> isLogin</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'很遗憾,不能添加如何商品'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> isLogin</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">login</span>(<span class="params">username, password</span>):</span><br><span class="line"> <span class="keyword">if</span> username == <span class="string">'zhao'</span> <span class="keyword">and</span> password == <span class="string">'123456'</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用函数</span></span><br><span class="line">username = <span class="built_in">input</span>(<span class="string">"请输入用户名:"</span>)</span><br><span class="line">password = <span class="built_in">input</span>(<span class="string">"请输入密码:"</span>)</span><br><span class="line"><span class="comment"># shoping = input("请输入你要买的商品:")</span></span><br><span class="line"><span class="comment"># add_shoppingcart(shoping)</span></span><br><span class="line">isLogin = login(username, password)</span><br><span class="line">add_shoppingcart(<span class="string">"isLogin"</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># # 函数综合应用</span></span><br><span class="line"><span class="comment"># '''</span></span><br><span class="line"><span class="comment"># 用户登录</span></span><br><span class="line"><span class="comment"># 输入密码</span></span><br><span class="line"><span class="comment"># 输入验证码 --->封装函数</span></span><br><span class="line"><span class="comment"># '''</span></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># 我写的</span></span><br><span class="line"><span class="comment"># def yzm():</span></span><br><span class="line"><span class="comment"># # 生成验证码</span></span><br><span class="line"><span class="comment"># code = ''</span></span><br><span class="line"><span class="comment"># ymzs = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0987654321'</span></span><br><span class="line"><span class="comment"># for i in range(4):</span></span><br><span class="line"><span class="comment"># a = random.randint(0, len(ymzs) - 1)</span></span><br><span class="line"><span class="comment"># code += ymzs[a]</span></span><br><span class="line"><span class="comment"># print('验证码是', code)</span></span><br><span class="line"><span class="comment"># return code</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def yz(codes):</span></span><br><span class="line"><span class="comment"># # 判断验证码</span></span><br><span class="line"><span class="comment"># if codes == x:</span></span><br><span class="line"><span class="comment"># return '验证码正确'</span></span><br><span class="line"><span class="comment"># # print('验证码正确')</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># return '验证码错误'</span></span><br><span class="line"><span class="comment"># # print('验证码错误')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def login(username, password, codes, x):</span></span><br><span class="line"><span class="comment"># if username == '1' and password == '1' and codes == x.lower():</span></span><br><span class="line"><span class="comment"># print('登录成功')</span></span><br><span class="line"><span class="comment"># if codes != x.lower():</span></span><br><span class="line"><span class="comment"># print('登录失败,验证码错误')</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># print('登录失败,密码或用户名错误')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># x = yzm()</span></span><br><span class="line"><span class="comment"># username = input('请输入用户名:')</span></span><br><span class="line"><span class="comment"># password = input('请输入密码:')</span></span><br><span class="line"><span class="comment"># codes = input('请输入验证码')</span></span><br><span class="line"><span class="comment"># cdoes = codes.lower()</span></span><br><span class="line"><span class="comment"># login(username, password, codes, x)</span></span><br><span class="line"><span class="comment"># if codes == x.lower():</span></span><br><span class="line"><span class="comment"># print('验证码正确')</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># print('登录失败,验证码错误')</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义一个生成验证码函数 老师写的</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">generate_chekcode</span>(<span class="params">n</span>):</span><br><span class="line"> code = <span class="string">''</span></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line"> str1 = <span class="string">'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0987654321'</span></span><br><span class="line"> a = random.randint(<span class="number">0</span>, <span class="built_in">len</span>(str1) - <span class="number">1</span>)</span><br><span class="line"> code += str1[a]</span><br><span class="line"> <span class="keyword">return</span> code</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义登录函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">login</span>():</span><br><span class="line"> username = <span class="built_in">input</span>(<span class="string">'输入用户名:'</span>)</span><br><span class="line"> password = <span class="built_in">input</span>(<span class="string">'输入密码:'</span>)</span><br><span class="line"> <span class="comment"># 得到一个验证码</span></span><br><span class="line"> code = generate_chekcode(<span class="number">5</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'验证码是:'</span>, code)</span><br><span class="line"> code1 = <span class="built_in">input</span>(<span class="string">'输入验证码:'</span>)</span><br><span class="line"> <span class="comment"># 验证</span></span><br><span class="line"> <span class="keyword">if</span> code1.lower() == code.lower():</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'验证码正确'</span>)</span><br><span class="line"> <span class="keyword">if</span> username == <span class="string">'admin'</span> <span class="keyword">and</span> password == <span class="string">'admin'</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'登录成功'</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'登录失败,用户名或密码有误!!!'</span>)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'验证码不正确'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">login()</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 局部和全局</span></span><br><span class="line"><span class="comment"># 如果修改的变量是全局变量时,在函数中修改时需要加global</span></span><br><span class="line"><span class="comment"># 如果修改的变量是可变的时候,不需要在函数中加global</span></span><br><span class="line">name = <span class="string">'月月'</span></span><br><span class="line">list1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>():</span><br><span class="line"> name = <span class="string">'蕊蕊'</span></span><br><span class="line"> <span class="built_in">print</span>(name)</span><br><span class="line"> <span class="built_in">print</span>(list1)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func1</span>():</span><br><span class="line"> <span class="keyword">global</span> name</span><br><span class="line"> <span class="built_in">print</span>(name)</span><br><span class="line"> name += <span class="string">'真漂亮'</span></span><br><span class="line"> list1.append(<span class="number">8</span>)</span><br><span class="line"> <span class="built_in">print</span>(list1)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func2</span>(): <span class="comment"># 可以使用不同的变量名字来使用global,不要和global起一样的名字</span></span><br><span class="line"> name1 = <span class="string">'lucy'</span></span><br><span class="line"> name1 += <span class="string">'嘻嘻'</span></span><br><span class="line"> <span class="keyword">global</span> name</span><br><span class="line"> <span class="built_in">print</span>(name)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func1()</span><br><span class="line">func()</span><br><span class="line">func2()</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># global 变量的范围</span></span><br><span class="line"><span class="comment"># 局部变量 全局变量</span></span><br><span class="line"><span class="comment"># 声明在函数外层的是全局的,所有函数都可以访问。(不能修改)</span></span><br><span class="line">name = <span class="string">'丽丽'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>():</span><br><span class="line"> <span class="comment"># 函数内部声明的变量,局部变量,局部变量仅限于函数内部使用</span></span><br><span class="line"> s = <span class="string">'abcd'</span></span><br><span class="line"> s += <span class="string">'X'</span></span><br><span class="line"> <span class="built_in">print</span>(s, name)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># print(s) 报错</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func1</span>():</span><br><span class="line"> <span class="keyword">global</span> name <span class="comment"># 声明我使用的是全局变量,声明之后就可以修改全局变量,</span></span><br><span class="line"> <span class="comment"># print(s)</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'没有修改的name'</span>, name) <span class="comment"># 报错:函数内部的变量可以随便修改赋值,但是全局变量就不能随便修改</span></span><br><span class="line"> name += <span class="string">'会吃shit'</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'修改后的name'</span>, name)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func2</span>():</span><br><span class="line"> name = <span class="string">'小丽丽'</span></span><br><span class="line"> name += <span class="string">'弹吉他的小美女'</span></span><br><span class="line"> <span class="built_in">print</span>(name)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func2()</span><br><span class="line">func1()</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 内部函数</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">特点:</span></span><br><span class="line"><span class="string">1、可以访问外部函数的变量</span></span><br><span class="line"><span class="string">2、内部函数可以修改外部函数的可变类型的变量比如:list1</span></span><br><span class="line"><span class="string">3、内部函数修改全局的不可变变量时,需要在内部函数声明global 变量名</span></span><br><span class="line"><span class="string"> 内部函数修改外部函数的不可变的变量时,需要在内部函数中声明:nonlocal 变量名</span></span><br><span class="line"><span class="string">4、</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>():</span><br><span class="line"> <span class="comment"># 声明变量</span></span><br><span class="line"> n = <span class="number">100</span> <span class="comment"># 内部函数</span></span><br><span class="line"> list1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>] <span class="comment"># 局部变量</span></span><br><span class="line"></span><br><span class="line"> <span class="comment"># 声明内部函数</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">inner_func</span>():</span><br><span class="line"> <span class="keyword">nonlocal</span> n <span class="comment"># 加nonlocal可以修改内部函数,nonlocal 关键字用于在嵌套函数内部使用变量,其中变量不应属于内部函数。</span></span><br><span class="line"> <span class="comment"># 对list1里面的元素进行加5操作</span></span><br><span class="line"> <span class="keyword">for</span> index, i <span class="keyword">in</span> <span class="built_in">enumerate</span>(list1): <span class="comment"># 枚举将list1中的每个元素取出来</span></span><br><span class="line"> <span class="comment"># index是元素下表,i是list1中的元素</span></span><br><span class="line"> list1[index] = i + n</span><br><span class="line"> list1.sort()</span><br><span class="line"> n += <span class="number">101</span></span><br><span class="line"></span><br><span class="line"> <span class="comment"># 调用一下内部函数才会执行</span></span><br><span class="line"> inner_func()</span><br><span class="line"> <span class="built_in">print</span>(list1)</span><br><span class="line"> <span class="built_in">print</span>(n)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用func</span></span><br><span class="line">func()</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">100</span> <span class="comment"># 全局变量</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>():</span><br><span class="line"> <span class="comment"># 声明变量</span></span><br><span class="line"> b = <span class="number">99</span></span><br><span class="line"></span><br><span class="line"> <span class="comment"># 声明函数</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">inner_func</span>():</span><br><span class="line"> <span class="keyword">nonlocal</span> b <span class="comment"># 修改局部变量</span></span><br><span class="line"> <span class="keyword">global</span> a <span class="comment"># 修改全局变量</span></span><br><span class="line"> c = <span class="number">88</span></span><br><span class="line"> <span class="comment"># 尝试修改</span></span><br><span class="line"> c += <span class="number">12</span></span><br><span class="line"> b += <span class="number">1</span></span><br><span class="line"> <span class="comment"># 尝试打印</span></span><br><span class="line"> a += <span class="number">10</span></span><br><span class="line"> <span class="built_in">print</span>(a, b, c)</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 调用内部函数</span></span><br><span class="line"> inner_func()</span><br><span class="line"> <span class="comment"># 使用locals()内置函数进行查看,可以看到当前函数中声明的内容有哪些</span></span><br><span class="line"> <span class="comment"># locals()是一个字典,key:value</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="built_in">locals</span>())</span><br><span class="line"> <span class="built_in">print</span>(<span class="built_in">globals</span>())</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用函数</span></span><br><span class="line">func()</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 闭包</span></span><br><span class="line"><span class="comment"># 函数中 提出的概念,</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">条件:</span></span><br><span class="line"><span class="string">1.外部函数中定义了内部函数</span></span><br><span class="line"><span class="string">2.外部函数是有返回值</span></span><br><span class="line"><span class="string">3.返回值的是:内部函数</span></span><br><span class="line"><span class="string">4.内部函数引用了外部函数的变量</span></span><br><span class="line"><span class="string">格式:</span></span><br><span class="line"><span class="string">def 外部函数():</span></span><br><span class="line"><span class="string"> ...</span></span><br><span class="line"><span class="string"> def 内部函数():</span></span><br><span class="line"><span class="string"> ...</span></span><br><span class="line"><span class="string"> return 内部函数</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>():</span><br><span class="line"> a = <span class="number">100</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">inner_func</span>():</span><br><span class="line"> b = <span class="number">99</span></span><br><span class="line"> <span class="built_in">print</span>(a, b)</span><br><span class="line"></span><br><span class="line"> <span class="built_in">print</span>(<span class="built_in">locals</span>())</span><br><span class="line"> <span class="keyword">return</span> inner_func <span class="comment"># 不能加括号,如果加括号表示调用,不加括号才是扔出去</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">x = func()</span><br><span class="line"><span class="built_in">print</span>(x)</span><br><span class="line"><span class="comment"># x就是内部函数,x()就表示调用函数</span></span><br><span class="line">x()</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 闭包</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>(<span class="params">a, b</span>):</span><br><span class="line"> c = <span class="number">10</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">inner_func</span>():</span><br><span class="line"> s = a + b + c</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'相加之后的结果是:'</span>, s)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> inner_func</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用func</span></span><br><span class="line">ifunc = func(<span class="number">6</span>, <span class="number">9</span>) <span class="comment"># ifunc就是inner_func ifunc = inner_func</span></span><br><span class="line">ifunc() <span class="comment"># 当你调用func(6,9)---->a=6,b=9---->inner_func()内部函数---->返回内部函数</span></span><br><span class="line"><span class="comment"># (此时返回的内部函数已经记录此时的a和b的值,所以不会收到a,b改变影响</span></span><br><span class="line">ifunc1 = func(<span class="number">2</span>, <span class="number">8</span>)</span><br><span class="line">ifunc1()</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 计数器</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">generate_count</span>():</span><br><span class="line"> container = [<span class="number">0</span>]</span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">add_one</span>():</span><br><span class="line"> container[<span class="number">0</span>] = container[<span class="number">0</span>] + <span class="number">1</span> <span class="comment"># [1]</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'当前是第{}次访问'</span>.<span class="built_in">format</span>(container[<span class="number">0</span>]))</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> add_one</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 内部函数就是一个计数器</span></span><br><span class="line">counter = generate_count()</span><br><span class="line">counter() <span class="comment"># 第一次访问</span></span><br><span class="line">counter() <span class="comment"># 第二次访问</span></span><br><span class="line">counter() <span class="comment"># 第三次访问</span></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">闭包有什么缺点呢?</span></span><br><span class="line"><span class="string">闭包的缺点1,作用域没有你们直观</span></span><br><span class="line"><span class="string">闭包的缺点2,因为变量是不会被垃圾回收所以有一定的内存占用问题。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">闭包作用:1.可以使用同级的作用域</span></span><br><span class="line"><span class="string">闭包作用:2.读取其它元素的内部变量</span></span><br><span class="line"><span class="string">闭包作用:3.延长作用域</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">闭包总结</span></span><br><span class="line"><span class="string">1.闭包似优化了变量,原来需要类对象完成的工作,闭包也可以完成</span></span><br><span class="line"><span class="string">2.由于闭包引用了外部函数的局部变量,则外部函数的局部变量没有及时释放,消耗内存,</span></span><br><span class="line"><span class="string">3.闭包的好处,使代码变的简洁,便于阅读代码</span></span><br><span class="line"><span class="string">4.闭包是理解装饰器的基础</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>():</span><br><span class="line"> a = <span class="number">100</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">inner_func1</span>():</span><br><span class="line"> b = <span class="number">90</span></span><br><span class="line"> s = a + b</span><br><span class="line"> <span class="built_in">print</span>(s)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">inner_func2</span>(): <span class="comment"># define</span></span><br><span class="line"> inner_func1() <span class="comment"># 调用inner_func1</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'------>inner_func2'</span>, a) <span class="comment"># 同级可以访问</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">'hello'</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> inner_func2 <span class="comment"># 加括号表示调用,而不是扔出去</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用func</span></span><br><span class="line">f = func() <span class="comment"># 接收return inner_func2</span></span><br><span class="line"><span class="comment"># print(f) # 输出内存地址才正确<function func.<locals>.inner_func2 at 0x0000026BEBBEED30></span></span><br><span class="line">f() <span class="comment"># 显示return inner_func2</span></span><br><span class="line">ff = f() <span class="comment"># 使用ff接收return ’hello‘,就可以输出hello</span></span><br><span class="line"><span class="built_in">print</span>(ff)</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 装饰器</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">加入购物车,付款,修改收货地址。。。。。</span></span><br><span class="line"><span class="string">判断用户的登录状态,</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>(<span class="params">number</span>):</span><br><span class="line"> a = <span class="number">100</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">inner_func</span>():</span><br><span class="line"> <span class="keyword">nonlocal</span> a, number</span><br><span class="line"> number += <span class="number">1</span></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(number):</span><br><span class="line"> a += <span class="number">1</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'修改后的a:'</span>, a)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> inner_func</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用func</span></span><br><span class="line">f = func(<span class="number">5</span>)</span><br><span class="line">f()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 装饰器函数作为参数</span></span><br><span class="line"></span><br><span class="line">a = <span class="number">50</span></span><br><span class="line">f1 = func(a) <span class="comment"># a是实参</span></span><br><span class="line">f1()</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># # 地址的引用</span></span><br><span class="line"><span class="comment"># a = 10 # 声明整形的变量</span></span><br><span class="line"><span class="comment"># b = a</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def test(): # 声明函数</span></span><br><span class="line"><span class="comment"># print('---------------test-------------')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># t = test # 将test()函数内存地址赋值给t</span></span><br><span class="line"><span class="comment"># t()</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># def func(f):</span></span><br><span class="line"><span class="comment"># print(f) # <function test at 0x000001AB91B0E040></span></span><br><span class="line"><span class="comment"># f() # -----------test-------</span></span><br><span class="line"><span class="comment"># print('-----------func----------')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 调用</span></span><br><span class="line"><span class="comment"># func(test)</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">特点:</span></span><br><span class="line"><span class="string">1.函数A是作为参数出现的(函数B就接收函数A作为参数)</span></span><br><span class="line"><span class="string">2.要有闭包的特点</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义一个装饰器</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">decorate</span>(<span class="params">func</span>):</span><br><span class="line"> a = <span class="number">100</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'wrapper外层打印测试'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">wrapper</span>():</span><br><span class="line"> func()</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'---------->刷漆'</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'---------->装门'</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'---------->铺地板'</span>, a)</span><br><span class="line"></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'wrapper加载结束'</span>)</span><br><span class="line"> <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用装饰器</span></span><br><span class="line"><span class="meta">@decorate </span><span class="comment"># 需要在decorate这个函数传入函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">house</span>():</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'我是毛胚房。。。。'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">1.house被装饰函数,</span></span><br><span class="line"><span class="string">2.将被装饰函数作为参数传给装饰器decorate</span></span><br><span class="line"><span class="string">3.执行decorate函数</span></span><br><span class="line"><span class="string">4.将返回值又赋值给house</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="built_in">print</span>(house)</span><br><span class="line">house()</span><br><span class="line"><span class="comment"># def house1():</span></span><br><span class="line"><span class="comment"># print('刷油漆')</span></span><br><span class="line"><span class="comment"># print('铺地板')</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用house</span></span><br><span class="line"><span class="comment"># house()</span></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 登录校验</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">decorate</span>(<span class="params">func</span>):</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">wrapper</span>(<span class="params">*args, **kwargs</span>): <span class="comment"># 万能装饰器</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'正在校验中.....'</span>)</span><br><span class="line"> time.sleep(<span class="number">2</span>) <span class="comment"># 休眠里面</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'校验完毕.....'</span>)</span><br><span class="line"> <span class="comment"># 调用原函数</span></span><br><span class="line"> func(*args, **kwargs)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@decorate</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">f1</span>(<span class="params">n</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'------f1-----'</span>, n)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">f1(<span class="number">1</span>) <span class="comment"># 此时f1是wrapper</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@decorate</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">f2</span>(<span class="params">name, age</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'------f2-----'</span>, name, age)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">f2(<span class="string">'丽丽'</span>, <span class="number">20</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@decorate</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">f3</span>(<span class="params">studens, clazz=<span class="string">'1905'</span></span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}班级的学生如下:'</span>.<span class="built_in">format</span>(clazz))</span><br><span class="line"> <span class="keyword">for</span> stu <span class="keyword">in</span> studens:</span><br><span class="line"> <span class="built_in">print</span>(stu)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">studens = [<span class="string">'lili'</span>, <span class="string">'gg'</span>, <span class="string">'bao'</span>]</span><br><span class="line">f3(studens, clazz=<span class="string">'1904'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@decorate</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">f4</span>():</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'---------f4'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">f4()</span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">print</span></span><br><span class="line"><span class="string">正在校验中.....</span></span><br><span class="line"><span class="string">校验完毕.....</span></span><br><span class="line"><span class="string">------f1----- 1</span></span><br><span class="line"><span class="string">正在校验中.....</span></span><br><span class="line"><span class="string">校验完毕.....</span></span><br><span class="line"><span class="string">------f2----- 丽丽 20</span></span><br><span class="line"><span class="string">正在校验中.....</span></span><br><span class="line"><span class="string">校验完毕.....</span></span><br><span class="line"><span class="string">1904班级的学生如下:</span></span><br><span class="line"><span class="string">lili</span></span><br><span class="line"><span class="string">gg</span></span><br><span class="line"><span class="string">bao</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 装饰器</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">如果装饰器是多层的,谁距离函数u最近就优先使用那个装饰器</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">zhuang1</span>(<span class="params">func</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'------1 start'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">wrapper</span>(<span class="params">*args, **kwargs</span>):</span><br><span class="line"> func()</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'刷漆'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'------1 end'</span>)</span><br><span class="line"> <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">zhuang2</span>(<span class="params">func</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'------2 start'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">wrapper</span>(<span class="params">*args, **kwargs</span>):</span><br><span class="line"> func()</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'扣地板'</span>)</span><br><span class="line"></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'------2 end'</span>)</span><br><span class="line"> <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@zhuang2</span></span><br><span class="line"><span class="meta">@zhuang1 </span><span class="comment"># 谁近先装谁</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">house</span>():</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'我是毛胚房.....'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">house()</span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">------1 start</span></span><br><span class="line"><span class="string">------1 end</span></span><br><span class="line"><span class="string">------2 start</span></span><br><span class="line"><span class="string">------2 end</span></span><br><span class="line"><span class="string">我是毛胚房.....</span></span><br><span class="line"><span class="string">刷漆</span></span><br><span class="line"><span class="string">扣地板</span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 装饰器带参数,需要三层函数</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">带参数的装饰器是三层的</span></span><br><span class="line"><span class="string">最外层的函数负责接收装饰器参数</span></span><br><span class="line"><span class="string">里面的内容函数原装饰器的内容</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">outer</span>(<span class="params">a</span>): <span class="comment"># 第一层</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">decorate</span>(<span class="params">func</span>): <span class="comment"># 第二层</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">wrapper</span>(<span class="params">*args, **kwargs</span>): <span class="comment"># 第三层</span></span><br><span class="line"> func(*args, **kwargs)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'------>铺地砖{}块'</span>.<span class="built_in">format</span>(a))</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> wrapper <span class="comment"># 返出来的是第三层</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> decorate <span class="comment"># 返出来的是第二层</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@outer(<span class="params"><span class="number">10</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">house</span>(<span class="params">time</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'我{}日期拿到房子的钥匙,毛坯房。。。'</span>.<span class="built_in">format</span>(time))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@outer(<span class="params"><span class="number">100</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">street</span>():</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'新修街道的名字是:黑泉路'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">house(<span class="string">'2019-6-12'</span>)</span><br><span class="line">street()</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 开发:登录验证</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line">isLogin = <span class="literal">False</span> <span class="comment"># 默认没有登录</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">login</span>():</span><br><span class="line"> username = <span class="built_in">input</span>(<span class="string">'输入用户名:'</span>)</span><br><span class="line"> password = <span class="built_in">input</span>(<span class="string">'输入密码:'</span>)</span><br><span class="line"> <span class="keyword">if</span> username == <span class="string">'admin'</span> <span class="keyword">and</span> password == <span class="string">'admin'</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'登录成功'</span>)</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'登录失败,用户名或密码错误'</span>)</span><br><span class="line"> login()</span><br><span class="line"> <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义一个装饰器,进行付款验证</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">login_required</span>(<span class="params">func</span>):</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">wrapper</span>(<span class="params">*args, **kwargs</span>):</span><br><span class="line"> <span class="keyword">global</span> isLogin</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'-----正在进行付款-----'</span>)</span><br><span class="line"> <span class="comment"># 验证用户是否登录</span></span><br><span class="line"> <span class="keyword">if</span> isLogin:</span><br><span class="line"> func(*args, **kwargs)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="comment"># 跳转到登录页面</span></span><br><span class="line"> time.sleep(<span class="number">2</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'用户没有登录,请登录之后再付款'</span>)</span><br><span class="line"> isLogin = login()</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> wrapper</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">@login_required</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">pay</span>(<span class="params">money</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'正在付款,付款金额是:{}元'</span>.<span class="built_in">format</span>(money))</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'付款中...'</span>)</span><br><span class="line"> time.sleep(<span class="number">2</span>)</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'付款完成'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">pay(<span class="number">10000</span>)</span><br><span class="line">pay(<span class="number">10000</span>)</span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">-----正在进行付款-----</span></span><br><span class="line"><span class="string">用户没有登录,请登录之后再付款</span></span><br><span class="line"><span class="string">输入用户名:admin</span></span><br><span class="line"><span class="string">输入密码:admin</span></span><br><span class="line"><span class="string">result True</span></span><br><span class="line"><span class="string">-----正在进行付款-----</span></span><br><span class="line"><span class="string">正在付款,付款金额是:10000元</span></span><br><span class="line"><span class="string">付款中...</span></span><br><span class="line"><span class="string">付款完成</span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> Python </tag>
</tags>
</entry>
<entry>
<title>python可变参数列表拆装</title>
<link href="/posts/10926/"/>
<url>/posts/10926/</url>
<content type="html"><![CDATA[<h1 id="笔记"><a href="#笔记" class="headerlink" title="笔记"></a>笔记</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>(<span class="params">a, *args</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}{}'</span>.<span class="built_in">format</span>(a, args))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func(<span class="number">2</span>, [<span class="number">1</span>, <span class="number">23</span>, <span class="number">4</span>], <span class="string">'fad'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func2</span>(<span class="params">a, b=<span class="number">10</span>, c=<span class="number">3</span>, **kwargs</span>):</span><br><span class="line"> <span class="built_in">print</span>(a, b, c, kwargs)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func2(<span class="number">1</span>) <span class="comment"># 1 10 3 {} 给默认值所以可传入一个参数</span></span><br><span class="line">func2(<span class="number">2</span>, b=<span class="number">11</span>) <span class="comment"># 2 11 3 {}</span></span><br><span class="line">func2(<span class="number">3</span>, c=<span class="number">5</span>, b=<span class="number">7</span>, x=<span class="number">1</span>, y=<span class="number">2</span>) <span class="comment"># 3 7 5 {'x': 1, 'y': 2},传值的时候可以打乱位置</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func3</span>(<span class="params">a, *args, **kwargs</span>):</span><br><span class="line"> <span class="built_in">print</span>(a, args, kwargs)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">t = (<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>)</span><br><span class="line">func3(<span class="number">1</span>, t) <span class="comment"># 1 ((1, 2, 3, 4),) {}</span></span><br><span class="line"></span><br><span class="line">l = [<span class="number">2</span>, <span class="number">5</span>, <span class="number">8</span>]</span><br><span class="line">func3(<span class="number">1</span>, l, c=<span class="number">9</span>, b=<span class="number">6</span>) <span class="comment"># 1 ([2, 5, 8],) {'c': 9, 'b': 6}</span></span><br><span class="line"></span><br><span class="line">dict1 = {<span class="string">'1'</span>: <span class="string">'a'</span>, <span class="string">'2'</span>: <span class="string">'b'</span>, <span class="string">'3'</span>: <span class="string">'c'</span>}</span><br><span class="line">func3(<span class="number">1</span>, *l, **dict1) <span class="comment"># 1 (2, 5, 8) {'1': 'a', '2': 'b', '3': 'c'} # *拆列表 **拆字典</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func1</span>(<span class="params">name, *args</span>):</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">len</span>(args) > <span class="number">0</span>:</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> args: <span class="comment"># 这里args是元组</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}学过了{}'</span>.<span class="built_in">format</span>(name, i))</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'没有学过任何语言'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">courses = [<span class="string">'html'</span>, <span class="string">'mysql'</span>, <span class="string">'c++'</span>, <span class="string">'python'</span>]</span><br><span class="line"><span class="comment"># 调用函数</span></span><br><span class="line">func1(<span class="string">'健健'</span>, *courses) <span class="comment"># *拆列表(拆包)</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">无参数函数:</span></span><br><span class="line"><span class="string"> def func():</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func() ---调用</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string"> 有参数函数</span></span><br><span class="line"><span class="string">1.普通参数</span></span><br><span class="line"><span class="string"> def func(name,age):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func('aa',19) --->形参与实参的个数要一致</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">2.可变参数:</span></span><br><span class="line"><span class="string"> A.</span></span><br><span class="line"><span class="string"> def func(*args):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func() ----> 函数调用时,实参的个数可以没有,也可以多个,*不能是关键字参数</span></span><br><span class="line"><span class="string"> func(4) </span></span><br><span class="line"><span class="string"> func(5,'h')</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> B.</span></span><br><span class="line"><span class="string"> def func(**kwargs):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func(a=1,b=2)----> 函数调用时,实参的个数可以没有,也可以多个,**必须是关键字参数</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string"> C.</span></span><br><span class="line"><span class="string"> def func(*args,**kwargs):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> list1 = [1,2,3,4,5]</span></span><br><span class="line"><span class="string"> dict1 = {'1':'a','2':'b','3':'c'}</span></span><br><span class="line"><span class="string"> func(*list1,**dict1) 拆包func(1,2,3,4,5,'1':'a','2':'b','3':'c')</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string"> D.混用</span></span><br><span class="line"><span class="string"> def func(name,*args,*8kwargs):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func('tom') ---->必须赋值</span></span><br><span class="line"><span class="string">3.默认值</span></span><br><span class="line"><span class="string"> def func(name,age=18):</span></span><br><span class="line"><span class="string"> pass</span></span><br><span class="line"><span class="string"> func('tom') ----> tom 18</span></span><br><span class="line"><span class="string"> func('tom',age = 20) ---->关键字赋值</span></span><br><span class="line"><span class="string"> </span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> Python </tag>
</tags>
</entry>
<entry>
<title>python集合</title>
<link href="/posts/19623/"/>
<url>/posts/19623/</url>
<content type="html"><![CDATA[<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">集合:</span><br><span class="line"> <span class="built_in">list</span> <span class="built_in">tuple</span> ----> <span class="built_in">set</span>()</span><br><span class="line"> 无序不重复的序列,集合</span><br><span class="line"> 无序----->跟下标相关</span><br><span class="line"> s = {<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>} ---->s[<span class="number">1</span>]</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> s:</span><br><span class="line"> <span class="built_in">print</span>(i)</span><br><span class="line">内置函数:</span><br><span class="line">添加:add update</span><br><span class="line">删除:remove discard pop clear</span><br><span class="line"></span><br><span class="line">运算相关函数</span><br><span class="line">- difference()</span><br><span class="line">| union</span><br><span class="line">& intersection()</span><br><span class="line">^ symmetric_difference()</span><br><span class="line"></span><br><span class="line">可变和不可变</span><br><span class="line">可变:地址不变里面内容改变 <span class="built_in">list</span> <span class="built_in">dict</span> <span class="built_in">set</span></span><br><span class="line">不可变:只要内容改变,必须改变地址 <span class="built_in">int</span> <span class="built_in">str</span> <span class="built_in">float</span> <span class="built_in">tuple</span> <span class="built_in">frozenset</span></span><br><span class="line"></span><br><span class="line">类型转换:</span><br><span class="line"><span class="built_in">str</span> ----> <span class="built_in">list</span> <span class="built_in">set</span> ... 相互转换</span><br><span class="line"><span class="built_in">list</span> ---> <span class="built_in">set</span> <span class="built_in">tuple</span> <span class="built_in">dict</span> 相互的转换</span><br><span class="line"></span><br><span class="line">函数:</span><br><span class="line"> 增加代码的复用,减少代码的冗余</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">函数名</span>(<span class="params">[参数,]</span>):</span><br><span class="line"> 函数体</span><br><span class="line">没有参数:</span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>():</span><br><span class="line"> add()</span><br><span class="line"></span><br><span class="line">有参数:</span><br><span class="line"><span class="keyword">def</span> <span class="title function_">add</span>(<span class="params">a, b</span>):</span><br><span class="line"> result = a+b</span><br><span class="line"> <span class="built_in">print</span>(result)</span><br><span class="line">调用:</span><br><span class="line">add(<span class="number">1</span>,<span class="number">3</span>)</span><br><span class="line">add(<span class="number">2</span>,<span class="number">8</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 不重复特点:</span></span><br><span class="line">list1 = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">6</span>, <span class="number">0</span>]</span><br><span class="line"><span class="comment"># 声明集合:set</span></span><br><span class="line">s1 = <span class="built_in">set</span>()</span><br><span class="line">s2 = {} <span class="comment"># 字典:{key:value} 集合{元素1, 元素2, 元素3}</span></span><br><span class="line"><span class="built_in">print</span>()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 应用:</span></span><br><span class="line">s3 = <span class="built_in">set</span>(list1)</span><br><span class="line"><span class="built_in">print</span>(s3)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 增删改查</span></span><br><span class="line"><span class="comment"># 1.增加 s1 = set()</span></span><br><span class="line"><span class="comment"># add() 添加一个元素</span></span><br><span class="line"><span class="comment"># update 可以添加多个元素</span></span><br><span class="line">s1.add(<span class="string">'hello'</span>)</span><br><span class="line">s1.add(<span class="string">'小猪佩奇'</span>)</span><br><span class="line"><span class="built_in">print</span>(s1)</span><br><span class="line">t1 = (<span class="string">'林志玲'</span>, <span class="string">'言承旭'</span>)</span><br><span class="line">s1.update(t1)</span><br><span class="line"><span class="built_in">print</span>(s1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2.删除 remove 如果存在删除不存在报错KeyError pop clear</span></span><br><span class="line">s1.remove(<span class="string">'言承旭'</span>)</span><br><span class="line"><span class="built_in">print</span>(s1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># s1.remove('道明寺') # KeyError: '道明寺'</span></span><br><span class="line"><span class="comment"># print(s1)</span></span><br><span class="line"></span><br><span class="line">s1.pop()</span><br><span class="line"><span class="built_in">print</span>(s1)</span><br><span class="line"></span><br><span class="line">s1.clear()</span><br><span class="line"><span class="built_in">print</span>(s1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># dicard() 类似remove 在移除不存在的值不会报错</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string"> 1.产生10个1-20的随机数去除里面的重复项</span></span><br><span class="line"><span class="string"> 2.键盘输入一个元素,将此元素从不重复的集合中删除</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"><span class="comment"># 方法1</span></span><br><span class="line"><span class="comment"># list1 = []</span></span><br><span class="line"><span class="comment"># # set1 = set()</span></span><br><span class="line"><span class="comment"># for i in range(10):</span></span><br><span class="line"><span class="comment"># ran = random.randint(1, 20)</span></span><br><span class="line"><span class="comment"># list1.append(ran)</span></span><br><span class="line"><span class="comment"># # set1.update(list1)</span></span><br><span class="line"><span class="comment"># set1 = set(list1)</span></span><br><span class="line"><span class="comment"># print(list1)</span></span><br><span class="line"><span class="comment"># print(set1)</span></span><br><span class="line"><span class="comment"># result = int(input('请输入一个想要删除的数字:'))</span></span><br><span class="line"><span class="comment"># set1.discard(result)</span></span><br><span class="line"><span class="comment"># print(set1)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 方法2</span></span><br><span class="line">set1 = <span class="built_in">set</span>()</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>):</span><br><span class="line"> ran = random.randint(<span class="number">1</span>, <span class="number">20</span>)</span><br><span class="line"> set1.add(ran)</span><br><span class="line"><span class="built_in">print</span>(set1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 其他:符号操作</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="number">6</span> <span class="keyword">in</span> set1)</span><br><span class="line"></span><br><span class="line">set2 = {<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>}</span><br><span class="line">set3 = {<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>}</span><br><span class="line"><span class="built_in">print</span>(set2 == set3) <span class="comment"># 判断两个集合中的内容是否相等</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 测试:print(set2 != set3)</span></span><br><span class="line"><span class="comment"># (+ * 不支持) - & |</span></span><br><span class="line"><span class="comment"># set4 = set2+set3 报错s</span></span><br><span class="line"><span class="comment"># print(set4)</span></span><br><span class="line"></span><br><span class="line">set4 = set3 - set2 <span class="comment"># 差集 difference</span></span><br><span class="line">set5 = set3.difference(set2)</span><br><span class="line"><span class="built_in">print</span>(set4, set5)</span><br><span class="line"></span><br><span class="line"><span class="comment"># &交集 intersection()</span></span><br><span class="line"></span><br><span class="line">set6 = set3 & set2</span><br><span class="line"><span class="built_in">print</span>(set6)</span><br><span class="line">set7 = set3.intersection(set2)</span><br><span class="line"><span class="built_in">print</span>(set7)</span><br><span class="line"></span><br><span class="line"><span class="comment"># | 交集 union() 联合</span></span><br><span class="line"></span><br><span class="line">set9 = set3 | set2</span><br><span class="line"><span class="built_in">print</span>(set9)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(set9)</span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">已知两个列表:</span></span><br><span class="line"><span class="string">l1 = [5, 1, 2, 9, 0, 3]</span></span><br><span class="line"><span class="string">l2 = [7, 2, 5, 7, 9]</span></span><br><span class="line"><span class="string">找出两个列表的不同元素</span></span><br><span class="line"><span class="string">找出两个列表的相同元素</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line">l1 = [<span class="number">5</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">9</span>, <span class="number">0</span>, <span class="number">3</span>]</span><br><span class="line">l2 = [<span class="number">7</span>, <span class="number">2</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>]</span><br><span class="line"></span><br><span class="line">l1 = <span class="built_in">set</span>(l1)</span><br><span class="line">l2 = <span class="built_in">set</span>(l2)</span><br><span class="line"><span class="comment"># 对称差集</span></span><br><span class="line">result = (l1 | l2) - (l1 & l2)</span><br><span class="line"><span class="built_in">print</span>(result)</span><br><span class="line"></span><br><span class="line">result = l1 ^ l2 <span class="comment"># 列表中不一样的元素,对称差集, symmetric_difference()</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(result)</span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">difference_update()</span></span><br><span class="line"><span class="string">l1.difference(l2)</span></span><br><span class="line"><span class="string">l1.difference_update(l2) update不需要赋值</span></span><br><span class="line"><span class="string">l1.intersection_update(l2) 交集并赋值</span></span><br><span class="line"><span class="string">symmetric_difference_update() 对称差集并赋值</span></span><br><span class="line"><span class="string">update不需要赋值</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(l1.difference_update(l2))</span><br><span class="line"><span class="built_in">print</span>(l1.intersection_update(l2))</span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">关键字:set</span></span><br><span class="line"><span class="string">作用:去重</span></span><br><span class="line"><span class="string">符号:- | & ^</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">内置函数:</span></span><br><span class="line"><span class="string"> 增加:add() update()</span></span><br><span class="line"><span class="string"> 删除:remove() discard() pop() clear()</span></span><br><span class="line"><span class="string"> 运算:difference() intersection() union() symmetric_difference()</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 可变 和不可变</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 不可变:对象所指向的内存中的值是不可变的</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 不可变的类型:int str float 元组tuple</span></span><br><span class="line"></span><br><span class="line">num = <span class="number">10</span></span><br><span class="line">s1 = <span class="string">'abc'</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(s1))</span><br><span class="line"></span><br><span class="line">s1 = <span class="string">'abcd'</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(s1))</span><br><span class="line"></span><br><span class="line">t1 = (<span class="number">3</span>, <span class="number">5</span>, <span class="number">6</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(t1))</span><br><span class="line"></span><br><span class="line">t1 = (<span class="number">3</span>, <span class="number">5</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">id</span>(t1))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 可变的元素:该对象所指向的内存中的值是可以改变的</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 可变类型: dict list set</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> Python </tag>
</tags>
</entry>
<entry>
<title>内置函数+匿名函数</title>
<link href="/posts/4090/"/>
<url>/posts/4090/</url>
<content type="html"><![CDATA[<p><img src="/%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0/image-20211004000841483.webp" alt="image-20211004000841483"></p><p><img src="/%E5%8C%BF%E5%90%8D%E5%87%BD%E6%95%B0/image-20211004011556646.webp" alt="image-20211004011556646"></p>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> 自学 </tag>
</tags>
</entry>
<entry>
<title>文件操作</title>
<link href="/posts/16177/"/>
<url>/posts/16177/</url>
<content type="html"><![CDATA[<p><img src="/%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C/image-20211004110415017.webp" alt="image-20211004110415017"></p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># file1</span></span><br><span class="line"><span class="comment"># 文件操作:</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">文件上传:</span></span><br><span class="line"><span class="string">保存log</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">系统函数</span></span><br><span class="line"><span class="string">open(file,mode,buffering,encodeing)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">读:</span></span><br><span class="line"><span class="string"> open(path/filename,'rt') ----->返回值:stream(管道)</span></span><br><span class="line"><span class="string"> container = stream.read() ---->读取管道中内容</span></span><br><span class="line"><span class="string"> 注意:如果传递的path/filename有误,则会报错:FileNotFoundError</span></span><br><span class="line"><span class="string"> 如果是图片则不能使用默认的读取方式,mode = 'rb'</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"> 总结:</span></span><br><span class="line"><span class="string"> read()</span></span><br><span class="line"><span class="string"> readline() 每次读取一行内容</span></span><br><span class="line"><span class="string"> readlines() 读取所有的行保存到列表中</span></span><br><span class="line"><span class="string"> readable() 判断是否是可读的</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line">stream = <span class="built_in">open</span>(<span class="string">r'../../个人工具/base.txt'</span>, <span class="string">'rt'</span>, encoding=<span class="string">'utf-8'</span>) <span class="comment"># rt文本文档读</span></span><br><span class="line"><span class="comment"># container = stream.read()</span></span><br><span class="line"><span class="comment"># print(container)</span></span><br><span class="line">result = stream.readable() <span class="comment"># able判断是否可以读取</span></span><br><span class="line"><span class="built_in">print</span>(result)</span><br><span class="line"></span><br><span class="line"><span class="comment"># while True:</span></span><br><span class="line"><span class="comment"># line = stream.readline()</span></span><br><span class="line"><span class="comment"># print(line)</span></span><br><span class="line"><span class="comment"># if not line:</span></span><br><span class="line"><span class="comment"># break</span></span><br><span class="line"></span><br><span class="line">lines = stream.readlines() <span class="comment"># 保存到列表中</span></span><br><span class="line"><span class="built_in">print</span>(lines)</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> lines:</span><br><span class="line"> <span class="built_in">print</span>(i)</span><br><span class="line"></span><br><span class="line">stream = <span class="built_in">open</span>(<span class="string">r'../../img/2021-09-26/OHR.MackenzieRiver_ZH-CN0214805768_480x800.jpg'</span>, <span class="string">'rb'</span>)</span><br><span class="line"></span><br><span class="line">container = stream.read()</span><br><span class="line"><span class="built_in">print</span>(container)</span><br><span class="line"></span><br><span class="line"><span class="comment"># file2</span></span><br><span class="line"><span class="comment"># 写文件</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">stream = open(r'aa.txt', 'w')</span></span><br><span class="line"><span class="string">mode 是'w'表示写操作</span></span><br><span class="line"><span class="string">方法:</span></span><br><span class="line"><span class="string"> write(内容) 每次都会将原来的内容清空,然后写当前的内容</span></span><br><span class="line"><span class="string"> writelines(Iterable) 没有换行的效果 4444444GGGGRE</span></span><br><span class="line"><span class="string"> straem.writelines(['赌神:小明明\n', '赌侠笑道\n', '赌圣周星星\n'])</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">如果有mode='a' 表示追加</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line">straem = <span class="built_in">open</span>(<span class="string">r'a.txt'</span>, <span class="string">'a'</span>, encoding=<span class="string">'utf-8'</span>)</span><br><span class="line">s = <span class="string">'''</span></span><br><span class="line"><span class="string">你好!</span></span><br><span class="line"><span class="string"> 欢迎来到澳门菠菜,赠送给你一个金币!</span></span><br><span class="line"><span class="string"> 赌王:小明</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line">result = straem.write(s)</span><br><span class="line"><span class="built_in">print</span>(result)</span><br><span class="line"></span><br><span class="line">straem.write(<span class="string">' 你需要戒指吗???\n'</span>) <span class="comment"># 可以\n换行</span></span><br><span class="line">straem.writelines([<span class="string">' 赌神:小明明\n'</span>, <span class="string">' 赌侠笑道\n'</span>, <span class="string">' 赌圣周星星\n'</span>])</span><br><span class="line">straem.write(<span class="string">' 僵尸先生'</span>)</span><br><span class="line">straem.close() <span class="comment"># 释放资源</span></span><br><span class="line"><span class="comment"># 关闭之后就无法写入</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># file3</span></span><br><span class="line"><span class="comment"># 文件的复制</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">原文件:../../img/2021-09-26/OHR.MackenzieRiver_ZH-CN0214805768_480x800.jpg</span></span><br><span class="line"><span class="string">目标文件:./1.jpg</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">r'./OHR.MackenzieRiver_ZH-CN0214805768_1920x1080.jpg'</span>, <span class="string">'rb'</span>) <span class="keyword">as</span> result:</span><br><span class="line"> container = result.read()</span><br><span class="line"> <span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">r'1.jpg'</span>, <span class="string">'wb'</span>) <span class="keyword">as</span> wstream:</span><br><span class="line"> wstream.write(container)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'文件复制完成'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># os</span></span><br><span class="line"><span class="comment"># 模块:os.py</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">os.path:</span></span><br><span class="line"><span class="string">os.path.dirname(__file__) 获取当前文件所在的文件目录(绝对路径)</span></span><br><span class="line"><span class="string">os.path.join(path,'') 返回的是一个拼接后的新的路径</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"><span class="comment"># print(os.path)</span></span><br><span class="line"><span class="comment"># path = os.path.dirname(__file__) # 显示当前目录的绝对路径</span></span><br><span class="line"><span class="comment"># print(path, '\n', type(path))</span></span><br><span class="line"><span class="comment"># result = os.path.join(path, 'a1.jpg')</span></span><br><span class="line"><span class="comment"># print(result)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">r'./OHR.MackenzieRiver_ZH-CN0214805768_1920x1080.jpg'</span>, <span class="string">'rb'</span>) <span class="keyword">as</span> stream: <span class="comment"># 要复制的文件</span></span><br><span class="line"> container = stream.read() <span class="comment"># 读取文件内容</span></span><br><span class="line"> file = stream.name</span><br><span class="line"> filename = file[file.rfind(<span class="string">r'/'</span>)+<span class="number">1</span>:]</span><br><span class="line"> path = os.path.dirname(__file__)</span><br><span class="line"> path1 = os.path.join(path, filename)</span><br><span class="line"> <span class="keyword">with</span> <span class="built_in">open</span>(path1, <span class="string">'wb'</span>) <span class="keyword">as</span> wstream:</span><br><span class="line"> wstream.write(container)</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="comment"># file 01</span></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">'book/a1.txt'</span>, <span class="string">'w'</span>) <span class="keyword">as</span> wstrem:</span><br><span class="line"> wstrem.write(<span class="string">'hello'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line">result = os.path.isabs(<span class="string">r'C:\\Users\\Wans\\AppData\\Local\\Programs\\Python39\\lib\\a1.txt'</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'------>'</span>, result)</span><br><span class="line"><span class="comment"># ------> True</span></span><br><span class="line"></span><br><span class="line">result1 = os.path.isabs(<span class="string">r'../day10_函数/func01.py'</span>) <span class="comment"># 当前文件的上一级</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'------>'</span>, result1)</span><br><span class="line"><span class="comment"># ------> False</span></span><br><span class="line"></span><br><span class="line">result1 = os.path.isabs(<span class="string">r'/day10_函数/func01.py'</span>) <span class="comment"># 找跟file01.py同级别的func01.py</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">'------>'</span>, result1)</span><br><span class="line"><span class="comment"># ------> True</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取路径,directory目录 文件夹</span></span><br><span class="line"><span class="comment"># 当前文件所在文件夹的路径</span></span><br><span class="line">path = os.path.dirname(__file__)</span><br><span class="line"><span class="comment"># 通过相对路径得到绝对路径</span></span><br><span class="line"><span class="built_in">print</span>(path)</span><br><span class="line"><span class="comment"># D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\a.txt</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">path = os.path.abspath(<span class="string">'a.txt'</span>)</span><br><span class="line"><span class="built_in">print</span>(path)</span><br><span class="line"><span class="comment"># D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\a.txt</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取当前文件的绝对路径</span></span><br><span class="line"><span class="comment"># __file__ 当前文件</span></span><br><span class="line">path = os.path.abspath(__file__)</span><br><span class="line"><span class="built_in">print</span>(path)</span><br><span class="line"><span class="comment"># D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\file01.py</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">path = os.getcwd() <span class="comment"># 类似os.path.dirname(__file__)</span></span><br><span class="line"><span class="built_in">print</span>(path)</span><br><span class="line"><span class="comment"># D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">r = os.path.isfile(os.getcwd()) <span class="comment"># isfile判断是不是文件</span></span><br><span class="line"><span class="built_in">print</span>(r)</span><br><span class="line"><span class="comment"># False</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">r = os.path.isdir(os.getcwd()) <span class="comment"># isdir判断是不是目录</span></span><br><span class="line"><span class="built_in">print</span>(r)</span><br><span class="line"><span class="comment"># True</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">path = <span class="string">r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\file01.py'</span></span><br><span class="line">result = os.path.split(path)</span><br><span class="line"><span class="built_in">print</span>(result) <span class="comment"># 输出前面是路径,后面是文件名</span></span><br><span class="line"><span class="comment"># ('D:\\Users\\Wans\\Desktop\\python stduy\\千峰笔记\\day13_文件', 'file01.py')</span></span><br><span class="line"><span class="built_in">print</span>(result[<span class="number">1</span>]) <span class="comment"># 这样可以输出文件名</span></span><br><span class="line"><span class="comment"># filename = path[path.rfind('\\')+1:]</span></span><br><span class="line"><span class="comment"># file01.py</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">result = os.path.splitext(path) <span class="comment"># 分割文件与扩展名查看文件的后缀</span></span><br><span class="line"><span class="built_in">print</span>(result)</span><br><span class="line"><span class="comment"># ('D:\\Users\\Wans\\Desktop\\python stduy\\千峰笔记\\day13_文件\\file01', '.py')</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(os.path.getsize(<span class="string">'a.txt'</span>)) <span class="comment"># 返回文件的大小,单位字节</span></span><br><span class="line"><span class="built_in">print</span>(os.path.join(os.getcwd(), <span class="string">'file'</span>, <span class="string">'a.txt'</span>)) <span class="comment"># 每多一个加一层</span></span><br><span class="line"><span class="comment"># D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\file\a.txt</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">os.path: 常用函数</span></span><br><span class="line"><span class="string"> dirname()</span></span><br><span class="line"><span class="string"> join()</span></span><br><span class="line"><span class="string"> split()</span></span><br><span class="line"><span class="string"> splittext()</span></span><br><span class="line"><span class="string"> getsize()</span></span><br><span class="line"><span class="string"> isabs()</span></span><br><span class="line"><span class="string"> isfile()</span></span><br><span class="line"><span class="string"> isdir()</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># file 02</span></span><br><span class="line"><span class="comment"># -*- codeing = utf-8 -*-</span></span><br><span class="line"><span class="comment"># @Time : 2021/10/26 19:08</span></span><br><span class="line"><span class="comment"># @Author : Wans</span></span><br><span class="line"><span class="comment"># @File : file02.py</span></span><br><span class="line"><span class="comment"># @Sofware : PyCharm</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># os.path里面的函数</span></span><br><span class="line"><span class="comment"># os函数</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"><span class="comment"># os.getcwd()</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># dir = os.getcwd()</span></span><br><span class="line"><span class="comment"># print(dir)</span></span><br><span class="line"><span class="comment"># # 返回指定目录下的所有的文件和文件夹</span></span><br><span class="line"><span class="comment"># print(os.listdir(r'D:\Users\Wans\Desktop\python stduy\千峰笔记'))</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># # 创建文件夹</span></span><br><span class="line"><span class="comment"># if not os.path.exists(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\book'):</span></span><br><span class="line"><span class="comment"># os.mkdir(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\book')</span></span><br><span class="line"><span class="comment"># os.rmdir(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\book') # 只能删除空文件夹</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment"># os.removedirs(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\book') # 目录不是空的</span></span><br><span class="line"><span class="comment"># os.remove(r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day13_文件\book\a1.txt')</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除p4文件夹</span></span><br><span class="line">path = <span class="string">r'/千峰笔记/day13_文件/book'</span></span><br><span class="line"><span class="comment"># filelist = os.listdir(path)</span></span><br><span class="line"><span class="comment"># for file in filelist:</span></span><br><span class="line"><span class="comment"># path1 = os.path.join(path, file)</span></span><br><span class="line"><span class="comment"># os.remove(path1)</span></span><br><span class="line"><span class="comment"># else:</span></span><br><span class="line"><span class="comment"># os.removedirs(path)</span></span><br><span class="line"><span class="comment"># # os.rmdir(path)</span></span><br><span class="line"><span class="comment"># print('删除成功')</span></span><br><span class="line"></span><br><span class="line">path = os.getcwd()</span><br><span class="line"><span class="built_in">print</span>(path)</span><br><span class="line"><span class="comment"># 切换目录</span></span><br><span class="line">f = os.chdir(<span class="string">r'D:\Users\Wans\Desktop\python stduy\千峰笔记\day12_函数'</span>)</span><br><span class="line"><span class="built_in">print</span>()</span><br><span class="line">path = os.getcwd()</span><br><span class="line"><span class="built_in">print</span>(path)</span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">os模块下的方法:</span></span><br><span class="line"><span class="string">os.getcwd() # 获取当前目录</span></span><br><span class="line"><span class="string">os.listdir() # 浏览文件夹</span></span><br><span class="line"><span class="string">os.mkdir() # 创建文件夹</span></span><br><span class="line"><span class="string">os.rmdir() # 删除空的文件夹</span></span><br><span class="line"><span class="string">os.remove() # 删除文件</span></span><br><span class="line"><span class="string">os.chdir() # 切换目录</span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> 自学 </tag>
</tags>
</entry>
<entry>
<title>计算机组成原理</title>
<link href="/posts/51917/"/>
<url>/posts/51917/</url>
<content type="html"><![CDATA[<h1 id="操作系统是对计算机硬件资源进行合理分配"><a href="#操作系统是对计算机硬件资源进行合理分配" class="headerlink" title="操作系统是对计算机硬件资源进行合理分配"></a>操作系统是对计算机硬件资源进行合理分配</h1><p>高级语言——汇编语言——机器语言</p><p>0低电平 1高电平</p><p>内存铜片代表高低电平,因为有很多小铜片所以可以同时传递多个01二进制</p><p>存储体里面放置存储单元每个地址对应一个存储单元</p><h1 id="主存储器的基本组成"><a href="#主存储器的基本组成" class="headerlink" title="主存储器的基本组成"></a>主存储器的基本组成</h1><p><img src="/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/image-20210926231230935.webp" alt="image-20210926233714348"></p><p>存储单元:每个存储单元存放一串二进制代码</p><p>存储字(word):存储单元中二进制代码的组合</p><p>存储字长:存储单元中二进制代码的位数</p><p>存储元:即存储二进制的电子元件,每个存储元可存1bit</p><p>1个**字(word) **= 16bit</p><p>易混淆:1个<strong>字节(byte)</strong> = 8 bit</p><p>1B = 1个字节(byte),1b = 1个bit</p><h1 id="运算器的基本组成"><a href="#运算器的基本组成" class="headerlink" title="运算器的基本组成"></a>运算器的基本组成</h1><p><img src="/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/image-20211006140309421.webp" alt="image-20211006140309421"></p><h1 id="控制器的基本组成"><a href="#控制器的基本组成" class="headerlink" title="控制器的基本组成"></a>控制器的基本组成</h1><p><img src="/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/image-20211006140340299.webp" alt="image-20211006140340299"></p><h1 id="计算机的工作过程"><a href="#计算机的工作过程" class="headerlink" title="计算机的工作过程"></a>计算机的工作过程</h1><p><img src="/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/image-20211006140400680.webp" alt="image-20211006140400680"></p><p><img src="/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/image-20211006140423760.webp" alt="image-20211006140423760"></p>]]></content>
<categories>
<category> 自学 </category>
</categories>
<tags>
<tag> 计算机原理 </tag>
</tags>
</entry>
<entry>
<title>python关键字参数和默认值参数</title>
<link href="/posts/57307/"/>
<url>/posts/57307/</url>
<content type="html"><![CDATA[<h1 id="关键字参数和默认值参数"><a href="#关键字参数和默认值参数" class="headerlink" title="关键字参数和默认值参数"></a>关键字参数和默认值参数</h1><p>python **<字典></p><p>可以将字典拆包</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>(<span class="params">**kwargs</span>):</span><br><span class="line"><span class="built_in">print</span>(kwargs)</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 这里的**代表可以传递字典(以字典的形式传递参数)</span></span><br><span class="line"><span class="comment"># key:value只能以键值对方式传递</span></span><br><span class="line"><span class="comment"># 或者以**传值</span></span><br><span class="line">func()</span><br><span class="line">func(a=<span class="number">1</span>,b=<span class="number">2</span>,c=<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 传递字典的方法</span></span><br><span class="line">dict1 = {<span class="string">'001'</span>: <span class="string">'python'</span>, <span class="string">'002'</span>: <span class="string">'java'</span>, <span class="string">'004'</span>: <span class="string">'c++'</span>}</span><br><span class="line">func(**dict1)</span><br><span class="line"><span class="comment"># 拆包步骤:</span></span><br><span class="line"><span class="comment"># 1、func(001 = python,002 = java,004 = c++)将字典拆包成关键字参数的形式</span></span><br><span class="line"><span class="comment"># 2、func里面的参数都是关键字参数</span></span><br><span class="line"><span class="comment"># 3、将关键字参数再一次进行装包动作</span></span><br><span class="line"><span class="comment"># 4、装包成功:kwargs</span></span><br><span class="line">studens = {</span><br><span class="line"> <span class="string">'001'</span>: (<span class="string">'蔡徐坤'</span>, <span class="number">20</span>),</span><br><span class="line"> <span class="string">'002'</span>: (<span class="string">'王源'</span>, <span class="number">18</span>),</span><br><span class="line"> <span class="string">'003'</span>: (<span class="string">'王俊凯'</span>, <span class="number">21</span>),</span><br><span class="line"> <span class="string">'004'</span>: (<span class="string">'易烊千玺'</span>, <span class="number">19</span>)</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">print_boy</span>(<span class="params">name, **persons</span>):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}喜欢吃肉肉'</span>.<span class="built_in">format</span>(name))</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">isinstance</span>(persons, <span class="built_in">dict</span>): <span class="comment"># 判断是不是什么类型:isinstance,这里判断是不是person是不是字典类型</span></span><br><span class="line"> values = persons.values() <span class="comment"># 如果是字典,那么使用values接受字典里面的键值对</span></span><br><span class="line"> <span class="comment"># print(values) # 输出字典</span></span><br><span class="line"> <span class="keyword">for</span> name, age <span class="keyword">in</span> values: <span class="comment"># 使用name和age接受values中的键值对</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'{}的年龄是{}'</span>.<span class="built_in">format</span>(name, age)) <span class="comment"># 循环输出字典中的元素</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">print_boy(<span class="string">'健健'</span>, **studens)</span><br></pre></td></tr></table></figure>]]></content>
<categories>
<category> python </category>
</categories>
<tags>
<tag> Python </tag>
</tags>
</entry>
</search>