Skip to content

Instantly share code, notes, and snippets.

@astropenguin
Created January 18, 2023 16:03
Show Gist options
  • Save astropenguin/91d590cf1497a19ce4960f620be68ece to your computer and use it in GitHub Desktop.
Save astropenguin/91d590cf1497a19ce4960f620be68ece to your computer and use it in GitHub Desktop.
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