Rajendra Dharmkar has Published 452 Articles

How do you properly ignore Exceptions in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Sep-2019 07:10:07

486 Views

This can be done by following codestry: x, y =7, 0 z = x/y except: passORtry: x, y =7, 0 z = x/y except Exception: passThese codes bypass the exception in the try statement and ignore the except clause and don’t raise any exception.The difference in the above codes is ... Read More

How to declare custom exceptions in modern Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 26-Sep-2019 20:13:52

148 Views

To override something or pass extra arguments to the exception we do like this in modern python:class ValidationError(Exception): def __init__(self, message, errors): super(ValidationError, self).__init__(message) self.errors = errorsThat way we could pass dictionary of error messages to the second parameter, and get to it later on when needed.Read More

How will you explain that an exception is an object in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 26-Sep-2019 20:04:17

141 Views

Yes in given code ‘err’ is an exception object.In python everything is an object. And every object has attributes and methods. So exceptions much like lists, functions, tuples etc are also objects. So exceptions too have attributes like other objects. These attributes can be set and accessed as follows. There ... Read More

How to get Python exception text?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 26-Sep-2019 20:03:49

890 Views

If a python code throws an exception, we can catch it and print the type, the error message, traceback and get information like file name and line number in python script where the exception occurred.We can find the type, value, traceback parameters of the errorType gives the type of exception ... Read More

Which one is the Python module to obfuscate javascript?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Sep-2019 07:26:07

715 Views

You can use the jsmin module to minimize/obfuscate javascript code using Python. Install jsmin using:$ pip install jsminTo use jsmin in your python project to minimize a js file, say hello.js, you can use it as follows:>>> from jsmin import jsmin >>> with open('hello.js') as js_file: ...     minified ... Read More

How to setup cookies in Python CGI Programming?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 30-Aug-2019 06:06:48

556 Views

Setting up CookiesIt is very easy to send cookies to browser. These cookies are sent along with HTTP Header before to Content-type field. Assuming you want to set UserID and Password as cookies. Setting the cookies is done as follows −#!/usr/bin/python print "Set-Cookie:UserID = XYZ;\r" print "Set-Cookie:Password = XYZ123;\r" print ... Read More

How many Python classes should I put in one file?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 30-Jul-2019 22:30:21

2K+ Views

Python code is organized in files called "modules" and groups of related modules called “packages".A module is a distinct unit that may have one or more closely-related classes. Modules need to be imported before they are read, used, maintained and extended if needed. So a module is a unit or ... Read More

Does Python have “private” variables in classes?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 30-Jul-2019 22:30:21

2K+ Views

There are no 'private variables' in Python classes as such. All variables and methods in Python are public by default. There is sometimes an emulation of private variables by using the double underscore __ prefix to the variable's names. This makes these variables invisible or not easily visible outside of ... Read More

Is there any tool that can convert an XSD file to a Python class as JAXB does for Java?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 30-Jul-2019 22:30:21

776 Views

I would recommend generateDS for converting a XSD file to a Python class . In my opinion, it is a good tool for the said purpose.It (generatS) generates the Python class with all methods (setters and getters, export to XML, import from XML). It does a good job and works ... Read More

When are python classes and class attributes garbage collected?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 30-Jul-2019 22:30:21

304 Views

A class attribute exists until the last reference goes away. A global variable also exists until the last reference goes away. Neither of these are guaranteed to last the entire duration of the program.Also, a class defined at module scope is a global variable. So the class (and, by implication, ... Read More

Advertisements