OT-ish: convert script from camelCase to snake_case

42 views
Skip to first unread message

jkn

unread,
Sep 14, 2022, 4:11:17 PM9/14/22
to leo-editor
Hi all
    slightly OT but I think this is a good place to ask:

I tend to write my personal python using camelCase for variables and method names;
I prefer this to the PEP8 standard for various reasons.

I now have a need to convert some such scripts to snake_case, to meet
a linting requirement. I thought that there would be plenty of tools to do this,
but rather to my surprise the various checkers and formatters I have found
(in a fairly cursory search, admittedly) don't cater for this. They all do plenty
of other things, and I will definitely be using them in the future, but I could
do with this as a staring point.

Any pointers to a tool which can do this job, probably with some flexible configuration?

Thanks & Regards
J^n

Thomas Passin

unread,
Sep 14, 2022, 6:16:25 PM9/14/22
to leo-editor
pylint by default will issue message C0103 for functions and methods that should be snake_case but aren't (Leo's configuration doesn't seem to pick that up).  Since the pylint message will include line and position of the name, it shouldn't be hard to write a program to convert these instances.  If you did a string.replace() for each of them, the names would get fixed in docstrings and comments too.

Pylint by default issues the same warning for short variable names without an underscore like "x", too.  I don't know if that can be turned off or not, but I imagine it could be checked for easily.

jkn

unread,
Sep 15, 2022, 3:02:50 AM9/15/22
to leo-editor
Hi Thomas
    that was pretty much the approach I was thinking of adopting myself, thanks. I was just a bit surprised that something like that didn't already exist.

Anyway, in the absence of anything else I'll see what that gives me.

Cheers, J^n

jkn

unread,
Sep 15, 2022, 7:05:23 AM9/15/22
to leo-editor
Yeah ... I wrote a small script which runs pylint and captures the output, then looks at the errors and applies a few simple heuristics to get a list of the changes I want, then does a replace() on that list.

It's a bit crude but gives me most of what I want. I am reminded why I prefer camelCase though, too many extra keystrokes and hand movements, for little to no extra readability IMO.

Oh well...

Thanks, J^n

Thomas Passin

unread,
Sep 15, 2022, 9:08:54 AM9/15/22
to leo-editor
FWIW (maybe not much), Wikipedia's page on CamelCase includes this -

'A 2010 follow-up study, with other subjects containing mainly pre-trained programmers and using an improved measurement method with use of eye-tracking equipment, indicates: "While results indicate no difference in accuracy between the two styles, subjects recognize identifiers in the underscore style more quickly." '

On Thursday, September 15, 2022 at 7:05:23 AM UTC-4 jkn wrote:
...
It's a bit crude but gives me most of what I want. I am reminded why I prefer camelCase though, too many extra keystrokes and hand movements, for little to no extra readability IMO.
...

jkn

unread,
Sep 15, 2022, 9:29:13 AM9/15/22
to leo-editor
Yeah, I've seen those kind of studies - and ones with different findings, as you may well have.

I don't think that (for *me*) there is much difference in the 'cognitive effort' between the two styles. But there is more effort in typing snake_case (both the extra character, and the necessary hand/finger movements). The latter effort might be a bit specific to me; I have a slightly malformed right hand which means I tend to type underscores in a particular way.

Anyway, there are better things than this to pound the table about...

J^n

Thomas Passin

unread,
Sep 15, 2022, 9:34:32 AM9/15/22
to leo-editor
For me, I barely notice typing the underscore, but my hands really dislike typing a "#" for a comment.  So when I need a line-oriented data format of my own, I usually allow a ";" as well as a "#" to comment out a line.

jkn

unread,
Sep 15, 2022, 12:34:17 PM9/15/22
to leo-editor
it probably depends on your keyboard nationality as well...
Reply all
Reply to author
Forward
0 new messages