What is your favourite package in the Python standard library and why?

James Derrick
James Derrick Administrator, Employee Posts: 242
First Anniversary Solution Developer Community of Practice Member Ansys Employee 5 Up Votes
admin
edited November 2022 in General Language Questions

And least favourite?

I really like getpass and dataclasses myself. dataclasses makes class creation so much quicker and easier whilst enforcing type hinting in an easy-to-understand way. It's just a really nice addition to the language. Although getpass is great too, because it does one small thing very well.


If you don't know dataclasses look like this:


Regular class:

class Car:
    def __init__(self, make: str, model: str):
        self.model = model
        self.make = make


But the same as a dataclass is just


from dataclasses import dataclass


@dataclass
class Car:
     make: str
     model: str


And they both work exactly the same! It makes writing classes so much easier, especially when teaching new people about classes.

Tagged:

Comments

  • Doug Addy
    Doug Addy Member, Employee Posts: 21
    First Anniversary Name Dropper First Answer Ansys Employee

    My favourite has to be Pathlib, it's a great example of a package that takes a tedious and error-prone task and makes it much simpler and more reliable.

    Using the package is as simple as creating a Path object from a string, python will then handle path separators and globs for you correctly regardless of your platform. This allows you to write much clearer code that's inherently cross-platform.

    My least favourite is probably gettext, this is the stdlib interface for internationalization and localization. It's a wrapper for a core utility on linux also called gettext, which allows applications to display text in different languages for different users. Unfortunately there are some existing bugs in the supporting tools, and neither the file format nor the packaging workflow are very pythonic.

  • jfthuong
    jfthuong Member, Employee Posts: 3
    First Anniversary Ansys Employee First Comment Photogenic

    +1 for pathlib and dataclasses . I use them all the time and this makes life so much easier for manipulating paths and data (respectively).

    I would also add the logging module, which is a bit hard to master, but improve the quality of code to control how messages are displayed.

    And of course re when you want to use regular expressions.

  • Chris Harrold
    Chris Harrold Member, Administrator, Employee Posts: 100
    First Anniversary First Answer Ansys Employee Solution Developer Community of Practice Member
    admin

    Massive +1 for logging!


    I also quite like the JSON reader/writer - dead handy for passing data.

  • James Derrick
    James Derrick Administrator, Employee Posts: 242
    First Anniversary Solution Developer Community of Practice Member Ansys Employee 5 Up Votes
    admin
    edited December 2022

    Ooo Logging is v powerful, plus we could really do with a guide to logging... When I get some time I might write an introductory article about it. Same for JSON.