Arjun Thakur

Arjun Thakur

749 Articles Published

Articles by Arjun Thakur

Page 2 of 75

The fcntl and ioctl System Calls in Python

Arjun Thakur
Arjun Thakur
Updated on 25-Mar-2026 3K+ Views

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 More

The most Common POSIX System Calls in Python

Arjun Thakur
Arjun Thakur
Updated on 25-Mar-2026 1K+ Views

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 More

Generate Secure Random Numbers for Managing Secrets using Python

Arjun Thakur
Arjun Thakur
Updated on 25-Mar-2026 505 Views

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 More

Python Container Datatypes

Arjun Thakur
Arjun Thakur
Updated on 25-Mar-2026 2K+ Views

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 More

Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.

Arjun Thakur
Arjun Thakur
Updated on 24-Mar-2026 523 Views

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 More

How to Find Hash of File using Python?

Arjun Thakur
Arjun Thakur
Updated on 24-Mar-2026 3K+ Views

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 More

How to handle exception inside a Python for loop?

Arjun Thakur
Arjun Thakur
Updated on 24-Mar-2026 3K+ Views

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 More

Can you please explain Python dictionary memory usage?

Arjun Thakur
Arjun Thakur
Updated on 24-Mar-2026 832 Views

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 More

How to Pretty print Python dictionary from command line?

Arjun Thakur
Arjun Thakur
Updated on 24-Mar-2026 1K+ Views

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 More

C# Nested Classes

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 2K+ Views

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
Showing 11–20 of 749 articles
« Prev 1 2 3 4 5 75 Next »
Advertisements