4 ms·
Show HN: Textcase: A Python Library for Text Case Conversion
- kianN 2y agoMy 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!
- zobweyt 2y agoThanks 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!
- esafak 2y agoIs there a GH badge for the dependency count? Depfu maybe. Someone should make one if not; it's worth advertising.
- zobweyt 2y agoThanks 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 https://img.shields.io/badge/dependencies-0-green
- danpalmer 2y agoDefinitely 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 2y agoI 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.
- cadamsdotcom 2y agoHAppY ApRiL FoOLs! If only this comment supported case conversion.. In any case congrats on shipping!
- zobweyt 2y agoHappy 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!
- twalkz 2y agoI 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 2y agoThank 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!
- Rendello 2y agoMany edge-cases can be found with regards to casing! Like title-case characters. https://www.unicode.org/versions/Unicode16.0.0/core-spec/chapter-3/#G34078 https://www.unicode.org/versions/Unicode16.0.0/core-spec/cha...
- marban 2y agoBonus: https://github.com/un33k/python-slugify https://github.com/un33k/python-slugify
- frizlab 2y agoGreat 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 2y agoThanks! 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
- zvr 2y agoNice work, but since it does not handle anything else than strings, maybe it should be named "stringcase" or something.
- zobweyt 2y agoThank 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
- zvr 2y agoMy 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.
- zobweyt 2y ago
- fake-name 2y 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 2y agoThat'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!
- anentropic 2y agoLooks 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 2y agoThank 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!
- lnenad 2y agoI 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 2y agoI 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!
- lnenad 2y agoNah 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.
- 7bit 2y agoYou 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.
- kseistrup 2y agoNow available from AUR: https://aur.archlinux.org/packages/python-textcase-git https://aur.archlinux.org/packages/python-textcase-git
- re 2y 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-you-call-something-that-has-a-lot-of-features https://english.stackexchange.com/questions/393517/what-do-y...
- zobweyt 2y agoThank 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!
- wodenokoto 2y agoThis 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 2y agoThanks for the feedback! I'm glad you found a use case for this library!
- ilmasalsabil 1y ago[dead]