IronPython in action 9781933988337

In 2005, Microsoft quietly announced an initiative to bring dynamic languages to the .NET platform. The starting point f

356 140 14MB

English Pages [494] Year 2009

Report DMCA / Copyright

DOWNLOAD FILE

IronPython in action
 9781933988337

Table of contents :
Front Cover......Page 1
brief contents......Page 8
contents......Page 10
foreword......Page 18
preface......Page 21
acknowledgments......Page 23
about this book......Page 24
Roadmap......Page 25
Code conventions and downloads......Page 26
About the authors......Page 27
About the cover illustration......Page 28
Getting started with IronPython......Page 30
A new language for .NET......Page 32
1.1 An introduction to IronPython......Page 34
1.1.1 What is IronPython?......Page 35
1.1.2 A brief history of IronPython......Page 38
1.1.3 IronPython for Python programmers......Page 40
1.1.4 IronPython for .NET programmers......Page 42
1.2.1 Dynamic languages on .NET and the DLR......Page 44
1.2.2 Silverlight: a new CLR......Page 47
1.2.3 The Python programming language......Page 49
1.2.4 Multiple programming paradigms......Page 51
1.3.1 Using the interactive interpreter......Page 52
1.3.3 Live objects and the interactive interpreter......Page 54
1.3.4 Object introspection with dir and help......Page 56
1.4 Summary......Page 57
Introduction to Python......Page 58
2.1 An overview of Python......Page 60
2.1.1 Python datatypes......Page 61
2.1.2 Names, objects, and references......Page 69
2.2 Python: basic constructs......Page 70
2.2.1 Statements and expressions......Page 71
2.2.2 Conditionals and loops......Page 72
2.2.3 Functions......Page 73
2.2.4 Built-in functions......Page 74
2.2.5 Classes......Page 76
2.3.1 Exception handling......Page 79
2.3.2 Closures and scoping rules......Page 81
2.3.3 List comprehensions......Page 83
2.3.4 Modules, packages, and importing......Page 84
2.3.6 The Python standard library......Page 87
2.4 Summary......Page 90
.NET objects and IronPython......Page 91
3.1.1 Translating MSDN documentation into IronPython......Page 92
3.1.2 The Form class......Page 94
3.2.1 Methods and properties inherited from Control......Page 96
3.2.2 Adding a Label to the Form: ControlCollection......Page 97
3.2.3 Configuring the Label: the Color structure......Page 99
3.2.4 The FormBorderStyle enumeration......Page 100
3.2.5 Hello World with Form and Label......Page 101
3.3 Handling events......Page 102
3.3.1 Delegates and the MouseMove event......Page 103
3.3.2 Event handlers in IronPython......Page 104
3.4 Subclassing .NET types......Page 106
3.5 Summary......Page 107
Core development techniques......Page 108
Writing an application and design patterns with IronPython......Page 110
4.1.1 Python and protocols......Page 111
4.1.2 Duck typing in action......Page 112
4.2 Model-View-Controller in IronPython......Page 113
4.2.1 Introducing the running example......Page 114
4.2.2 The view layer: creating a user interface......Page 115
4.2.3 A data model......Page 117
4.2.4 A controller class......Page 118
4.3 The command pattern......Page 120
4.3.1 The SaveFileDialog......Page 121
4.3.2 Writing files: the .NET and Python ways......Page 122
4.3.3 Handling exceptions and the system message box......Page 124
4.3.4 The SaveCommand......Page 127
4.4 Integrating commands with our running example......Page 129
4.4.1 Menu classes and lambda......Page 130
4.4.2 .NET classes: ToolBar and images......Page 132
4.4.3 Bringing the GUI to life......Page 134
4.5 Summary......Page 137
First-class functions in action with XML......Page 139
5.1.1 Higher order functions......Page 140
5.1.3 A null-argument-checking decorator......Page 142
5.2 Representing documents with XML......Page 143
5.2.1 The .NET XmlWriter......Page 145
5.2.2 A DocumentWriter Class......Page 147
5.2.3 An alternative with an inner function......Page 149
5.3.1 XMLReader......Page 150
5.3.2 An IronPython XmlDocumentReader......Page 152
5.4 Handler functions for MultiDoc XML......Page 155
5.5 The Open command......Page 158
5.6 Summary......Page 161
Properties, dialogs, and Visual Studio......Page 162
6.1.1 Python properties......Page 163
6.1.2 Adding the OpenCommand......Page 167
6.2.1 Remove pages: OK and Cancel dialog box......Page 168
6.2.2 Rename pages: a modal dialog......Page 172
6.2.3 Visual Studio Express and IronPython......Page 177
6.2.4 Adding pages: code reuse in action......Page 180
6.2.5 Wiring the commands to the view......Page 181
6.3 Object serializing with BinaryFormatter......Page 183
6.4 Summary......Page 185
Agile testing: where dynamic typing shines......Page 186
7.1 The unittest module......Page 187
7.1.1 Creating a TestCase......Page 188
7.1.2 setUp and tearDown......Page 191
7.1.3 Test suites with multiple modules......Page 192
7.2.1 Mock objects......Page 195
7.2.2 Modifying live objects: the art of the monkey patch......Page 198
7.2.3 Mocks and dependency injection......Page 202
7.3 Functional testing......Page 204
7.3.1 Interacting with the GUI thread......Page 205
7.3.2 An AsyncExecutor for asynchronous interactions......Page 207
7.3.3 The functional test: making MultiDoc dance......Page 208
7.4 Summary......Page 211
Metaprogramming, protocols, and more......Page 212
8.1.1 A myriad of magic methods......Page 213
8.1.2 Operator overloading......Page 216
8.1.3 Iteration......Page 220
8.1.4 Generators......Page 221
8.1.5 Equality and inequality......Page 222
8.2 Dynamic attribute access......Page 224
8.2.1 Attribute access with built-in functions......Page 225
8.2.2 Attribute access through magic methods......Page 226
8.2.3 Proxying attribute access......Page 227
8.3 Metaprogramming......Page 228
8.3.1 Introduction to metaclasses......Page 229
8.3.2 Uses of metaclasses......Page 230
8.3.3 A profiling metaclass......Page 231
8.4.1 .NET arrays......Page 234
8.4.3 out, ref, params, and pointer parameters......Page 237
8.4.4 Value types......Page 239
8.4.5 Interfaces......Page 240
8.4.6 Attributes......Page 241
8.4.7 Static compilation of IronPython code......Page 242
8.5 Summary......Page 243
IronPython and advanced .NET......Page 244
WPF and IronPython......Page 246
9.1 Hello World with WPF and IronPython......Page 249
9.1.1 WPF from code......Page 250
9.1.2 Hello World from XAML......Page 252
9.2 WPF in action......Page 255
9.2.1 Layout with the Grid......Page 256
9.2.2 The WPF ComboBox and CheckBox......Page 258
9.2.3 The Image control......Page 260
9.2.4 The Expander......Page 261
9.2.5 The ScrollViewer......Page 262
9.2.6 The TextBlock: a lightweight document control......Page 263
9.3 XPS documents and flow content......Page 265
9.3.1 FlowDocument viewer classes......Page 267
9.3.2 Flow document markup......Page 268
9.3.3 Document XAML and object tree processing......Page 269
9.4 Summary......Page 272
Windows system administration with IronPython......Page 273
10.1.1 Simple scripts......Page 274
10.1.2 Shell scripting with IronPython......Page 275
10.2.1 System.Management......Page 280
10.2.2 Connecting to remote computers......Page 284
10.3.1 Using PowerShell from IronPython......Page 289
10.3.2 Using IronPython from PowerShell......Page 293
10.4 Summary......Page 300
IronPython and ASP.NET......Page 302
11.1.1 Web controls......Page 303
11.1.3 Rendering, server code, and the page lifecycle......Page 304
11.2 Adding IronPython to ASP.NET......Page 305
11.2.1 Writing a first application......Page 306
11.2.2 Handling an event......Page 308
11.3.1 The App_Script folder......Page 309
11.3.2 The Global.py file......Page 310
11.4 A web-based MultiDoc Viewer......Page 311
11.4.1 Page structure......Page 312
11.4.2 Code-behind......Page 314
11.5 Editing MultiDocs......Page 316
11.5.1 Swapping controls......Page 317
11.5.2 Handling view state......Page 318
11.5.3 Additional events......Page 321
11.6 Converting the Editor into a user control......Page 323
11.6.1 View state again......Page 324
11.6.2 Adding parameters......Page 325
11.7 Summary......Page 327
Databases and web services......Page 328
12.1 Relational databases and ADO.NET......Page 329
12.1.1 Trying it out using PostgreSQL......Page 330
12.1.2 Connecting to the database......Page 332
12.1.3 Executing commands......Page 333
12.1.4 Setting parameters......Page 334
12.1.5 Querying the database......Page 335
12.1.6 Reading multirow results......Page 336
12.1.7 Using transactions......Page 338
12.1.8 DataAdapters and DataSets......Page 340
12.2 Web services......Page 342
12.2.1 Using a simple web service......Page 343
12.2.2 Using SOAP services from IronPython......Page 346
12.2.3 REST services in IronPython......Page 348
12.3 Summary......Page 357
Silverlight: IronPython in the browser......Page 358
13.1 Introduction to Silverlight......Page 359
13.1.1 Dynamic Silverlight......Page 361
13.1.2 Your Python application......Page 363
13.1.3 Silverlight controls......Page 364
13.1.4 Packaging a Silverlight application......Page 368
13.2.1 Cross-domain policies......Page 370
13.2.2 Debugging Silverlight applications......Page 372
13.2.3 The user interface......Page 373
13.2.4 Accessing network resources......Page 375
13.2.5 Threads and dispatching onto the UI thread......Page 378
13.2.6 IsolatedStorage in the browser......Page 380
13.3.1 The MediaElement video player......Page 382
13.3.2 Accessing the browser DOM......Page 383
13.4 Summary......Page 385
Reaching out with IronPython......Page 386
Extending IronPython with C#/VB.NET......Page 388
14.1 Writing a class library for IronPython......Page 389
14.1.1 Working with Visual Studio or MonoDevelop......Page 390
14.1.2 Python objects from class libraries......Page 391
14.1.3 Calling unmanaged code with the P/Invoke attribute......Page 395
14.1.4 Methods with attributes through subclassing......Page 399
14.2.1 Providing dynamic attribute access......Page 403
14.2.3 APIs with keyword and multiple arguments......Page 407
14.3 Compiling and using assemblies at runtime......Page 411
14.4 Summary......Page 414
Embedding the IronPython engine......Page 415
15.1.1 The IronPython engine......Page 416
15.1.2 Executing a Python file......Page 418
15.2 IronPython as a scripting engine......Page 422
15.2.1 Setting and fetching variables from a scope......Page 423
15.2.2 Providing modules and assemblies for the engine......Page 427
15.2.3 Python code as an embedded resource......Page 429
15.3 Python plugins for .NET applications......Page 431
15.3.1 A plugin class and registry......Page 432
15.3.2 Autodiscovery of user plugins......Page 433
15.3.3 Diverting standard output......Page 435
15.3.4 Calling the user plugins......Page 436
15.4.1 Expressions, functions, and Python types......Page 438
15.4.2 Dynamic operations with ObjectOperations......Page 441
15.4.3 The built-in Python functions and modules......Page 443
15.4.4 The future of interacting with dynamic objects......Page 446
15.5 Summary......Page 447
A.1 Namespaces......Page 448
A.3 Classes......Page 449
A.4 Attributes......Page 450
A.6 Enums......Page 451
A.8 Methods......Page 452
A.8.3 Parameter passing......Page 453
A.10 Events......Page 454
A.12 Properties and indexers......Page 455
A.13.2 do loop......Page 456
A.15 if......Page 457
A.17 try/catch/finally and throw......Page 458
A.20 null......Page 459
A.22 Operators......Page 460
A.23 Generics......Page 461
appendix B: Python magic methods......Page 462
B.2 Comparison......Page 463
B.4 Containers and iteration......Page 464
B.4.1 Mapping and sequence protocol methods......Page 465
B.5 Conversion to string......Page 466
B.7 Numeric types......Page 467
B.7.1 Arithmetic operations......Page 468
B.8 Context managers and the with statement......Page 469
B.9 The descriptor protocol......Page 471
B.10 Magic attributes......Page 472
B.11 Functions and modules......Page 473
C.2 Mailing lists and newsgroups......Page 474
C.6 IronPython team......Page 475
C.10 IDEs and tools......Page 476
A......Page 478
C......Page 479
D......Page 481
F......Page 482
G......Page 483
I......Page 484
L......Page 485
N......Page 486
P......Page 487
R......Page 488
S......Page 489
U......Page 491
W......Page 492
Z......Page 493
Back Cover......Page 494

Polecaj historie