First time here? Check out the FAQ!
1

How to Handle ANSI Escape Sequences in Stack Data display?

Hi everyone,

I'm currently using Wing Pro IDE for my Python development, and I've encountered an issue while debugging. When I inspect variables or print output in the Stack Data panel, I see a lot of ANSI escape sequences (e.g., ?[4m, ?[1m, ?[0;0m). These sequences are used for text formatting, like colors and styles, but they clutter the output and make it hard to read.

Is there a way to configure Wing Pro IDE to either interpret these sequences correctly or strip them out so that the output is clean and readable?

Here is an example of the output I'm seeing:

image description

The testing code is as follows:

# Now import pymatgen and other modules
from mp_api.client import MPRester
from pymatgen.io.vasp.sets import MPRelaxSet


# Use MPRester to fetch data
with MPRester() as mpr:
    # Search for materials with chemical system "Au" and space group number 225 (Fm-3m)
    #results = mpr.materials.search(formula="Au", spacegroup_number=225)
    results = mpr.materials.search(chemsys="Au", num_elements=(1, 1), spacegroup_number=225)
    #https://next-gen.materialsproject.org/materials/mp-568430?formula=BaRh2
    # results = mpr.materials.search(formula="BaRh2", spacegroup_number=227)

    # Print results
    for result in results:
        material_id = result.material_id
        print(f"Material ID: {material_id}, Formula: {result.formula_pretty}")

        # Get material structure
        structure = mpr.materials.get_structure_by_material_id(material_id)

        # Generate VASP input files using MPRelaxSet
        vasp_input_set = MPRelaxSet(structure)
        incar = vasp_input_set.incar

        # Print INCAR file content
        print(incar)

Any suggestions or guidance on how to handle this within Wing Pro would be greatly appreciated!

Thanks in advance for your help!

Best regards, Zhao

hongyi-zhao's avatar
547
hongyi-zhao
asked 2024-06-03 10:21:51 -0500, updated 2024-06-03 10:23:42 -0500
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

2 Answers

0

There's no way to configure Wing to change what it's displaying in Stack Data. The intention for that is to show what is there in the data. Anything else could be very confusing and counter-productive to debugging, for example if you're trying to track down a bug caused by the presence of those escape codes.

One idea would be to write a function to strip them, say "strip_junk", and call that either by adding strip_junk(value) to the Watch tool or more probably preferably by using the Debug Console and typing strip_junk(value).

Or if this is a place in your code where you're constantly running into this for some reason then you could add conditional code like:

if 'WINGDB_ACTIVE' in os.environ:
   other_value = strip_junk(value)

Then other_value would show up in Stack Data.

Wingware Support's avatar
4.1k
Wingware Support
answered 2024-06-03 11:51:25 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments
0

I tried with the following code, but failed to do the trick:

import os
import re
from typing import Any

def strip_ansi(s: str) -> str:
    """Strip ANSI escape sequences from a string."""
    return re.sub(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])', '', s)

def strip_junk(value: Any) -> Any:
    """Strip unwanted characters or sequences from a value."""
    if isinstance(value, str):
        # Strip ANSI escape sequences
        value = strip_ansi(value)
    # Optionally, you can add more conditions here to handle other types of values
    return value

# Now import pymatgen and other modules
from mp_api.client import MPRester
from pymatgen.io.vasp.sets import MPRelaxSet

# Use MPRester to fetch data
with MPRester() as mpr:
    # Search for materials with chemical system "Au" and space group number 225 (Fm-3m)
    results = mpr.materials.search(chemsys="Au", num_elements=(1, 1), spacegroup_number=225)

    # Process and print results
    for result in results:
        material_id = result.material_id
        formula_pretty = result.formula_pretty
        print(f"Material ID: {strip_junk(material_id)}, Formula: {strip_junk(formula_pretty)}")

        # Get material structure
        structure = mpr.materials.get_structure_by_material_id(material_id)

        # Generate VASP input files using MPRelaxSet
        vasp_input_set = MPRelaxSet(structure)
        incar = vasp_input_set.incar

        # Print INCAR file content
        if 'WINGDB_ACTIVE' in os.environ:
            other_value = strip_junk(incar)
            print(other_value)
        else:
            print(incar)
hongyi-zhao's avatar
547
hongyi-zhao
answered 2024-06-04 05:26:17 -0500
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer