Home  >  Article  >  Backend Development  >  Introducing efficient programming techniques in Python

Introducing efficient programming techniques in Python

巴扎黑
巴扎黑Original
2017-04-05 13:36:521225browse

I've been programming in Python for many years, and even today I'm amazed at how clean the language can make code appear and how well it applies DRY programming principles. Over the years, I have learned a lot of tips and knowledge, most of which were obtained by reading popular open source software, such as Django, Flask, and Requests.

The techniques I have selected below are often overlooked by people, but they can really help us a lot in daily programming.

1. Dictionary comprehensions and Set comprehensions

Most Python programmers know and have used list comprehensions. If you're not familiar with the concept of list comprehensions - a list comprehension is a shorter, more concise way of creating a list.

>>> some_list = [1, 2, 3, 4, 5]

>>> another_list = [ x + 1 for x in some_list ]

>>> another_list
[2, 3, 4, 5, 6]

Since Python 3.1 (and even Python 2.7), we can use the same syntax to create sets and dictionaries:

>>> # Set Comprehensions
>>> some_list = [1, 2, 3, 4, 5, 2, 5, 1, 4, 8]

>>> even_set = { x for x in some_list if x % 2 == 0 }

>>> even_set
set([8, 2, 4])

>>> # Dict Comprehensions

>>> d = { x: x % 2 == 0 for x in range(1, 11) }

>>> d
{1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False, 10: True}

In the first example, we create a set with unique elements based on some_list, and the set only contains even numbers. In the example of the dictionary table, we created a key that is a non-repeating integer between 1 and 10, and the value is a Boolean type that indicates whether the key is an even number.

Another thing worth noting here is the literal representation of sets. We can simply create a collection using this method:

>>> my_set = {1, 2, 1, 2, 3, 4}

>>> my_set
set([1, 2, 3, 4])

There is no need to use the built-in function set().

2. Use Counter counting object when counting.

This sounds obvious, but is often forgotten. Counting something is a common task for most programmers, and in most cases not very challenging - here are a few ways to make it easier.

Python’s collections class library has a built-in subclass of the dict class, which is specially designed to do this kind of thing:

>>> from collections import Counter
>>> c = Counter('hello world')

>>> c
Counter({'l': 3, 'o': 2, ' ': 1, 'e': 1, 'd': 1, 'h': 1, 'r': 1, 'w': 1})

>>> c.most_common(2)
[('l', 3), ('o', 2)]

3. Beautifully print out JSON

JSON is a very good form of data serialization and is widely used by various APIs and web services today. Using Python's built-in json processing can make the JSON string readable to a certain extent, but when encountering large data, it appears as a long, continuous line, which is difficult for the human eye to view.

In order to make JSON data more friendly, we can use the indent parameter to output beautiful JSON. This is especially useful when programming interactively at the console or logging:

>>> import json

>>> print(json.dumps(data))  # No indention
{"status": "OK", "count": 2, "results": [{"age": 27, "name": "Oz", "lactose_intolerant": true}, {"age": 29, "name": "Joe", "lactose_intolerant": false}]}

>>> print(json.dumps(data, indent=2))  # With indention

{
  "status": "OK",
  "count": 2,
  "results": [

    {
      "age": 27,
      "name": "Oz",

      "lactose_intolerant": true
    },
    {
      "age": 29,

      "name": "Joe",
      "lactose_intolerant": false
    }
  ]

}

Similarly, using the built-in pprint module can also make anything else print more beautifully.

4. Create a one-time, fast small web service

Sometimes, we need to do some simple, very basic RPC-like interactions between two machines or services. We want to use program B to call a method in program A in a simple way - sometimes on another machine. Internal use only.

I do not encourage the use of the methods described here for non-internal, one-off programming. We can use a protocol called XML-RPC (corresponding to this Python library) to do this kind of thing.

The following is an example of using the SimpleXMLRPCServer module to build a fast small file reading server:

from SimpleXMLRPCServer import SimpleXMLRPCServer

def file_reader(file_name):

    with open(file_name, 'r') as f:
        return f.read()

server = SimpleXMLRPCServer(('localhost', 8000))
server.register_introspection_functions()

server.register_function(file_reader)

server.serve_forever()

Client:

import xmlrpclib
proxy = xmlrpclib.ServerProxy('http://localhost:8000/')

proxy.file_reader('/tmp/secret.txt')

In this way, we get a remote file reading tool, with no external dependencies and only a few lines of code (of course, without any security measures, so you can only do this at home).

5. Python’s amazing open source community

The several things I mentioned here are all in the Python standard library. If you have Python installed, you can already use it in this way. For many other types of tasks, there are a large number of community-maintained third-party libraries you can use.

The following list is what I consider necessary for a useful and robust open source library:

A good open source library must...
  • Include a clear permission statement that applies to your use case.


  • The development and maintenance work is active (or, you can participate in the development and maintenance of it.)


  • Can be easily installed or deployed repeatedly using pip.


  • Have a test suite with adequate test coverage.

If you find a good library that meets your requirements, don't be embarrassed - most open source projects welcome code donations and help - even if you are not a Python master.

Original link: Improving Your Python Productivity

The above is the detailed content of Introducing efficient programming techniques in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn