There's no denying the popularity of Python in recent years, as it's consistently ranked at the top of any programming language rankings. But Python is also a language that is easy to learn and difficult to master, and while you can quickly write simple, usable programs in it, if you need to build more complex projects in Python at work, you can waste a lot of time and energy in them. In this live broadcast, Teacher Yin Huisheng will analyze the most common pitfalls in your work, help you improve your work efficiency, and also let you know more advanced Python skills.Copy the code

What is Python good for

  • The script
    • Encapsulate the API
    • Automated o&M automated testing
  • Text processing
    • Financial statistics text related regular expression
    • The recognition of words invokes deep learning libraries
    • The text combined
    • Crawler data is not normalized and needs to be processed
  • Website development
    • Internal demand for vast land
      • Simple interface, RestfulApi
      • Graphical presentation class requirements
      • Public opinion monitoring
    • WEB frameworks Django, Flask
  • Scheduling system
    • Program coupling
    • Scheduling system
    • Scheduling combination and linkage between clusters
  • Scientific computing
    • Numpy Pandas
  • The crawler
    • Beautifulsoup, urllib
  • Machine learning
  • Introduction to programming

pit

  • Initial use of Python to implement fast functionality landing
  • Later concurrency is large, we will use Go, Java reconstruction

1. Grammar

  • 1. String resident
    • Python everything is an object
    • Length 0, A-z, A-Z will stay
    • The interpreter considers two strings to be the same, and the pure character form is considered the same even though it has been declared twice
      • The advantage is that you can save a lot of memory that works for older servers; For modern servers
      • a = “apple” b = “apple” a is b True
    • Not with special strings
      • a = “apple!” b = “apple!” a is b False
  • 2. Immutable objects with the same value
    • dict1 = dict()

Regular key collisions should maintain a linked list that Python overrides directly

In [1]: dict1 = dict1 In [2]: dict1[1] = "golang" In [3]: dict1[1.0] = "python" In [4]: dict1 Out[4]: {1: 'python'} [5] : In dict1. The keys () Out [5] : [1] In [6] : 1 = = 1.0 Out [6] : TrueCopy the code
  • 3 the generator
    • To keep the agreement consistent
    • It can only be called once and then it’s empty
In [23]: genexpr = ( lambda x: x*i for i in range(10) ) In [24]: for i in genexpr: ... : print(i(10)) ... : 0 10 20 30 40 50 60 70 80 90 In [25]: for i in genexpr: ... : print(i(10)) ... :Copy the code
  • 4. Function parameters are passed

All arguments to a function are an object of the specified type

def function1(var1, var2=[]):
	var2.append(var1)
	return var2
    
In [34]: data1= function1(111)

In [35]: print function1(111)
[111, 111]

In [36]: print function1(111)
[111, 111, 111]

In [37]: print function1(111)
[111, 111, 111, 111]

Copy the code

2. The framework

  • 1 Database connection pool
  • 2 Database character set
    • Emjoy need utf8mb4

3. The module

  • 1. Multiple inheritance of classes

Python2 Classical class PYTHon3 New class inheritance order different one depth-first one breadth-first Suggestion look at mrO () python variable scope rules LEGB rules

  • 2. Use modules instead of wheels
    • Self-developed things do not consider the multithreaded atomic safety of the transmission of exceptions recommended to give priority to the own module
    • If you can’t do Pythonic then go to a third-party library and write your own
      • The requirement is neat and convenient for later analysis

      • The Logging module formats the output and customizes it more easily than writing one yourself

      • Urllib-requests – aiohttp(asynchronous requests)

4. The cache

  • Cache avalanches with downgrades
    • Cache penetration workaround

other

  • A virtual environment
  • Decorators and design patterns
    • function.warps()

    • Call the callable object

  • The GIL affects row-intensive tasks