Fundamentals 8 min read

Boost Your Python Productivity with 5 Overlooked Tricks

This article shares five practical Python tips—dictionary and set comprehensions, the Counter class, pretty‑printing JSON, a quick XML‑RPC web service, and criteria for choosing solid open‑source libraries—each illustrated with clear code examples to help developers write cleaner, more efficient code.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Boost Your Python Productivity with 5 Overlooked Tricks

I have been programming in Python for many years and continue to be amazed by the language’s elegance and support for DRY principles; most of the tricks I share were learned from popular open‑source projects such as Django, Flask, and Requests.

1. Dictionary and Set Comprehensions

Most Python developers are familiar with list comprehensions, a concise way to create lists. Since Python 3.1 (and even 2.7) the same syntax can be used to create sets and dictionaries.

>> some_list = [1, 2, 3, 4, 5]
>>> another_list = [x + 1 for x in some_list]
>>> another_list
[2, 3, 4, 5, 6]

Set comprehension example:

>> # 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}

Sets can also be created directly with literal syntax, e.g., {1, 2, 1, 2, 3, 4} yields set([1, 2, 3, 4]) without calling set().

2. Use Counter for Counting

The Counter subclass of dict in the collections module makes counting elements easy.

>> 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‑Print JSON

Using the built‑in json module with the indent parameter produces readable, formatted JSON, which is especially useful for logging or interactive sessions.

>> import json
>>> print(json.dumps(data))
{"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))
{
  "status": "OK",
  "count": 2,
  "results": [
    {
      "age": 27,
      "name": "Oz",
      "lactose_intolerant": true
    },
    {
      "age": 29,
      "name": "Joe",
      "lactose_intolerant": false
    }
  ]
}

The pprint module can also be used to format other data structures.

4. Create a Quick One‑Off Web Service

For simple internal RPC between machines, the SimpleXMLRPCServer module can spin up a tiny file‑reading service.

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 example:

import xmlrpclib
proxy = xmlrpclib.ServerProxy('http://localhost:8000/')
proxy.file_reader('/tmp/secret.txt')

This provides a remote file‑reading tool with no external dependencies, though it lacks security measures and should only be used in trusted environments.

5. The Power of Python’s Open‑Source Community

All the tools demonstrated are part of Python’s standard library; beyond that, a vast ecosystem of third‑party packages exists. When evaluating a library, consider these criteria:

Clear licensing that matches your use case.

Active development and maintainership (or the ability to contribute).

Easy installation via pip and straightforward deployment.

Comprehensive test suite with good coverage.

If you find a library that meets these standards, feel free to contribute or donate, even if you’re not an expert.

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

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonJSONopen sourceproductivityComprehensionsCounterXML-RPC
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.