top | item 43551915

Show HN: Textcase: A Python Library for Text Case Conversion

71 points| zobweyt | 11 months ago |github.com

41 comments

order

twalkz|11 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!

zobweyt|11 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!

lnenad|11 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.

zobweyt|11 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!

7bit|11 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.

fake-name|11 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.

zobweyt|11 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!

re|11 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...

zobweyt|11 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!

frizlab|11 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”.

zobweyt|11 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

anentropic|11 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

zobweyt|11 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!

kianN|11 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!

danpalmer|11 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.

zobweyt|11 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!

cadamsdotcom|11 months ago

HAppY ApRiL FoOLs!

If only this comment supported case conversion..

In any case congrats on shipping!

zobweyt|11 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!

wodenokoto|11 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)

zobweyt|11 months ago

Thanks for the feedback! I'm glad you found a use case for this library!