Show HN: Textcase: A Python Library for Text Case Conversion
71 points
12 months ago
| 11 comments
| github.com
| HN
twalkz
12 months ago
[-]
I am really impressed by how thoroughly this library thinks through all the applications and edge cases of text casing.

On a recent project I spent about an hour trying to do something similar (and far less sophisticated) before I realized it was a problem I had no desire in really solving, so I backed out all my changes and just went with string.capitalize(), even though it didn’t really do what I was looking for. Looking forward to using this instead!

reply
zobweyt
12 months ago
[-]
Thank you for the kind words! I'm glad you appreciate the effort put into covering all those edge cases.

It sounds like you had quite the adventure with text casing on your project. I'm happy this library can save you some time and hassle. Looking forward to see what can be built with it!

reply
Rendello
12 months ago
[-]
Many edge-cases can be found with regards to casing! Like title-case characters.

https://www.unicode.org/versions/Unicode16.0.0/core-spec/cha...

reply
lnenad
12 months ago
[-]
I might be jaded, but I think having libraries for such simple use cases leads to the inevitable `left-pad` situation.

When I say simple use cases I mean that since you probably don't need all of these functions at once that it would be easier to copy the code you need if you don't feel comfortable writing it instead of adding yetanotherlibrary to your dependency tree.

reply
zobweyt
12 months ago
[-]
I understand your perspective, and it's a valid concern. However, this library is designed to support not only simple use cases but also more advanced scenarios, providing a comprehensive solution for various needs. Additionally, it has zero dependencies, which helps keep your project lightweight. This way, you can benefit from the library's features without adding unnecessary complexity to your dependency tree. Thank you for sharing your thoughts!
reply
lnenad
12 months ago
[-]
Nah it's not you or your library, there is definitely a place for such utilities. The issue is broader, related to everyone installing libs for two liners and having bajillion dependencies.
reply
axegon_
12 months ago
[-]
Don't forget space... npm install and 500GBs go "bye-bye"
reply
lnenad
12 months ago
[-]
Hey I'll have you know that memory is cheap nowadays and that I'd be happy to fill out my drives with node libraries for converting a's into A's.
reply
7bit
12 months ago
[-]
You can always just take the code and put it in your app. Having libraries like these don't force you to add them as a dependency. Assuming the right OSS license.
reply
lnenad
12 months ago
[-]
I agree but in reality many will take the easier path of [`pip` `npm` `cargo` `yarn` `go`] [`install`, `add`] when seeing the functionality out there. I was also making a broader talking point.
reply
fake-name
12 months ago
[-]
> A feature complete Python text case conversion library

Considering it supports unicode input, I somehow doubt that. Given that there's no mention of unicode normalization it'll likely break some strings.

reply
zobweyt
12 months ago
[-]
That's a great observation! Instead of seeing it as a limitation, it can be treated as a feature. Users can handle Unicode normalization using Python's built-in unicodedata module to ensure proper case conversion. Thanks for pointing that out!
reply
re
12 months ago
[-]
> A feature complete Python text case conversion library

I suspect you mean "featureful", "full-featured" or similar[1]—"feature complete" means that you're not going to add any more features.

[1] https://english.stackexchange.com/questions/393517/what-do-y...

reply
zobweyt
12 months ago
[-]
Thank you for the clarification! I appreciate your input. I've updated the wording to "feature-rich" to better convey the intended meaning. Your feedback is valuable!
reply
frizlab
12 months ago
[-]
Great library!

Does it support non-English title casing?

For instance in French, title casing for “les maisons bleues” is “Les Maisons bleues” while for “des maisons bleues” it’s “Des maisons bleues”.

reply
zobweyt
12 months ago
[-]
Thanks!

It does not support non-English title casing. From the documentation:

> It also works non-ascii characters. However, no inferences on the language itself is made. For instance, the digraph ij in Dutch will not be capitalized, because it is represented as two distinct Unicode characters. However, æ would be capitalized

reply
frizlab
12 months ago
[-]
I was talking about the specific rules that are in place for title capitalization. As you can see in my example the uppercase letters seem randomly placed for a title, but they are indeed correct. For German too there are issues where capitalization has a meaning on the word itself. That kind of things.

It looks like your library does not support it, which is understandable, it is a huge problem to tackle, but I just wanted to be sure.

reply
zobweyt
12 months ago
[-]
Thank you for the clarification! I understand that title capitalization can be quite complex, especially with specific rules in languages like German where capitalization can change the meaning of a word.

I guess handling these nuances falls under the broader categories of internationalization (i18n) and localization (l10n).

reply
frizlab
12 months ago
[-]
Just to be excessively clear and maybe borderline annoying, this is not a simple nuance. In German the meaning of a word can actually change depending on its capitalization. Even in English, lowercasing the I is very weird.
reply
re
12 months ago
[-]
> It does not support non-English title casing

Perhaps document that clearly—it's an important restriction that the library assumes English-language strings. ("no inferences on the language itself is made" isn't quite true since the language is inferred to be English, or to at least follow English-compatible rules for casing)

reply
zobweyt
12 months ago
[-]
Thanks for your feedback! You're right; I should clarify that the library assumes English-language strings for casing. I'll update the documentation to make this limitation clear. I appreciate you pointing it out!
reply
zvr
12 months ago
[-]
Nice work, but since it does not handle anything else than strings, maybe it should be named "stringcase" or something.
reply
zobweyt
12 months ago
[-]
Thank you for the feedback!

I appreciate your suggestion regarding the name, but unfortunately this name was already taken, so "textcase" was chosen.

I also have ideas for adding dictionary key conversion and other features in the future that will handle more than just strings. In addition, you can use this library to convert cases of Iterable[str] using textcase.pattern

reply
zvr
12 months ago
[-]
My issue with using "text" is that I assume that a text like "I THINK I DO" should be converted to "I think I do", not "i think i do".

And that's just in English...

If "text" is in Greek, like "Καλημέρα", the upper form should be "ΚΑΛΗΜΕΡΑ", not a juxtaposition of upper() conversions of each letter.

reply
zobweyt
12 months ago
[-]
Thanks for the clarification!

Yeah, there is such a problem with the naming "text" suggests something different than just a "string".

I guess handling these nuances falls under the broader categories of internationalization (i18n) and localization (l10n).

reply
anentropic
12 months ago
[-]
Looks brilliant!

My only suggestion is here:

> It also ignores any leading, trailing, or duplicate delimiters:

    from textcase import case, convert

    print(convert("IOStream", case.SNAKE))             # io_stream
    print(convert("myJSONParser", case.SNAKE))         # my_json_parser
    print(convert("__weird--var _name-", case.SNAKE))  # weird_var_name
In the case of a conversion target that has delimiters (snake, kebab) it might be nice to have an alternative option to preserve such features but normalise them to the target delimiter

i.e.

    print(convert("__weird--var _name-", case.SNAKE, preserve=True))  # __weird__var__name
reply
zobweyt
12 months ago
[-]
Thank you for your suggestion! Adding a preserve option to maintain leading, trailing, and duplicate delimiters while normalizing them to the target delimiter is a great idea. I’ll consider implementing this feature. Thanks again!
reply
kianN
12 months ago
[-]
My favorite part of this library is that it seems to have zero dependencies!

Python packages seem to often rope in a surprising number of dependencies for relatively limited libraries.

I can easily imagine pulling this package into my work: thank you for keeping the requirements to a minimum!

reply
danpalmer
12 months ago
[-]
Definitely something to be championed, although I suspect this is a matter of perspective. I find Python packages to have refreshingly few dependencies compared to packages in the JS ecosystem, although compared to the Swift ecosystem which I’m somewhat familiar with, they do tend to have a few more.
reply
zobweyt
12 months ago
[-]
I appreciate your perspective! It's interesting to consider how the built-in libraries of a language can influence its ecosystem. Python does have a rich standard library that often reduces the need for external dependencies. In contrast, JavaScript's ecosystem has evolved around web development, where modularity and flexibility are prioritized, leading to a proliferation of packages.
reply
zobweyt
12 months ago
[-]
Thanks for the kind words!

This library actually has zero dependencies! I'm glad you appreciate the no-dependency design.

It's great to hear that it fits well with your work!

reply
esafak
12 months ago
[-]
Is there a GH badge for the dependency count? Depfu maybe. Someone should make one if not; it's worth advertising.
reply
zobweyt
12 months ago
[-]
Thanks for the suggestion!

Right now, there's no such GH badge. Since the project will always have zero dependencies, I think we can simply use a static badge like this:

https://img.shields.io/badge/dependencies-0-green

reply
cadamsdotcom
12 months ago
[-]
HAppY ApRiL FoOLs!

If only this comment supported case conversion..

In any case congrats on shipping!

reply
zobweyt
12 months ago
[-]
Happy April!

Actually, this library supports conversion of even such strings!

```python

>>> import textcase

>>> textcase.convert("HAppY ApRiL FoOLs!", textcase.case.SNAKE, (textcase.boundary.SPACE,))

'happy_april_fools!'

```

Thanks for the congratulations!

reply
wodenokoto
12 months ago
[-]
This should be implemented in editors.

It also looks to be nice in exploratory data analysis:

    df = pd.read_csv(f)
    df.columns = map(convert, df.columns, case.snake)
reply
zobweyt
12 months ago
[-]
Thanks for the feedback! I'm glad you found a use case for this library!
reply
marban
12 months ago
[-]
reply
kseistrup
12 months ago
[-]
reply
zobweyt
12 months ago
[-]
Thank you for the update! I’ve added a badge to the GitHub repository to reflect its availability on AUR.
reply
kseistrup
12 months ago
[-]
Great, thanks!
reply