Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

achieveordie
Copy link
Contributor

Initial discussion present in: python-visualization/folium#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
Copy link
Contributor Author

FYI @hansthen

@achieveordie
Copy link
Contributor Author

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
Copy link
Contributor Author

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>
==================================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant