Efficient Python programming skills

  • 2020-04-02 09:47:02
  • OfStack

These tips I've picked out are often overlooked, but they can really help us in everyday programming.

1. Dictionary comprehensions and Set comprehensions
Most Python programmers know and use list comprehensions. If you are not familiar with list comprehensions -- a list comprehensions is a shorter, more concise way to create 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 (or even python 2.7), we can use the same syntax to create collections and dictionary tables:
 
>>> # 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, based on some_list, we created a collection with no repeating elements and only an even number. In the dictionary table example, we created an integer between 1 and 10 where the key is not repeated, and the value is Boolean to indicate whether the key is even.
The other thing to note here is the literal representation of the set. We can easily create a collection in this way:
 
>>> my_set = {1, 2, 1, 2, 3, 4} 
>>> my_set 
set([1, 2, 3, 4]) 

Instead of using the built-in function set().

2. Count objects using Counter when counting.
This sounds obvious, but is often forgotten. Counting things is a common task for most programmers, and in most cases it's not too challenging -- there are ways to do it more easily.
Python's collections class library has a built-in subclass of the dict class that does 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. Print JSON nicely
JSON is an excellent form of data serialization and is used extensively by various apis and web services today. Using python's built-in json processing makes the json string somewhat readable, but when it comes to large data, it appears as a long, continuous line that is hard for the human eye to see.
To make our JSON data more friendly, we can use the indent parameter to output nice JSON. This is especially useful when doing interactive programming or logging at the console:
 
>>> 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 
} 
] 
} 

Also, use the built-in pprint module to make everything else print prettier.

Create small, one-time, fast web services
Sometimes we need to do simple, basic RPC interactions between two machines or services. We want to use program B to call A method in program A in an easy way -- sometimes on another machine. For internal use only.
I do not encourage the methods described here to be used in non-internal, one-time programming. We can do this using a protocol called xml-rpc (the corresponding Python library).
Here 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') 


This gives us a remote file reader with no external dependencies and just a few lines of code (of course, no security measures, so you can only do this at home).

Python's amazing open source community
The few things I mentioned here are from the Python standard library, which you can use if you have Python installed. For many other types of tasks, there are plenty of community-maintained third-party libraries you can use.
Here's a list of what I consider essential for a good and robust open source library:

Good open source libraries must...

The & # 8226; Include a clear license statement that applies to your usage scenario.
The & # 8226; Development and maintenance work is active (or, you can participate in developing and maintaining it).
The & # 8226; The ability to simply use a PIP installation or repeat deployment.
The & # 8226; There are test suites with adequate test coverage.
If you find a good library that matches your needs, don't be shy -- most open source projects welcome code donations and help -- even if you're not a Python expert.

Original: http://www.aqee.net/improving-your-python-productivity/

Related articles: