Found 10805 Articles for Python

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

988 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

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

238 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

411 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

289 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

1K+ 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

143 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

Advertisements