python-visualization / branca

This library is a spinoff from folium, that would host the non-map-specific features.
https://python-visualization.github.io/branca/
MIT License
110 stars 63 forks source link

Add `Text`: a string abstraction to replace vanilla string objects #170

Open achieveordie opened 2 months ago

achieveordie commented 2 months ago

Initial discussion present in: https://github.com/python-visualization/folium/issues/1907

The Text class will be used wherever plain strings are used (popup labels, tooltip text etc).

Currently, the only feature is to enable escaping characters (specifically: backticks, backslashes and double quotes). I chose these three for the time being since I saw them being used in some capacity in the codebase.

This PR is incomplete (need to add magic dunders, more examples, extension template to other operations, test cases etc) but I wanted to open a draft PR so we can begin our discussion without much ado.

achieveordie commented 2 months ago

FYI @hansthen

achieveordie commented 2 months ago

The implementation is not yet complete/correct but at this point, I'm only hoping to discuss the design decisions I've made and whether any of it doesn't fit well with the imagined use-case.

achieveordie commented 2 months ago

Here's a quick script (specifically made for backslashes) and the results:

from branca.text import Text

def scenario_1():
    string = r"<div>\\file\2019</div>"
    text = Text(text_str=string)

    print("Scenario_1: ", text)

def scenario_2():
    string = r"<div>\\file\2019</div>"
    text = Text(text_str=string, parse=False)

    print("Scenario_2: ", text)

def scenario_3():
    string = r"<div>\\\\file\\2019</div>"
    text = Text(text_str=string, parse=True)

    print("Scenario_3: ", text)

def scenario_4():
    string = r"<div>\\\\file\\2019</div>"
    text = Text(text_str=string, parse=False)

    print("Scenario_4: ", text) 

if __name__ == "__main__":
    scenario_1()
    print("==" * 25)
    scenario_2()
    print("==" * 25)
    scenario_3()
    print("==" * 25)
    scenario_4()
    print("==" * 25)

Output:

Scenario_1:  <div>\\\\file\\2019</div>
==================================================
C:\Users\sagar\Work\FLOSS\branca\branca\text.py:190: UserWarning: Detected unescaped characters, pass `parse=True` to enable escaping them automatically. 
  warnings.warn(
Scenario_2:  <div>\\file\2019</div>
==================================================
C:\Users\sagar\Work\FLOSS\branca\branca\text.py:204: UserWarning: The passed string: <div>\\\\file\\2019</div> was found to be already compliant in terms of escapting characters but `parse` was set to `True`. To avoid over-escaping the strings, pass `parse=False`. Ignore the warning if you are sure that the escaping string is still required.
  warnings.warn(
Scenario_3:  <div>\\\\\\\\file\\\\2019</div>
==================================================
Scenario_4:  <div>\\\\file\\2019</div>
==================================================