-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Systematically enforce valid field and class names
- Loading branch information
1 parent
f36fdb7
commit 70009d8
Showing
5 changed files
with
499 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import re | ||
from typing import Iterable | ||
# Characters that should be deleted from identifiers, after separators are replaced | ||
INVALID_CHARS = re.compile(r"[^a-zA-Z]+", flags=re.ASCII) | ||
|
||
def to_segments(name: str) -> Iterable[str]: | ||
""" | ||
Splits a string into segments, each of which is a valid Python identifier. | ||
""" | ||
# return (INVALID_CHARS.sub(segment, "") for segment in SEPARATOR_CHARS.split(name)) | ||
return (segment for segment in INVALID_CHARS.split(name) if segment) | ||
|
||
def clean_class_name(name: str) -> str: | ||
""" | ||
Cleans a string to be a valid Python class name. | ||
""" | ||
return "".join([segment.title() for segment in to_segments(name)]) | ||
|
||
def clean_field_name(name: str) -> str: | ||
""" | ||
Cleans a string to be a valid Python class name. | ||
""" | ||
return "_".join([segment.lower() for segment in to_segments(name)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from mondantic.formatting import clean_class_name, clean_field_name | ||
|
||
def test_class_name(): | ||
assert clean_class_name("board name") == "BoardName" | ||
assert clean_class_name("board#name") == "BoardName" | ||
assert clean_class_name("354;8wlfdrstdno") == "Wlfdrstdno" | ||
assert clean_class_name("board-name") == "BoardName" | ||
assert clean_class_name("board3453name") == "BoardName" | ||
|
||
def test_field_name(): | ||
assert clean_field_name("field name") == "field_name" | ||
assert clean_field_name("field#name") == "field_name" | ||
assert clean_field_name("354;8wlfdrstdno") == "wlfdrstdno" | ||
assert clean_field_name("field-name") == "field_name" | ||
assert clean_field_name("field3453name") == "field_name" |
File renamed without changes.
Oops, something went wrong.