|
ShoulderDaemon posted:But correct code, in general, must always handle errors using exceptions, because it is impossible to detect error conditions before actually attempting whatever action. Even in this case, what is best depends on the particulars of the situation. If it is not possible to recover from the case where the file cannot be used, then I would say the "correct" thing to do is simply to let any exceptions escape, failing quickly and loudly.
|
# ? Aug 31, 2013 05:17 |
|
|
# ? Jun 13, 2024 03:20 |
|
BigRedDot posted:This particular example has a race condition, but many things can be tested for with certainty, especially in single threaded applications. Forgive me for not having in-depth knowledge of issues surrounding working with filesystems, but is it not possible that an entirely separate program might be manipulating the filesystem at the same time as the application in question? And if so, the actions attempted by the application (even if they are very predictable considered in isolation) might fail unpredictably?
|
# ? Aug 31, 2013 14:46 |
|
Hammerite posted:Forgive me for not having in-depth knowledge of issues surrounding working with filesystems, but is it not possible that an entirely separate program might be manipulating the filesystem at the same time as the application in question? And if so, the actions attempted by the application (even if they are very predictable considered in isolation) might fail unpredictably? Not everything that throws an exception touches the filesystem; accessing past the end of an array generally throws an exception, but you can check the array length beforehand.
|
# ? Aug 31, 2013 15:59 |
|
yaoi prophet posted:Not everything that throws an exception touches the filesystem; accessing past the end of an array generally throws an exception, but you can check the array length beforehand. Yes, I know that. Forgive me, but I don't understand what this comment is in aid of. It seems like a non sequitur.
|
# ? Aug 31, 2013 17:47 |
|
Hammerite posted:Yes, I know that. Forgive me, but I don't understand what this comment is in aid of. It seems like a non sequitur. I was clarifying BigRedDot's comment that "many things can be tested for with certainty, especially in single threaded applications". Out-of bounds array accesses are one such thing; divide-by-zero exceptions are another.
|
# ? Aug 31, 2013 17:56 |
|
The point of exceptions is that the code that needs to throw exception shouldn't necessarily have to be the code deals with the exception. You can throw it and let it get caught by whatever point in the call stack it makes the most sense to handle it. edit: For example, in the divide-by-zero case, lets say you have a function that for whatever reason just divides two numbers. If it sees it gets a zero divisor, what's the right course of action? What should it return? There is no correct answer (although I guess with python you could return None). It's better to just throw the exception and let whatever called it sort it out. evensevenone fucked around with this message at 22:43 on Aug 31, 2013 |
# ? Aug 31, 2013 22:38 |
|
Hammerite posted:Forgive me for not having in-depth knowledge of issues surrounding working with filesystems, but is it not possible that an entirely separate program might be manipulating the filesystem at the same time as the application in question? And if so, the actions attempted by the application (even if they are very predictable considered in isolation) might fail unpredictably? Yes, anything that deals with files should probably be assumed to have potential race conditions. I did say many things, not all things.
|
# ? Sep 2, 2013 17:10 |
|
So, I mainly know and work with scientific computation using the pylab and numpy packages. I'd like to branch out. What are some interesting and possibly related packages I could learn?
|
# ? Sep 2, 2013 19:18 |
|
the posted:So, I mainly know and work with scientific computation using the pylab and numpy packages. I'd like to branch out. What are some interesting and possibly related packages I could learn? I think the question that you should ask yourself is what I am interested in doing, rather than what is available. That being said, I have wanted to find an excuse you try the scikit-learn package for a while now. http://scikit-learn.org/stable/
|
# ? Sep 2, 2013 19:29 |
|
the posted:So, I mainly know and work with scientific computation using the pylab and numpy packages. I'd like to branch out. What are some interesting and possibly related packages I could learn? You could mess with more specific packages like scipy and matplotlib, and then maybe move into PyQt. This would be a reasonable way to go if you just want to learn some new things
|
# ? Sep 2, 2013 19:52 |
|
the posted:So, I mainly know and work with scientific computation using the pylab and numpy packages. I'd like to branch out. What are some interesting and possibly related packages I could learn? You would probably find something interesting in pandas.
|
# ? Sep 2, 2013 21:28 |
|
If you are interested in scientific visualizatio, we will be releasing a 0.1 of Bokeh this week. It's a python library for interactive brower-based visualizations (including directly inline in ipython notebooks). It's model is graphical primitives vectorized over data, inspired by the Grammar of Graphics (similar to ggplot). It's also going to integrate some fairly cutting edge and awesome research from our collaborator Joseph Cottam (who wrote the papers on Stencil) for abstract rendering to do intelligent things when you have more points than you can naively plot. You can see some live plot examples on the page for the browser frontend bokehjs but hopefully we will have a gallery up on on the python Bokeh project page very soon. As the 0.1 implies, this is an early release, but the good news is development of this open-source project also has funding behind it so development will continue apace for the foreseeable future.
|
# ? Sep 3, 2013 02:32 |
|
BigRedDot posted:You can see some live plot examples on the page for the browser frontend bokehjs but hopefully we will have a gallery up on on the python Bokeh project page very soon. As the 0.1 implies, this is an early release, but the good news is development of this open-source project also has funding behind it so development will continue apace for the foreseeable future. Wow that's pretty cool. Can I integrate the package into Canopy?
|
# ? Sep 3, 2013 03:23 |
|
the posted:Wow that's pretty cool. Can I integrate the package into Canopy?
|
# ? Sep 3, 2013 03:29 |
|
Hey goons, I'm making a simple e-mail sending app with Tkinter and I'm having a problem:code:
|
# ? Sep 3, 2013 07:51 |
|
TUBALLINATOR posted:Basically the function of sending mail takes a few seconds to run, depending on the size of attachment. The problem is the user can still click on the send button while the function is running (and therefore make the function run again and again), which is horrible and I'm not sure how to prevent it. I don't have any experience with GUI programming, so maybe you guys have a better idea before I come up with a terrible solution. Maybe something like this? code:
|
# ? Sep 3, 2013 08:25 |
|
I've come to the end of the python track on codeacademy. It's a great resource for learning the basics, but I can't help but feel it's missed out a lot. I'm sure I can pick up how to use extra modules for when I need to do something specific, like send emails, but it seems like some of the more core concepts have been missed out. Just a quick look at this list shows that it has missed out iterators, complex numbers, the string format method and unicode strings. It didn't seem to focus for too long on tuples either, but this might just be that there's not much else to know other than they're immutable. Finishing the course has given me a pretty good understanding of most of the core concepts of python and I can definitely go ahead and write some scripts and small applications now, but I can't help but feel that I've missed out on some concepts that could improve my code. If I don't even know these concepts exist, I'd probably end up re-inventing the wheel and producing something worse. Would anyone who has either learned python using codeacademy or is able to have a quick browse of the course contents point me towards what I should be trying next, or any essential things that I would have missed?
|
# ? Sep 3, 2013 13:07 |
|
sharktamer posted:I've come to the end of the python track on codeacademy. It's a great resource for learning the basics, but I can't help but feel it's missed out a lot. I'm sure I can pick up how to use extra modules for when I need to do something specific, like send emails, but it seems like some of the more core concepts have been missed out. Just a quick look at this list shows that it has missed out iterators, complex numbers, the string format method and unicode strings. It didn't seem to focus for too long on tuples either, but this might just be that there's not much else to know other than they're immutable. When I was learning programming with Python, I found that I got a lot better by going through several beginner resources since they seemed to cover areas the others missed. Look at Think Like a Computer Scientist and Learn Python the Hard Way. Also think of a project you want to code and start doing it and Google or ask questions when you get stuck.
|
# ? Sep 3, 2013 15:10 |
|
Thermopyle posted:Also think of a project you want to code and start doing it and Google or ask questions when you get stuck.
|
# ? Sep 3, 2013 15:48 |
|
I'm new to Python. I've been playing around with Pygame writing a virtual midi keyboard. Is this an acceptable way to implement a simple state machine?code:
|
# ? Sep 3, 2013 17:04 |
|
[quote="Germstore" post="419070929"] I'm new to Python. I've been playing around with Pygame writing a virtual midi keyboard. Is this an acceptable way to implement a simple state machine? --snip-- Way more efficient code below. Maybe not efficient but it worked nicely. C++ but the general concept carries over. The use is "if (mouse.ButtonPressed(Mouse::MouseButton::Left)) etc" Jewel fucked around with this message at 04:50 on Sep 4, 2013 |
# ? Sep 3, 2013 17:09 |
|
Germstore posted:I'm new to Python. I've been playing around with Pygame writing a virtual midi keyboard. Is this an acceptable way to implement a simple state machine? I recommend callbacks. Something like: code:
|
# ? Sep 3, 2013 17:34 |
|
Germstore posted:
Just a note, in your code you have self.keyIsHeld which would return the reference to the function self.keyIsHeld. I don't think that this is what you intend to do.
|
# ? Sep 3, 2013 18:20 |
|
accipter posted:Just a note, in your code you have self.keyIsHeld which would return the reference to the function self.keyIsHeld. I don't think that this is what you intend to do. It is. The state shouldn't changed if the previous state is KeyIsHeld and the key is pressed.
|
# ? Sep 3, 2013 18:40 |
|
About moving on from code academy, I'm planning on trying some if the challenges on hackerrank if there are some doable ones. Hopefully they give answers to show what I should have done.
|
# ? Sep 3, 2013 23:28 |
|
Germstore posted:I'm new to Python. I've been playing around with Pygame writing a virtual midi keyboard. Is this an acceptable way to implement a simple state machine? You're being too clever for your own good. Just keep a member variable if the key is pressed, and do the edge trigger when you transition from one to the other: Python code:
|
# ? Sep 4, 2013 00:28 |
|
That is a lot simpler. Thanks.
|
# ? Sep 4, 2013 00:47 |
|
Is object-oriented programming considered unpythonic? Serious question. Professionally I see a lot of modules that import in a pile of methods and some globals for any state they retain. It makes me sad but I wonder if I just have to see this a different way.
|
# ? Sep 5, 2013 04:00 |
|
Hard to say without seeing the code but I'm not a big fan of state in module globals. If you think of the module as a singleton class instance that has methods does that help?
|
# ? Sep 5, 2013 04:32 |
|
Rocko Bonaparte posted:Is object-oriented programming considered unpythonic? Serious question. Professionally I see a lot of modules that import in a pile of methods and some globals for any state they retain. It makes me sad but I wonder if I just have to see this a different way. Not at all. There are a few modules in the standard library that offer an interface like that for convenience. (like random and turtle), but this is done with a default instance of a class that you can also make your own instances of. Some of the popular web frameworks keep the request and response in thread local variables for easy access. I'm not a big fan of that myself, and I wouldn't call it Pythonic, but I guess that's something you could encounter. If you see modules with a lot of global state, I would suspect laziness or incompetence, just as with other languages. That being said, some of the "object-oriented" patterns used in Java would be considered unpythonic. You normally would not have a factory class, or a class that just holds a set of static functions. So coming in from that angle, Python could feel less object-oriented.
|
# ? Sep 5, 2013 04:52 |
|
suffix posted:That being said, some of the "object-oriented" patterns used in Java would be considered unpythonic. You normally would not have a factory class, or a class that just holds a set of static functions. On that token, are there patterns you'd see in Python you wouldn't see in your typical statically-typed enterprise language? (At this point I've given up trying to rationalize what I see a lot here. I think they might just be on drugs.)
|
# ? Sep 5, 2013 06:12 |
|
Rocko Bonaparte posted:On that token, are there patterns you'd see in Python you wouldn't see in your typical statically-typed enterprise language? Unless module is very simple, there really shouldnt be global state in it. Classes arent as ubiquitous as they are in, say, java. Often you can get away with a bunch of functions. Thinks statics in java. If you really need some state, you can nest a bunch of functions as well. But i wouldnt go over board with that. Can we some examples? Maybe anonymize it a litte, if you cant post verbatim.
|
# ? Sep 5, 2013 16:05 |
|
In the current script I'm working on, I'm reading a 37k line file into a string and then using regex on it. It should probably be obvious that using regex on a string this large would take ages, but I haven't worked with files this big before. Does anyone have any suggestions for what I should be doing instead? I can't imagine reading straight from the file would be any quicker.
|
# ? Sep 5, 2013 18:19 |
|
37k lines isn't really that much unless they are really long lines. I would not expect it to be glacial based on what you've said. What exactly are you doing and how long is it taking?
|
# ? Sep 5, 2013 19:01 |
|
Here is the class init, where all the regexing happens:code:
|
# ? Sep 5, 2013 19:13 |
|
So you're just matching the whole line then processing it? I'm pretty sure does the same thing without regexes and easier to read... code:
|
# ? Sep 5, 2013 19:25 |
|
Neither of those struck me as being SLOW (like minutes) and I had a couple minutes, so I generated a 40k line file with some test data that ended up being around 6 megs. The regexp took about 1.4s and the oneliner took 0.05. So depending on your needs/definition of slow, you may want to switch to something more like the second one. I was a little surprised the regexp was that slow actually, but vv.
|
# ? Sep 5, 2013 20:02 |
|
HatfulOfHollow posted:So you're just matching the whole line then processing it? Wow, that's tonnes quicker, thanks a lot. breaks, I think the system I'm testing it on is just a lot less powerful. It is being run 24 times, but that's still a lot slower. This new code is taking 3s rather than 50s, so that's all good. No idea why using regexp would make it so slow though.
|
# ? Sep 5, 2013 21:12 |
|
sharktamer posted:Wow, that's tonnes quicker, thanks a lot. Regex has abitrary length so the matching time is fairly slow, probably Θ(m |Σ|) preprocess and Θ(n+m) | Θ((n−m+1) m) (avg | worst) for matching, in on strings does magical tomfoolery with pointers and performs a modified Boyer-Moore-Horspool search. e: I found where I first read about this: here. deimos fucked around with this message at 22:25 on Sep 5, 2013 |
# ? Sep 5, 2013 22:22 |
|
|
# ? Jun 13, 2024 03:20 |
|
Just in case anyone is interested, I work with the original Pyglet developer in a fairly large game dev company, and he is releasing a successor to it after many years of non-involvement. He doesn't have an account here, but I know it's still fairly popular in the Python world, so I thought at least one of you guys might be interested in looking at his new project. The URL is here: http://pypi.python.org/pypi/bacon/ I'll pass on any suggestions to him directly
|
# ? Sep 6, 2013 16:04 |