Found 10784 Articles for Python

Byte-compile Python libraries

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

721 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

468 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

261 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

892 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

505 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

573 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

The ElementTree XML API in Python

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

10K+ Views

The Extensible Markup Language (XML) is a markup language much like HTML. It is a portable and it is useful for handling small to medium amounts of data without using any SQL database.Python's standard library contains xml package. This package has ElementTree module. This is a simple and lightweight XML processor API.XML is a tree like hierarchical data format. The 'ElementTree' in this module treats the whole XML document as a tree. the 'Element' class represents a single node in this tree. Reading and writing operations on XML files are done on the ElementTree level. Interactions with a single XML ... Read More

SMTP protocol client in Python (smtplib)

George John
Updated on 29-Jun-2020 13:05:39

1K+ Views

Python's standard library has 'smtplib' module which defines an SMTP client session object that can be used to send mail via Python program.A mail server is an application that handles and delivers e-mail over the internet. Outgoing mail servers implement SMTP, or Simple MailTransfer Protocol, servers which are an Internet standard for email transmission.Incoming mail servers come in two main varieties. POP3, or Post office protocol and IMAP, or Internet Message Access Protocol.smptlib.SMTP()functionThis function returns an object of SMTP class. It encapsulates and manages a connection to an SMTP or ESMTP server. Following arguments are defined in the signature of ... Read More

Why is Python slower than other languages?

Dev Kumar
Updated on 26-Jun-2020 12:15:53

1K+ Views

Python is a scripting language while C is a programming language. C/C++ is relatively fast as compared to Python because when you run the Python script, its interpreter will interpret the script line by line and generate output but in C, the compiler will first compile it and generate an output which is optimized with respect to the hardware. In case of other languages such as Java and.NET, Java bytecode, and .NET bytecode respectively run faster than Python because a JIT compiler compiles bytecode to native code at runtime. CPython cannot have a JIT compiler because the dynamic nature of ... Read More

Advertisements