Python package definition, structure, import process

Source: Internet
Author: User

Suppose you want to design a module set (that is, a python package) to process audio files and sound data in a unified manner. Normally, their extensions have different sound formats, such as WAV, AIFF, AU ), therefore, you may need to create and maintain a set of conversion modules between the increasing file formats. In addition, audio data processing (such as mixing, adding echo, and applying the balance function) may be performed. Therefore, you can write a endless stream module to perform these operations: the module design package is as follows:

Sound/top-level package
_ Init _. py initialize the sound package
Formats/subpackage For File Format Conversions
_ Init _. py
Wavread. py
Wavwrite. py
Aiffread. py
Aiffwrite. py
Auread. py
Auwrite. py
...
Effects/subpackage For Sound Effects
_ Init _. py
Echo. py
Surround. py
Reverse. py
...
Filters/subpackage For Filters
_ Init _. py
Equalizer. py
Vocoder. py
Karaoke. py
...

After the package is imported, Python searches for sub-directories of the package through the directory in SYS. Path. Each package must have the _ init _. py file to prevent some directories from having a common name. In the simplest case, __init _. py can be just an empty file, but it can also perform package initialization.Code, Including setting the _ all _ variable, which will be introduced later. We can import a single module from the package,

For example, import sound. effects. Echo loads the sound. effects. Echo sub-module. It must reference the full name. Sound. effects. Echo. echofilter (input, output, delay = 0.7, atten = 4)

Another method for importing sub-modules: from sound. effects import echo loads the echo submodule without the package prefix. Therefore, it can be used as follows: Echo. echofilter (input, output, delay = 0.7, atten = 4)

Alternatively, you can import echofilter from sound. effects. Echo (input, output, delay = 0.7, atten = 4)

Note that if you use a package to import a sub-module (or sub-package), such as a function, class, or variable. The Import Statement first tests whether the imported object is defined in the package. If not, it assumes that this is a module and tries to load it. If not found, an importerror exception is thrown.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.