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

fix: support consecutive capital letters #220

Merged
merged 7 commits into from
Apr 10, 2019
Merged

fix: support consecutive capital letters #220

merged 7 commits into from
Apr 10, 2019

Conversation

bcoe
Copy link
Contributor

@bcoe bcoe commented Apr 9, 2019

The simple algorithm I wrote for decamelize did not support acronyms well (the bug is seen here).

This PR extends the method adding this functionality and adds tests for this edge-case; CC: @steffnay

fixes: #219

@bcoe bcoe requested a review from busunkim96 April 9, 2019 15:31
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Apr 9, 2019
@@ -70,8 +70,8 @@ def main(synthfile: str, metadata: str, extra_args: Sequence[str]):
if os.path.lexists(synth_file):
synthtool.log.debug(f"Executing {synth_file}.")
# https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
spec = importlib.util.spec_from_file_location("synth", synth_file)
synth_module = importlib.util.module_from_spec(spec)
spec = util.spec_from_file_location("synth", synth_file)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was receiving linting errors locally without switching to this approach (upstream regression perhaps?).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly that should be a linting error. It's not always guaranteed that submodules are actually loaded when you import a top-level module.

That said, I'd prefer that you instead change the above import to import importlib.util instead. util is too common of a name to be meaningful.

str2 += " " + chr.upper()
prev = " "
str2 = ""
for i in range(0, len(str)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

for i, chr in enumerate(str):

would be more idiomatic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, chr is a built-in, so it's best to avoid that as a variable name. Maybe current to correspond to prev?

if prev == " ":
str2 += chr.upper()
elif re.match(r"[A-Z]", chr):
# handle "JSONFrom" -> "JSON From" case.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not finding these comments helpful. Why does getting the next character solve this case?

if re.match(r"[A-Z]", chr):
str2 += " " + chr.upper()
prev = " "
str2 = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is existing code, but output or decamelized would be much more readable than str2.

str2 += chr.upper()
elif re.match(r"[A-Z]", chr):
# handle "JSONFrom" -> "JSON From" case.
next = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next is also a built-in, so it's best not to overwrite it. Usually we use a trailing underscore to avoid these kinds of collisions. next_.

@@ -153,10 +153,22 @@ def _load_partials(self, metadata: Dict):

def decamelize(str: str):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str is a built-in, so we probably shouldn't use it as a variable name. How about value or str_?

else:
str2 += chr
prev = str2[len(str2) - 1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is to handle when prev should be " "? I'd prefer if the above set a temporary variable for the string to append, then this becomes

output += to_append
prev = to_append

@@ -70,8 +70,8 @@ def main(synthfile: str, metadata: str, extra_args: Sequence[str]):
if os.path.lexists(synth_file):
synthtool.log.debug(f"Executing {synth_file}.")
# https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
spec = importlib.util.spec_from_file_location("synth", synth_file)
synth_module = importlib.util.module_from_spec(spec)
spec = util.spec_from_file_location("synth", synth_file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly that should be a linting error. It's not always guaranteed that submodules are actually loaded when you import a top-level module.

That said, I'd prefer that you instead change the above import to import importlib.util instead. util is too common of a name to be meaningful.

@tswast tswast merged commit 4862dc9 into master Apr 10, 2019
@tswast tswast deleted the acronym-support branch April 10, 2019 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

README generation handles acronyms poorly
3 participants