Skip to content

Commit

Permalink
refactor: convert camelCase to snake_case (#37)
Browse files Browse the repository at this point in the history
Purposes:
1. To match typical modern Python coding styles (recommended by PEP 8)
2. To prepare for argparse usage, which uses snake_case for default dests

Via https://ast-grep.github.io/, with rule:

```
id: _
language: python

rule:
  pattern: $ID
  kind: identifier
  all:
  - regex: '^[^A-Z]'  # not PascalCase
  - regex: '[a-z][A-Z]'  # has camelCase transition

  not:
    any:
    # backcompat (part of cog module API)
    - regex: ^sOut$
    - inside:
        kind: attribute
        has:
          field: object
          regex: \.cogmodule$

    # unittest methods
    - regex: ^assert
    - regex: ^setUp$
    - regex: ^tearDown$
    - regex: ^addCleanup$

transform:
  ID_SNAKE:
    convert:
      source: $ID
      toCase: snakeCase
  UNDERSCORE:
    replace:
      source: $ID
      replace: '^(_)?.*'
      by: '$1'

fix: $UNDERSCORE$ID_SNAKE
```

plus manual review of all changes
  • Loading branch information
vergenzt authored May 1, 2024
1 parent 5a6f8c1 commit 1ef2543
Show file tree
Hide file tree
Showing 8 changed files with 736 additions and 736 deletions.
460 changes: 230 additions & 230 deletions cogapp/cogapp.py

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions cogapp/makefiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import os.path

from .whiteutils import reindentBlock
from .whiteutils import reindent_block


def makeFiles(d, basedir="."):
def make_files(d, basedir="."):
"""Create files from the dictionary `d` in the directory named by `basedir`."""
for name, contents in d.items():
child = os.path.join(basedir, name)
Expand All @@ -14,14 +14,14 @@ def makeFiles(d, basedir="."):
if isinstance(contents, bytes):
mode += "b"
with open(child, mode) as f:
f.write(reindentBlock(contents))
f.write(reindent_block(contents))
else:
if not os.path.exists(child):
os.mkdir(child)
makeFiles(contents, child)
make_files(contents, child)


def removeFiles(d, basedir="."):
def remove_files(d, basedir="."):
"""Remove the files created by `makeFiles`.
Directories are removed if they are empty.
Expand All @@ -32,6 +32,6 @@ def removeFiles(d, basedir="."):
if isinstance(contents, (bytes, str)):
os.remove(child)
else:
removeFiles(contents, child)
remove_files(contents, child)
if not os.listdir(child):
os.rmdir(child)
Loading

0 comments on commit 1ef2543

Please sign in to comment.