Found 10784 Articles for Python

Python object serialization (Pickle)

Krantik Chavan
Updated on 27-Jun-2020 15:00:01

849 Views

The term object serialization refers to process of converting state of an object into byte stream. Once created, this byte stream can further be stored in a file or transmitted via sockets etc. On the other hand reconstructing the object from the byte stream is called deserialization.Python’s terminology for serialization and deserialization is pickling and unpickling respectively. The pickle module available in Python’s standard library provides functions for serialization (dump() and dumps()) and deserialization (load() and loads()).The pickle module uses very Python specific data format. Hence, programs not written in Python may not be able to deserialize the encoded (pickled) ... Read More

Python Standard operators as functions

Rishi Rathor
Updated on 27-Jun-2020 15:01:34

292 Views

In programming, operator is generally a symbol (key) predefined to perform a certain operation such as addition, subtraction, comparison etc. Python has a large set of built-in operations divided in different categories such as arithmetic, comparison, bit-wise, membership etc.The operator module in python library consists of functions corresponding to built-in operators. Names of the functions are analogous to type of corresponding operator. For example, add() function in operator module corresponds to + operator.Python’s Object class has dunder (double underscore before and after name) methods corresponding to operator symbols. These dunder methods can be suitably overloaded in user defined classes to ... Read More

Internal Python object serialization (marshal)

Nancy Den
Updated on 27-Jun-2020 15:01:53

869 Views

Even though marshal module in Python’s standard library provides object serialization features (similar to pickle module), it is not really useful for general purpose data persistence or transmission of Python objects through sockets etc. This module is mostly used by Python itself to support read/write operations on compiled versions of Python modules (.pyc files). The data format used by the marshal module is not compatible across Python versions (not even subversions). That’s why a compiled Python script (.pyc file) of one version most probably won’t execute on another. The marshal module is thus used for Python’s internal object serialization.Just as ... Read More

Python Functions creating iterators for efficient looping

Daniol Thomas
Updated on 30-Jul-2019 22:30:24

191 Views

As in most programming languages Python provides while and for statements to form a looping construct. The for statement is especially useful to traverse the iterables like list, tuple or string. More efficient and fast iteration tools are defined in itertools module of Python’s standard library. These iterator building blocks are Pythonic implementations of similar tools in functional programming languages such as Haskell and SML.Functions in itertools module are of three types.Infinite iteratorsFinite iteratorsCombinatoric iteratorsFollowing functions generate infinite sequences.count() − This function returns an iterator of evenly spaced values from start value. The function can have optional step value to ... Read More

Decimal fixed point and floating point arithmetic in Python

Krantik Chavan
Updated on 30-Jul-2019 22:30:24

1K+ Views

Floating point numbers are represented in the memory as a base 2 binary fraction. As a result floating point arithmetic operations can be weird at times. Addition of 0.1 and 0.2 can give annoying result as follows −>>> 0.1 + 0.2 0.30000000000000004In fact this is the nature of binary floating point representation. This is prevalent in any programming language. Python provides a decimal module to perform fast and correctly rounded floating-point arithmetic.The decimal module is designed to represent floating points exactly as one would like them to behave, and arithmetic operation results are consistent with expectations. The precision level of ... Read More

Python Parser for command line options

Chandu yadav
Updated on 26-Jun-2020 06:27:19

844 Views

Very often we need to pass arguments to Python script when executing from command line. However, the script raises exception when parameters needed are not provided in equal number or type or order. That’s where the need to properly parse the command line argument occurs.The argparse module provides tools for writing very easy to use command line interfaces. It handles how to parse the arguments collected in sys.argv list, automatically generate help and issues error message when invalid options are given.First step to desing the command line interface is to set up parser object. This is done by ArgumentParser() function ... Read More

Support for line-oriented command interpreters in Python

Ankith Reddy
Updated on 26-Jun-2020 06:29:29

354 Views

The cmd module contains only one class called Cmd. This is used as base class for a user defined framework for line oriented command line interpreters.CmdAn object of this class or its subclass provides the line oriented interpreter framework. Important methods of this class inherited by the subclass are listed below.cmdloop()This method sends the object in a loop, accepts inputs and sends the same to appropriate command handler method in the class.As the loop starts an introductory message (give as parameter to cmdloop() method) will be displayed with a default (cmd) prompt which may be customized by prompt attribute.The interpreter ... Read More

Python getpass Module

Arjun Thakur
Updated on 26-Jun-2020 06:31:13

1K+ Views

There are two functions defined in getpass module of Python’s standard library. They are useful whenever a terminal based application needs to be executed only after validating user credentials.getpass()This function prompts the user to enter a password. By default the keys entered by user in the terminal are not echoed. Also the default prompt that appears on terminal is ‘password’ which can be customized by providing a string as parameter.In following example, Python prompt is invoked from Command prompt terminal on Windows. The password entered is not echoed in the terminal.C:\python36>python Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 ... Read More

Work with ZIP archives in Python (zipfile)

George John
Updated on 25-Jun-2020 14:25:59

797 Views

The ZIP is one of the most popular file formats used for archiving and compression. It has been in use since the days of MSDOS and PC and has been used by famous PKZIP application.The zipfile module in Python’s standard library provides classes that facilitate the tools for creating, extracting, reading and writing to ZIP archives.ZipFile()This function returns a ZipFile object from a file parameter which can be a string or file object as created by built-in open() function. The function needs a mode parameter whose default value is ‘r’ although it can take ‘w’ or ‘a’ value for opening ... Read More

High-level file operations in Python (shutil)

Chandu yadav
Updated on 25-Jun-2020 14:26:52

974 Views

A number of functions for hgh level operations on files and directories have been defined in shutil module of Python’s standard library.copy()This function copies a file to a specified file in same or other directory. First parameter to the function is a string representation of existing file. Second argument is either name of resultant file or directory. If it is a directory, the file is coped in it with same name. The metadata of original file is not maintained.>>> import shutil >>> shutil.copy("hello.py", "newdir/") 'newdir/hello.py'copy2()This function is similar to copy() function except for the fact that it retains metadata of ... Read More

Advertisements