Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 606 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
606
Dung lượng
3,96 MB
Nội dung
Learning Python Table of Contents Learning Python Credits About the Author Acknowledgements About the Reviewers www.PacktPub.com Support files, eBooks, discount offers, and more Why subscribe? Free access for Packt account holders Preface What this book covers What you need for this book Who this book is for Conventions Reader feedback Customer support Downloading the example code Errata Piracy Questions Introduction and First Steps – Take a Deep Breath A proper introduction Enter the Python About Python Portability Coherence Developer productivity An extensive library Software quality Software integration Satisfaction and enjoyment What are the drawbacks? Who is using Python today? Setting up the environment Python 2 versus Python 3 – the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code organized How do we use modules and packages Python’s execution model Names and namespaces Scopes Object and classes Guidelines on how to write good code The Python culture A note on the IDEs Summary Built-in Data Types Everything is an object Mutable or immutable? That is the question Numbers Integers Booleans Reals Complex numbers Fractions and decimals Immutable sequences Strings and bytes Encoding and decoding strings Indexing and slicing strings Tuples Mutable sequences Lists Byte arrays Set types Mapping types – dictionaries The collections module Named tuples Defaultdict ChainMap Final considerations Small values caching How to choose data structures About indexing and slicing About the names Summary Iterating and Making Decisions Conditional programming A specialized else: elif The ternary operator Looping The for loop Iterating over a range Iterating over a sequence Iterators and iterables Iterating over multiple sequences The while loop The break and continue statements A special else clause Putting this all together Example 1 – a prime generator Example 2 – applying discounts A quick peek at the itertools module Infinite iterators Iterators terminating on the shortest input sequence Combinatoric generators Summary Functions, the Building Blocks of Code Why use functions? Reduce code duplication Splitting a complex task Hide implementation details Improve readability Improve traceability Scopes and name resolution The global and nonlocal statements Input parameters Argument passing Assignment to argument names don’t affect the caller Changing a mutable affects the caller How to specify input parameters Positional arguments Keyword arguments and default values Variable positional arguments Variable keyword arguments Keyword-only arguments Combining input parameters Avoid the trap! Mutable defaults Return values Returning multiple values A few useful tips Recursive functions Anonymous functions Function attributes Built-in functions One final example Documenting your code Importing objects Relative imports Summary Saving Time and Memory map, zip, and filter map zip filter Comprehensions Nested comprehensions Filtering a comprehension dict comprehensions set comprehensions Generators Generator functions Going beyond next The yield from expression Generator expressions Some performance considerations Don’t overdo comprehensions and generators Name localization Generation behavior in built-ins One last example Summary Advanced Concepts – OOP, Decorators, and Iterators Decorators A decorator factory Object-oriented programming The simplest Python class Class and object namespaces Attribute shadowing I, me, and myself – using the self variable Initializing an instance OOP is about code reuse Inheritance and composition Accessing a base class Multiple inheritance Method resolution order Static and class methods Static methods Class methods Private methods and name mangling The property decorator Operator overloading Polymorphism – a brief overview Writing a custom iterator Summary Testing, Profiling, and Dealing with Exceptions Testing your application The anatomy of a test Testing guidelines Unit testing Writing a unit test Mock objects and patching Assertions A classic unit test example Making a test fail Interface testing Comparing tests with and without mocks Boundaries and granularity A more interesting example Test-driven development Exceptions Profiling Python When to profile? Summary The Edges – GUIs and Scripts First approach – scripting The imports Parsing arguments The business logic Second approach – a GUI application The imports The layout logic The business logic Fetching the web page Saving the images Alerting the user How to improve the application? Where do we go from here? N NameError exception / Scopes name localization about / Name localization name mangling about / Private methods and name mangling names about / Names and namespaces defining / About the names namespaces about / Names and namespaces nano about / Using console editors negative indexing about / About indexing and slicing nose-parameterized URL / A more interesting example Numba URL / Where do we go from here? numbers about / Numbers integers / Integers booleans / Booleans real numbers / Reals complex numbers / Complex numbers fractions and decimals / Fractions and decimals Numeric Python about / Creating the DataFrame NumPy URL / Where do we go from here? O object defining / Everything is an object mutable / Mutable or immutable? That is the question immutable / Mutable or immutable? That is the question object-oriented programming about / Object-oriented programming Python class / The simplest Python class class and object namespaces / Class and object namespaces attribute shadowing / Attribute shadowing self variable, using / I, me, and myself – using the self variable instance, initializing / Initializing an instance code, reusing / OOP is about code reuse inheritance and composition / Inheritance and composition base class, accessing / Accessing a base class multiple inheritance / Multiple inheritance method resolution order / Method resolution order class and static methods / Static and class methods private methods / Private methods and name mangling name mangling / Private methods and name mangling property decorator / The property decorator operator overloading / Operator overloading polymorphism / Polymorphism – a brief overview object-relational mapping (ORM) about / The model layer objects about / A proper introduction, Object and classes, Object-oriented programming importing / Importing objects relative imports / Relative imports one way encryption algorithm about / The model layer one way hash function about / The model layer operating system (OS) about / Portability operator overloading about / Lists, Operator overloading outer about / A specialized else: elif P package about / How is Python code organized packages using / How do we use modules and packages Pandas URL / Where do we go from here? patching about / Mock objects and patching URL / A classic unit test example pdb about / Using the Python debugger PEP328 about / Relative imports performance considerations about / Some performance considerations pivot table about / Visualizing the results plaintext about / The model layer polymorphism / Polymorphism – a brief overview primary key about / The model layer, Adding the Entry model prime generator about / Example 1 – a prime generator primitive about / Don’t overdo comprehensions and generators principle of least astonishment about / The principle of least astonishment principles, Django web framework DRY / Django design philosophy Loose coupling / Django design philosophy Less code / Django design philosophy Consistency / Django design philosophy profiling about / Exceptions, When to profile? properties about / A proper introduction protocols about / What is the Web? pull protocol about / How does the Web work? push protocol about / How does the Web work? PyGTK about / Second approach – a GUI application, wxPython, PyQt, and PyGTK PyPy URL / What are the drawbacks? PyQt about / Second approach – a GUI application, wxPython, PyQt, and PyGTK pytest URL / A more interesting example Pythagorean triple about / Filtering a comprehension Python about / Enter the Python features / About Python, Software integration drawbacks / What are the drawbacks? users / Who is using Python today? installing / Installing Python references / Setting up the Python interpreter running, as service / Running Python as a service running, as GUI application / Running Python as a GUI application execution model / Python’s execution model profiling / Profiling Python Python 2 versus Python 3 / Python 2 versus Python 3 – the great debate Python code organizing / How is Python code organized Python culture about / The Python culture Python Enhancement Proposal (PEP) / Guidelines on how to write good code Python interactive shell running / Running the Python interactive shell Python interpreter setting up / Setting up the Python interpreter Python module reference / Everything is an object Python Package Index (PyPI) / An extensive library Python program running / How you can run a Python program Python scripts running / Running Python scripts Q quality assurance (QA) about / Testing your application R radix-64 about / The imports recursive functions about / Recursive functions Red phase about / Test-driven development Refactor about / Test-driven development regex website defining / A regex website Django, setting up / Setting up Django Entry model, adding / Adding the Entry model admin panel, customizing / Customizing the admin panel form, creating / Creating the form views, writing / Writing the views URLs and views, using / Tying up URLs and views templates, writing / Writing the templates regular expression / Regular expressions relational database about / The model layer relative imports URL / Relative imports request-response client-server protocol about / How does the Web work? return values about / Return values multiple values, returning / Returning multiple values S scheduler about / Threading considerations schema migration about / Adding the Entry model Schwartzian transform about / map Scikit-Learn URL / Where do we go from here? SciPy URL / Where do we go from here? scopes about / Scopes local / Scopes enclosing / Scopes global / Scopes built-in / Scopes scopes and name resolution defining / Scopes and name resolution global and nonlocal statements / The global and nonlocal statements scripting about / First approach – scripting imports / First approach – scripting, The imports arguments, parsing / Parsing arguments business logic / The business logic service Python, running as / Running Python as a service service-oriented architecture (SOA) about / Inspecting log files service-oriented architectures about / Inspecting log files setter about / The property decorator set types about / Set types Single-Page Application (SPA) about / The future of web development Single Responsibility Principle (SRP) about / Writing the helpers slicing about / About indexing and slicing small values caching about / Small values caching space about / Some performance considerations Sphinx about / Documenting your code SQL (Structured Query Language) about / The model layer static methods about / Static methods statistical profiling about / Profiling Python strings encoding / Encoding and decoding strings decoding / Encoding and decoding strings indexing / Indexing and slicing strings slicing / Indexing and slicing strings str objects about / Strings and bytes symmetric encryption algorithm about / The model layer system-exiting exceptions about / Exceptions T Tcl (Tool Command Language) / Running Python as a GUI application TCP/IP (Transmission Control Protocol/Internet Protocol) about / How does the Web work? template layer about / The template layer home and footer templates / Home and footer templates records, listing / Listing all records records, creating / Creating and editing records records, editing / Creating and editing records API, defining / Talking to the API records, deleting / Deleting records terminal about / Setting up the Python interpreter ternary operator about / The ternary operator test defining / The anatomy of a test preparation / The anatomy of a test execution / The anatomy of a test verification / The anatomy of a test failing / Making a test fail test-driven development (TDD) about / Test-driven development benefits / Test-driven development disadvantages / Test-driven development testing guidelines defining / Testing guidelines tests front-end tests / Testing your application scenario tests / Testing your application integration tests / Testing your application smoke tests / Testing your application Acceptance tests / Testing your application functional tests / Testing your application destructive tests / Testing your application performance tests / Testing your application usability tests / Testing your application security and penetration tests / Testing your application unit tests / Testing your application regression tests / Testing your application comparing, with mocks / Comparing tests with and without mocks comparing, without mocks / Comparing tests with and without mocks boundaries / Boundaries and granularity granularity / Boundaries and granularity example / A more interesting example thread about / Threading considerations time about / Some performance considerations Timsort about / Lists Tk / Running Python as a GUI application tkinter about / Second approach – a GUI application Tkinter / Running Python as a GUI application tkinter.tix (Tk Interface Extension) module about / The tkinter.tix module tkinter.tix module about / The tkinter.tix module Tk interface about / Second approach – a GUI application triangulation about / Comparing tests with and without mocks troubleshooting guidelines about / Troubleshooting guidelines console editors, using / Using console editors inspecting / Where to inspect tests used, for debugging / Using tests to debug monitoring / Monitoring true division (/) about / Integers tuple about / Tuples turtle module about / The turtle module U unicode code points about / Strings and bytes Uniform Resource Locator (URL) about / The Django URL dispatcher unit test about / Unit testing writing / Writing a unit test mock objects / Mock objects and patching patching / Mock objects and patching assertions / Assertions example / A classic unit test example unit testing defining / Unit testing unpacking about / Variable positional arguments Upcasting about / Booleans user acceptance testing (UAT) about / Testing your application user experience (UX) about / Testing your application Utf-8 about / Encoding and decoding strings V view layer about / The view layer imports and home view / Imports and home view records, listing / Listing all records records, creating / Creating records records, updating / Updating records records, deleting / Deleting records views writing / Writing the views home view / The home view entry list view / The entry list view form view / The form view vim about / Using console editors virtualenv about / About virtualenv reference link / About virtualenv virtual environment creating / Your first virtual environment W wasted time about / About the names Web (World Wide Web) defining / What is the Web? working / How does the Web work? web development defining / The future of web development web framework about / The Django web framework white-box tests about / Testing your application wxPython about / Second approach – a GUI application, wxPython, PyQt, and PyGTK Y yield from expression about / The yield from expression Z zip about / map, zip, and filter defining / zip ... Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code organized How do we use modules and packages Python s execution model... Setting up the environment Python 2 versus Python 3 – the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program... Learning Python Table of Contents Learning Python Credits About the Author Acknowledgements About the Reviewers www.PacktPub.com