|
Gobbeldygook posted:I am a coding newbie working through Learn Python The Hard Way. For exercise 36 he says to make a text-based adventure game. I decided to add quicktime events, which requires timed input. I found a complicated, Windows-only method on reddit and it works fine, but I decided to try to make the seemingly simple crossplatform solution work just for it's own sake. Here is what I have: The easiest way to fix this would just be to reset t to a new Timer object, like code:
|
# ? Jul 19, 2021 03:20 |
|
|
# ? May 14, 2024 02:53 |
|
Gobbeldygook posted:I am a coding newbie working through Learn Python The Hard Way. For exercise 36 he says to make a text-based adventure game. I decided to add quicktime events, which requires timed input. I found a complicated, Windows-only method on reddit and it works fine, but I decided to try to make the seemingly simple crossplatform solution work just for it's own sake. Here is what I have: To elaborate on Deadite's answer, threading.Timer objects inherit from threading.Threads, which cannot be start()ed more than once. So you can create new objects as suggested above (and/or use the RepeatableTimer solution in that SO link) instead of cancel()ing them, or for extra credit, use time.time() and a loop to make your own timer.
|
# ? Jul 19, 2021 04:14 |
Please please learn from anything other than Learn Python the Hard Way. Author doesn't even understand python 3 properly.
|
|
# ? Jul 19, 2021 09:39 |
|
Deadite posted:The easiest way to fix this would just be to reset t to a new Timer object, like code:
quote:You take one out and engage the other! You have 5 seconds per move A bigger problem is that the timer going off and triggering the dead() function doesn't actually prevent the player from just ignoring the error, putting in the correct move, and continuing like nothing happened. King Cocoa Butter posted:To elaborate on Deadite's answer, threading.Timer objects inherit from threading.Threads, which cannot be start()ed more than once. So you can create new objects as suggested above (and/or use the RepeatableTimer solution in that SO link) instead of cancel()ing them, or for extra credit, use time.time() and a loop to make your own timer. After some fumbling I figured out how to make my own timer with time.time(), that's neat. I did some further searching and found a simple module that does exactly what I want and then some via the time.time() method, so I'm just going to use his work. NinpoEspiritoSanto posted:Please please learn from anything other than Learn Python the Hard Way. Author doesn't even understand python 3 properly.
|
# ? Jul 20, 2021 00:07 |
|
NinpoEspiritoSanto posted:Please please learn from anything other than Learn Python the Hard Way. Author doesn't even understand python 3 properly. I know people had some opinions about Zed Shaw, mostly because he has a lot of opinions that he can't seem to keep his mouth shut about, but what about the book is improper re: python 3? I learned my basics from the Python 2 version and flipped through the third, and at the time nothing stood out to me.
|
# ? Jul 20, 2021 00:13 |
|
Bit of a newbie to python and regressors... so I’m running nested loops with scikit-learn to iterate through parameters on training a Gradient Tree Boosted Regressor and saving the “high score”. Dataset is 6,000 samples with 25 features. The loop has been running for 2 days. If I interrupt the kernel, will it dump the values for the current block or will it still save my current “high score” for the loop? Also, yes, I know about overfitting but I don’t know enough to train a Regressor manually on a Dataset yet. The Decision Tree Regressor ran 5400 times and took about 20 minutes. There are 94500 iterations in the Gradient Tree Boosted Regressor nested loops. Does this sound like days or years here?
|
# ? Jul 20, 2021 05:38 |
Epsilon Plus posted:I know people had some opinions about Zed Shaw, mostly because he has a lot of opinions that he can't seem to keep his mouth shut about, but what about the book is improper re: python 3? I learned my basics from the Python 2 version and flipped through the third, and at the time nothing stood out to me. It's just not good learning material, concepts are often taught out of order, a lot is outdated or just not pythonic and it's all rote style. Think Python is far superior as a beginner's resource and actually explains programming concepts as well as how they apply with Python. I can dig up more specifics if you like when not phone posting but it's been a while since it was generally dropped as a recommended starting point and considered shite.
|
|
# ? Jul 20, 2021 08:17 |
|
What's the latest simple API for generating a HTTP service with Swagger spec? Falsy looks pleasantly minimal but hasn't updated in 4 years.
|
# ? Jul 20, 2021 19:32 |
|
MrMoo posted:What's the latest simple API for generating a HTTP service with Swagger spec? Falsy looks pleasantly minimal but hasn't updated in 4 years. FastAPI is the currently talked about quick to make python API and swagger UI is included by default. I havent used it but have been meaning to for a while.
|
# ? Jul 20, 2021 20:30 |
|
CarForumPoster posted:FastAPI is the currently talked about quick to make python API and swagger UI is included by default. I havent used it but have been meaning to for a while. This actually looks neat, although my only exposure to it is limited, and the API that my offshore peers put together with FastAPI is not very stable. It's not really fair to blame FastAPI itself at this point, might point fingers at the offshore team instead, but that's neither here nor there anyway.
|
# ? Jul 20, 2021 22:13 |
I've done stuff with FastAPI it's pretty good
|
|
# ? Jul 20, 2021 23:25 |
|
NinpoEspiritoSanto posted:I've done stuff with FastAPI it's pretty good I deployed an unauthenticated hello world API to heroku with FastAI just now in about 10 minutes. Gonna try adding auth, if this works it might just be when I use for the backend I need to build.
|
# ? Jul 21, 2021 00:20 |
|
CarForumPoster posted:I deployed an unauthenticated hello world API to heroku with FastAI just now in about 10 minutes. Gonna try adding auth, if this works it might just be when I use for the backend I need to build. I came back to it to see if it was easy enough to do API keys. There was a blog about it. And I got that deployed by basically copy pasting and changing the API code. This combined with coming up with some dependencies I could get smaller versions of means I can likely put my deep learning app's back end on Heroku, simply downloading the large model(s) from S3!
|
# ? Jul 21, 2021 03:51 |
|
CarForumPoster posted:FastAPI is the currently talked about quick to make python API and swagger UI is included by default. I havent used it but have been meaning to for a while. Thanks, surprisingly easy, even with almost zero Python knowledge. Trying to get Python do everything async is hilarious.
|
# ? Jul 21, 2021 22:25 |
Trio takes a lot of pain out of that for general python, but so does FastAPI here since you can easily perform async tasks staying within the framework's idioms.
|
|
# ? Jul 21, 2021 22:57 |
|
I have written a function in Python named load_clustering. I have used this function in a number of places in other code. I now realise that a more appropriate name for this function is load_classification. Is there a way to modify the name of the function so that from now on I can use it as load_clustering, but keep the old name as well so that the older code with the old name of load_clustering does not break?
|
# ? Jul 26, 2021 19:12 |
|
Jose Cuervo posted:I have written a function in Python named load_clustering. I have used this function in a number of places in other code. I now realise that a more appropriate name for this function is load_classification. Is there a way to modify the name of the function so that from now on I can use it as load_clustering, but keep the old name as well so that the older code with the old name of load_clustering does not break? Pycharm has refactoring options that I'd bet cover this use case by simply replacing that function where called.
|
# ? Jul 26, 2021 19:16 |
|
CarForumPoster posted:Pycharm has refactoring options that I'd bet cover this use case by simply replacing that function where called. I would normally do that but another person is using the code as well, and for the sake of argument assume I cannot get them to refactor the code which they are using.
|
# ? Jul 26, 2021 19:22 |
|
Jose Cuervo posted:I have written a function in Python named load_clustering. I have used this function in a number of places in other code. I now realise that a more appropriate name for this function is load_classification. Is there a way to modify the name of the function so that from now on I can use it as load_clustering, but keep the old name as well so that the older code with the old name of load_clustering does not break? Assuming this is exactly what you want, you can just do load_clustering = old_func_name and use/import the new name. Functions are just objects that you can create new references to like most other objects. I would recommend refactoring, which could be potentially as easy as a find/replace but is indeed made more easy/reliable with refactoring tools like PyCharm or other IDEs have.
|
# ? Jul 26, 2021 19:23 |
Submit a pull request to the other person’s code?
|
|
# ? Jul 26, 2021 19:25 |
|
Jose Cuervo posted:I would normally do that but another person is using the code as well, and for the sake of argument assume I cannot get them to refactor the code which they are using. Can you not just work with the other person to have like a cutoff day for the old function name?
|
# ? Jul 26, 2021 19:27 |
|
Okay, it seems like the right thing to do is refactoring and getting the other person to start using the new code. Will do. Thanks.
|
# ? Jul 26, 2021 19:31 |
|
OnceIWasAnOstrich posted:Assuming this is exactly what you want, you can just do load_clustering = old_func_name and use/import the new name. Functions are just objects that you can create new references to like most other objects. I would recommend refactoring, which could be potentially as easy as a find/replace but is indeed made more easy/reliable with refactoring tools like PyCharm or other IDEs have. In the case where for whatever reason it's difficult/impossible to get the existing calls refactored it's not super uncommon to leave the old function in place in some form (even if that's just basically calling the new function) and have it emit a deprecation warning for an appropriate amount of time.
|
# ? Jul 27, 2021 00:09 |
|
My program writes a bunch of output files I open using WITH. If the program crashes due to an exception or interrupt (Ctrl+c) I'd rather erase all the files than have partially written output. What's the bast way to handle these cases? Since my program creates a new output directory do I just delete the output directory in the signal and exception handlers then exit?
|
# ? Jul 28, 2021 21:34 |
Making an assumption that the partially written output is not like, a security risk or something, my inclination would be to have the output directory be named with a temp-file prefix of some sort and then whenever you run the script it cleans up all pre-existing temp directories. Then if it successfully completes, it renames the folder to a "good" name.
|
|
# ? Jul 28, 2021 21:39 |
I'd have a clean up function in your exception handling. Nothing worse than a program that leaves random useless poo poo laying around because it crashed especially if the data is sizeable and someone else is wondering what it is 6 months later
|
|
# ? Jul 28, 2021 22:59 |
|
Soylent Pudding posted:My program writes a bunch of output files I open using WITH. If the program crashes due to an exception or interrupt (Ctrl+c) I'd rather erase all the files than have partially written output. What's the bast way to handle these cases? Since my program creates a new output directory do I just delete the output directory in the signal and exception handlers then exit? Yep, and python makes that pretty easy: https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions
|
# ? Jul 29, 2021 03:21 |
|
CarForumPoster posted:Yep, and python makes that pretty easy: https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions Note that this always gets executed, so unless you move your correctly written files, it'll happily delete those without an exception as well.
|
# ? Jul 29, 2021 11:08 |
|
Do I have to go back and change everyone else's code from using "with" to "try-finally" for every file write or is there a similar final functionality I can use with the with syntax?
|
# ? Jul 29, 2021 13:41 |
|
Soylent Pudding posted:Do I have to go back and change everyone else's code from using "with" to "try-finally" for every file write or is there a similar final functionality I can use with the with syntax? Yea, how many with statements we talkin? Is the code conducive to making a function then just passing that function your params?
|
# ? Jul 29, 2021 13:55 |
|
Maybe a dozen or so minimum. Without getting into too many details since it's a work thing we're taking a bunch of tiny helper diagnostic scripts people have written and writing a central controller to manage the whole library and deploy them as narrowly or widely as needed. Could I do what I want with: code:
|
# ? Jul 29, 2021 14:22 |
|
If you’re doing that you might as well write a new context manager that deletes the tempfile on completion. Phone posting so can’t provide a snippet, but could post one later if no one else obliges in the meantime…
|
# ? Jul 29, 2021 14:55 |
|
Yeah if you just want to guarantee that something gets done, even if an exception gets raised, that's already a core feature of context managers. So the simpler change would be to modify your context manager, not to rip out a dozen context blocks and replace them with try/finally blocks. More generally, if you were about to write a bunch of near-identical try/except/finally blocks, I would argue that you should define a context manager instead
|
# ? Jul 31, 2021 01:44 |
|
QuarkJets posted:Yeah if you just want to guarantee that something gets done, even if an exception gets raised, that's already a core feature of context managers. So the simpler change would be to modify your context manager, not to rip out a dozen context blocks and replace them with try/finally blocks. I guess my homework assignment is learning about the context manager.
|
# ? Jul 31, 2021 22:15 |
|
the context manager is pretty easy and is a great python feature, you basically just define __enter__ and __exit__ methods on your class. __enter__ should return the thing that you want assigned as the result of your "as", __exit__ happens when your program leaves the scope of your "with". your __exit__ needs to have 4 params because the python runtime will pass those params in an exit block, they're for handling return codes and stuff like that temp file access is gnarly which is probably why nobody posted a code sample. here's a really lovely one: Python code:
|
# ? Aug 2, 2021 20:25 |
|
Thanks! This is definitely a part of python I know almost nothing about. My CS degree focused on C++ and I've spent the past few years working in IT not as a developer.
|
# ? Aug 2, 2021 20:37 |
|
Sure thing, keep in mind that your OS might have a convention for temp files as well, for example /tmp on a linux/unix system is often a tmpfs which resides in virtual memory and doesn't survive reboots. e: OnceIWasAnOstrich posted:You could also use the built-in tempfile TemporaryDirectory which comes pre-built as a context manager and move the files out of the directory once they are successfully written. This also has the advantage of automatically choosing a platform-dependent location by default. this is badass and yeah you should totally use this instead of writing your own thing. python standard library is pretty good 12 rats tied together fucked around with this message at 20:54 on Aug 2, 2021 |
# ? Aug 2, 2021 20:47 |
|
You could also use the built-in tempfile TemporaryDirectory which comes pre-built as a context manager and move the files out of the directory once they are successfully written. This also has the advantage of automatically choosing a platform-dependent location by default. You can also do this on a file-by-file basis with TemporaryFile context managers but the directory seemed more appropriate for the original request. As mentioned, this is probably going to be better and handle edge cases more effectively than anyone is this thread could manage on their first try. OnceIWasAnOstrich fucked around with this message at 20:58 on Aug 2, 2021 |
# ? Aug 2, 2021 20:50 |
|
Soylent Pudding posted:Thanks! This is definitely a part of python I know almost nothing about. My CS degree focused on C++ and I've spent the past few years working in IT not as a developer. Just know that __exit__ is roughly equivalent to finally (i believe), it always gets called even if an exception is raised. That's the real beauty of a context manager, it lets you encapsulate your exit behavior and leaves behind a nice and concise statement that is reusable
|
# ? Aug 2, 2021 21:54 |
|
|
# ? May 14, 2024 02:53 |
|
Already kicking myself. I've got 4 kids and sometimes I want to randomly pick one for something, or randomize the order they do things. I typically use a d4 for this...but I just thought "hey, I'm learning Python, why not build an app for it"? Android development on Windows looks like fun! All seriousness though i adore a challenge so...any advice is more than welcome.
|
# ? Aug 2, 2021 21:58 |