-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #309 from int3l/main
Code examples for episode 456
- Loading branch information
Showing
8 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# [python regex dynamic replacements (intermediate)](https://youtu.be/o_PD7AAbs4U) | ||
|
||
Today I show a cool technique for regex replacement in python! | ||
|
||
## Interactive examples | ||
|
||
### Bash | ||
|
||
```bash | ||
python t.py | ||
python t.py | flake8 - | ||
|
||
virtualenv venv | ||
. venv/bin/activate | ||
|
||
pip install yesqa | ||
yesqa src.py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import os # noqa: F401 | ||
|
||
print('hello demo') # some comment | ||
|
||
# another comment here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import os # noqa: F401 | ||
|
||
print('hello demo') # noqa | ||
|
||
# another comment here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import re | ||
|
||
with open('src.py') as f: | ||
contents = f.read() | ||
|
||
comment = re.compile('#[^\n]+') | ||
|
||
print(comment.sub('', contents)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import os # noqa: F401 | ||
|
||
print('hello demo') # noqa | ||
|
||
# another comment here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from __future__ import annotations | ||
from typing import Match | ||
|
||
import re | ||
|
||
with open('src.py') as f: | ||
contents = f.read() | ||
|
||
comment = re.compile('#([^\n]+)') | ||
|
||
|
||
def cb(match: Match[str]) -> str: | ||
return f'#{len(match[1]) * "."}' | ||
|
||
|
||
print(comment.sub(cb, contents), end='') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import os # noqa: F401 | ||
|
||
print('hello demo') # noqa | ||
|
||
# another comment here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from __future__ import annotations | ||
from typing import Match | ||
|
||
import re | ||
|
||
with open('src.py') as f: | ||
contents = f.read() | ||
|
||
comment = re.compile('(# ?)([^\n]+)') | ||
|
||
|
||
def cb(match: Match[str]) -> str: | ||
return f'{match[1]}{len(match[2]) * "."}' | ||
|
||
|
||
print(comment.sub(cb, contents), end='') |