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 Arjun Thakur
Page 2 of 75
The fcntl and ioctl System Calls in Python
The fcntl module in Python provides an interface to the fcntl() and ioctl() Unix system calls for file control operations. This module is essential for low-level file descriptor manipulation, file locking, and I/O control operations on Unix-like systems. All methods in this module take a file descriptor (integer) or io.IOBase object as their first argument. To use this module, import it first: import fcntl fcntl.fcntl(fd, op[, arg]) Method This method performs operations on file descriptors. The op parameter defines the operation, and the optional arg can be an integer or string. When arg is ...
Read MoreThe most Common POSIX System Calls in Python
The posix module provides low-level UNIX system call functionality in Python. While you shouldn't import it directly, understanding its core functions helps you work with file descriptors and system operations through the os module. Overview The posix module works on UNIX environments and provides operating system functionality. Instead of importing posix directly, use the os module, which acts as a superset of posix on UNIX systems. On non-Unix systems, posix is not available, but os provides similar functionality with some limitations. posix.environ Constant The environ is a dictionary object containing environment variables. Keys and values are ...
Read MoreGenerate Secure Random Numbers for Managing Secrets using Python
To generate secure random numbers cryptographically we can use the secrets module in Python. This module is helpful to create secure passwords, account authentication tokens, security tokens, and other cryptographic secrets. The secrets module provides access to the most secure source of randomness that your operating system provides, making it suitable for managing secrets where security is essential. Importing the Secrets Module To use the classes and functions of the secrets module, we need to import it into our code ? import secrets Random Number Generation Methods The secrets module provides several ...
Read MorePython Container Datatypes
Python's collections module provides specialized container datatypes that serve as alternatives to built-in containers like dict, list, and set. These containers offer enhanced functionality and performance for specific use cases. Collections Module Container Types Container Description namedtuple() Creates tuple subclasses with named fields for better readability deque Double-ended queue optimized for adding/removing from both ends Counter Dict subclass for counting hashable objects ChainMap Creates a single view of multiple mappings OrderedDict Dict that remembers insertion order (deprecated in Python 3.7+) UserList Wrapper around list ...
Read MorePython program to find the length of the largest consecutive 1's in Binary Representation of a given string.
Given a number, we need to find the length of the longest consecutive 1's in its binary representation. This problem can be solved using bit manipulation techniques. Example Input: n = 15 Output: 4 The binary representation of 15 is 1111. Algorithm The algorithm uses bit manipulation to count consecutive 1's: Step 1: Input the number Step 2: Use a counter variable to track iterations Step 3: Apply bitwise AND with left-shifted number Step 4: This reduces each sequence of 1's by one in each iteration Step 5: Count iterations until the number becomes 0 Using Bit Manipulation This method uses the property that n & (n
Read MoreHow to Find Hash of File using Python?
You can find the hash of a file using Python's hashlib library. Since files can be very large, it's best to use a buffer to read chunks and process them incrementally to calculate the file hash efficiently. Basic File Hashing Example Here's how to calculate MD5 and SHA1 hashes of a file using buffered reading − import hashlib BUF_SIZE = 32768 # Read file in 32KB chunks md5 = hashlib.md5() sha1 = hashlib.sha1() with open('program.cpp', 'rb') as f: while True: data ...
Read MoreHow to handle exception inside a Python for loop?
You can handle exceptions inside a Python for loop using try-except blocks. There are two main approaches: handling exceptions for each iteration individually, or handling exceptions for the entire loop. Exception Handling Per Iteration Place the try-except block inside the loop to handle exceptions for each iteration separately ? for i in range(5): try: if i % 2 == 0: raise ValueError(f"Error at iteration {i}") ...
Read MoreCan you please explain Python dictionary memory usage?
Python dictionaries use a sophisticated hash table structure that affects memory usage. Understanding how dictionaries store data helps optimize memory usage in your applications. Dictionary Internal Structure Each dictionary consists of multiple buckets, where each bucket contains ? The hash code of the stored object (unpredictable due to collision resolution) A pointer to the key object A pointer to the value object This structure requires at least 12 bytes per bucket on 32-bit systems and 24 bytes on 64-bit systems. Example: Basic Dictionary Memory import sys # Empty dictionary empty_dict ...
Read MoreHow to Pretty print Python dictionary from command line?
You can pretty print a Python dictionary using multiple approaches. The pprint module provides capability to "pretty-print" arbitrary Python data structures in a readable format, while the json module offers another elegant solution with customizable indentation. Using pprint Module The pprint module is specifically designed for pretty-printing Python data structures ? import pprint a = { 'bar': 22, 'foo': 45, 'nested': {'key1': 'value1', 'key2': 'value2'} } pprint.pprint(a, width=30) {'bar': 22, 'foo': 45, 'nested': {'key1': 'value1', ...
Read MoreC# Nested Classes
A nested class is a class declared inside another enclosing class. It is a member of its enclosing class, and nested classes can access private members of their enclosing class. However, the enclosing class cannot directly access private members of the nested class without creating an instance. Syntax Following is the syntax for declaring a nested class − public class OuterClass { // outer class members public class NestedClass { // nested class members } } ...
Read More