This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: add string.snake function
Type: enhancement Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, jeffolsi10, remi.lapeyre
Priority: normal Keywords:

Created on 2020-04-29 12:48 by jeffolsi10, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (13)
msg367643 - (view) Author: jeffolsi10 (jeffolsi10) Date: 2020-04-29 12:48
Like we have:
capitalize
swapcase
and others we should also have snake case

Which converts:
before: First Name, Last Name, Employee Status, Subject
after: first_name, last_name, employee_status, subject

This is very useful when working with titles of columns that are to be used in databases columns
usage example
https://github.com/pandas-dev/pandas/issues/33826
msg367646 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-04-29 14:08
What would be the full specification of this? If you want to use it for column names, what happens if the string starts with a $, or some character that can't be used by your particular database?

I'm skeptical that this could be general purpose enough to be used in a wide range of situations, without having a dozen or so parameters to it.

I'm thinking of things like:
- ascii-only?
- result must be a valid python identifier
- certain special characters not allowed anywhere
- certain other special characters not allowed at the start of the string

etc.

We'd need a full specification before deciding to accept this.
msg367667 - (view) Author: jeffolsi10 (jeffolsi10) Date: 2020-04-29 15:56
snake case has very specific definition :
https://en.wikipedia.org/wiki/Snake_case
I expect the function to implement the definition and not something that I or someone else desire.

As for your question about '$' char I could ask the same thing for lower()
The snake case convert only letters and space. It doesn't handle spacial chars. it ignores them.
msg367669 - (view) Author: jeffolsi10 (jeffolsi10) Date: 2020-04-29 16:06
I'd like also to point that there are few other cases:
https://stackoverflow.com/questions/11273282/whats-the-name-for-hyphen-separated-case

This is PascalCase: SomeSymbol
This is camelCase: someSymbol
This is snake_case: some_symbol

So a possible function could be:

convert_case(string, input_format, output_format)
for example:

convert_case(someSymbol, input_format='camelCase', output_format='snake_case')
output: some_symbol

It should be noted that you can not convert something without knowing what it is.
so something like : convert('helloworld','camelCase') can not be done because you don't know where one first word start and when one ends.
msg367670 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-04-29 16:10
So then it appears the snake case function couldn't be used for database column names, without some additional processing. So, what is the use case for it?

I just don't see a lot of use for this.
msg367680 - (view) Author: jeffolsi10 (jeffolsi10) Date: 2020-04-29 17:25
it can. This is why I'm asking this.
Consider APIs that return list of names in camelCase. You must convert the keys to snakeCase to create tables from it as it's bad practice to have capitalised letters in columns or table names.

Further more, consider someone who wants to create tests to make sure all developers used snakeCase for function names. There are many many use cases.

You can also see the examples in stackoverflow that I posted. 500 votes up and there are many more.

Again I think this is totally in the domain of capitalize, swapcase which we already have. I think that snakeCase has way more use cases than capitalize.
msg367683 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-04-29 17:35
I remain unconvinced, but I'm only one person. You might want to bring this up on python-ideas to see if it can get some more support.

But be aware this is going to have much less support that the recent PEP 616 removeprefix/removesuffix discussion, which went on for weeks or months, and involved writing a PEP.

While the Wikipedia page you cite discusses how to convert a string of words into snake case, a PEP will require much more detail. Just a few off the top of my head: What happens to multiple space: does that become a single underscore, or multiple? Is all whitespace converted to underscores, including newlines and tabs?  Does it work if the string is already in CamelCase?

I also don't think swapcase or capitalize (plus some others) would be added today if they didn't already exist.

To me, this seems like something that belongs on PyPI (like https://pypi.org/project/stringcase/), and not in the standard library.
msg367687 - (view) Author: jeffolsi10 (jeffolsi10) Date: 2020-04-29 18:23
I feel this should be in core.
I still don't understand why capitalize is supported and others do not.
snake case is very well defined. Issues like tabs and spaces are not relevant. can you show example that you have that dilemma?

To be honest I don't feel very strongly about it. I thought it would be cool to have native support for it as this is a small feature which isn't subject to changes thus shouldn't be break and has a lot of usage. I see many people implement it themselves with regex and other methods. 

Feel free to close this if I didn't convince you it.
I opened this because I thought it can help others.. I can live without this being supported in Python native.
msg367694 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-04-29 19:37
I don't understand the motivation, why would it be very useful when working with titles of columns that are to be used in databases columns?

Databases can handle columns with spaces in their name:

postgres=# create temporary table foo ("column with spaces" text);
CREATE TABLE
Time: 29,973 ms
postgres=# insert into foo values ('bar');
INSERT 0 1
Time: 0,746 ms
postgres=# select * from foo;
 column with spaces 
--------------------
 bar
(1 row)

Time: 3,452 ms


There is also various library available to do this on Pypi like https://github.com/okunishinishi/python-stringcase and https://github.com/jpvanhal/inflection. I'm pretty sure I used inflection at one company and it worked great.

Finally, the issue on the Pandas repository says how to do it: you can use Series.apply.
msg367699 - (view) Author: jeffolsi10 (jeffolsi10) Date: 2020-04-29 20:32
Rémi Lapeyre not all of them.
I can give list of many examples where snake case is needed.

But the question is: Are we discussing if snake case is even needed
or are we discussing if this should be in python core.

Those are two totally different things. 

1. The need is shown in many questions over the internat. Again see stackoverflow.

2. To my understanding core should provide infra for the common simple usages. One can also argue why python need to maintain str conversion at all if we have such good extensions in different libs.


Are you saying that in your opinion people won't find it useful so there is no need for it or are you saying that no matter how many people would like it the feature should not be in Python core.
msg367700 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-04-29 20:39
> One can also argue why python need to maintain str conversion at all if we have such good extensions in different libs.

Back when we were starting python 3.x there was discussion or removing these from str and bytes, but it was decided that breaking existing code just wasn't worth it. You can imagine how rarely b"Some String".swapcase() is used.

As I said, if we were designing the language from scratch today I doubt we would include swapcase, title, and capitalize, at least. I don't think using their existence as an argument to add more methods like them is very convincing.
msg367745 - (view) Author: jeffolsi10 (jeffolsi10) Date: 2020-04-30 08:19
I can respect that.
So you may close this request.
msg367746 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-04-30 08:26
Thanks for the suggestion and the discussion. I'll close the issue.
History
Date User Action Args
2022-04-11 14:59:30adminsetgithub: 84617
2020-04-30 08:26:55eric.smithsetstatus: open -> closed
resolution: rejected
messages: + msg367746

stage: resolved
2020-04-30 08:19:02jeffolsi10setmessages: + msg367745
2020-04-29 20:39:20eric.smithsetmessages: + msg367700
2020-04-29 20:32:02jeffolsi10setmessages: + msg367699
2020-04-29 19:37:39remi.lapeyresetnosy: + remi.lapeyre
messages: + msg367694
2020-04-29 18:23:20jeffolsi10setmessages: + msg367687
2020-04-29 17:35:43eric.smithsetmessages: + msg367683
2020-04-29 17:25:58jeffolsi10setmessages: + msg367680
2020-04-29 16:10:12eric.smithsetmessages: + msg367670
2020-04-29 16:06:31jeffolsi10setmessages: + msg367669
2020-04-29 15:56:50jeffolsi10setmessages: + msg367667
2020-04-29 14:08:17eric.smithsetnosy: + eric.smith
messages: + msg367646
2020-04-29 12:48:52jeffolsi10create