BOBOBK

Sharing These Python Tips

MISCELLANEOUS

Despite having programmed in Python for many years, I’m still amazed by how clean the code can be and how well it adheres to the DRY (Don’t Repeat Yourself) programming principle. My experience over the years has taught me many small tricks and pieces of knowledge, mostly gained from reading popular open-source software like Django, Flask, and Requests.

Here are a few tips I’ve picked out that are often overlooked, but can genuinely help us in daily programming.


1. Dictionary Comprehensions and Set Comprehensions

Most Python programmers know and use list comprehensions. If you’re not familiar with the concept of list comprehensions, it’s a shorter, more concise way to create a list.

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

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

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

Since Python 3, 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])

>>> 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 created a set of unique elements based on _some_list_, containing only even numbers. In the dictionary example, we created a dictionary where the keys are unique integers from 1 to 10, and the values are booleans indicating whether the key is even.

Another notable point here is the literal representation of sets. We can simply create a set this way:

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

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

Without needing to use built-in functions.


2. Count things using the Counter object

This might sound obvious, but it’s often forgotten. For most programmers, counting something is a common task, and in most cases, it’s not very challenging—but there are simpler ways to accomplish it. Python’s collections library has a built-in subclass specifically for this purpose:

>>> 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. Pretty Printing JSON

JSON is an excellent data serialization format, widely used today by various APIs and web services. While Python’s built-in JSON handling makes strings somewhat readable, for large datasets, they often appear as long, continuous lines, making them difficult to read with the naked eye. To make JSON data more user-friendly, we can use parameters to output pretty JSON. This is especially useful when programming interactively in 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 make any other output print more beautifully.


4. Creating One-Time, Quick Small Web Services

Sometimes, we need to perform simple, basic RPC-like interactions between two machines or services. We might want a simple way for program B to call a method in program A—sometimes on another machine. This is strictly for internal use. I do not encourage using the methods described here for non-internal, one-time programming. We can use a protocol called XML-RPC (corresponding Python library) for this purpose. Below is an example of setting up a quick small file reading server using the SimpleXMLRPCServer module:

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 reading tool, with no external dependencies and just a few lines of code (of course, no security measures, so only do this at home).


5. Python’s Amazing Open-Source Community

These tips are from Python’s standard library, so if you’ve installed Python, you can already use them. For many other types of tasks, there’s a vast community of maintained third-party libraries available. Here’s a list of what I consider essential qualities for good and robust open-source libraries:

  • Includes a clear license statement that suits your use case.
  • Active development and maintenance (or, you can participate in its development and maintenance).
  • Easy to use.
  • Simple to install or repeatedly deploy.
  • Has a test suite with sufficient test coverage.

If you find a good library that meets your requirements, don’t be shy—most open-source projects welcome code contributions and help—even if you’re not a Python expert. Try to contribute your part.

Related

Parallelism in One Line of Python Code

TECHNOLOGY
Parallelism in One Line of Python Code

Python has a somewhat notorious reputation when it comes to program parallelization. Technical issues aside, such as thread implementation and the GIL, I believe incorrect teaching guidance is the main problem. Common classic Python multithreading and multiprocessing tutorials often seem "heavy" and tend to scratch the surface without deeply exploring the most useful content for daily work.