Problems encountered in Python development

Opening chapter

It has been two weeks since using python to develop the web (flask for framework) project. Although it is easy to transfer from java to python at one stroke, the process still has some problems, which may be the wrong way to cut in.

On the other hand, because of the time, we didn't know the specific content of Python in advance, so in the development process, we basically took java things into python.

For example:

  • A function can be solved by ArrayList in java. Is there something similar in python?
  • In java, int i = 0 is used to define shaping. What about python?

Because of this, there are many problems. Let's summarize.

Outline the questions

I don't need to look down on the useless ones. They probably involve python syntax and API, redis module, requests module and flash framework

  • The scope of python local variables
  • python constant definition
  • python replaces strings with placeholders
  • python redis package, get method return value is byte
  • Timeout setting of requests Library
  • When the flash project runs in debug mode, the startup log will be printed twice.
  • flask project cannot be accessed through ip in LAN

concrete problems

1. The scope of variables in Python function in try catch block

First of all, in python, the variables defined in try can still be accessed in except

        i = 1
        # Simulated abnormality
        j = 1 / 0
        print(i) # Printing result is 1

In java, direct compilation fails

    public static void main(String[] args) {
            int i = 0;
            j = 1/0;
        }catch (Exception e) {
            System.out.println(i); # Compile and report errors

2. python constant definition

There is no explicit way to define constants in python, that is, you think it is, but it is still recommended to name them in uppercase. Of course, this way of definition can still change the value of 'constants' at any time


In java, the final keyword is used to modify

final int RETRY_TIMES = 0

3. The data type of redis hash cannot set the expiration time for the field

This problem has nothing to do with python.

It is the first time to use the redis hash data structure.

First, the redis hash data type stores data in the following ways.

set key fieldname fieldvalue

And you can't set the expiration time for fieldname, you can only set the expiration time for key.

4. python string replacement

In java, string replacement is performed by the following methods:

    String s = "hello world";
    s.replace("world", "dog");

In addition to this, there is another way in python:

    str = 'hello, {}'.format('world')
  • python redis module, the return value of get method is byte
    value = my_redis.get('key')
    print(value) # b'1234'

5. requests library setting timeout

First, the requests library sets the timeout in the following ways

    # Timeout is passed in by the way of ancestor, data, timeout=(connect_timeout, read_timeout))
    # Will automatically change to

If timeout=1.0 is passed in, it will automatically change to the following form, data, timeout=(1.0, 1.0))

6. The flask project cannot be accessed through ip in the LAN

Solution: pass in the parameter host=' ''', port=5001, debug=True, threaded=True)

7. When the flash project is started in debug mode, it will print the log twice

First, let's look at the startup entry code

if __name__ == '__main__':
    # Initialize redis
    # Omit some codes'', port=5001, debug=True, threaded=True)

Then start the project in debug mode, and the console outputs the following logs (some of them are deleted)

pydev debugger: process 72880 is connecting

Connected to pydev debugger (build 193.6494.30)
2020-03-13 19:27:06 INFO  ( MainThread - port = 5001
2020-03-13 19:27:06 INFO  ( MainThread - redis_host = localhost
2020-03-13 19:27:06 INFO  ( MainThread - redis_port = 6379
2020-03-13 19:27:06 INFO  ( MainThread - redis_db = 15
2020-03-13 19:27:06 INFO  ( MainThread - redis_password = 
 * Serving Flask app "anti-fraud" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on (Press CTRL+C to quit)
 * Restarting with stat
pydev debugger: process 72882 is connecting

2020-03-13 19:27:07 INFO  ( MainThread - port = 5001
2020-03-13 19:27:07 INFO  ( MainThread - redis_host = localhost
2020-03-13 19:27:07 INFO  ( MainThread - redis_port = 6379
2020-03-13 19:27:07 INFO  ( MainThread - redis_db = 15
2020-03-13 19:27:07 INFO  ( MainThread - redis_password = 

You can see that the redis startup log has been printed twice. The solution is as follows:

# Added the use ﹐ reload = false parameter'', port=5001, debug=True, use_reloader=False, threaded=True)


The above is the problem encountered in the python development process. There is no difficulty. It's just a simple record.

Tags: Programming Python Redis Java

Posted on Fri, 13 Mar 2020 08:20:44 -0400 by dizzy1