Found 10784 Articles for Python

The most Common POSIX System Calls in Python

Arjun Thakur
Updated on 25-Jun-2020 14:04:38

666 Views

The posix module is works on the UNIX environment. It provides the Operating system functionality.We should not import this module directly. We can use the os module. The os module is acts as a superset of the posix module on UNIX. On non-Unix system the posix is not available, but the os is available with some less functionality.To use the posix module, we should import it using.import posixThere are different methods and constants in the POSIX module.Constant posix.environThe environ is a dictionary object. It holds keys and values. The keys and values are of bytes type for UNIX. For example, ... Read More

Convenient Web-browser controller in Python

Chandu yadav
Updated on 30-Jul-2019 22:30:23

792 Views

To display web based documents to users by using python, there is a module called webbrowser. It provides high level interface to handle web documents. On UNIX based system, this module supports lynx, Netscape, Mosaic etc browsers. For Windows and Macintosh, it uses the standard browsers. To use this module, we need to import the following module. import webbrowser The webbrowser module has different methods, and exceptions, these are as follows − Exception webbrowser.Error This error will raise when there is an error in the webbrowser interface. Method webbrowser.open(url, new=0, autoraise=True) This method is used to display the ... Read More

Generate Secure Random Numbers for Managing Secrets using Python

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

292 Views

To generate secure random numbers cryptographically we can use the secrets module in python. This module is helpful to create secure password, account authentication, security tokens or some related secrets. To use the classes and modules of the secrets module, we should import that module into our code. import secrets Random Numbers The secrets module is used to access some secure source of randomness. That is provided by the operating systems. Classes and functions, related to random numbers of the secrets modules are − Class secrets.SystemRandom It is a class to generate random numbers, by using some highest ... Read More

Secure Hashes and Message Digest in Python

Chandu yadav
Updated on 30-Jul-2019 22:30:23

1K+ Views

For the secure hash and message digest process, we should use the hashlib module. This module implements a common interface for different secure hash algorithm like SHA1, SHA224, SHA256, SHA512 etc. Also the RSA’s MD5 algorithm. Older algorithms are known as the Message Digest and the new methods are called Secure Hash. To use this module, we need to import the hashlib module in the python code. import hashlib In this method, there are some predefined algorithms like md5, sha1, sha224, sha256, sha512 are present. We can add additional algorithms from the OpenSSL library. Some methods, constants of ... Read More

Python Alternate repr() implementation

George John
Updated on 30-Jul-2019 22:30:23

312 Views

In Python, if we want to limit the large amount of data from displaying, we can use the reprlib module. To use this module, we should import it using. import reprlib There are different classes and methods related to reprlib. These are − Class reprlib.Repr The Repr class provides formatting services. It creates functions like built-in repr(). In this class we can add size limits and different object types. Method reprlib.repr(object) This method is used to return string like built-in repr() method, But in this case there is a limits on most sizes. Repr Objects The Repr object ... Read More

Python Heap Queue Algorithm

Chandu yadav
Updated on 30-Jul-2019 22:30:23

268 Views

The heap data structures can be used to represents a priority queue. In python it is available into the heapq module. Here it creates a min-heap. So when the priority is 1, it represents the highest priority. When new elements are inserted, the heap structure updates. To use this module, we should import it using − import heapq There are some heap related operations. These are − Method heapq.heapify(iterable) It is used to convert an iterable dataset to heap data structure. Method heapq.heappush(heap, element) This method is used to insert the element into the heap. After ... Read More

Python Container Datatypes

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

2K+ Views

In the collections there are some container datatypes, which are alternatives to python’s general purpose built-in containers like dict, list, set etc.Some of the containers are −Sr.No.Container & Description1namedtuple()Used to create tuple subclass with the name field2dequeQueue using list type data3CounterSubclass of dict to count the hash-table objects4ChainMapUsed to create single view of multiple mappings5OrderedDictSubclass of dict, where data are added in ordered manner6UserListWrapper for list to easier access.To use this module, we should import it using −import collectionsDeque ObjectThe Deque is basically a generalization of stack and queue structure, where it is initialized from left to right. It uses ... Read More

Python Basic date and time types

George John
Updated on 30-Jul-2019 22:30:23

6K+ Views

To manipulate dates and times in the python there is a module called datetime. There are two types of date and time objects. The types are naïve and the aware. In the naïve object, there is no enough information to unambiguously locate this object from other date-time objects. In this approach it uses Coordinate Universal Time (UTC). In the aware type objects there are different information regarding algorithmic and political time adjustments. This type of objects is used to represent some specific time moments. To use this module, we should import it using − import datetime There are ... Read More

Python Completion function for GNU readline

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

269 Views

Unix readline module has tab completion mechanism. To get these features, we have to use rlcompleter module. It can be used in python’s interactive mode. To use this module, we should import it using − import rlcompleter There is a class called Completer class − Method Completer.complete(text, state) This method is used to return the tab completion output. If there is a ‘.’ in the text, then it will try to get all related members of that command. When there is no dot ‘.’ it will just complete the text. Example Code import rlcompleter import sys ... Read More

Python Helpers for Computing Deltas

AmitDiwan
Updated on 11-Aug-2022 11:02:59

177 Views

The difflib module is used in Python to compute deltas. It is used to compare files, and can produce information about file differences in various formats, including HTML and context and unified diffs. We need to first import the difflib module before using it − import difflib Class (difflib.SequenceMatcher) This class is used to compare two sequences of any type. It has different methods. Some of the methods − set_seqs(a, b) − Set the sequence files which will be compared. It computes and caches detailed information about the second file. So for matching multiple files, we should set ... Read More

Advertisements