Python: Journey from Novice to Expert

Book description

Learn core concepts of Python and unleash its power to script highest quality Python programs

About This Book

  • Develop a strong set of programming skills with Pyhton that you will be able to express in any situation, on every platform, thanks to Python's portability
  • Stop writing scripts and start architecting programs by applying object-oriented programming techniques in Python
  • Learn the trickier aspects of Python and put it in a structured context for deeper understanding of the language

Who This Book Is For

This course is meant for programmers who wants to learn Python programming from a basic to an expert level. The course is mostly self-contained and introduces Python programming to a new reader and can help him become an expert in this trade.

What You Will Learn

  • Get Python up and running on Windows, Mac, and Linux in no time
  • Grasp the fundamental concepts of coding, along with the basics of data structures and control flow
  • Understand when to use the functional or the object-oriented programming approach
  • Extend class functionality using inheritance
  • Exploit object-oriented programming in key Python technologies, such as Kivy and Django
  • Understand how and when to use the functional programming paradigm
  • Use the multiprocessing library, not just locally but also across multiple machines

In Detail

Python is a dynamic and powerful programming language, having its application in a wide range of domains. It has an easy-to-use, simple syntax, and a powerful library, which includes hundreds of modules to provide routines for a wide range of applications, thus making it a popular language among programing enthusiasts.This course will take you on a journey from basic programming practices to high-end tools and techniques giving you an edge over your peers. It follows an interesting learning path, divided into three modules. As you complete each one, you'll have gained key skills and get ready for the material in the next module.The first module will begin with exploring all the essentials of Python programming in an easy-to-understand way. This will lay a good foundation for those who are interested in digging deeper. It has a practical and example-oriented approach through which both the introductory and the advanced topics are explained.

Starting with the fundamentals of programming and Python, it ends by exploring topics, like GUIs, web apps, and data science.In the second module you will learn about object oriented programming techniques in Python. Starting with a detailed analysis of object-oriented technique and design, you will use the Python programming language to clearly grasp key concepts from the object-oriented paradigm. This module fully explains classes, data encapsulation, inheritance, polymorphism, abstraction, and exceptions with an emphasis on when you can use each principle to develop well-designed software.With a good foundation of Python you will move onto the third module which is a comprehensive tutorial covering advanced features of the Python language.

Start by creating a project-specific environment using venv. This will introduce you to various Pythonic syntax and common pitfalls before moving onto functional features and advanced concepts, thereby gaining an expert level knowledge in programming and teaching how to script highest quality Python programs.

Style and approach

This course follows a theory-cum-practical approach having all the ingredients that will help you jump into the field of Python programming as a novice and grow-up as an expert. The aim is to create a smooth learning path that will teach you how to get started with Python and carry out expert-level programming techniques at the end of course.

Table of contents

  1. Python: Journey from Novice to Expert
    1. Table of Contents
    2. Python: Journey from Novice to Expert
    3. Python: Journey from Novice to Expert
    4. Credits
    5. Preface
      1. What this learning path covers
      2. What you need for this learning path
      3. Who this learning path is for
      4. Reader feedback
      5. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    6. 1. Module 1
      1. 1. Introduction and First Steps – Take a Deep Breath
        1. A proper introduction
        2. Enter the Python
        3. About Python
          1. Portability
          2. Coherence
          3. Developer productivity
          4. An extensive library
          5. Software quality
          6. Software integration
          7. Satisfaction and enjoyment
        4. What are the drawbacks?
        5. Who is using Python today?
        6. Setting up the environment
          1. Python 2 versus Python 3 – the great debate
        7. Installing Python
          1. Setting up the Python interpreter
          2. About virtualenv
          3. Your first virtual environment
          4. Your friend, the console
        8. How you can run a Python program
          1. Running Python scripts
          2. Running the Python interactive shell
          3. Running Python as a service
          4. Running Python as a GUI application
        9. How is Python code organized
          1. How do we use modules and packages
        10. Python's execution model
          1. Names and namespaces
          2. Scopes
          3. Object and classes
        11. Guidelines on how to write good code
        12. The Python culture
        13. A note on the IDEs
        14. Summary
      2. 2. Built-in Data Types
        1. Everything is an object
        2. Mutable or immutable? That is the question
        3. Numbers
          1. Integers
          2. Booleans
          3. Reals
          4. Complex numbers
          5. Fractions and decimals
        4. Immutable sequences
          1. Strings and bytes
            1. Encoding and decoding strings
            2. Indexing and slicing strings
          2. Tuples
        5. Mutable sequences
          1. Lists
          2. Byte arrays
        6. Set types
        7. Mapping types – dictionaries
        8. The collections module
          1. Named tuples
          2. Defaultdict
          3. ChainMap
        9. Final considerations
          1. Small values caching
          2. How to choose data structures
          3. About indexing and slicing
          4. About the names
        10. Summary
      3. 3. Iterating and Making Decisions
        1. Conditional programming
          1. A specialized else: elif
          2. The ternary operator
        2. Looping
          1. The for loop
            1. Iterating over a range
            2. Iterating over a sequence
          2. Iterators and iterables
          3. Iterating over multiple sequences
          4. The while loop
          5. The break and continue statements
          6. A special else clause
        3. Putting this all together
          1. Example 1 – a prime generator
          2. Example 2 – applying discounts
        4. A quick peek at the itertools module
          1. Infinite iterators
          2. Iterators terminating on the shortest input sequence
          3. Combinatoric generators
        5. Summary
      4. 4. Functions, the Building Blocks of Code
        1. Why use functions?
          1. Reduce code duplication
          2. Splitting a complex task
          3. Hide implementation details
          4. Improve readability
          5. Improve traceability
        2. Scopes and name resolution
          1. The global and nonlocal statements
        3. Input parameters
          1. Argument passing
          2. Assignment to argument names don't affect the caller
          3. Changing a mutable affects the caller
          4. How to specify input parameters
            1. Positional arguments
            2. Keyword arguments and default values
            3. Variable positional arguments
            4. Variable keyword arguments
            5. Keyword-only arguments
            6. Combining input parameters
            7. Avoid the trap! Mutable defaults
        4. Return values
          1. Returning multiple values
        5. A few useful tips
        6. Recursive functions
        7. Anonymous functions
        8. Function attributes
        9. Built-in functions
        10. One final example
        11. Documenting your code
        12. Importing objects
          1. Relative imports
        13. Summary
      5. 5. Saving Time and Memory
        1. map, zip, and filter
          1. map
          2. zip
          3. filter
        2. Comprehensions
          1. Nested comprehensions
          2. Filtering a comprehension
          3. dict comprehensions
          4. set comprehensions
        3. Generators
          1. Generator functions
          2. Going beyond next
          3. The yield from expression
          4. Generator expressions
        4. Some performance considerations
        5. Don't overdo comprehensions and generators
        6. Name localization
        7. Generation behavior in built-ins
        8. One last example
        9. Summary
      6. 6. Advanced Concepts – OOP, Decorators, and Iterators
        1. Decorators
          1. A decorator factory
        2. Object-oriented programming
          1. The simplest Python class
          2. Class and object namespaces
          3. Attribute shadowing
          4. I, me, and myself – using the self variable
          5. Initializing an instance
          6. OOP is about code reuse
            1. Inheritance and composition
          7. Accessing a base class
          8. Multiple inheritance
            1. Method resolution order
          9. Static and class methods
            1. Static methods
            2. Class methods
          10. Private methods and name mangling
          11. The property decorator
          12. Operator overloading
          13. Polymorphism – a brief overview
        3. Writing a custom iterator
        4. Summary
      7. 7. Testing, Profiling, and Dealing with Exceptions
        1. Testing your application
          1. The anatomy of a test
          2. Testing guidelines
          3. Unit testing
            1. Writing a unit test
            2. Mock objects and patching
            3. Assertions
            4. A classic unit test example
            5. Making a test fail
            6. Interface testing
            7. Comparing tests with and without mocks
            8. Boundaries and granularity
            9. A more interesting example
        2. Test-driven development
        3. Exceptions
        4. Profiling Python
          1. When to profile?
        5. Summary
      8. 8. The Edges – GUIs and Scripts
        1. First approach – scripting
          1. The imports
          2. Parsing arguments
          3. The business logic
        2. Second approach – a GUI application
          1. The imports
          2. The layout logic
          3. The business logic
            1. Fetching the web page
            2. Saving the images
            3. Alerting the user
          4. How to improve the application?
        3. Where do we go from here?
          1. The tkinter.tix module
          2. The turtle module
          3. wxPython, PyQt, and PyGTK
          4. The principle of least astonishment
          5. Threading considerations
        4. Summary
      9. 9. Data Science
        1. IPython and Jupyter notebook
        2. Dealing with data
          1. Setting up the notebook
          2. Preparing the data
          3. Cleaning the data
          4. Creating the DataFrame
            1. Unpacking the campaign name
            2. Unpacking the user data
            3. Cleaning everything up
          5. Saving the DataFrame to a file
          6. Visualizing the results
        3. Where do we go from here?
        4. Summary
      10. 10. Web Development Done Right
        1. What is the Web?
        2. How does the Web work?
        3. The Django web framework
          1. Django design philosophy
            1. The model layer
            2. The view layer
            3. The template layer
          2. The Django URL dispatcher
            1. Regular expressions
        4. A regex website
          1. Setting up Django
            1. Starting the project
            2. Creating users
          2. Adding the Entry model
          3. Customizing the admin panel
          4. Creating the form
          5. Writing the views
            1. The home view
            2. The entry list view
            3. The form view
          6. Tying up URLs and views
          7. Writing the templates
        5. The future of web development
          1. Writing a Flask view
          2. Building a JSON quote server in Falcon
        6. Summary
      11. 11. Debugging and Troubleshooting
        1. Debugging techniques
          1. Debugging with print
          2. Debugging with a custom function
          3. Inspecting the traceback
          4. Using the Python debugger
          5. Inspecting log files
          6. Other techniques
            1. Profiling
            2. Assertions
          7. Where to find information
        2. Troubleshooting guidelines
          1. Using console editors
          2. Where to inspect
          3. Using tests to debug
          4. Monitoring
        3. Summary
      12. 12. Summing Up – A Complete Example
        1. The challenge
        2. Our implementation
        3. Implementing the Django interface
          1. The setup
          2. The model layer
          3. A simple form
          4. The view layer
            1. Imports and home view
            2. Listing all records
            3. Creating records
            4. Updating records
            5. Deleting records
          5. Setting up the URLs
          6. The template layer
            1. Home and footer templates
            2. Listing all records
            3. Creating and editing records
            4. Talking to the API
            5. Deleting records
        4. Implementing the Falcon API
          1. The main application
          2. Writing the helpers
            1. Coding the password validator
            2. Coding the password generator
          3. Writing the handlers
            1. Coding the password validator handler
            2. Coding the password generator handler
          4. Running the API
          5. Testing the API
            1. Testing the helpers
            2. Testing the handlers
        5. Where do you go from here?
        6. Summary
    7. 2. Module 2
      1. 1. Object-oriented Design
        1. Introducing object-oriented
        2. Objects and classes
        3. Specifying attributes and behaviors
          1. Data describes objects
          2. Behaviors are actions
        4. Hiding details and creating the public interface
        5. Composition
        6. Inheritance
          1. Inheritance provides abstraction
          2. Multiple inheritance
        7. Case study
        8. Exercises
        9. Summary
      2. 2. Objects in Python
        1. Creating Python classes
          1. Adding attributes
          2. Making it do something
            1. Talking to yourself
            2. More arguments
          3. Initializing the object
          4. Explaining yourself
        2. Modules and packages
          1. Organizing the modules
            1. Absolute imports
            2. Relative imports
        3. Organizing module contents
        4. Who can access my data?
        5. Third-party libraries
        6. Case study
        7. Exercises
        8. Summary
      3. 3. When Objects Are Alike
        1. Basic inheritance
          1. Extending built-ins
          2. Overriding and super
        2. Multiple inheritance
          1. The diamond problem
          2. Different sets of arguments
        3. Polymorphism
        4. Abstract base classes
          1. Using an abstract base class
          2. Creating an abstract base class
          3. Demystifying the magic
        5. Case study
        6. Exercises
        7. Summary
      4. 4. Expecting the Unexpected
        1. Raising exceptions
          1. Raising an exception
          2. The effects of an exception
          3. Handling exceptions
          4. The exception hierarchy
          5. Defining our own exceptions
        2. Case study
        3. Exercises
        4. Summary
      5. 5. When to Use Object-oriented Programming
        1. Treat objects as objects
        2. Adding behavior to class data with properties
          1. Properties in detail
          2. Decorators – another way to create properties
          3. Deciding when to use properties
        3. Manager objects
          1. Removing duplicate code
          2. In practice
        4. Case study
        5. Exercises
        6. Summary
      6. 6. Python Data Structures
        1. Empty objects
        2. Tuples and named tuples
          1. Named tuples
        3. Dictionaries
          1. Dictionary use cases
          2. Using defaultdict
            1. Counter
        4. Lists
          1. Sorting lists
        5. Sets
        6. Extending built-ins
        7. Queues
          1. FIFO queues
          2. LIFO queues
          3. Priority queues
        8. Case study
        9. Exercises
        10. Summary
      7. 7. Python Object-oriented Shortcuts
        1. Python built-in functions
          1. The len() function
          2. Reversed
          3. Enumerate
          4. File I/O
          5. Placing it in context
        2. An alternative to method overloading
          1. Default arguments
          2. Variable argument lists
          3. Unpacking arguments
        3. Functions are objects too
          1. Using functions as attributes
          2. Callable objects
        4. Case study
        5. Exercises
        6. Summary
      8. 8. Strings and Serialization
        1. Strings
          1. String manipulation
          2. String formatting
            1. Escaping braces
            2. Keyword arguments
            3. Container lookups
            4. Object lookups
            5. Making it look right
          3. Strings are Unicode
            1. Converting bytes to text
            2. Converting text to bytes
          4. Mutable byte strings
        2. Regular expressions
          1. Matching patterns
            1. Matching a selection of characters
            2. Escaping characters
            3. Matching multiple characters
            4. Grouping patterns together
          2. Getting information from regular expressions
            1. Making repeated regular expressions efficient
        3. Serializing objects
          1. Customizing pickles
          2. Serializing web objects
        4. Case study
        5. Exercises
        6. Summary
      9. 9. The Iterator Pattern
        1. Design patterns in brief
        2. Iterators
          1. The iterator protocol
        3. Comprehensions
          1. List comprehensions
          2. Set and dictionary comprehensions
          3. Generator expressions
        4. Generators
          1. Yield items from another iterable
        5. Coroutines
          1. Back to log parsing
          2. Closing coroutines and throwing exceptions
          3. The relationship between coroutines, generators, and functions
        6. Case study
        7. Exercises
        8. Summary
      10. 10. Python Design Patterns I
        1. The decorator pattern
          1. A decorator example
          2. Decorators in Python
        2. The observer pattern
          1. An observer example
        3. The strategy pattern
          1. A strategy example
          2. Strategy in Python
        4. The state pattern
          1. A state example
          2. State versus strategy
          3. State transition as coroutines
        5. The singleton pattern
          1. Singleton implementation
        6. The template pattern
          1. A template example
        7. Exercises
        8. Summary
      11. 11. Python Design Patterns II
        1. The adapter pattern
        2. The facade pattern
        3. The flyweight pattern
        4. The command pattern
        5. The abstract factory pattern
        6. The composite pattern
        7. Exercises
        8. Summary
      12. 12. Testing Object-oriented Programs
        1. Why test?
          1. Test-driven development
        2. Unit testing
          1. Assertion methods
          2. Reducing boilerplate and cleaning up
          3. Organizing and running tests
          4. Ignoring broken tests
        3. Testing with py.test
          1. One way to do setup and cleanup
          2. A completely different way to set up variables
          3. Skipping tests with py.test
        4. Imitating expensive objects
        5. How much testing is enough?
        6. Case study
          1. Implementing it
        7. Exercises
        8. Summary
      13. 13. Concurrency
        1. Threads
          1. The many problems with threads
            1. Shared memory
            2. The global interpreter lock
          2. Thread overhead
        2. Multiprocessing
          1. Multiprocessing pools
          2. Queues
          3. The problems with multiprocessing
        3. Futures
        4. AsyncIO
          1. AsyncIO in action
          2. Reading an AsyncIO future
          3. AsyncIO for networking
          4. Using executors to wrap blocking code
          5. Streams
            1. Executors
        5. Case study
        6. Exercises
        7. Summary
    8. 3. Module 3
      1. 1. Getting Started – One Environment per Project
        1. Creating a virtual Python environment using venv
          1. Creating your first venv
          2. venv arguments
          3. Differences between virtualenv and venv
        2. Bootstrapping pip using ensurepip
          1. ensurepip usage
          2. Manual pip install
        3. Installing C/C++ packages
          1. Debian and Ubuntu
          2. Red Hat, CentOS, and Fedora
          3. OS X
          4. Windows
        4. Summary
      2. 2. Pythonic Syntax, Common Pitfalls, and Style Guide
        1. Code style – or what is Pythonic code?
          1. Formatting strings – printf-style or str.format?
          2. PEP20, the Zen of Python
            1. Beautiful is better than ugly
            2. Explicit is better than implicit
            3. Simple is better than complex
            4. Flat is better than nested
            5. Sparse is better than dense
            6. Readability counts
            7. Practicality beats purity
            8. Errors should never pass silently
            9. In the face of ambiguity, refuse the temptation to guess
            10. One obvious way to do it
            11. Now is better than never
            12. Hard to explain, easy to explain
            13. Namespaces are one honking great idea
            14. Conclusion
          3. Explaining PEP8
            1. Duck typing
            2. Differences between value and identity comparisons
            3. Loops
            4. Maximum line length
          4. Verifying code quality, pep8, pyflakes, and more
            1. flake8
              1. Pep8
              2. pyflakes
              3. McCabe
              4. flake8
            2. Pylint
        2. Common pitfalls
          1. Scope matters!
            1. Function arguments
            2. Class properties
            3. Modifying variables in the global scope
          2. Overwriting and/or creating extra built-ins
          3. Modifying while iterating
          4. Catching exceptions – differences between Python 2 and 3
          5. Late binding – be careful with closures
          6. Circular imports
          7. Import collisions
        3. Summary
      3. 3. Containers and Collections – Storing Data the Right Way
        1. Time complexity – the big O notation
        2. Core collections
          1. list – a mutable list of items
          2. dict – unsorted but a fast map of items
          3. set – like a dict without values
          4. tuple – the immutable list
        3. Advanced collections
          1. ChainMap – the list of dictionaries
          2. counter – keeping track of the most occurring elements
          3. deque – the double ended queue
          4. defaultdict – dictionary with a default value
          5. namedtuple – tuples with field names
          6. enum – a group of constants
          7. OrderedDict – a dictionary where the insertion order matters
          8. heapq – the ordered list
          9. bisect – the sorted list
        4. Summary
      4. 4. Functional Programming – Readability Versus Brevity
        1. Functional programming
        2. list comprehensions
        3. dict comprehensions
        4. set comprehensions
        5. lambda functions
          1. The Y combinator
        6. functools
          1. partial – no need to repeat all arguments every time
          2. reduce – combining pairs into a single result
            1. Implementing a factorial function
            2. Processing trees
        7. itertools
          1. accumulate – reduce with intermediate results
          2. chain – combining multiple results
          3. combinations – combinatorics in Python
          4. permutations – combinations where the order matters
          5. compress – selecting items using a list of Booleans
          6. dropwhile/takewhile – selecting items using a function
          7. count – infinite range with decimal steps
          8. groupby – grouping your sorted iterable
          9. islice – slicing any iterable
        8. Summary
      5. 5. Decorators – Enabling Code Reuse by Decorating
        1. Decorating functions
          1. Why functools.wraps is important
          2. How are decorators useful?
          3. Memoization using decorators
          4. Decorators with (optional) arguments
          5. Creating decorators using classes
        2. Decorating class functions
          1. Skipping the instance – classmethod and staticmethod
          2. Properties – smart descriptor usage
        3. Decorating classes
          1. Singletons – classes with a single instance
          2. Total ordering – sortable classes the easy way
        4. Useful decorators
          1. Single dispatch – polymorphism in Python
          2. Contextmanager, with statements made easy
          3. Validation, type checks, and conversions
          4. Useless warnings – how to ignore them
        5. Summary
      6. 6. Generators and Coroutines – Infinity, One Step at a Time
        1. What are generators?
          1. Advantages and disadvantages of generators
          2. Pipelines – an effective use of generators
          3. tee – using an output multiple times
          4. Generating from generators
          5. Context managers
        2. Coroutines
          1. A basic example
          2. Priming
          3. Closing and throwing exceptions
          4. Bidirectional pipelines
          5. Using the state
        3. Summary
      7. 7. Async IO – Multithreading without Threads
        1. Introducing the asyncio library
          1. The async and await statements
            1. Python 3.4
            2. Python 3.5
            3. Choosing between the 3.4 and 3.5 syntax
          2. A simple example of single-threaded parallel processing
          3. Concepts of asyncio
            1. Futures and tasks
            2. Event loops
              1. Event loop implementations
              2. Event loop policies
              3. Event loop usage
            3. Processes
          4. Asynchronous servers and clients
            1. Basic echo server
        2. Summary
      8. 8. Metaclasses – Making Classes (Not Instances) Smarter
        1. Dynamically creating classes
          1. A basic metaclass
          2. Arguments to metaclasses
          3. Accessing metaclass attributes through classes
        2. Abstract classes using collections.abc
          1. Internal workings of the abstract classes
          2. Custom type checks
          3. Using abc.ABC before Python 3.4
        3. Automatically registering a plugin system
          1. Importing plugins on-demand
          2. Importing plugins through configuration
          3. Importing plugins through the file system
        4. Order of operations when instantiating classes
          1. Finding the metaclass
          2. Preparing the namespace
          3. Executing the class body
          4. Creating the class object (not instance)
          5. Executing the class decorators
          6. Creating the class instance
          7. Example
        5. Storing class attributes in definition order
          1. The classic solution without metaclasses
          2. Using metaclasses to get a sorted namespace
        6. Summary
      9. 9. Documentation – How to Use Sphinx and reStructuredText
        1. The reStructuredText syntax
          1. Getting started with reStructuredText
          2. Inline markup
          3. Headers
          4. Lists
            1. Enumerated list
            2. Bulleted list
            3. Option list
            4. Definition list
            5. Nested lists
          5. Links, references, and labels
          6. Images
          7. Substitutions
          8. Blocks, code, math, comments, and quotes
          9. Conclusion
        2. The Sphinx documentation generator
          1. Getting started with Sphinx
            1. Using sphinx-quickstart
            2. Using sphinx-apidoc
          2. Sphinx directives
            1. The table of contents tree directive (toctree)
            2. Autodoc, documenting Python modules, classes, and functions
          3. Sphinx roles
        3. Documenting code
          1. Documenting a class with the Sphinx style
          2. Documenting a class with the Google style
          3. Documenting a class with the NumPy style
          4. Which style to choose
        4. Summary
      10. 10. Testing and Logging – Preparing for Bugs
        1. Using examples as tests with doctest
          1. A simple doctest example
          2. Writing doctests
          3. Testing with pure documentation
          4. The doctest flags
            1. True and False versus 1 and 0
            2. Normalizing whitespace
            3. Ellipsis
          5. Doctest quirks
            1. Testing dictionaries
            2. Testing floating-point numbers
            3. Times and durations
        2. Testing with py.test
          1. The difference between the unittest and py.test output
          2. The difference between unittest and py.test tests
            1. Simplifying assertions
            2. Parameterizing tests
            3. Automatic arguments using fixtures
              1. Cache
              2. Custom fixtures
            4. Print statements and logging
            5. Plugins
              1. pytest-cov
              2. pytest-pep8 and pytest-flakes
              3. Configuring plugins
        3. Mock objects
          1. Using unittest.mock
          2. Using py.test monkeypatch
        4. Logging
          1. Configuration
            1. Basic logging configuration
            2. Dictionary configuration
            3. JSON configuration
            4. Ini file configuration
            5. The network configuration
          2. Logger
            1. Usage
        5. Summary
      11. 11. Debugging – Solving the Bugs
        1. Non-interactive debugging
          1. Inspecting your script using trace
          2. Debugging using logging
          3. Showing call stack without exceptions
          4. Debugging asyncio
          5. Handling crashes using faulthandler
        2. Interactive debugging
          1. Console on demand
          2. Debugging using pdb
            1. Breakpoints
            2. Catching exceptions
            3. Commands
          3. Debugging using ipdb
          4. Other debuggers
          5. Debugging services
        3. Summary
      12. 12. Performance – Tracking and Reducing Your Memory and CPU Usage
        1. What is performance?
        2. Timeit – comparing code snippet performance
        3. cProfile – finding the slowest components
          1. First profiling run
          2. Calibrating your profiler
          3. Selective profiling using decorators
          4. Using profile statistics
        4. Line profiler
        5. Improving performance
          1. Using the right algorithm
          2. Global interpreter lock
          3. Try versus if
          4. Lists versus generators
          5. String concatenation
          6. Addition versus generators
          7. Map versus generators and list comprehensions
          8. Caching
          9. Lazy imports
          10. Using optimized libraries
          11. Just-in-time compiling
          12. Converting parts of your code to C
        6. Memory usage
          1. Tracemalloc
          2. Memory profiler
          3. Memory leaks
          4. Reducing memory usage
            1. Generators versus lists
            2. Recreating collections versus removing items
            3. Using slots
        7. Performance monitoring
        8. Summary
      13. 13. Multiprocessing – When a Single CPU Core Is Not Enough
        1. Multithreading versus multiprocessing
        2. Hyper-threading versus physical CPU cores
        3. Creating a pool of workers
        4. Sharing data between processes
        5. Remote processes
          1. Distributed processing using multiprocessing
          2. Distributed processing using IPyparallel
            1. ipython_config.py
            2. ipython_kernel_config.py
            3. ipcontroller_config.py
            4. ipengine_config.py
            5. ipcluster_config.py
        6. Summary
      14. 14. Extensions in C/C++, System Calls, and C/C++ Libraries
        1. Introduction
          1. Do you need C/C++ modules?
          2. Windows
          3. OS X
          4. Linux/Unix
        2. Calling C/C++ with ctypes
          1. Platform-specific libraries
            1. Windows
            2. Linux/Unix
            3. OS X
            4. Making it easy
          2. Calling functions and native types
          3. Complex data structures
          4. Arrays
          5. Gotchas with memory management
        3. CFFI
          1. Complex data structures
          2. Arrays
          3. ABI or API?
          4. CFFI or ctypes?
        4. Native C/C++ extensions
          1. A basic example
          2. C is not Python – size matters
          3. The example explained
            1. static
            2. PyObject*
            3. Parsing arguments
          4. C is not Python – errors are silent or lethal
          5. Calling Python from C – handling complex types
        5. Summary
      15. 15. Packaging – Creating Your Own Libraries or Applications
        1. Installing packages
        2. Setup parameters
        3. Packages
        4. Entry points
          1. Creating global commands
          2. Custom setup.py commands
        5. Package data
        6. Testing packages
          1. Unittest
          2. py.test
          3. Nosetests
        7. C/C++ extensions
          1. Regular extensions
          2. Cython extensions
        8. Wheels – the new eggs
          1. Distributing to the Python Package Index
        9. Summary
    9. A. Bibliography
    10. Index

Product information

  • Title: Python: Journey from Novice to Expert
  • Author(s): Fabrizio Romano, Dusty Phillips, Rick van Hattem
  • Release date: August 2016
  • Publisher(s): Packt Publishing
  • ISBN: 9781787120761