Learn Python's everyday 6

Source: Internet
Author: User
Tags serialization

Error, Debug, and test:

Error handling:

Try:    print (' Try ... ')    r = 10/0    print (' Result: ', R) except Zerodivisionerror as E:    print (' except: ', E) Finally: print ('    finally ... ') print (' END ')

The first is to execute the statement, then find the error will jump to execute except, statement, and then executed sequentially, if it is correct, will not execute the except statement.

The error can also be broken down, the fault itself is a class, all inherit from Baseexception, so try not to appear the wrong parent class and subclass capture simultaneously,

Because only the parent class's catch error will be executed at this time.

Call stack:

If an error is not captured, it will always be thrown up and then captured by the Python interpreter, which can be positioned by parsing the wrong call stack information at the time of the error.

The wrong location.

Error logged:

Python's built-in logging module makes it easy to log error messages

# Err_logging.pyimport Loggingdef foo (s):    return 10/int (s) def Bar (s):    return foo (s) * 2def main ():    try:
   
    bar (' 0 ')    except Exception as E:        logging.exception (e) Main () print (' END ') $ python3 err_logging.pyERROR:root: Division by Zerotraceback (most recent call last):  file "err_logging.py", line +, in main    bar (' 0 ')  file "Err_ logging.py ", line 9, in Bar    return foo (s) * 2  File" err_logging.py ", line 6, Foo    return 10/int (s) zerodi Visionerror:division by Zeroend
   

Throw Error:

Because the error is class, and then capturing an error is capturing to an instance, if you want to throw an error, you can define an incorrect class as needed.

Select a good inheritance relationship and then throw an instance of the error with the raise statement. Define our own error types only when necessary, and if possible,

Try to use Python's built-in error types.

Finally, there is a way to handle the error:

# err_reraise.pydef Foo (s):     n = Int (s)    if n==0:        raise ValueError (' Invalid value:%s '% s)    return 10/ndef Bar ():    try:        foo (' 0 ')    except ValueError as E:        print (' valueerror! ')        Raisebar ()

After catching an exception, the error is thrown out of the raise statement because the purpose of capturing the error is simply to record, because the current function does not know

What to do with the error, so throwing up is a good way to end up with the top-level caller.

Debugging:

1. One method is simple and rough, print directly with print ().

2. Assert that any use of print () to assist viewing can be overridden by an assertion (assert).

3.logging, replace print () with logging is the third way, logging can specify the level of logging information, there are debug,info,warning,error

Wait a few levels, specify a high level, the lower levels will not work.

4.pdb, enable the Python debugger pdb, let the program run in a single step, you can view the running state at any time. Pdb.set_trace (), also with PDB, only

You need to import the PDB, and then place a pdb.set_trace () where it might go wrong, you can set a breakpoint and then pause and go to the PDB

Debug the environment, use p to see the variables, and C to continue running.

5.IDE, IDE with debugging capabilities, some of the better Python Ides.

Unit tests:

A test that is used to validate a module, a function, or a class for correctness. We give a series of data and expect the output to be the result of our

The expected results, if not consistent, indicate a problem in our code.

Document testing:

Use annotations to tell the code what we want to get, and then some tools to automatically generate the document.

IO Programming:

IO refers to Input/output, which is the input and output. IO programming due to internal and external device speed mismatch, but also can be divided into synchronous IO and asynchronous IO two ways,

Synchronous IO is now involved

File Read and write:

Read the file

>>> f = open ('/users/michael/test.txt ', ' R ')

If it does not exist, it will be an error, and then if the successful open call Read () method can be read into memory once, with a Str object, remember to call

The close () method to close the file, if the error is unable to close the file, F.close () will not be called, so use try...finally to implement, but the total write will be very troublesome,

So you can call the Python with statement to call the close () method automatically:

With open ('/path/to/file ', ' R ') as F:    print (F.read ())

Like the open () function, which returns an object with the Read () method, called File-like object in Python, there are many other streams besides file, but as long as

There is a read () method to do it. Read

>>> from IO import stringio>>> f = Stringio () >>> f.write (' hello ') 5>>> f.write (') 1& Gt;>> f.write (' world! ') 6>>> print (F.getvalue ()) Hello world!

You need to use Bytesio to manipulate the binary data.

Stringio and Bytesio are methods of operating str and bytes in memory, allowing for a consistent interface to read and write files.

Take the binary file, open the file with ' RB ' mode, read the character encoded text file, add a encoding= ' gkb ', encounter

Non-canonical can add parameters errors= ' Ignore '.

Write a file

>>> f = open ('/users/michael/test.txt ', ' W ') >>> f.write (' Hello, world! ') >>> F.close ()

Write the file and read the file, but the difference is the incoming identifier, ' W ' or ' WB ' represents a text file or write a binary file, if you want to write a specific encoded text

file, to pass the encoding parameter to open (), the string is automatically converted to a specific encoding.

Stringio and Bytesio

Manipulating Files and directories:

Python's OS module encapsulates the operating system's directory and file operations, some of which are in the OS module and some in the Os.path module.

Serialization:

The process of changing a variable from memory to a stored or transportable procedure is called serialization, which in turn re-reads the variable contents from the serialized object into memory called deserialization.

Python provides the Pickle module for serialization, but the JSON module can be used to make the sequence more generic and more web-compliant.

The dumps () and loads () functions of the JSON module are used for serialization and deserialization, and if the default serialization and deserialization mechanisms do not meet the requirements, you can pass in more

Parameters to customize the serialization or deserialization rules.

Processes and Threads:

There are 3 ways to implement multiple tasks: 1. Multi-process mode;

2. Multithreading mode;

3. Multi-process + multi-Threading mode

The smallest execution unit of a thread, and the process is made up of at least one thread.

Multi-process:

Under Unix/linux, you can use the fork () call to implement multiple processes.

If you want to implement multi-process across platforms, you can use the multiprocessing module.

The subprocess module makes it easy to start a subprocess and then control its input and output.

Inter-process communication is achieved through queue, pipes, etc.

Multithreading:

With multiple threads in the same process, the Python standard library provides _thread and threading two modules, most of which use the Advanced module threading.

Starting a thread is to pass a function in and create a thread instance, and then call Start () to start execution. In multi-threaded, due to thread scheduling is caused by the system

The decision is that when threads are executed alternately, the content is easily changed. To ensure that a variable can be locked to a thread, a process provides a lock and then lets

Thread to fetch the lock, so that the thread can execute the statement only if the lock is acquired, without causing a conflict. And then the downside is as long as it lowers.

Efficiency and easy to create deadlocks. When the Python interpreter executes the code, there is a Gil Lock: Global interpreter lock, before any Python thread executes,

The Gil lock must be obtained first. This Gil global lock actually locks the execution code of all threads.

ThreadLocal:

Import threading# Create global threadlocal object: Local_school = threading.local () def process_student ():    # Gets the student of the current thread association:    std = local_school.student    print (' Hello,%s (in%s) '% (Std, Threading.current_thread (). Name)) def Process_ Thread (name):    # bind Threadlocal's student:    local_school.student = name    process_student () T1 = Threading. Thread (target= process_thread, args= (' Alice ',), name= ' thread-a ') t2 = threading. Thread (target= process_thread, args= (' Bob ',), name= ' Thread-b ') T1.start () T2.start () T1.join () T2.join ()

  

Local variables are cumbersome in function calls, one layer at a time is cumbersome, and the global variable Local_school is a Threadlocal object, each thread

It can read and write student properties, but it does not affect each other. You can think of Local_school as a global variable, and each property, such as Local_school.student, is a line

Process local variables, can read and write without interfering with each other, do not need to manage the lock problem, threadlocal internal processing.

The most common place for threadlocal is to bind a database connection, HTTP request, user identity information, and so on for each thread, so that all the tuning of a thread

These resources can be accessed very easily using the handler functions. A threadlocal variable is a global variable, but each thread can only read and write its own

Independent copy of the thread, not interfering with each other. Threadlocal solves the argument that each function in a thread can access these resources with great ease.

Distributed processes:

The Python Distributed Process interface is simple and well packaged, and is suitable for the need to distribute heavy tasks across multiple machines and environments.

Note that the queue function is used to transfer tasks and receive results, and the amount of descriptive data for each task should be as small as possible. such as sending a task to process the log file,

Instead of sending a hundreds of-megabyte log file itself, send the full path to the log file and read the file from the worker process to the shared disk.

Learn Python's everyday 6

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.