Is there an easy way to get all the common module extensions?

I am making a library that deals with Python modules. Without going into details, I need a list of common Python module extensions.

Obviously I want .py , but I would also like to include ones like .pyw , .pyd , etc. In other words, I want something that you can import.

Is there a tool in the standard library that will make this list for me? Or do I need to do this myself (and hard-code all the values)?

 extensions = ['.py', '.pyw', ...] 
+6
source share
1 answer

This function can be found in the importlib.machinery module . There are many constants inside that relate to various extensions of the Python module:

 >>> import importlib >>> importlib.machinery.SOURCE_SUFFIXES ['.py', '.pyw'] >>> importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES ['.pyo'] >>> importlib.machinery.EXTENSION_SUFFIXES ['.pyd'] >>> importlib.machinery.DEBUG_BYTECODE_SUFFIXES ['.pyc'] >>> 

This way you can easily join them in global set 1 :

 >>> set(importlib.machinery.SOURCE_SUFFIXES + ... importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES + ... importlib.machinery.EXTENSION_SUFFIXES + ... importlib.machinery.DEBUG_BYTECODE_SUFFIXES) {'.pyw', '.py', '.pyd', '.pyc', '.pyo'} >>> 

You may also be interested in the all_suffixes function:

 >>> importlib.machinery.all_suffixes() ['.py', '.pyw', '.pyc', '.pyd'] >>> 

Note that this function will replace .pyc with .pyo if Python is running with the -O or -OO . To avoid this, you can:

 >>> set(importlib.machinery.all_suffixes() + ... importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES + ... importlib.machinery.DEBUG_BYTECODE_SUFFIXES) {'.pyw', '.py', '.pyd', '.pyc', '.pyo'} >>> 

This ensures that both .pyc and .pyo are in the set.


Finally, you should be wary of importlib.machinery.BYTECODE_SUFFIXES . As @MartijnPieters notes in the comments, it will always be equal to either OPTIMIZED_BYTECODE_SUFFIXES or DEBUG_BYTECODE_SUFFIXES . This means that if you add it to the collection, you will get either a duplicate .pyc value or a duplicate .pyo value (if you are not using a set of courses).

From docs :

importlib.machinery.BYTECODE_SUFFIXES

A list of strings representing the suffix of the recognized file for bytecode modules. Set the value to DEBUG_BYTECODE_SUFFIXES or OPTIMIZED_BYTECODE_SUFFIXES depending on whether __debug__ is true.

I did not use this constant because I need both OPTIMIZED_BYTECODE_SUFFIXES and DEBUG_BYTECODE_SUFFIXES in the collection. So there is no reason to add it.


1 I decided to use the kit because they have faster search times than lists. Meaning, they are better suited for a global collection of values ​​that will not change and that do not require a special order. In addition, they ensure that we do not accidentally add duplicate extensions to the collection.

+7
source

Source: https://habr.com/ru/post/979929/


All Articles