Rajendra Dharmkar has Published 452 Articles

How would you convert string to bytes in Python 3?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 30-Sep-2019 07:19:27

181 Views

To convert string to bytes in Python 3, you can use the encode() function from the string class. For example,>>> s = u"HellΘ WΘrld" >>> s.encode('utf-8') 'Hell\xce\x98 W\xce\x98rld'

How to parse a string to float or int in python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 30-Sep-2019 06:50:30

176 Views

To parse a string to int, you can use the following:try:     print int('112') except ValueError:     print 'Cannot parse'This will give you the output:112To parse a string to float, you can use the following:try:     print float('112.15') except ValueError:     print 'Cannot parse'This will give ... Read More

How to handle python exception inside if statement?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Sep-2019 12:02:36

2K+ Views

The code can be written as follows to catch the exceptiona, b=5, 0 try:    if b != 0:        print a/b    else:        a/b        raise ZeroDivisionError except Exception as e:        print eWe get the following outputC:/Users/TutorialsPoint1/~.py integer division or modulo by zero

How to catch OSError Exception in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Sep-2019 11:36:21

634 Views

OSError serves as the error class for the os module, and is raised when an error comes back from an os-specific function.We can re-write the given code as follows to handle the exception and know its type.#foobar.py import os import sys try: for i in range(5): print i, os.ttyname(i) except ... Read More

How to handle Python exception in Threads?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Sep-2019 11:35:03

1K+ Views

The given code is rewritten to catch the exceptionimport sys import threading import time import Queue def thread(args1, stop_event, queue_obj): print "start thread" stop_event.wait(12) if not stop_event.is_set(): try: raise Exception("boom!") except Exception: queue_obj.put(sys.exc_info()) pass try: queue_obj = Queue.Queue() t_stop = threading.Event() t = threading.Thread(target=thread, args=(1, t_stop, queue_obj)) t.start() time.sleep(15) print ... Read More

How to rethrow Python exception with new type?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Sep-2019 11:33:50

153 Views

In Python 3.x, the code is subject to exception chaining and we get the output as followsC:/Users/TutorialsPoint1/~.py Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line 2, in 1/0 ZeroDivisionError: division by zeroThe above exception was the direct cause of the following exception:Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line ... Read More

How to write custom Python Exceptions with Error Codes and Error Messages?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Sep-2019 11:23:45

811 Views

We can write custom exception classes with error codes and error messages as follows:class ErrorCode(Exception):     def __init__(self, code):         self.code = code     try:     raise ErrorCode(401) except ErrorCode as e:     print "Received error with code:", e.codeWe get outputC:/Users/TutorialsPoint1/~.py Received error ... Read More

Is there a standard way of using exception chains in Python 3?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 27-Sep-2019 10:47:31

222 Views

During the handling of one exception ‘A’, it is possible that another exception ‘B’ may occur. In Python 2.0 versions, if this happens, exception B is propagated outward and exception A is lost. It is useful to know about both exceptions in order to debug the problem.Sometimes it is useful ... Read More

How to raise Python exception from a C extension?

Rajendra Dharmkar

Rajendra Dharmkar

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

131 Views

For the above module, we need to prepare following setup.py script −from distutils.core import setup, Extension setup(name='helloworld', version='1.0', \ ext_modules=[Extension('helloworld', ['hello.c'])])Now, we use the following command, $ python setup.py installOnce we install the extension, we would be able to import and call that extension in our Python script test.py and ... Read More

How we can call Python function from MATLAB?

Rajendra Dharmkar

Rajendra Dharmkar

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

271 Views

The Python libraries are now available in MATLAB (since 2014b). We can run code directly in MATLAB if we are using version 2014b or later.This makes it possible to use python modules in MATLAB. Without any other changes, just prefix ‘py’ before the python library name you want to use. ... Read More

Advertisements