在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python语言基础 → MODERN-PYTHON-COOKBOOK

MODERN-PYTHON-COOKBOOK

Python语言基础

下载此实例
  • 开发语言:Python
  • 实例大小:14.10M
  • 下载次数:9
  • 浏览次数:44
  • 发布时间:2023-03-12
  • 实例类别:Python语言基础
  • 发 布 人:zhouxx123
  • 文件格式:.pdf
  • 所需积分:2
 相关标签: Cookbook python DER MOD OD

实例介绍

【实例简介】MODERN-PYTHON-COOKBOOK

【实例截图】

【核心代码】

Table of Contents
Preface 1
Chapter 1: Numbers, Strings, and Tuples 6
Introduction 6
Creating meaningful names and using variables 7
Getting ready 7
How to do it… 8
Choosing names wisely 8
Assigning names to objects 10
How it works… 11
There's more… 12
See also 12
Working with large and small integers 13
Getting ready 13
How to do it… 13
How it works… 14
There's more… 16
See also 17
Choosing between float, decimal, and fraction 17
Getting ready 17
How to do it… 18
Doing currency calculations 18
Fraction calculations 19
Floating-point approximations 20
Converting numbers from one type to another 21
How it works… 22
There's more… 23
See also 24
Choosing between true division and floor division 25
Getting ready 25
How to do it… 25
Doing floor division 26
Doing true division 26
Rational fraction calculations 27
How it works… 27
See also 28
Rewriting an immutable string 28
[ ii ]
Getting ready 28
How to do it… 29
Slicing a piece of a string 29
Updating a string with a replacement 30
Making a string all lowercase 31
Removing extra punctuation marks 31
How it works… 31
There's more… 33
See also 33
String parsing with regular expressions 34
Getting ready 34
How to do it… 34
How it works… 36
There's more… 37
See also 37
Building complex strings with “template”.format() 38
Getting ready 38
How to do it… 38
How it works… 40
There's more… 42
See also 42
Building complex strings from lists of characters 43
Getting ready 43
How to do it… 44
How it works… 44
There's more 45
See also 46
Using the Unicode characters that aren't on our keyboards 46
Getting ready 46
How to do it… 47
How it works… 47
See also 48
Encoding strings – creating ASCII and UTF-8 bytes 49
Getting ready 49
How to do it… 49
How it works… 50
See also 51
Decoding bytes – how to get proper characters from some bytes 52
Getting ready 52
How to do it.. 53
[ iii ]
How it works… 54
See also 55
Using tuples of items 55
Getting ready 55
How to do it… 55
Creating tuples 56
Extracting items from a tuple 56
How it works… 57
There's more 58
See also… 58
Chapter 2: Statements and Syntax 59
Introduction 59
Writing Python script and module files – syntax basics 60
Getting ready 60
How to do it… 61
How it works… 63
There's more… 64
See also 65
Writing long lines of code 65
Getting ready 66
How to do it… 66
Using backslash to break a long statement into logical lines 67
Using the () characters to break a long statement into sensible pieces 67
Using string literal concatenation 68
Assigning intermediate results to separate variables 69
How it works… 69
There's more… 70
See also 70
Including descriptions and documentation 71
Getting ready 71
How to do it… 72
Writing docstrings for scripts 72
Writing docstrings for library modules 74
How it works… 74
There's more… 75
See also 76
Writing better RST markup in docstrings 76
Getting ready 76
How to do it… 77
How it works… 78
[ iv ]
There's more… 79
Using directives 81
Using inline markup 81
See also 82
Designing complex if…elif chains 82
Getting ready 83
How to do it… 84
How it works… 85
There's more… 86
See also 87
Designing a while statement which terminates properly 87
Getting ready 87
How to do it… 88
How it works… 91
See also 92
Avoiding a potential problem with break statements 93
Getting ready 93
How to do it… 94
How it works… 95
There's more… 96
See also 96
Leveraging the exception matching rules 97
Getting ready 98
How to do it… 99
How it works… 100
There's more… 100
See also 101
Avoiding a potential problem with an except: clause 102
Getting ready 102
How to do it… 102
How it works… 103
See also 103
Chaining exceptions with the raise from statement 104
Getting ready 104
How to do it… 104
How it works… 105
There's more… 106
See also 107
Managing a context using the with statement 107
Getting ready 107
[ v ]
How to do it… 108
How it works… 109
There's more… 110
See also 110
Chapter 3: Function Definitions 111
Introduction 111
Designing functions with optional parameters 112
Getting ready 112
How to do it… 112
Particular to General Design 113
General to Particular design 115
How it works… 116
There's more… 117
See also 118
Using super flexible keyword parameters 118
Getting ready 119
How to do it… 119
How it works… 121
There's more… 121
See also 122
Forcing keyword-only arguments with the * separator 122
Getting ready 123
How to do it… 124
How it works… 125
There's more… 125
See also 126
Writing explicit types on function parameters 126
Getting ready 127
How to do it… 128
How it works… 129
There's more… 131
See also 132
Picking an order for parameters based on partial functions 132
Getting ready 132
How to do it… 134
Wrapping a function 134
Creating a partial function with keyword parameters 135
Creating a partial function with positional parameters 135
How it works… 136
There's more… 137
[ vi ]
See also 137
Writing clear documentation strings with RST markup 137
Getting ready 138
How to do it… 138
How it works… 141
There's more… 142
See also 142
Designing recursive functions around Python's stack limits 142
Getting ready 143
How to do it… 144
How it works… 145
There's more… 146
See also 148
Writing reusable scripts with the script library switch 148
Getting ready 148
How to do it… 149
How it works… 151
There's more… 151
See also 152
Chapter 4: Built-in Data Structures – list, set, dict 153
Introduction 153
Choosing a data structure 154
Getting ready 154
How to do it… 154
How it works… 156
There's more… 158
See also 158
Building lists – literals, appending, and comprehensions 159
Getting ready 159
How to do it… 160
Building a list with the append() method 160
Writing a list comprehension 161
Using the list function on a generator expression 162
How it works… 163
There's more… 163
Other ways to extend a list 164
See also 165
Slicing and dicing a list 165
Getting ready 165
[ vii ]
How to do it… 167
How it works… 169
There's more… 169
See also 170
Deleting from a list – deleting, removing, popping, and filtering 170
Getting ready 170
How to do it… 171
Deleting items from a list 171
The remove() method 172
The pop() method 173
The filter() function 173
How it works… 174
There's more… 175
See also 176
Reversing a copy of a list 176
Getting ready 176
How to do it… 178
How it works… 179
See also 179
Using set methods and operators 179
Getting ready 181
How to do it… 182
How it works… 183
There's more… 184
See also 185
Removing items from a set – remove(), pop(), and difference 186
Getting ready 186
How to do it… 187
How it works… 188
There's more… 188
See also 189
Creating dictionaries – inserting and updating 189
Getting ready 190
How to do it… 191
How it works… 192
There's more… 192
See also 194
Removing from dictionaries – the pop() method and the del statement 194
Getting ready 195
How to do it… 196
[ viii ]
How it works… 197
There's more… 197
See also 197
Controlling the order of dict keys 198
Getting ready 198
How to do it… 199
How it works… 200
There's more… 200
See also 201
Handling dictionaries and sets in doctest examples 201
Getting ready 201
How to do it… 202
How it works… 203
There's more… 203
Understanding variables, references, and assignment 204
How to do it… 204
How it works… 205
There's more… 206
See also 207
Making shallow and deep copies of objects 207
Getting ready 208
How to do it… 209
How it works… 210
See also 211
Avoiding mutable default values for function parameters 211
Getting ready 212
How to do it… 213
How it works… 214
There's more… 215
See also 216
Chapter 5: User Inputs and Outputs 217
Introduction 217
Using features of the print() function 218
Getting ready 218
How to do it… 220
How it works… 221
There's more… 222
See also 223
Using input() and getpass() for user input 223
[ ix ]
Getting ready 224
How to do it… 225
How it works… 227
There's more… 228
Input string parsing 228
Interaction via the cmd module 229
See also 229
Debugging with “format”.format_map(vars()) 230
Getting ready 230
How to do it… 231
How it works… 231
There's more… 232
See also 232
Using argparse to get command-line input 232
Getting ready 233
How to do it… 235
How it works… 237
There's more… 238
See also 240
Using cmd for creating command-line applications 240
Getting ready 242
How to do it… 243
How it works… 246
There's more… 247
See also 248
Using the OS environment settings 248
Getting ready 249
How to do it… 250
How it works… 251
There's more… 252
See also 252
Chapter 6: Basics of Classes and Objects 253
Introduction 253
Using a class to encapsulate data and processing 254
Getting ready 254
How to do it… 255
How it works… 258
There's more… 259
See also 260
[ x ]
Designing classes with lots of processing 260
Getting ready 260
How to do it… 263
How it works… 265
There's more… 266
See also 268
Designing classes with little unique processing 268
Getting ready 268
How to do it… 269
Stateless objects 269
Stateful objects with a new class 270
Stateful objects using an existing class 271
How it works… 271
There's more… 272
See also 273
Optimizing small objects with __slots__ 273
Getting ready 273
How to do it… 274
How it works… 276
There's more… 276
See also 277
Using more sophisticated collections 277
Getting ready 277
How to do it… 278
How it works… 280
There's more… 281
See also 282
Extending a collection – a list that does statistics 282
Getting ready 282
How to do it… 282
How it works… 284
There's more… 285
See also 286
Using properties for lazy attributes 286
Getting ready… 286
How to do it… 288
How it works… 290
There's more… 291
See also… 291
Using settable properties to update eager attributes 292
[ xi ]
Getting ready 292
How to do it… 293
How it works… 296
There's more… 296
Initialization 296
Calculation 297
See also 298
Chapter 7: More Advanced Class Design 299
Introduction 299
Choosing between inheritance and extension – the is-a question 300
Getting ready 301
How to do it… 302
Wrapping – aggregation and composition 303
Extending – inheritance 305
How it works… 306
There's more… 307
See also 308
Separating concerns via multiple inheritance 309
Getting ready 309
How to do it… 310
How it works… 312
There's more… 313
See also 315
Leveraging Python's duck typing 315
Getting ready 316
How to do it… 316
How it works… 318
There's more… 319
See also 319
Managing global and singleton objects 320
Getting ready 320
How to do it… 321
Module global variable 321
Class-level static variable 323
How it works… 324
There's more… 325
Using more complex structures – maps of lists 325
Getting ready 326
How to do it… 326
How it works… 327
[ xii ]
There's more… 328
See also 328
Creating a class that has orderable objects 329
Getting ready 329
How to do it… 330
How it works… 332
There's more… 333
See also 334
Defining an ordered collection 335
Getting ready 336
How to do it… 337
How it works… 339
There's more… 340
See also 342
Deleting from a list of mappings 342
Getting ready 343
How to do it… 345
How it works… 346
There's more… 347
See also 348
Chapter 8: Functional and Reactive Programming Features 349
Introduction 349
Writing generator functions with the yield statement 351
Getting ready 351
How to do it… 352
How it works… 355
There's more… 357
See also 359
Using stacked generator expressions 359
Getting ready 360
How to do it… 361
How it works… 364
There's more… 366
Namespace instead of list 367
See also 369
Applying transformations to a collection 369
Getting ready… 370
How to do it… 372
How it works… 373
[ xiii ]
There's more… 373
See also… 374
Picking a subset – three ways to filter 374
Getting ready… 375
How to do it… 377
How it works… 378
There's more… 379
See also… 380
Summarizing a collection – how to reduce 380
Getting ready 381
How to do it… 383
How it works… 384
There's more… 384
Maxima and minima 385
Potential for abuse 386
Combining map and reduce transformations 386
Getting ready 387
How to do it… 388
How it works… 392
There's more… 393
See also 393
Implementing “there exists” processing 394
Getting ready 394
How to do it… 395
How it works… 396
There's more… 397
The itertools module 398
Creating a partial function 399
Getting ready 400
How to do it… 402
Using functools.partial() 403
Creating a lambda object 403
How it works… 403
There's more… 404
Simplifying complex algorithms with immutable data structures 406
Getting ready 407
How to do it… 410
How it works… 411
There's more… 412
Writing recursive generator functions with the yield from statement 413
[ xiv ]
Getting ready 414
How to do it… 416
How it works… 417
There's more… 417
See also 419
Chapter 9: Input/Output, Physical Format, and Logical Layout 420
Introduction 420
Using pathlib to work with filenames 422
Getting ready 423
How to do it… 424
Making the output filename by changing the input suffix 424
Making a number of sibling output files with distinct names 425
Creating a directory and a number of files 426
Comparing file dates to see which is newer 427
Removing a file 428
Finding all files that match a given pattern 429
How it works… 429
There's more… 430
See also 431
Reading and writing files with context managers 432
Getting ready 432
How to do it… 434
How it works… 435
There's more… 436
See also 436
Replacing a file while preserving the previous version 437
Getting ready 437
How to do it… 438
How it works… 440
There's more… 440
See also 441
Reading delimited files with the CSV module 441
Getting ready 442
How to do it… 443
How it works… 444
There's more… 445
See also 447
Reading complex formats using regular expressions 447
Getting ready 447
How to do it… 449
[ xv ]
Defining the parse function 449
Using the parse function 450
How it works… 451
There's more… 452
See also 453
Reading JSON documents 453
Getting ready 454
How to do it… 456
How it works… 457
There's more… 457
Serializing a complex data structure 458
Deserializing a complex data structure 460
See also 461
Reading XML documents 461
Getting ready 462
How to do it… 465
How it works… 465
There's more… 466
See also 468
Reading HTML documents 468
Getting ready 469
How to do it… 471
How it works… 472
There's more… 474
See also 475
Upgrading CSV from DictReader to namedtuple reader 475
Getting ready 475
How to do it… 476
How it works… 478
There's more… 478
See also 480
Upgrading CSV from a DictReader to a namespace reader 481
Getting ready 481
How to do it… 482
How it works… 483
There's more… 483
See also 485
Using multiple contexts for reading and writing files 485
Getting ready 485
How to do it… 487
[ xvi ]
How it works… 489
There's more… 490
See also 491
Chapter 10: Statistical Programming and Linear Regression 492
Introduction 492
Using the built-in statistics library 493
Getting ready 493
How to do it… 497
How it works… 499
There's more… 500
Average of values in a Counter 502
Getting ready 502
How to do it… 504
How it works… 505
There's more… 505
See also 507
Computing the coefficient of a correlation 507
Getting ready 508
How to do it… 509
How it works… 511
There's more… 512
Computing regression parameters 512
Getting ready 513
How to do it… 514
How it works… 516
There's more… 516
Computing an autocorrelation 517
Getting ready 517
How to do it… 521
How it works… 523
There's more… 523
Long-term model 524
See also 525
Confirming that the data is random – the null hypothesis 525
Getting ready 525
How to do it… 527
How it works… 530
There's more… 531
See also 532
[ xvii ]
Locating outliers 532
Getting ready 533
How to do it… 534
How it works… 536
There's more… 537
See also 539
Analyzing many variables in one pass 539
Getting ready 540
How to do it… 542
How it works… 544
There's more… 544
Using map() 547
See also 547
Chapter 11: Testing 548
Introduction 548
Using docstrings for testing 549
Getting ready 550
How to do it… 551
Writing examples for stateless functions 552
Writing examples for stateful objects 552
How it works… 554
There's more… 555
See also 556
Testing functions that raise exceptions 557
Getting ready 557
How to do it… 558
How it works… 559
There's more… 560
See also 561
Handling common doctest issues 561
Getting ready 562
How to do it… 564
Writing doctest examples for mapping or set values 564
Writing doctest examples for floating-point values 565
How it works… 566
There's more… 566
See also 567
Creating separate test modules and packages 568
Getting ready 568
How to do it… 570
[ xviii ]
How it works… 571
There's more… 572
Some other assertions 574
Separate tests directory 575
See also 576
Combining unittest and doctest tests 576
Getting ready 576
How to do it… 578
How it works… 579
There's more… 580
See also 580
Testing things that involve dates or times 580
Getting ready 581
How to do it… 582
How it works… 584
There's more… 585
See also 587
Testing things that involve randomness 587
Getting ready 587
How to do it… 588
How it works… 589
There's more… 590
See also 591
Mocking external resources 591
Getting ready 592
Creating an entry document in the entrylog collection 593
Seeing a typical response 595
Client class for database access 596
How to do it… 598
How it works… 601
Creating a context manager 601
Creating a dynamic, stateful test 602
Mocking a complex object 603
Using the load_tests protocol 604
There's more… 605
See also 605
Chapter 12: Web Services 606
Introduction 606
Implementing web services with WSGI 609
Getting ready 609
[ xix ]
How to do it… 614
How it works… 617
There's more… 618
See also 621
Using the Flask framework for RESTful APIs 622
Getting ready 622
How to do it… 623
How it works… 626
There's more… 627
See also 629
Parsing the query string in a request 629
Getting ready 631
How to do it… 631
How it works… 633
There's more… 634
See also 636
Making REST requests with urllib 636
Getting ready 636
How to do it… 638
How it works… 641
There's more… 642
The OpenAPI (Swagger) specification 643
Adding Swagger to the server 643
See also 645
Parsing the URL path 645
Getting ready 646
How to do it… 647
Server 647
Client 650
How it works… 652
Deck slicing 653
Client side 654
There's more… 655
Providing a Swagger specification 655
Using a Swagger specification 658
See also 658
Parsing a JSON request 658
Getting ready 659
How to do it… 660
Swagger specification 660
Server 663
[ xx ]
Client 664
How it works… 666
There's more… 667
Location header 668
Additional resources 668
Query for a specific player 669
Exception handling 670
See also 671
Implementing authentication for web services 671
Getting ready 673
Configuring SSL 673
Users and credentials 674
Flask view function decorator 676
How to do it… 676
Defining the User class 677
Defining a view decorator 680
Creating the server 681
Creating an example client 683
How it works… 686
There's more… 687
Creating a command-line interface 689
Building the Authentication header 689
See also 690
Chapter 13: Application Integration 691
Introduction 691
Finding configuration files 692
Getting ready 693
Why so many choices? 694
How to do it… 695
How it works… 697
There's more… 697
See also 702
Using YAML for configuration files 702
Getting ready 702
How to do it… 703
How it works… 704
There's more… 706
See also 709
Using Python for configuration files 709
Getting ready 710
How to do it… 711
[ xxi ]
How it works… 711
There's more… 712
See also 713
Using class-as-namespace for configuration 713
Getting ready 714
How to do it… 715
How it works… 716
There's more… 716
Configuration representation 718
See also 719
Designing scripts for composition 720
Getting ready 720
How to do it… 722
How it works… 724
There's more… 726
Designing as a class hierarchy 727
See also 728
Using logging for control and audit output 729
Getting ready 729
How to do it… 733
How it works… 737
There's more… 739
Combining two applications into one 740
Getting ready 740
How to do it… 742
How it works… 743
There's more… 744
Refactoring 744
Concurrency 745
Logging 747
See also 748
Combining many applications using the Command design pattern 748
Getting ready 748
How to do it… 749
How it works… 750
There's more… 751
See also 753
Managing arguments and configuration in composite applications 753
Getting ready 754
How to do it… 755
[ xxii ]
How it works… 757
The Command design pattern 758
There's more… 759
See also 760
Wrapping and combining CLI applications 760
Getting ready 760
How to do it… 761
How it works… 762
There's more… 763
Unit test 764
See also 767
Wrapping a program and checking the output 767
Getting ready 767
How to do it… 768
How it works… 770
There's more… 771
See also 772
Controlling complex sequences of steps 772
Getting ready 772
How to do it… 773
How it works… 775
There's more… 776
Building conditional processing 776
See also 777
Index 778

标签: Cookbook python DER MOD OD

实例下载地址

MODERN-PYTHON-COOKBOOK

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警