|
I have a small discrete event simulation which I wrote to solve this problem. I was wondering if anyone could give me some feedback on the way I wrote it, especially if there are bad practices in there regarding how to structure code, things you would do differently and why, etc. The code can be found here. Thanks.
|
# ? Feb 12, 2014 03:39 |
|
|
# ? Jun 1, 2024 17:47 |
|
If you're writing a class, what is the preferred way to handle exceptions that may be thrown by other classes that you depend on? For example: Python code:
I'm leaning towards catching and re-throwing, because any application that utilizes doohickey does not need to import other stuff in order to catch exceptions from doohickey. Edit: loving gevent is breaking our stack. Monkey patching breaks a bunch of stuff we want to use *sigh* My Rhythmic Crotch fucked around with this message at 04:55 on Feb 12, 2014 |
# ? Feb 12, 2014 04:46 |
|
I don't normally catch and re-throw exceptions unless they happen to have really poorly written or really confusing (given the context) error messages. Explicit is better than implicit and so on. Also if there's an exception from one of my dependencies then it probably means I'm doing something wrong, not that my user is doing something wrong.
|
# ? Feb 12, 2014 05:35 |
|
My Rhythmic Crotch posted:If you're writing a class, what is the preferred way to handle exceptions that may be thrown by other classes that you depend on?
|
# ? Feb 12, 2014 05:44 |
|
Is there a way to re-throw an exception in a way that references the original exception-raising line?
|
# ? Feb 13, 2014 00:09 |
|
QuarkJets posted:Is there a way to re-throw an exception in a way that references the original exception-raising line? A bare raise is supposed to re-throw the exception without altering it I think. If not, there's sys.exc_info(), which returns a tuple of (type, value, traceback), which you can re-raise directly with python's long-form raise statement code:
I have only once found it useful, and that was for this abomination.
|
# ? Feb 13, 2014 01:33 |
|
QuarkJets posted:Is there a way to re-throw an exception in a way that references the original exception-raising line? Python code:
|
# ? Feb 13, 2014 02:29 |
|
Lysidas posted:
|
# ? Feb 13, 2014 04:22 |
|
Misogynist posted:I did not know this syntax existed despite working with Python since ~2004 Well that's understandable, since that syntax didn't exist in 2004. It was added in 3.0 and is not available in Python 2
|
# ? Feb 13, 2014 04:40 |
|
Aha, I asked in here a while ago about how to find the CPU temperature with Python and it was too difficult to wade through the bullshit of Windows to do it. Turns out there's a program called Open Hardware Monitor that fetches all the info for you and publishes it into a WMI namespace which you can easily parse with the WMI module. Now I can send it out over USB to my Arduino Python code:
|
# ? Feb 13, 2014 05:40 |
|
Python docs posted:Note It is strongly advised that you do not add any handlers other than NullHandler to your library’s loggers. This is because the configuration of handlers is the prerogative of the application developer who uses your library. The application developer knows their target audience and what handlers are most appropriate for their application: if you add handlers ‘under the hood’, you might well interfere with their ability to carry out unit tests and deliver logs which suit their requirements. Is it not possible to specify a default logging config on a module level, and not have it interfere with the global logging configuration?
|
# ? Feb 14, 2014 00:39 |
|
stderr/stdout is a global resource. Libraries should never take over global resources.
|
# ? Feb 14, 2014 00:47 |
|
e: nm, realized that this isn't right cause I wasn't doing MVP correctly anyway
KICK BAMA KICK fucked around with this message at 03:01 on Feb 15, 2014 |
# ? Feb 14, 2014 05:16 |
|
What's the preferred way to install a bash alias or system script alongside your package (presumably in setup.py)? I found myself running python -m mymodule a bunch of times so I made a bash alias. It would make things similar if setup.py could do this. Presumably there's an easy way as I think I've come across other packages doing this but I can't find the right terms to search for.
|
# ? Feb 14, 2014 06:09 |
|
One of the args to setup() is a list of scripts that will be copied to /usr/local/bin (or somewhere similar). You can put whatever there; it doesn't have to be a python script.
|
# ? Feb 14, 2014 06:21 |
|
Are you referring to this? The documentation is a bit sparse here. It doesn't say that it's copied somewhere but this is probably it. Time to experiment... E: It's much simpler. The option is called entry_points. SurgicalOntologist fucked around with this message at 07:38 on Feb 14, 2014 |
# ? Feb 14, 2014 07:12 |
|
The one I use is scripts, which seems to work fine. I don't know what entry_points does.
|
# ? Feb 14, 2014 09:10 |
|
So I finally finished my first PySide application, but it occasionally likes to crash after exiting (as a Windows "python has stopped working" error), it's completely random, seems to happen more often when I open and close it quickly but it's otherwise impossible to reproduce. How would I go about figuring out what the gently caress is going on? I've read this https://stackoverflow.com/questions/11945183/what-are-good-practices-for-avoiding-crashes-hangs-in-pyqt but I have no idea where to start. I don't use threading, but I do use a QTableView which I'm pretty sure is involved in a way.
|
# ? Feb 14, 2014 12:04 |
|
The faulthandler module is designed for exactly this purpose -- you can enable it and give a file handle as the file argument, and it'll dump a traceback if the interpreter crashes. I've found it very useful in debugging extension modules written in C, since the traceback actually includes function calls in native code and it's far more helpful than "Segmentation fault (core dumped)" as the only output of the program.
|
# ? Feb 14, 2014 14:20 |
|
Daynab posted:So I finally finished my first PySide application, but it occasionally likes to crash after exiting (as a Windows "python has stopped working" error), it's completely random, seems to happen more often when I open and close it quickly but it's otherwise impossible to reproduce.
|
# ? Feb 14, 2014 15:57 |
|
Daynab posted:So I finally finished my first PySide application, but it occasionally likes to crash after exiting (as a Windows "python has stopped working" error), it's completely random, seems to happen more often when I open and close it quickly but it's otherwise impossible to reproduce. Yeah I would definitely look into the logging module and begin using it. To solve the problem right now, you might consider wrapping your entry point of the code in a try, except. code:
While the global try catch is frowned upon, this will show you what is causing the problem, and you can fix it while adding more logging into your program.
|
# ? Feb 14, 2014 16:33 |
|
evensevenone posted:The one I use is scripts, which seems to work fine. I don't know what entry_points does. Seems like scripts is for installing scripts you've already written, entry_points is for automatically creating a script that points to some function in your package.
|
# ? Feb 14, 2014 16:37 |
|
Innocent Bystander posted:this will show you what is causing the problem I disagree. Python exceptions don't cause Windows to tell you "python has stopped working" or segfault the interpreter on Unix systems. Crashing in native code precludes the raising of a Python exception, so the except clause that you propose would never be reached. EDIT: except Exception, e: is invalid syntax too
|
# ? Feb 14, 2014 16:39 |
|
Lysidas posted:EDIT: except Exception, e: is invalid syntax too It's merely deprecated in python 2.
|
# ? Feb 14, 2014 17:19 |
|
Plorkyeran posted:It's merely deprecated in python 2. Exactly. Hence, it was valid and now it's invalid Unrelated: I think I can count how many times I've been disappointed with the Python standard library on one hand. Most of those times are related to ancient modules whose interface can use some improvements, like tarfile. It's rare that I'm unhappy about something recent, like the addition of an exist_ok parameter to os.makedirs. This was added in 3.2 and seems like it would be nice to use, except that it still throws a FileExistsError (OSError in 3.2) if the directory exists and has different permissions than what makedirs would have created. There is no way to override this behavior. There was a patch for this proposed after 3.2 was released, which attempted to add a strange on_wrong_mode parameter taking exactly one of two integer values: FAIL = 0, IGNORE = 1. I think it would make a lot more sense to add another boolean argument along the lines of ignore_mode or something like that, but this won't even make it into 3.4. I wrote a simple makedirs_exist_ok wrapper function a long time ago, and I was looking forward to getting rid of it. Guess I'll have to keep using it for the indefinite future.
|
# ? Feb 14, 2014 18:39 |
|
i wrote a really stupid tail call optimizer in python this afternoon. in my defense, i had nothing else to do and also wanted to play around with the AST module a bit. i will combine this with my stupid-er currying function to finally make python the slow functional programming language it was never meant to be.
|
# ? Feb 14, 2014 22:47 |
|
Daynab posted:So I finally finished my first PySide application, but it occasionally likes to crash after exiting (as a Windows "python has stopped working" error), it's completely random, seems to happen more often when I open and close it quickly but it's otherwise impossible to reproduce. If it's Windows crashing, your best bet (unfortunately) is to install the Windows debugging tools (namely, WinDbg) and get a stack trace, and pray to god something jumps out at you. I had to do this for the first time a few weeks ago to track down a crash in 64-bit builds of my PyQt application, and it turned out to be a fault in the backup software my company uses (Druva inSync) injecting itself into filesystem queries and loving up the handle returned to Qt (both PyQt and PySide). That was fun.
|
# ? Feb 15, 2014 01:05 |
|
Lysidas posted:I think I can count how many times I've been disappointed with the Python standard library on one hand. I wish they'd add a prod function to go with sum. As far as I can tell the rationale for not having one is that no one would have a use for it. I would use it
|
# ? Feb 15, 2014 01:57 |
|
Lysidas posted:Unrelated: urllib2, urllib, httplib, socket, urlllib2
|
# ? Feb 15, 2014 02:58 |
|
Tkinter is also a bit disappointing.
|
# ? Feb 15, 2014 05:46 |
|
concurrent.futures is dumb because it wasn't a well-tested, in-the-field library and everybody on the mailing list was talking what to name it.
|
# ? Feb 15, 2014 06:17 |
|
pretty psyched that as a long time user of configobj for my configuration file needs, I was able to help get it python 3 after it had be lying untouched for the last 3 years. I'm now one of the new maintainers and we did the full 2014 python open source makeover: source was on google docs, now on github docs were on the original maintainers blog, now on readthedocs I don't think there was any specific build process in place, but it's using tox and travis to guarantee single source compatibility with 2.6, 2.7, 3.2, and 3.3 next step, replace the thousands of doctests with something, anything else but we just release version 5.0.0. Feels good.
|
# ? Feb 15, 2014 07:22 |
|
Lysidas posted:I think I can count how many times I've been disappointed with the Python standard library on one hand. The datetime module needs timezone support, like tzinfo/dateutil.
|
# ? Feb 15, 2014 14:12 |
|
Nippashish posted:I wish they'd add a prod function to go with sum. As far as I can tell the rationale for not having one is that no one would have a use for it. I would use it reduce+operator.mul?
|
# ? Feb 15, 2014 17:26 |
|
fritz posted:reduce+operator.mul? Good point, we should probably get rid of sum in that case; people can use reduce+operator.add and be grateful for it.
|
# ? Feb 15, 2014 17:32 |
|
Lurchington posted:pretty psyched that as a long time user of configobj for my configuration file needs, I was able to help get it python 3 after it had be lying untouched for the last 3 years. I'm now one of the new maintainers and we did the full 2014 python open source makeover: This is pretty cool and I'm glad you're stepping up to help out with stuff
|
# ? Feb 15, 2014 18:31 |
|
Nippashish posted:Good point, we should probably get rid of sum in that case; people can use reduce+operator.add and be grateful for it. I don't particularly think a*b*c is that great of an API to be in the standard library by default. You have to fight over an API: what should prod("foo", 5) do? Is it as simple as a*b, so it would return "foofoofoofoofoo"[/fixed}? Or should it be restricted to numbers only, like [fixed]sum() is? What about numpy? Decimal and Fraction classes? SymPy? Will prod(["foo", 5]) work, or to multiply a list of numbers together, should you do prod(*my_list)? Both? If both, what does prod(["foo", 5], 5) do? Somebody has to document it. You have to triple-check that adding a new name to the globals doesn't break any existing code. Once you decide on a set of edge cases, you have to test them. Adding another just global increases cognitive load, and discourages exploration and play: dir() and help() become longer. There's bound to be a bug. Who will fix it? Who will make the test for every fix? Just write a method to call a*b*c.
|
# ? Feb 15, 2014 19:25 |
|
Suspicious Dish posted:Just write a method to call a*b*c. Do you think having sum in the standard library was a mistake?
|
# ? Feb 15, 2014 19:32 |
|
ffs just implement prod in terms of sum as math.exp(sum(math.log(value) for value in iterable))
|
# ? Feb 15, 2014 19:42 |
|
|
# ? Jun 1, 2024 17:47 |
|
Just write a library that spins up a hadoop cluster to do your prod operation via mapreduce, then make it part of the standard library.
|
# ? Feb 15, 2014 19:49 |