Skip to content

Commit d1db116

Browse files
committed
Format code
1 parent 74b1ed2 commit d1db116

4 files changed

Lines changed: 68 additions & 61 deletions

File tree

casefy/__init__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
__version__ = "1.0.0"
22

33
from .casefy import (
4+
alphanumcase,
45
camelcase,
5-
pascalcase,
6-
snakecase,
6+
capitalcase,
77
constcase,
88
kebabcase,
9-
upperkebabcase,
10-
separatorcase,
9+
lowercase,
10+
pascalcase,
1111
sentencecase,
12+
separatorcase,
13+
snakecase,
1214
titlecase,
13-
alphanumcase,
14-
lowercase,
1515
uppercase,
16-
capitalcase
16+
upperkebabcase,
1717
)
1818

1919
# Don't expose the submodule itself
@@ -32,5 +32,5 @@
3232
"alphanumcase",
3333
"lowercase",
3434
"uppercase",
35-
"capitalcase"
35+
"capitalcase",
3636
]

casefy/casefy.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ def camelcase(string: str) -> str:
1717
if not string:
1818
return ""
1919
# Turn into snake_case, then remove "_" and capitalize first letter
20-
string = "".join(f"{s[0].upper()}{s[1:].lower()}"
21-
for s in re.split(r"_", snakecase(string)) if s)
20+
string = "".join(
21+
f"{s[0].upper()}{s[1:].lower()}" for s in re.split(r"_", snakecase(string)) if s
22+
)
2223
# Make first letter lower
2324
return f"{string[0].lower()}{string[1:]}" if string else ""
2425

@@ -73,17 +74,9 @@ def snakecase(string: str, keep_together: List[str] = None) -> str:
7374
# Manage separators
7475
string = re.sub(r"[\W]", "_", string)
7576
# Manage capital letters and numbers
76-
string = re.sub(
77-
fr"{keep_pattern}([A-Z]|\d+)",
78-
fr"_{capturing_groups}",
79-
string
80-
)
77+
string = re.sub(rf"{keep_pattern}([A-Z]|\d+)", rf"_{capturing_groups}", string)
8178
# Add "_" after numbers
82-
string = re.sub(
83-
fr"{keep_pattern}(\d+)",
84-
fr"{capturing_groups}_",
85-
string
86-
).lower()
79+
string = re.sub(rf"{keep_pattern}(\d+)", rf"{capturing_groups}_", string).lower()
8780
# Remove repeated "_"
8881
string = re.sub(r"[_]{2,}", "_", string)
8982
string = re.sub(r"^_", "", string) if not leading_underscore else string
@@ -136,11 +129,7 @@ def upperkebabcase(string: str) -> str:
136129
return uppercase(kebabcase(string))
137130

138131

139-
def separatorcase(
140-
string: str,
141-
separator: str,
142-
keep_together: List[str] = None
143-
) -> str:
132+
def separatorcase(string: str, separator: str, keep_together: List[str] = None) -> str:
144133
"""Convert a string into a case with an arbitrary separator.
145134
146135
Args:
@@ -157,14 +146,24 @@ def separatorcase(
157146
if not string:
158147
return ""
159148
string_conv = snakecase(string, keep_together).replace("_", separator)
160-
before = ("" if (string[0].isalnum()
161-
or string[:len(separator)] == separator
162-
or string_conv[:len(separator):] == separator)
163-
else separator)
164-
after = ("" if (string[-1].isalnum()
165-
or string[:-len(separator)] == separator
166-
or string_conv[:-len(separator)] == separator)
167-
else separator)
149+
before = (
150+
""
151+
if (
152+
string[0].isalnum()
153+
or string[: len(separator)] == separator
154+
or string_conv[: len(separator) :] == separator
155+
)
156+
else separator
157+
)
158+
after = (
159+
""
160+
if (
161+
string[-1].isalnum()
162+
or string[: -len(separator)] == separator
163+
or string_conv[: -len(separator)] == separator
164+
)
165+
else separator
166+
)
168167
return f"{before}{string_conv}{after}"
169168

170169

docs/source/conf.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,43 @@
1212
#
1313
import os
1414
import sys
15-
sys.path.insert(0, os.path.abspath('../../casefy'))
15+
16+
sys.path.insert(0, os.path.abspath("../../casefy"))
1617

1718
# -- Project information -----------------------------------------------------
1819

19-
project = 'Casefy'
20-
copyright = '2022, Diego Miguel Lozano'
21-
author = 'Diego Miguel Lozano'
20+
project = "Casefy"
21+
copyright = "2022, Diego Miguel Lozano"
22+
author = "Diego Miguel Lozano"
2223

2324
# The full version, including alpha/beta/rc tags
24-
release = '0.1.2'
25+
release = "0.1.2"
2526

2627

2728
# -- General configuration ---------------------------------------------------
2829

2930
# Add any Sphinx extension module names here, as strings. They can be
3031
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3132
# ones.
32-
extensions = [
33-
'sphinx.ext.autodoc',
34-
'sphinx.ext.napoleon'
35-
]
33+
extensions = ["sphinx.ext.autodoc", "sphinx.ext.napoleon"]
3634

3735
# Add any paths that contain templates here, relative to this directory.
38-
templates_path = ['_templates']
36+
templates_path = ["_templates"]
3937

4038
# The language for content autogenerated by Sphinx. Refer to documentation
4139
# for a list of supported languages.
4240
#
4341
# This is also used if you do content translation via gettext catalogs.
4442
# Usually you set "language" from the command line for these cases.
45-
language = 'en'
43+
language = "en"
4644

4745
# List of patterns, relative to source directory, that match files and
4846
# directories to ignore when looking for source files.
4947
# This pattern also affects html_static_path and html_extra_path.
50-
exclude_patterns = ['requirements.txt']
48+
exclude_patterns = ["requirements.txt"]
5149

5250
source_suffix = {
53-
'.rst': 'restructuredtext',
51+
".rst": "restructuredtext",
5452
# '.txt': 'markdown',
5553
# '.md': 'markdown',
5654
}
@@ -60,21 +58,21 @@
6058
# The theme to use for HTML and HTML Help pages. See the documentation for
6159
# a list of builtin themes.
6260
#
63-
html_theme = 'sphinx_rtd_theme'
64-
html_baseurl = 'dmlls.github.io/casefy'
61+
html_theme = "sphinx_rtd_theme"
62+
html_baseurl = "dmlls.github.io/casefy"
6563
numfig = True
6664

67-
html_logo = '_static/images/cover_mono.png'
68-
html_favicon = '_static/images/favicon.png'
65+
html_logo = "_static/images/cover_mono.png"
66+
html_favicon = "_static/images/favicon.png"
6967

7068
html_context = {
71-
'display_github': True,
72-
'github_user': 'dmlls',
73-
'github_repo': 'casefy',
74-
'github_version': 'main/docs/source/',
69+
"display_github": True,
70+
"github_user": "dmlls",
71+
"github_repo": "casefy",
72+
"github_version": "main/docs/source/",
7573
}
7674

7775
# Add any paths that contain custom static files (such as style sheets) here,
7876
# relative to this directory. They are copied after the builtin static files,
7977
# so a file named "default.css" will overwrite the builtin "default.css".
80-
html_static_path = ['_static']
78+
html_static_path = ["_static"]

tests/test_casefy.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
__version__ = "0.1.2"
44

55

6-
import sys
76
import pathlib
7+
import sys
8+
89
sys.path.insert(0, str(pathlib.Path(__file__).parent.parent.resolve()))
910
import casefy
1011

@@ -67,8 +68,12 @@ def main():
6768
assert "bar_baz" == casefy.snakecase(".bar_baz")
6869
assert "foo_bar_baz" == casefy.snakecase("fooBARbaz", keep_together=["BAR"])
6970
assert "foo_bar_v2" == casefy.snakecase("FooBarV2", keep_together=["V2"])
70-
assert "foo_bar_ids_v2" == casefy.snakecase("FooBarIDsV2", keep_together=["IDs", "V2"])
71-
assert "foobar_ids_v2" == casefy.snakecase("FooBarIDsV2", keep_together=["FooBar", "IDs", "V2"])
71+
assert "foo_bar_ids_v2" == casefy.snakecase(
72+
"FooBarIDsV2", keep_together=["IDs", "V2"]
73+
)
74+
assert "foobar_ids_v2" == casefy.snakecase(
75+
"FooBarIDsV2", keep_together=["FooBar", "IDs", "V2"]
76+
)
7277
assert "" == casefy.snakecase("")
7378
assert "" == casefy.snakecase(None)
7479

@@ -136,9 +141,14 @@ def main():
136141
assert "@bar@baz" == casefy.separatorcase("_bar_baz", separator="@")
137142
assert "#!bar#!baz" == casefy.separatorcase(".bar_baz", separator="#!")
138143
assert "foo/bar/baz" == casefy.separatorcase(
139-
"fooBARbaz", separator="/", keep_together=["BAR"])
140-
assert "foo|bar|v2" == casefy.separatorcase("FooBarV2", separator="|", keep_together=["V2"])
141-
assert "foo|bar|ids|v2" == casefy.separatorcase("FooBarIDsV2", separator="|", keep_together=["IDs", "V2"])
144+
"fooBARbaz", separator="/", keep_together=["BAR"]
145+
)
146+
assert "foo|bar|v2" == casefy.separatorcase(
147+
"FooBarV2", separator="|", keep_together=["V2"]
148+
)
149+
assert "foo|bar|ids|v2" == casefy.separatorcase(
150+
"FooBarIDsV2", separator="|", keep_together=["IDs", "V2"]
151+
)
142152
assert "" == casefy.separatorcase("", separator="")
143153
assert "" == casefy.separatorcase(None, separator="")
144154

0 commit comments

Comments
 (0)