Manogna

Manogna

5 Articles Published

Articles by Manogna

5 articles

How can I remove the ANSI escape sequences from a string in python?

Manogna
Manogna
Updated on 13-Mar-2026 9K+ Views

You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re.sub(). The regex you can use for removing ANSI escape sequences is: (\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]. Example Here's how to create a function to remove ANSI escape sequences − import re def escape_ansi(line): ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]') return ansi_escape.sub('', line) # Test with a string containing ANSI escape sequences test_string = '\t\u001b[0;35mSomeText\u001b[0m\u001b[0;36m172.18.0.2\u001b[0m' result = escape_ansi(test_string) print(repr(result)) The output of the above ...

Read More

How to implement a custom Python Exception with custom message?

Manogna
Manogna
Updated on 13-Mar-2026 331 Views

In Python, you can create custom exceptions by inheriting from built-in exception classes. This allows you to define specific error types with meaningful messages for your application. Custom exceptions help make your code more readable and provide better error handling. Creating a Custom Exception Class To implement a custom Python exception with a custom message, you need to create a class that inherits from a built-in exception class like Exception, ValueError, or RuntimeError. The custom class should have an __init__ method to store the custom message. Example Here's how to create and use a custom exception ...

Read More

How can I write a try/except block that catches all Python exceptions?

Manogna
Manogna
Updated on 13-Mar-2026 257 Views

It is a general thumb rule that though you can catch all exceptions using code like below, you shouldn't − try: # do_something() pass except: print("Exception Caught!") However, this will also catch exceptions like KeyboardInterrupt and SystemExit that we may not be interested in handling. This can make it difficult to interrupt your program or cause other unexpected behaviors. Better Approach with Exception Re-raising A better approach is to catch all exceptions but re-raise them after logging or handling. Here's a complete ...

Read More

How do I find the location of Python module sources?

Manogna
Manogna
Updated on 30-Sep-2019 12K+ Views

For a pure python module you can find the location of the source files by looking at the module.__file__. For example,  >>> import mymodule >>> mymodule.__file__ C:/Users/Ayush/mymodule.py  Many built in modules, however, are written in C, and therefore module.__file__ points to a .so file (there is no module.__file__ on Windows), and therefore, you can't see the source. You can manually go and check the PYTHONPATH variable contents to find the directories from where these built in modules are being imported.  Running "python -v"from the command line tells you what is being imported and from where. This is useful if you want to ...

Read More

How do I unload (reload) a Python module?

Manogna
Manogna
Updated on 30-Sep-2019 764 Views

The function reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "importmoduleName" without exiting the script. It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again. For example, >>> import mymodule >>> # Edited mymoduleand want to reload it in this script >>> reload(mymodule)Note that the moduleName is the actual name of the module, not a string containing its name. The python docs state following about reload function: Python modules’ code is recompiled and the module-level code re-executed, defining ...

Read More
Showing 1–5 of 5 articles
« Prev 1 Next »
Advertisements