Advanced Guide to Python 3 Programming [1st edition 2019] 9783030259426, 9783030259433, 3030259420, 3030259439


144 118 22MB

English Pages 497 [494] Year 2019

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Chapter Organisation......Page 7
What You Need......Page 8
Useful Python Resources......Page 9
Conventions......Page 10
Example Code and Sample Solutions......Page 11
Contents......Page 13
1.1 Introduction......Page 27
Computer Graphics......Page 29
2.1 Introduction......Page 30
2.3 The Graphical Computer Era......Page 31
2.4 Interactive and Non Interactive Graphics......Page 32
2.5 Pixels......Page 33
2.8 Python and Computer Graphics......Page 35
2.10 Online Resources......Page 36
3.2.1 The Turtle Module......Page 37
3.2.2 Basic Turtle Graphics......Page 38
3.2.3 Drawing Shapes......Page 41
3.3 Other Graphics Libraries......Page 43
3.4.1 PyOpenGL......Page 44
3.6 Exercises......Page 45
4.1 Creating Computer Art......Page 46
4.2 A Computer Art Generator......Page 48
4.3.1 The Koch Snowflake......Page 51
4.3.2 Mandelbrot Set......Page 54
4.5 Exercises......Page 56
5.1 Introduction......Page 57
5.2 Matplotlib......Page 58
5.3 Plot Components......Page 59
5.4 Matplotlib Architecture......Page 60
5.4.1 Backend Layer......Page 61
5.4.2 The Artist Layer......Page 62
5.4.3 The Scripting Layer......Page 63
5.5 Online Resources......Page 64
6.2 The pyplot API......Page 65
6.3 Line Graphs......Page 66
6.3.1 Coded Format Strings......Page 68
6.4 Scatter Graph......Page 69
6.4.1 When to Use Scatter Graphs......Page 71
6.5 Pie Charts......Page 72
6.5.1 Expanding Segments......Page 74
6.5.2 When to Use Pie Charts......Page 75
6.6 Bar Charts......Page 76
6.6.1 Horizontal Bar Charts......Page 77
6.6.2 Coloured Bars......Page 78
6.6.3 Stacked Bar Charts......Page 79
6.6.4 Grouped Bar Charts......Page 80
6.7 Figures and Subplots......Page 82
6.8 3D Graphs......Page 85
6.9 Exercises......Page 87
7.1 Introduction......Page 88
7.2 GUIs and WIMPS......Page 89
7.3 Windowing Frameworks for Python......Page 90
7.3.2 Platform-Specific GUI Libraries......Page 91
7.4 Online Resources......Page 92
8.1 The wxPython Library......Page 93
8.1.1 wxPython Modules......Page 94
8.1.3 A Simple Example......Page 95
8.2 The wx.App Class......Page 96
8.3 Window Classes......Page 98
8.4 Widget/Control Classes......Page 100
8.5 Dialogs......Page 101
8.6 Arranging Widgets Within a Container......Page 102
8.7 Drawing Graphics......Page 104
8.9.1 Simple GUI Application......Page 106
9.2 Event Definitions......Page 107
9.3 Types of Events......Page 108
9.5 Implementing Event Handling......Page 109
9.6 An Interactive wxPython GUI......Page 112
9.8.1 Simple GUI Application......Page 116
9.8.2 GUI Interface to a Tic Tac Toe Game......Page 118
10.2 The PyDraw Application......Page 119
10.3 The Structure of the Application......Page 120
10.3.1 Model, View and Controller Architecture......Page 121
10.3.2 PyDraw MVC Architecture......Page 122
10.3.3 Additional Classes......Page 123
10.3.4 Object Relationships......Page 124
10.4.1 The PyDrawApp......Page 125
10.4.3 Changing the Application Mode......Page 126
10.4.4 Adding a Graphic Object......Page 127
10.5.1 The PyDrawConstants Class......Page 128
10.5.2 The PyDrawFrame Class......Page 129
10.5.3 The PyDrawMenuBar Class......Page 130
10.5.5 The PyDrawController Class......Page 131
10.5.7 The DrawingPanel Class......Page 133
10.5.8 The DrawingController Class......Page 134
10.5.10 The Square Class......Page 135
10.5.12 The Line Class......Page 136
10.7 Exercises......Page 137
Computer Games......Page 138
11.2 Games Frameworks and Libraries......Page 139
11.3 Python Games Development......Page 140
11.5 Online Resources......Page 141
12.1 Introduction......Page 142
12.2 The Display Surface......Page 143
12.3.1 Event Types......Page 144
12.3.2 Event Information......Page 145
12.3.3 The Event Queue......Page 146
12.4 A First pygame Application......Page 147
12.5 Further Concepts......Page 150
12.6 A More Interactive pygame Application......Page 153
12.8 pygame Modules......Page 155
12.9 Online Resources......Page 156
13.1 Creating a Spaceship Game......Page 157
13.2 The Main Game Class......Page 158
13.3 The GameObject Class......Page 160
13.4 Displaying the Starship......Page 161
13.5 Moving the Spaceship......Page 162
13.6 Adding a Meteor Class......Page 166
13.8 Identifying a Collision......Page 168
13.10 Increasing the Number of Meteors......Page 170
13.11 Pausing the Game......Page 171
13.12 Displaying the Game Over Message......Page 172
13.13 The StarshipMeteors Game......Page 173
13.15 Exercises......Page 178
Testing......Page 179
14.2 Types of Testing......Page 180
14.3 What Should Be Tested?......Page 181
14.4 Testing Software Systems......Page 182
14.4.1 Unit Testing......Page 183
14.4.3 System Testing......Page 184
14.5 Automating Testing......Page 185
14.6 Test Driven Development......Page 186
14.6.1 The TDD Cycle......Page 187
14.7.1 Testability Rules of Thumb......Page 188
14.9 Book Resources......Page 189
15.2 What Is PyTest?......Page 190
15.4 A Simple PyTest Example......Page 191
15.5 Working with PyTest......Page 194
15.6 Parameterised Tests......Page 198
15.8 Exercises......Page 200
16.1 Introduction......Page 202
16.2 Why Mock?......Page 203
16.3 What Is Mocking?......Page 205
16.4 Common Mocking Framework Concepts......Page 206
16.6 The unittest.mock Library......Page 207
16.6.1 Mock and Magic Mock Classes......Page 208
16.6.2 The Patchers......Page 209
16.6.3 Mocking Returned Objects......Page 210
16.6.4 Validating Mocks Have Been Called......Page 211
16.7.2 Mock Classes......Page 212
16.7.3 Attributes on Mock Classes......Page 213
16.7.6 Raising Exceptions with Mocks......Page 214
16.7.8 Using Patch as a Context Manager......Page 215
16.9 Patch Order Issues......Page 216
16.11 Mocking Considerations......Page 217
16.13 Exercises......Page 218
File Input/Output......Page 220
17.1 Introduction......Page 221
17.2 File Attributes......Page 223
17.3 Paths......Page 225
17.4 File Input/Output......Page 226
17.5 Sequential Access Versus Random Access......Page 227
17.7 Online Resources......Page 228
18.2 Obtaining References to Files......Page 229
18.3 Reading Files......Page 231
18.5 Writing Data to Files......Page 232
18.7 The Fileinput Module......Page 233
18.9 Deleting Files......Page 234
18.10 Random Access Files......Page 235
18.11 Directories......Page 236
18.12 Temporary Files......Page 238
18.13 Working with Paths......Page 239
18.15 Exercise......Page 243
19.2 What is a Stream?......Page 245
19.3 Python Streams......Page 246
19.4 IOBase......Page 247
19.6 Binary IO/Buffered IO Classes......Page 248
19.7 Text Stream Classes......Page 250
19.8 Stream Properties......Page 251
19.10 Returning to the open() Function......Page 252
19.12 Exercise......Page 254
20.2 CSV Files......Page 255
20.2.1 The CSV Writer Class......Page 256
20.2.2 The CSV Reader Class......Page 257
20.2.3 The CSV DictWriter Class......Page 258
20.2.4 The CSV DictReader Class......Page 259
20.4 Exercises......Page 260
21.2 Excel Files......Page 262
21.5 Working with Cells......Page 263
21.6 Sample Excel File Creation Application......Page 264
21.7 Loading a Workbook from an Excel File......Page 266
21.9 Exercises......Page 267
22.2 What Are Regular Expressions?......Page 269
22.3 Regular Expression Patterns......Page 270
22.3.2 Special Sequences......Page 271
22.3.3 Sets......Page 272
22.5.1 Using Raw Strings......Page 273
22.5.3 The Match Object......Page 274
22.5.4 The search() Function......Page 275
22.5.5 The match() Function......Page 276
22.5.7 The findall() Function......Page 277
22.5.9 The split() Function......Page 278
22.5.10 The sub() Function......Page 279
22.5.11 The compile() Function......Page 280
22.7 Exercises......Page 282
Database Access......Page 284
23.2 What Is a Database?......Page 285
23.2.1 Data Relationships......Page 286
23.2.2 The Database Schema......Page 287
23.3 SQL and Databases......Page 289
23.4 Data Manipulation Language......Page 290
23.5 Transactions in Databases......Page 291
23.6 Further Reading......Page 292
24.2 The DB-API......Page 293
24.2.2 The Connection Object......Page 294
24.2.3 The Cursor Object......Page 295
24.2.5 Generating Errors......Page 296
24.2.6 Row Descriptions......Page 297
24.4 Online Resources......Page 298
25.2 Working with the PyMySQL Module......Page 300
25.2.2 Connect to the Database......Page 301
25.2.4 Using the Cursor Object......Page 302
25.2.6 Fetching Results......Page 303
25.3 Complete PyMySQL Query Example......Page 304
25.4 Inserting Data to the Database......Page 305
25.5 Updating Data in the Database......Page 307
25.6 Deleting Data in the Database......Page 308
25.7 Creating Tables......Page 309
25.9 Exercises......Page 310
Logging......Page 311
26.2 Why Log?......Page 312
26.4 What Should You Log?......Page 313
26.5 What Not to Log......Page 314
26.6 Why Not Just Use Print?......Page 315
26.7 Online Resources......Page 316
27.1 The Logging Module......Page 317
27.2 The Logger......Page 318
27.3 Controlling the Amount of Information Logged......Page 319
27.4 Logger Methods......Page 321
27.5 Default Logger......Page 322
27.6 Module Level Loggers......Page 323
27.7 Logger Hierarchy......Page 324
27.8.2 Formatting Log Output......Page 325
27.10 Exercises......Page 328
28.2 Handlers......Page 329
28.2.1 Setting the Root Output Handler......Page 331
28.2.2 Programmatically Setting the Handler......Page 332
28.2.3 Multiple Handlers......Page 334
28.3 Filters......Page 335
28.4 Logger Configuration......Page 336
28.5 Performance Considerations......Page 339
28.6 Exercises......Page 340
Concurrency and Parallelism......Page 341
29.2 Concurrency......Page 342
29.3 Parallelism......Page 344
29.5 Grid Computing......Page 345
29.7 Object Orientation and Concurrency......Page 347
29.8 Threads V Processes......Page 348
29.10 Online Resources......Page 349
30.3 Thread States......Page 351
30.4 Creating a Thread......Page 352
30.5 Instantiating the Thread Class......Page 353
30.6 The Thread Class......Page 354
30.8 Passing Arguments to a Thread......Page 356
30.9 Extending the Thread Class......Page 358
30.10 Daemon Threads......Page 359
30.11 Naming Threads......Page 360
30.12 Thread Local Data......Page 361
30.13 Timers......Page 362
30.14 The Global Interpreter Lock......Page 363
30.16 Exercise......Page 364
31.2 The Process Class......Page 366
31.3 Working with the Process Class......Page 368
31.4 Alternative Ways to Start a Process......Page 369
31.5 Using a Pool......Page 371
31.6 Exchanging Data Between Processes......Page 375
31.7.1 Process Shared Memory......Page 377
31.8 Online Resources......Page 378
31.9 Exercises......Page 379
32.2 Using a Barrier......Page 380
32.3 Event Signalling......Page 383
32.4 Synchronising Concurrent Code......Page 385
32.5 Python Locks......Page 386
32.6 Python Conditions......Page 389
32.7 Python Semaphores......Page 391
32.8 The Concurrent Queue Class......Page 392
32.10 Exercises......Page 394
33.2 The Need for a Future......Page 397
33.3 Futures in Python......Page 398
33.3.2 Simple Example Future......Page 399
33.4 Running Multiple Futures......Page 401
33.4.1 Waiting for All Futures to Complete......Page 402
33.4.2 Processing Results as Completed......Page 404
33.5 Processing Future Results Using a Callback......Page 405
33.7 Exercises......Page 407
34.2 Asynchronous IO......Page 408
34.3 Async IO Event Loop......Page 409
34.4.1 Using Async and Await......Page 410
34.5 Async IO Tasks......Page 412
34.6.1 Collating Results from Multiple Tasks......Page 415
34.6.2 Handling Task Results as They Are Made Available......Page 416
34.7 Online Resources......Page 417
34.8 Exercises......Page 418
Reactive Programming......Page 419
35.2 What Is a Reactive Application?......Page 420
35.4 The Observer Pattern......Page 421
35.5 Hot and Cold Observables......Page 422
35.5.3 Implications of Hot and Cold Observables......Page 423
35.7 Advantages of Reactive Programming......Page 424
35.10 Online Resources......Page 425
35.11 Reference......Page 426
36.2 Observables in RxPy......Page 427
36.3 Observers in RxPy......Page 428
36.4 Multiple Subscribers/Observers......Page 430
36.5 Subjects in RxPy......Page 431
36.6 Observer Concurrency......Page 433
36.6.1 Available Schedulers......Page 435
36.8 Exercises......Page 436
37.2 Reactive Programming Operators......Page 437
37.3 Piping Operators......Page 438
37.5 Transformational Operators......Page 439
37.6 Combinatorial Operators......Page 441
37.7 Filtering Operators......Page 442
37.8 Mathematical Operators......Page 443
37.9 Chaining Operators......Page 444
37.11 Exercises......Page 446
Network Programming......Page 447
38.2 Sockets......Page 448
38.4 Addressing Services......Page 449
38.5 Localhost......Page 450
38.6 Port Numbers......Page 451
38.8 Sockets and Web Services in Python......Page 452
38.9 Online Resources......Page 453
39.2 Socket to Socket Communication......Page 454
39.4.1 The System Structure......Page 455
39.4.2 Implementing the Server Application......Page 456
39.6 Implementing the Client Application......Page 458
39.7 The Socketserver Module......Page 460
39.8 HTTP Server......Page 462
39.10 Exercises......Page 466
40.2 RESTful Services......Page 468
40.3 A RESTful API......Page 469
40.4 Python Web Frameworks......Page 470
40.6.1 Using JSON......Page 471
40.6.3 A Simple Service......Page 472
40.6.4 Providing Routing Information......Page 473
40.6.5 Running the Service......Page 474
40.6.6 Invoking the Service......Page 475
40.7 Online Resources......Page 476
41.2 The Design......Page 478
41.3 The Domain Model......Page 479
41.4 Encoding Books Into JSON......Page 481
41.5 Setting Up the GET Services......Page 483
41.6 Deleting a Book......Page 485
41.7 Adding a New Book......Page 486
41.8 Updating a Book......Page 488
41.9 What Happens if We Get It Wrong?......Page 489
41.10 Bookshop Services Listing......Page 491
41.11 Exercises......Page 494

Advanced Guide to Python 3 Programming [1st edition 2019]
 9783030259426, 9783030259433, 3030259420, 3030259439

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Recommend Papers