Found 34488 Articles for Programming

NavigableMap lowerEntry() method in Java

Ankith Reddy
Updated on 29-Jun-2020 13:12:15

75 Views

The lowerEntry() method in NavigabelMap returns a key-value mapping associated with the greatest key strictly less than the given key.The following is an example to implement lowerEntry() methodExample Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       NavigableMap n = new TreeMap();       n.put(5, "Tom");       n.put(9, "John");       n.put(14, "Jamie");       n.put(1, "Tim");       n.put(4, "Jackie");       n.put(15, "Kurt");       n.put(19, "Tiger");       n.put(24, "Jacob");       System.out.println("NavigableMap elements..."+n);       System.out.println("Lower Entry is ... Read More

NavigableMap higherEntry() method in Java

George John
Updated on 29-Jun-2020 13:13:04

84 Views

The higherEntry() method in NavigableMap returns a key-value mapping associated with the least key strictly greater than the given key.The following is an example to implement higherEntry() method −Example Live Demoimport java.util.*; public class Demo {    public static void main(String[] args) {       NavigableMap n = new TreeMap();       n.put(5, "Tom");       n.put(9, "John");       n.put(14, "Jamie");       n.put(1, "Tim");       n.put(4, "Jackie");       n.put(15, "Kurt");       n.put(19, "Tiger");       n.put(24, "Jacob");       System.out.println("NavigableMap elements..."+n);       System.out.println("Higher Entry ... Read More

Finding modules used by a Python script (modulefinder)

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

1K+ Views

The ModuleFinder class in 'modulefinder' module can determine set of modules imported by a certain script. This module has a command line interface as well as programmatic interface.For demonstration of functionality, use following script#modfinder.py import hello try: import trianglebrowser import nomodule, mymodule except ImportError: passCommand line interfaceFollowing command displays list of modules located as well as not found.E:\python37>python -m modulefinder modfinder.pyOutputName File ---- ---- m __main__ modfinder.py m hello hello.py m math m trianglebrowser trianglebrowser.py Missing modules: ? mymodule imported from __main__ ? nomodule imported from __main__Programmatic interfaceModuleFinder class in ... Read More

Python Garbage Collector interface (gc)

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

154 Views

Automatic garbage collection is one of the important features of Python. Garbage collector mechanism attempts to reclaim memory occupied by objects that are no longer in use by the program.Python uses reference counting mechanism for garbage collection. Python interpreter keeps count of number of times an object is referenced by other objects. When references to an object are removed, the count for an object is decremented. When the reference count becomes zero, the object memory is reclaimed.Normally this mechanism is performed automatically. However, it can be done on purpose if a certain situation arises in the program. The 'gc' module ... Read More

Warning control in Python Programs

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

2K+ Views

Warning is different from error in a program. If error is encountered, Python program terminates instantly. Warning on the other hand is not fatal. It displays certain message but program continues. Warnings are issued to alert the user of certain conditions which aren't exactly exceptions. Typically warning appears if some deprecated usage of certain programming element like keyword/function/class etc. is found.Warning messages are displayed by warn() function defined in 'warning' module of Python's standard library. Warning is actually a subclass of Exception in built-in class hierarchy. There are a number of built-in Warning subclasses. User defined subclass can also be ... Read More

Python Binary Data Services

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

297 Views

Provisions of the struct module in the Python library are useful in performing conversions between C type structs and Python bytes objects. This can be achieved by module level functions as well as Struct class and its methods as defined in the struct module.The conversion functions use a format string. The byte order, size, and alignment used in the format string is determined by formatting character as per the following tableCharacterByte orderSizeAlignment@nativenativenative=nativestandardnonebig-endianstandardnone!network (= big-endian)standardnoneFollowing table shows format characters used to denote C type variables and corresponding Python types.FormatC TypePython typexpad byteno valueccharbytes of length 1b/Bsigned/unsigned charinteger?_Boolboolh/Hshort/unsigned shortintegeri/Iint/unsigned intintegerl/Llong/unsigned longintegerffloatfloatddoublefloatschar[]bytespchar[]bytesPvoid *integerFollowing ... Read More

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

200 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

722 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

Advertisements