Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Chandu yadav
Page 2 of 81
Read and write AIFF and AIFC files using Python (aifc)
The aifc module in Python provides functionality for reading and writing AIFF (Audio Interchange File Format) and AIFF-C files. AIFF is a standard format for storing digital audio samples, while AIFF-C is an extended version that supports audio compression. Audio File Parameters Audio files contain several key parameters that describe the audio data ? Sampling rate (frame rate): Number of times per second the sound is sampled Number of channels: Indicates mono (1), stereo (2), or quadro (4) audio Frame: Consists of one sample per channel Sample size: Size in bytes of each sample ...
Read MorePython import modules from Zip archives (zipimport)
The zipimport module allows Python to import modules and packages directly from ZIP archives. This feature is useful for distributing Python applications as single ZIP files or organizing multiple modules in a compressed format. Creating a ZIP Archive First, let's create a ZIP archive containing Python modules. We'll use the zipfile module to compress multiple Python files ? import zipfile import os # Create some sample Python files first with open('hello.py', 'w') as f: f.write('def greet(name): return f"Hello, {name}!"print(greet("World"))') with open('math_utils.py', 'w') as f: ...
Read MoreByte-compile Python libraries
Python is an interpreter-based language that internally compiles source code to bytecode when a script is executed. While bytecode is automatically removed for scripts, imported modules create .pyc files in the __pycache__ folder for faster subsequent imports. You can also explicitly byte-compile Python files using the py_compile module. What is Byte Compilation? When Python imports a module, it automatically creates a compiled bytecode version with .pyc extension. This bytecode loads faster than parsing the original .py file. The py_compile module allows you to create these bytecode files manually without running or importing the code. Command Line Compilation ...
Read MoreRandom access to text lines in Python (linecache)
The linecache module in Python's standard library provides random access to any text file by line number. This module is extensively used by Python's traceback system to generate error traces and caches previously read lines to improve performance when reading lines repeatedly. Key Functions getline(filename, lineno) Returns the specified line number from a file. If the line doesn't exist, it returns an empty string. If the file isn't in the current directory, it searches in sys.path. getlines(filename) Returns all lines from a file as a list object. clearcache() Clears the internal cache when ...
Read MoreResource Usage Information using Python
Python's resource module helps monitor and control system resource usage in UNIX-based systems. It provides methods to get resource usage statistics and set resource limits to prevent processes from consuming excessive system resources. Importing the Resource Module First, import the resource module ? import resource Resource Limits The module uses two types of limits: soft limits (current limit that can be changed) and hard limits (maximum value for soft limits). Soft limits cannot exceed hard limits, and hard limits can only be reduced. Method resource.getrlimit(resource) Returns the current soft and hard ...
Read MoreConvenient Web-browser controller in Python
To display web-based documents to users in Python, there is a module called webbrowser. It provides a high-level interface to handle web documents and control web browsers programmatically. On UNIX-based systems, this module supports lynx, Netscape, Mosaic and other browsers. For Windows and macOS, it uses the standard browsers. Importing the Module To use this module, we need to import it first ? import webbrowser Main Methods webbrowser.open(url, new=0, autoraise=True) This method opens the specified URL using the default web browser. The new parameter controls how the URL opens ? ...
Read MoreSecure Hashes and Message Digest in Python
The hashlib module in Python provides a common interface for secure hash algorithms like SHA1, SHA224, SHA256, SHA512, and RSA's MD5. These algorithms are essential for data integrity verification, password storage, and digital signatures. To use hashlib, import it at the beginning of your Python code ? import hashlib Available Hash Algorithms You can check which algorithms are available on your system ? import hashlib print("Guaranteed algorithms:", hashlib.algorithms_guaranteed) print("Available algorithms:", hashlib.algorithms_available) Guaranteed algorithms: {'sha3_224', 'sha256', 'sha3_256', 'md5', 'sha1', 'sha224', 'sha512', 'sha3_384', 'sha384', 'sha3_512'} Available algorithms: {'sha3_224', 'sha256', 'sha3_256', ...
Read MorePython GNU readline Interface
The readline module is a UNIX-specific interface to the GNU readline library. It provides functions to manage command history, line editing, and tab completion in Python's interactive interpreter. This module can enhance the user experience by enabling features like command history persistence and custom completion. For macOS systems, the readline module may use the libedit library instead of GNU readline, which has different configuration options. Importing the Module To use the readline functionality, import the module in your Python code ? import readline Key Functions Function Description readline.parse_and_bind(string) ...
Read MorePython Context Manager Types
Python context managers enable automatic resource management using the with statement. They define what happens when entering and exiting a runtime context, ensuring proper cleanup of resources like files, database connections, or locks. Context managers implement two special methods to define their behavior ? The __enter__() Method The __enter__() method is called when entering the runtime context. It sets up the resource and returns an object that will be assigned to the variable after the as keyword in the with statement. class SimpleContext: def __enter__(self): ...
Read MorePython program to print all the common elements of two lists.
Given two lists, we need to find and print all the common elements between them. Python provides several approaches to solve this problem efficiently. Examples Input : list1 = [5, 6, 7, 8, 9] list2 = [5, 13, 34, 22, 90] Output : {5} Explanation The common element between both lists is 5, which appears in both lists. Using Set Intersection Convert both lists to sets and use the intersection operator (&) to find common elements ? def find_common_elements(list1, list2): ...
Read More