|
|
|
|
|
by alexpovel
635 days ago
|
|
These sorts of cases are why I wrote srgn [0]. It's based on tree-sitter too. Calling it as cat file.py | srgn --py def --py identifiers 'database' 'db'
will replace all mentions of `database` inside identifiers inside (only!) function definitions (`def`) with `db`.An input like import database
import pytest
@pytest.fixture()
def test_a(database):
return database
def test_b(database):
return database
database = "database"
class database:
pass
is turned into import database
import pytest
@pytest.fixture()
def test_a(db):
return db
def test_b(db):
return db
database = "database"
class database:
pass
which seems roughly like what the author is after. Mentions of "database" outside function definitions are not modified. That sort of logic I always found hard to replicate in basic GNU-like tools. If run without stdin, the above command runs recursively, in-place (careful with that one!).Note: I just wrote this, and version 0.13.2 is required for the above to work. [0]: https://github.com/alexpovel/srgn |
|