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

PICARD-1386: Add $title function #1018

Merged
merged 6 commits into from Oct 29, 2018
Merged

PICARD-1386: Add $title function #1018

merged 6 commits into from Oct 29, 2018

Conversation

virusMac
Copy link
Contributor

Summary

  • This is a…
    • Bug fix
    • Feature addition
    • Refactoring
    • Minor / simple change (like a typo)
    • Other
  • Describe this change in 1-2 sentences:

Problem

Creating more flexible title-case function, than function in plugin titlecase

Solution

Added func_title function

Action

@phw phw changed the title PICARD-1386: Add $title function WIP PICARD-1386: Add $title function Oct 25, 2018
picard/script.py Outdated
@@ -846,6 +846,17 @@ def func_ne_any(parser, x, *args):
"""
return func_not(parser, func_eq_all(parser, x, *args))

def func_title(parser, text):
""" Title-case a text """
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please make a more complete description of the function, including an example.

Also add a test for it in https://github.com/metabrainz/picard/blob/master/test/test_script.py

picard/script.py Outdated
words.remove(words[0])
for i in words:
capital.append(i.capitalize())
return " ".join(capital)
Copy link
Collaborator

@zas zas Oct 26, 2018

Choose a reason for hiding this comment

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

How does this differ from string.capwords ?

Copy link
Member

@phw phw left a comment

Choose a reason for hiding this comment

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

@virusMac Thanks a lot, this is already looking pretty good. I actually like your general approach of implementing this function (see my comment below). But nevertheless one change, please use the implementation at https://github.com/metabrainz/picard-plugins/blob/2.0/plugins/titlecase/titlecase.py as a basis for this function. This is a bit advanced and handles cases with word boundaries other than blanks like the examples below.

s title(s)
unchained (tales of the unexcpected) Unchained (Tales Of The Unexcpected)
…alone …Alone

For completeness, Python already provides a title function for strings, you can just do text.title(). But this for example already would fail on your #1abc 4def - g test case and convert it to #1Abc 4Def - G, which is definitely unexpected.

Now just one note about your own implementation of the function. As I said I like the general approach you took, but you could have simplified it by not treating the first word special like so:

def func_title(parser, text):
    if not text:
        return ""
    words = text.split()
    capital = []
    for i in words:
        capital.append(i.capitalize())
    return " ".join(capital)

That avoids the need to remove the first word again for the list :)

self.assertScriptResultEquals("$title(Abc Def G)","Abc Def G")
self.assertScriptResultEquals("$title(abc def g)","Abc Def G")
self.assertScriptResultEquals("$title(#1abc 4def - g)","#1abc 4def - G")

Copy link
Member

Choose a reason for hiding this comment

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

Oh, please keep the test cases and add also the two test cases I mentioned in my previous comment. I think then we are good to go here :)

phw
phw previously approved these changes Oct 27, 2018
Copy link
Member

@phw phw left a comment

Choose a reason for hiding this comment

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

Thanks a lot, this is looking good. From my side this is fine to merge.

@phw phw changed the title WIP PICARD-1386: Add $title function PICARD-1386: Add $title function Oct 27, 2018
zas
zas previously approved these changes Oct 27, 2018
@samj1912
Copy link
Collaborator

Please rebase and squash this before merging.

@samj1912
Copy link
Collaborator

Also make sure your commit message follows the contribution guidelines

@virusMac virusMac dismissed stale reviews from zas and phw via ca9e90f October 28, 2018 11:52
@virusMac virusMac force-pushed the master branch 2 times, most recently from ca9e90f to 15419cd Compare October 28, 2018 12:25
@virusMac
Copy link
Contributor Author

Omg, i've accidentally done something wrong...

@zas
Copy link
Collaborator

zas commented Oct 28, 2018

git reflog is your friend

@virusMac virusMac force-pushed the master branch 5 times, most recently from 0536206 to ca9e90f Compare October 28, 2018 13:07
+ unit tests for func_test

Add func_title and tests for it in test_script.py
added 2 tests to func_title

added 2 tests to func_title
@phw phw merged commit 3310f99 into metabrainz:master Oct 29, 2018
@samj1912
Copy link
Collaborator

@phw, the commits were still not fixed?

@phw
Copy link
Member

phw commented Oct 30, 2018

@samj1912 There was some mess with unrelated commits in the PR in between, but it had been fixed.

@phw phw mentioned this pull request Nov 11, 2018
17 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants