Created
January 18, 2023 16:03
-
-
Save astropenguin/91d590cf1497a19ce4960f620be68ece to your computer and use it in GitHub Desktop.
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
from random import randrange | |
from re import sub | |
from time import sleep | |
def update(lineno: int, pattern: str, repl: str) -> None: | |
"""Update part of this file by given pattern and replacement.""" | |
with open(__file__, "r") as f: | |
lines = f.readlines() | |
lines[lineno - 1] = sub(pattern, repl, lines[lineno - 1]) | |
with open(__file__, "w") as f: | |
f.writelines(lines) | |
def main(): | |
"""Regularly update the integer of line no.20 in this file.""" | |
while True: | |
print(0) # integer here will be updated | |
sleep(1) | |
update(20, r"\d+", str(randrange(10))) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment