Found 10784 Articles for Python

Disassembler for Python bytecode

Anvi Jain
Updated on 27-Jun-2020 14:24:11

2K+ Views

The dis module in Python standard library provides various functions useful for analysis of Python bytecode by disassembling it into a human-readable form. This helps to perform optimizations. Bytecode is a version-specific implementation detail of the interpreter.dis() functionThe function dis() generates disassembled representation of any Python code source i.e. module, class, method, function, or code object.>>> def hello(): print ("hello world") >>> import dis >>> dis.dis(hello) 2    0 LOAD_GLOBAL 0 (print)      3 LOAD_CONST 1 ('hello world')      6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)      9 POP_TOP      10 LOAD_CONST 0 (None)      13 RETURN_VALUEBytecode ... Read More

Accessing The Unix/Linux password database (pwd)

Vrundesha Joshi
Updated on 27-Jun-2020 14:24:42

264 Views

The pwd module in standard library of Python provides access to the password database of user accounts in a Unix/Linux operating system. Entries in this Password database are atored as a tuple-like object. The structure of tuple is according to following passwd structure pwd.h file in CPython APIIndexAttributeMeaning0pw_nameLogin name1pw_passwdOptional encrypted password2pw_uidNumerical user ID3pw_gidNumerical group ID4pw_gecosUser name or comment field5pw_dirUser home directory6pw_shellUser command interpreterThe pwd module defines the following functions −>>> import pwd >>> dir(pwd) ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'getpwall', 'getpwnam', 'getpwuid', 'struct_passwd']getpwnam() − This function returns record in the password database corresponding to the specified user name>>> pwd.getpwnam('root') pwd.struct_passwd(pw_name ... Read More

Built-in objects in Python (builtins)

Anvi Jain
Updated on 27-Jun-2020 14:28:23

2K+ Views

The builtins module is automatically loaded every time Python interpreter starts, either as a top level execution environment or as interactive session. The Object class, which happens to be the base class for all Python objects, is defined in this module. All built-in data type classes such as numbers, string, list etc are defined in this module. The BaseException class, as well as all built-in exceptions, are also defined in it. Further, all built-in functions are also defined in the built-ins module.Since this module is imported in the current session automatically, normally it is not imported explicitly. All the built-in ... Read More

Top-level script environment in Python (__main__)

Nitya Raut
Updated on 27-Jun-2020 14:28:42

457 Views

A module object is characterized by various attributes. Attribute names are prefixed and post-fixed by double underscore __. The most important attribute of module is __name__. When Python is running as a top level executable code, i.e. when read from standard input, a script, or from an interactive prompt the __name__ attribute is set to '__main__'.>>> __name__ '__main__'From within a script also, we find a value of __name__ attribute is set to '__main__'. Execute the following script.'module docstring' print ('name of module:', __name__)Outputname of module: __main__However, for an imported module this attribute is set to name of the Python script. ... Read More

Python Utilities for with-statement contexts (contextlib)

Daniol Thomas
Updated on 27-Jun-2020 14:29:17

310 Views

contextlib module of Python's standard library defines ContextManager class whose object properly manages the resources within a program. Python has with keyword that works with context managers. The file object (which is returned by the built-in open() function) supports ContextManager API. Hence we often find with keyword used while working with files.Following code block opens a file and writes some data in it. After the operation is over, the file is closed, failing which file descriptor is likely to leak leading to file corruption.f = open("file.txt", "w") f.write("hello world") f.close()However same file operation is done using file's context manager capability ... Read More

The Python Debugger (pdb)

Jennifer Nicholas
Updated on 27-Jun-2020 14:38:10

2K+ Views

In software development jargon, 'debugging' term is popularly used to process of locating and rectifying errors in a program. Python's standard library contains pdb module which is a set of utilities for debugging of Python programs.The debugging functionality is defined in a Pdb class. The module internally makes used of bdb and cmd modules.The pdb module has a very convenient command line interface. It is imported at the time of execution of Python script by using –m switchpython –m pdb script.pyIn order to find more about how the debugger works, let us first write a Python module (fact.py) as follows ... Read More

Measure execution time of small Python code snippets (timeit)

Nancy Den
Updated on 27-Jun-2020 14:38:42

169 Views

The Timer class and other convenience functions in timeit module of Python's standard library are designed to provide a mechanism to measure time taken by small bits of Python code to execute. The module has a command line interface and the functions can be called from within program as well.Easiest way to measure time of execution is by using following convenience functiontimeit()This function returns object of Timer class. It mainly requires two parameters.stmt − a string containing valid Python statement whose execution time is to be measured.setup − a string containing Python statement which will be executed once, primarily to ... Read More

Python Program Exit handlers (atexit)

Daniol Thomas
Updated on 27-Jun-2020 14:38:59

523 Views

The atexit module in the standard distribution of Python has two functions – register() and unregister(). Both functions take some existing function as an argument. Registered functions are executed automatically when the interpreter session is terminated normally.If more than one functions are registered, their execution is in the reverse order of registration. It means, functions f1(), f2() and f3() are registered one after other, their order of execution will be f3(), f2() and f1().The unregister() function removes the specified function from list of functions to be automatically invoked.Following code shows how a function is registered for automatic execution upon termination ... Read More

Generate pseudo-random numbers in Python

Nancy Den
Updated on 27-Jun-2020 14:59:02

3K+ Views

Many computer applications need random number to be generated. However, none of them generate a truly random number. Python, like any other programming technique, uses a pseudo-random generator. Python’s random generation is based upon Mersenne Twister algorithm that produces 53-bit precision floats. The technique is fast and thread-safe but not suitable from cryptographic purpose.Python’s standard library contains random module which defines various functions for handling randomization.random.seed() − This function initializes the random number generator. When random module is imported, the generator is initialized with the help of system time. To reseed the generator, use any int, str, byte or bytearray ... Read More

pprint module (Data pretty printer)

Daniol Thomas
Updated on 27-Jun-2020 14:59:38

1K+ Views

The pprint module (lib/pprint.py) is a part of Python’s standard library which is distributed along with standard Python distribution. The name pprint stands for pretty printer. The pprint module’s functionality enables aesthetically good looking appearance of Python data structures. Any data structure that can be correctly parsed by Python interpreter is elegantly formatted. The formatted expression is kept in one line as far as possible, but breaks into multiple lines if the length exceeds the width parameter of formatting. One unique feature of pprint output is that the dictionaries are automatically sorted before the display representation is formatted.The pprint module ... Read More

Advertisements