Found 10805 Articles for Python

Abstract Base Classes in Python (abc)

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

11K+ Views

A class is called an Abstract class if it contains one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and its abstract methods must be implemented by its subclasses.Abstract base classes provide a way to define interfaces when other techniques like hasattr() would be clumsy or subtly wrong (for example with magic methods). ABCs introduce virtual subclasses, which are classes that don’t inherit from a class but are still recognized by isinstance() and issubclass() functions. There are many built-in ABCs in Python. ABCs for Data ... Read More

Python import modules from Zip archives (zipimport)

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

2K+ Views

Use of 'zipimport' module makes it possible to import Python modules and packages from ZIP-format archives. This module also allows an item of sys.path to be a string naming a ZIP file archive. Any files may be present in the ZIP archive, but only files .py and .pyc are available for import. ZIP import of dynamic modules is disallowed.Functionality of this module is explained by first building a zip archive of files in 'newdir' directory. Following files are assumed to be present in newdir directory['guess.py', 'hello.py', 'impzip.py', 'mytest.py', 'prime.py', 'prog.py', 'tmp.py']import sys, glob import zipfile files = glob.glob("*.py") print (files) ... Read More

Python class browser support

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

186 Views

The pyclbr module in Python library extracts information about the functions, classes, and methods defined in a Python module. The information is extracted from the Python source code rather than by importing the module.This module defines readmodule() function that return a dictionary mapping module-level class names to class descriptors. The function takes a module name as parameter. It may be the name of a module within a package. In that case path is a sequence of directory paths prepended to sys.path, which is used to locate the module source code.Following code uses readmodule() function to parse classes and methods in ... Read More

Byte-compile Python libraries

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

682 Views

Python is an interpreter based language. However it internally compiles the source code to byte code when a script (.py extension) is run and afterwards the bytecode version is automatically removed. When a module (apart from the precompiled built-in modules) is first imported, its compiled version is also automatically built but saved with .pyc extension in __pycache__ folder. Subsequent calls to import same module again won't recompile the module instead uses the one already built.However, a Python script file with .py extension can be compiled expilicitly without running it. The 'py_compile' module contains 'compile()' function for that purpose. Name of ... Read More

Trace or track Python statement execution (trace)

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

4K+ Views

Function in the 'trace' module in Python library generates trace of program execution, and annotated statement coverage. It also has functions to list functions called during run by generating caller relationships.Following two Python scripts are used as an example to demonstrate features of trace module.#myfunctions.py import math def area(x): a = math.pi*math.pow(x, 2) return a def factorial(x): if x==1: return 1 else: return x*factorial(x-1)#mymain.py import myfunctions def main(): x = 5 print ('area=', myfunctions.area(x)) ... Read More

Python Low-level threading API

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

411 Views

The '_thread' module in Python library provides a low-level interface for working with light-weight processes having multiple threads sharing a global data space. For synchronization, simple locks (also called mutexes or binary semaphores) are defined in this module. The 'threading' built-in module provides a higher-level threading API built on top of this module.start_new_thread()This module-level function is used to open a new thread in the current process. The function takes a function object as an argument. This function gets invoked on successful creation of the new thread. The span of this function corresponds to the lifespan of the thread. The thread ... Read More

Python interface for SQLite databases

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

234 Views

SQLite is an open source database and is serverless that needs no configuration. Entire database is a single disk file that can be placed anywhere in operating system's file system. SQLite commands are similar to standard SQL. SQLite is extensively used by applications such as browsers for internal data storage. It is also convenient data storage for embedded devices.Standard Python library has in built support for SQLite database connectivity. It contains sqlite3 module which is a DB-API V2 compliant module written by Gerhad Haring. It adheres to DB-API 2.0.The DB-API has been defined in accordance with PEP-249 to ensure similarity ... Read More

Low-level networking interface in Python (socket)

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

823 Views

The 'socket' module in Python's standard library defines how server and client machines can communicate using socket endpoints on top of the operating system. The 'socket' API contains functions for both connection-oriented and connectionless network protocols.Socket is an end-point of a two-way communication link. It is characterized by IP address and the port number. Proper configuration of sockets on either ends is necessary so as to initiate a connection. This makes it possible to listen to incoming messages and then send the responses in a client-server environment.The socket() function in 'socket' module sets up a socket object.import socket obj = ... Read More

Python bootstrapping the pip installer

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

469 Views

In addition to the modules and packages built in to the standard distribution of Python, large number of packages from third party developers are uploaded to Python package repository called Python Package Index (https://pypi.org/. To install packages from here, pip utility is needed. The pip tool is an independent project, but since Python 3.4, it has been bootstrapped in Python distribution.The ensurepip module provides support for bootstrapping pip in existing installation of Python. Normally user doesn't need to use it explicitly. If however, installation of pip is skipped in normal installation or virtual environment, it may be needed.Following command will ... Read More

Data Classes in Python (dataclasses)

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

546 Views

The dataclasses is a new module added in Python's standard library since version 3.7. It defines @dataclass decorator that automatically generates constructor magic method __init__(), string representation method __repr__(), the __eq__() method which overloads == operator (and a few more) for a user defined class.The dataclass decorator has following signaturedataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)All the arguments take a Boolean value indicating whether a respective magic method or methods will be automatically generated or not.The 'init' argument is True by default. It will automatically generate __init__() method for the class.Let us define Student class using dataclass decorator as followsfrom dataclasses ... Read More

Advertisements