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
Python Articles
Page 708 of 855
Encode and decode binhex4 files using Python (binhex)
The binhex module encodes and decodes files in binhex4 format, which was used to represent Macintosh files in ASCII format for transmission over text-based protocols. This module handles only the data fork of files. Functions The binhex module provides two main functions: binhex.binhex(input, output): Converts a binary file to binhex4 format. The input parameter is the filename to encode, and output can be either a filename or a file-like object with write() and close() methods. binhex.hexbin(input, output): Decodes a binhex4 file back to binary format. The input can be a filename or file-like object, and output ...
Read MorePython Context Variables
Context variables can have different values depending on their context. Unlike Thread-Local Storage where each execution thread may have a different value for a variable, a context variable may have several contexts in one execution thread. This is useful for keeping track of variables in concurrent asynchronous tasks. The ContextVar class is used to declare and work with Context Variables. Creating a Context Variable You can create a context variable with an optional default value ? import contextvars name = contextvars.ContextVar("name", default="Hello") print(f"Variable name: {name.name}") print(f"Default value: {name.get()}") Variable name: name ...
Read MoreGenerate and parse Mac OS X .plist files using Python (plistlib)
Files with .plist extension are used by macOS applications to store application properties. The plistlib module provides an interface to read and write these property list files in Python. The plist file format serializes basic object types like dictionaries, lists, numbers, and strings. Usually, the top-level object is a dictionary. Values can be strings, integers, floats, booleans, tuples, lists, and dictionaries (but only with string keys). Main Functions Function Description load() Read a plist file from a readable binary file object dump() Write value to a plist file via writable binary ...
Read MoreInspect live objects in Python
The inspect module in Python provides powerful functions to examine live objects such as modules, classes, methods, functions, and code objects. These functions perform type checking, retrieve source code, inspect classes and functions, and examine the interpreter stack. Key Functions getmembers() − Returns all members of an object in a list of name-value pairs sorted by name. Optional predicate parameter filters results. getmodulename() − Returns the module name from a file path, excluding enclosing package names. Example Module Let's create a sample module to demonstrate inspect functionality ? # inspect_example.py '''This is module ...
Read MoreConfiguration file parser in Python (configparser)
The configparser module from Python's standard library provides functionality for reading and writing configuration files as used by Microsoft Windows OS. Such files usually have .INI extension. The INI file consists of sections, each led by a [section] header. Between square brackets, we can put the section's name. Section is followed by key/value entries separated by = or : character. It may include comments, prefixed by # or ; symbol. A sample INI file is shown below − [Settings] # Set detailed log for additional debugging info DetailedLog=1 RunStatus=1 StatusPort=6090 StatusRefresh=10 Archive=1 # Sets the location of ...
Read MoreEssential Python Tips And Tricks For Programmers?
Python offers numerous shortcuts and tricks that can make your code more concise and efficient. These techniques are particularly useful in competitive programming and professional development where clean, optimized code matters. Finding Largest and Smallest Elements with heapq Getting n Largest Elements The heapq module provides efficient functions to find the largest elements without sorting the entire list − import heapq marks = [91, 67, 34, 56, 78, 99, 87, 23, 78, 66] print("Marks =", marks) print("2 Largest =", heapq.nlargest(2, marks)) Marks = [91, 67, 34, 56, 78, 99, 87, 23, ...
Read MoreReading images using Python?
Reading images is a fundamental task in computer vision and image processing. Python offers several powerful libraries for this purpose, with OpenCV and PIL (Pillow) being the most popular choices. This tutorial covers both approaches for reading, displaying, and saving images. Installing Required Libraries First, install the necessary packages using pip − $ pip install opencv-python $ pip install numpy $ pip install Pillow Reading Images Using OpenCV OpenCV (Open Source Computer Vision) is a comprehensive library for computer vision tasks. It provides over 2, 500 optimized algorithms for image processing, machine learning, ...
Read MoreSound-playing interface for Windows in Python (winsound)
The winsound module is specific to Python installations on Windows operating systems. It provides a simple interface for playing sounds and system beeps. The module defines several functions for different types of audio playback. Beep() When this function is called, a beep is heard from the PC's speaker. The function needs two parameters: frequency and duration. The frequency parameter specifies the frequency of the sound and must be in the range 37 through 32, 767 hertz. The duration parameter specifies the duration of sound in milliseconds. Example import winsound # Play a beep at ...
Read MoreDetection of ambiguous indentation in python
Indentation is a crucial feature of Python syntax. Code blocks in functions, classes, or loops must follow the same indent level for all statements within them. The tabnanny module in Python's standard library can detect violations in proper indentation. This module is primarily intended for command line usage with the -m switch, but it can also be imported in an interpreter session to check Python files for indentation problems. Command Line Usage To check a Python file for indentation issues, use the following command ? python -m tabnanny -q example.py For verbose output ...
Read MoreDetermine type of sound file using Python (sndhdr)
The sndhdr module in Python's standard library provides utility functions to determine the type and properties of sound files. It analyzes file headers to extract audio metadata without loading the entire file. Return Value Structure The functions return a namedtuple containing five attributes ? Attribute Description filetype String representing 'aifc', 'aiff', 'au', 'hcom', 'sndr', 'sndt', 'voc', 'wav', '8svx', 'sb', 'ub', or 'ul' framerate Sampling rate (actual value or 0 if unknown) nchannels Number of channels (or 0 if undetermined) nframes Number of frames (or -1 if unknown) ...
Read More