Found 10476 Articles for Python

Interpreter base classes in Python

Rishi Raj
Updated on 30-Jul-2019 22:30:26

512 Views

Python's interactive mode works on the principle of REPL (Read - Evaluate - Print - Loop). The code module in Python's standard library provides classes nd convenience functions to set up REPL environment from within Python script.Following two classes are defined in code module:InteractiveInterpreter: This class deals with parsing and interpreter state (the user’s namespace)InteractiveConsole: Closely emulate the behavior of the interactive Python interpreter.Two convenience functions in the module are:interact(): Convenience function to run a read-eval-print loop.compile_command(): This function is useful for programs that want to emulate Python’s interpreter main loop (the REPL).Interactive Interpreter methodsrunsource(): Compile and run some source ... Read More

Package extension utility in Python

Vikyath Ram
Updated on 30-Jul-2019 22:30:26

743 Views

When you want to add to the module search path for a specific package and work with resources included in a package, you need to use pkgutil module from Python library. It includes functions for changing the import rules for Python packages. It is also possible to load non-code resources from files distributed within a package.extend_path(path, name)Extend the search path for the modules which comprise a package. Intended use is to place the following code in a package’s __init__.pyimport pkgutil __path__ = pkgutil.extend_path(__path__, __name__)extend_path() scans sys.path for directories that include a subdirectory named for the package given as the second ... Read More

POP3 protocol client in Python

Rishi Raj
Updated on 30-Jul-2019 22:30:26

684 Views

The poolib module from Python's standard library defines POP3 and POP3_SSL classes. POP3 class encapsulates a connection to a POP3 server and implements the protocol as defined in RFC 1939. POP3_SSL classsupports POP3 servers that use SSL as an underlying protocol layer.POP3 protocolis obsolescent as its implementation quality of POP3 servers is quite poor. If your mailserver supports IMAP, it is recommended to use the imaplib.IMAP4 class.Both classes have following methods defined −getwelcome()Returns the greeting string sent by the POP3 server.user(username)Send user command, response should indicate that a password is required.pass_(password)Send password.Stat()Get mailbox status. The result contains 2 integers: (message ... Read More

FTP protocol client in Python

Vikyath Ram
Updated on 30-Jul-2019 22:30:26

1K+ Views

The all important The FTP class in ftplib module implements the client side of the FTP protocol.To establish connection with a FTP server, obtain FTP object.con=FTP(hostname)The FTP class supports following methods −connect()Connect to the given host and port. The default port number is 21, as specified by the FTP protocol specification.Getwelcome()Return the welcome message sent by the server in reply to the initial connection.login(user='anonymous', passwd='', acct='')Log in as the given user. The passwd and acct parameters are optional and default to the empty string. If no user is specified, it defaults to 'anonymous'. If user is 'anonymous', the default passwd ... Read More

zipapp - Manage executable Python zip archives

Arushi
Updated on 30-Jul-2019 22:30:26

680 Views

The zipapp module has been introduced in Python's standard library since ver 3.5. This module is used to manage the creation of zip files containing Python code, which can be executed directly by the Python interpreter. The module provides both a Command-Line Interface and a programming interface.To use zipapp module programmatically, we should have a module in which main function is present. The executable archive is built by following command −python -m zipapp myapp -m "example:main"Here, the current path should have a folder called myapp. In this folder, there should be example.py which must have main() function.Create myapp folder and ... Read More

Access to the underlying platform’s identifying data in Python

Arushi
Updated on 30-Jul-2019 22:30:26

256 Views

Functions in the platform module help us probe the underlying platform’s hardware, operating system, and interpreter version information.architecture()This function queries the given executable (defaults to the Python interpreter executable) for various architecture information.>>> import platform >>> platform.architecture() ('64bit', '')machine()This function returns the machine type, e.g. 'i386'. An empty string is returned if the value cannot be determined.>>> platform.machine() 'x86_64'node()This function returns the computer’s network name.>>> platform.node() 'malhar-ubuntu'platform(aliased=0, terse=0)This function returns a single string identifying the underlying platform.>>> platform.platform() 'Linux-4.13.0-46-generic-x86_64-with-debian-stretch-sid'processor()This function returns the (real) processor name.>>> platform.processor() 'x86_64'python_build()This function returns a tuple (buildno, builddate)>>> platform.python_build() ('default', 'Oct 13 2017 12:02:49')python_compiler()This function ... Read More

C-style parser for command line options in Python

Rishi Raj
Updated on 30-Jul-2019 22:30:26

349 Views

Python’s sys module provides access to any command-line arguments via the sys.argv. sys.argv is the list of command-line arguments and sys.argv[0] is the program ie. the script name.Save following code as args.pyimport sys print ('argument list', sys.argv)Execute above script from command line as follows:C:\python37>python args.py 11 22 argument list ['args.py', '11', '22']The getopt module has funcions toparse the command line arguments in sys.argv. It supports the same conventions as the Unix getopt() function (including the special meanings of arguments of the form ‘-‘ and ‘--‘).API is designed to be familiar to users of the C getopt() function.getopt(args, shortopts, longopts=[])Parses command ... Read More

Working with Images in Python?

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

3K+ Views

One of the most popular and considered as default library of python for image processing is Pillow. Pillow is an updated version of the Python Image Library or PIL and supports a range of simple and advanced image manipulation functionality. It is also the basis for simple image support in other Python libraries such as sciPy and Matplotlib.Installing PillowBefore we start, we need python and pillow. Incase of Linux, pillow will probably be there already, since major flavour of linux including Fedora, Debian/Ubuntu and ArchLinux includes Pillow in packages that previously contained PIL.The easiest way to install it is to ... Read More

Short Circuiting Techniques in Python?

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

2K+ Views

A common mistake for people new to programming is a misunderstanding of the way that boolean operators works, which stems from the way the python interpreter reads these expressions. For example, after initially learning about "and " and "or" statements, one might assume that the expression X = (‘x’ or ‘y’) would check to see if the variable X was equivalent to one of the strings ‘a’ or ‘b’. This is not so. To understand, what i’m trying to say, start an interactive session with the interpreter and enter the following expressions:>>> 'x' == ('x' or 'y') True >>> 'y' ... Read More

NZEC error in Python?

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

898 Views

NZEC is non-zero exit code.Exit codes are codes (number) return by running program to operating system upon either their successfully termination (Exit code 0) or failed termination due to error (Non zero exit code).As python or Java programming language supports exception handling, we can use exception handling using try-catch blocks to catch this error.NZEC error is a runtime error and occurs mostly when negative array index is accesed or the program which we have written is utilizing more memory space than the allocated memory for our program to run.In python Exception class is the super class of all the errors ... Read More

Advertisements