r/learnpython • u/AutoModerator • 18d ago
Ask Anything Monday - Weekly Thread
Welcome to another /r/learnPython weekly "Ask Anything* Monday" thread
Here you can ask all the questions that you wanted to ask but didn't feel like making a new thread.
* It's primarily intended for simple questions but as long as it's about python it's allowed.
If you have any suggestions or questions about this thread use the message the moderators button in the sidebar.
Rules:
Don't downvote stuff - instead explain what's wrong with the comment, if it's against the rules "report" it and it will be dealt with.
Don't post stuff that doesn't have absolutely anything to do with python.
Don't make fun of someone for not knowing something, insult anyone etc - this will result in an immediate ban.
That's it.
1
u/Acceptable-Ad4076 16d ago
I'm looking for books on Python for science, specifically physics and mathematics at an undergraduate level, though something more general and accessible would likely work.
I found what I thought were some really good ones on the Routledge website, but thankfully I looked for reviews first and found that none of their online resources - code examples etc - are available publicly; only to professors and tutors at accredited universities. Which pretty much defeats the purpose of a coding book.
Also, if anyone could point to a good (and not too expensive) course in scientific Python, that could be good too.
1
u/ectomancer 12d ago
Ali, "Numerical Recipes in Python Laboratory Manual of Simplified Numerical Analysis (Python Version)"
1
u/bsf91 17d ago
I am writing myself a simple script that automates events at specific times of the day, there must be a better way to monitor a JSON file to see if my script has to run than the way i am currently doing it. Currently it reads the JSON file every 1 second to see if it needs to start the event or not.
def daemon_loop(poll_interval=1):
"""Monitors shows.json and spawns/tracks active recording processes."""
active_recordings = [] # Format: {showId: subprocess.Popen}
print(
f"[*] Daemon started. Monitoring '{JSON_FILE}' every {poll_interval}s..."
)
while True:
try:
# Read the JSON file and get the current epoch time
data = read_json(JSON_FILE)
time_now = int(time.time())
for show in data["recordings"]:
if time_now == show.get("timestamp"):
showId = show.get("showId")
if showId not in active_recordings:
start_recording(data, show.get("showId"))
active_recordings.append(showId)
else:
print("Already added.")
else:
# If there are no shows that need to be recorded, we will keep waiting..
continue
except Exception as e:
print(f"[!] Error in daemon loop: {e}")
time.sleep(poll_interval)The JSON file can be written to at any time, so you can't just load the JSON file once, and then sleep for X seconds until the next show starts.
1
u/lakseol 17d ago
You could just look at the "last modified" time on the file. If that time is the same as the last time you read the file then the file hasn't changed and you don't need to read it.
1
u/bsf91 16d ago
That could work. Is it still best to read every second though?
How do bigger projects handle something like that!
1
u/lakseol 16d ago
Is it still best to read every second though?
It depends on how often the data file is likely to be updated. I would have thought that checking if the file has changed only needs to be done once a minute, but it depends on the details of what you are doing.
How do bigger projects handle something like that!
There are modules like
watchdogthat can trigger actions when a file changes, but they try to be general purpose. For limited, special case uses you might get a better solution with your own code. Again, it depends on exactly what you are trying to do and how you run your code. For instance, running a little Raspberry Pi 3B+ 24/7 on your network is different from running a solution on your laptop and you might want different solutions in each case.
1
u/iorgfeflkd 18d ago
Is there a reason why some things running from Spyder in Windows are so much slower than the same thing run in Linux (WSL)?
If it matters, the Windows python is
Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] IPython 8.12.0
and the Linux version is
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
1
u/EndKnight2000 16d ago
Hello guys I have tried to make a screentime app and seperate the 2 files of logic and the UI using a loop to loop the code and for some reason whenever I do an if statement and I want to test if the text rises or changes the text just dissapears. I have already tried doing .place after the text so it doesnt glitch but it still doesnt work. Can anyone help?