Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
BigRedDot
Mar 6, 2008

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.
This particular example has a race condition, but many things can be tested for with certainty, especially in single threaded applications.

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.

Adbot
ADBOT LOVES YOU

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

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?

Opinion Haver
Apr 9, 2007

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.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

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.

Opinion Haver
Apr 9, 2007

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.

evensevenone
May 12, 2001
Glass is a solid.
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

BigRedDot
Mar 6, 2008

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.

the
Jul 18, 2004

by Cowcaster
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?

accipter
Sep 12, 2003

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/

QuarkJets
Sep 8, 2008

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

FoiledAgain
May 6, 2007

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.

BigRedDot
Mar 6, 2008

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.

the
Jul 18, 2004

by Cowcaster

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?

BigRedDot
Mar 6, 2008

the posted:

Wow that's pretty cool. Can I integrate the package into Canopy?
I don't have any experience with Canopy. However Bokeh has a standard setup.py so as long as Canopy is compatible with that, I would suppose so. There will be an conda package available along with the release, however.

Gmaz
Apr 3, 2011

New DLC for Aoe2 is out: Dynasties of India
Hey goons, I'm making a simple e-mail sending app with Tkinter and I'm having a problem:

code:
root = Tk()

main_screen = Frame(root)
main_screen.pack()

username_box = Entry(main_screen)
username_box.pack(side=LEFT, padx=5, pady = 5)

password_box = Entry(main_screen, show="*")
password_box.pack(side=LEFT, padx=5, pady = 5)

send_button = Button(main_screen, text="send", command= lambda: sendMailViaGUI(addreslist, img_attach))
send_button.pack(side= LEFT)
root.mainloop()
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.

FoiledAgain
May 6, 2007

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:

import Tkinter

def button_function(self,button,*args):
    button['state'] = Tkinter.DISABLED

    self.do_button_stuff(args)

    button['state'] = Tkinter.ACTIVE

sharktamer
Oct 30, 2011

Shark tamer ridiculous
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?

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

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?

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.

Dominoes
Sep 20, 2007

Thermopyle posted:

Also think of a project you want to code and start doing it and Google or ask questions when you get stuck.
I agree. Codeacademy misses a lot of tools/abstractions that can make your code cleaner and more elegant, but it teaches the fundamentals well enough that you should be able to dive into a project. You'll learn new things based on what your project requires. Bookmark the official Python documentation, and reference items as they come up.

Germstore
Oct 17, 2012

A Serious Candidate For a Serious Time
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:
class KeyState(object):
    def __init__(self):
        self.__currentState = self.keyIsNotHeld
    def keyIsNotHeld(self, pressed):
        return self.keyIsDown if pressed else self.keyIsNotHeld
    def keyIsDown(self, pressed):
        return self.keyIsHeld if pressed else self.keyIsUp
    def keyIsHeld(self, pressed):
        return self.keyIsHeld if pressed else self.keyIsUp
    def keyIsUp(self, pressed):
        return self.keyIsDown if pressed else self.keyIsNotHeld
    def advanceState(self, pressed):
        self.__currentState = self.__currentState(pressed)
    def currentState(self):
        return self.__currentState
If the state is keyIsUp/keyIsDown I start and stop the note, and if the state is keyIsDown/keyIsHeld I draw the virtual key as pressed. I realize I could be using the keyup/keydown to start and stop the note, and key.get_pressed to draw the virtual key and forget about a state machine all together, but I'd still probably need something fairly similar if I want to seperate all the input processing from the drawing and midi stuff.

Jewel
May 2, 2009

[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

Dren
Jan 5, 2001

Pillbug

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?

code:
class KeyState(object):
    def __init__(self):
        self.__currentState = self.keyIsNotHeld
    def keyIsNotHeld(self, pressed):
        return self.keyIsDown if pressed else self.keyIsNotHeld
    def keyIsDown(self, pressed):
        return self.keyIsHeld if pressed else self.keyIsUp
    def keyIsHeld(self, pressed):
        return self.keyIsHeld if pressed else self.keyIsUp
    def keyIsUp(self, pressed):
        return self.keyIsDown if pressed else self.keyIsNotHeld
    def advanceState(self, pressed):
        self.__currentState = self.__currentState(pressed)
    def currentState(self):
        return self.__currentState
If the state is keyIsUp/keyIsDown I start and stop the note, and if the state is keyIsDown/keyIsHeld I draw the virtual key as pressed. I realize I could be using the keyup/keydown to start and stop the note, and key.get_pressed to draw the virtual key and forget about a state machine all together, but I'd still probably need something fairly similar if I want to seperate all the input processing from the drawing and midi stuff.

I recommend callbacks. Something like:
code:
class Note(object):
    def __init__(self, midi, notename):
        self.midi = midi
        self.notename = notename
    def play():
        self.midi.play(notename)
    def stop():
        self.midi.stop(notename)

class Key(object):
    def __init__(self, note):
        self.__currentState = self.keyIsNotHeld
    def keyDown(self, pressed):
        note.play()
    def keyUp(self, pressed):
        note.stop()

midi = #something
n = Note(midi, 'bflat')
k = Key(n)

k.keyDown()
sleep(1)
k.keyUp()
I have no clue what the midi stuff looks like under the hood but I assume it can go this way fairly simply. The nice thing is that there is no state related to the buttons to manage, just events to connect together.

accipter
Sep 12, 2003

Germstore posted:

code:
class KeyState(object):
    def __init__(self):
        self.__currentState = self.keyIsNotHeld
    def keyIsNotHeld(self, pressed):
        return self.keyIsDown if pressed else self.keyIsNotHeld
    def keyIsDown(self, pressed):
        return self.keyIsHeld if pressed else self.keyIsUp
    def keyIsHeld(self, pressed):
        return self.keyIsHeld if pressed else self.keyIsUp
    def keyIsUp(self, pressed):
        return self.keyIsDown if pressed else self.keyIsNotHeld
    def advanceState(self, pressed):
        self.__currentState = self.__currentState(pressed)
    def currentState(self):
        return self.__currentState

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.

Germstore
Oct 17, 2012

A Serious Candidate For a Serious Time

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.

sharktamer
Oct 30, 2011

Shark tamer ridiculous
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.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

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?

code:
class KeyState(object):
    def __init__(self):
        self.__currentState = self.keyIsNotHeld
    def keyIsNotHeld(self, pressed):
        return self.keyIsDown if pressed else self.keyIsNotHeld
    def keyIsDown(self, pressed):
        return self.keyIsHeld if pressed else self.keyIsUp
    def keyIsHeld(self, pressed):
        return self.keyIsHeld if pressed else self.keyIsUp
    def keyIsUp(self, pressed):
        return self.keyIsDown if pressed else self.keyIsNotHeld
    def advanceState(self, pressed):
        self.__currentState = self.__currentState(pressed)
    def currentState(self):
        return self.__currentState
If the state is keyIsUp/keyIsDown I start and stop the note, and if the state is keyIsDown/keyIsHeld I draw the virtual key as pressed. I realize I could be using the keyup/keydown to start and stop the note, and key.get_pressed to draw the virtual key and forget about a state machine all together, but I'd still probably need something fairly similar if I want to seperate all the input processing from the drawing and midi stuff.

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:
class KeyState(object):
    def __init__(self):
        self.pressed = False

    def set_pressed(self, pressed):
        if self.pressed == pressed:
            return
        self.pressed = pressed
        self.pressed_changed()

    def pressed_changed(self):
        pass

Germstore
Oct 17, 2012

A Serious Candidate For a Serious Time
That is a lot simpler. Thanks.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
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.

Dren
Jan 5, 2001

Pillbug
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?

suffix
Jul 27, 2013

Wheeee!

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.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

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.
So coming in from that angle, Python could feel less object-oriented.

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.)

evilentity
Jun 25, 2010

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?

(At this point I've given up trying to rationalize what I see a lot here. I think they might just be on drugs.)

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.

sharktamer
Oct 30, 2011

Shark tamer ridiculous
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.

breaks
May 12, 2001

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?

sharktamer
Oct 30, 2011

Shark tamer ridiculous
Here is the class init, where all the regexing happens:

code:
with open("file.txt") as f:
    resp_file = f.read()

class Reader(object):
    def __init__(self, name, resp_file):
        self.name = name
        pattern = re.compile(".*" + name + ".*")
        last_10_results = pattern.findall(resp_file)[-10:]
        self.values = [int(x.split(",")[7])/float(1000) for x in last_10_results]
The file just contains comma separated values, about 135 characters long.

deedee megadoodoo
Sep 28, 2000
Two roads diverged in a wood, and I, I took the one to Flavortown, and that has made all the difference.


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:
values = [int(i.split(",")[7])/float(1000) for i in open("file.txt").readlines() if "pattern" in i][-10:]

breaks
May 12, 2001

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 v:shobon:v.

sharktamer
Oct 30, 2011

Shark tamer ridiculous

HatfulOfHollow posted:

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:
values = [int(i.split(",")[7])/float(1000) for i in open("file.txt").readlines() if "pattern" in i][-10:]

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.

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

sharktamer posted:

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.

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

Adbot
ADBOT LOVES YOU

Cirian
Nov 7, 2002
Fun Shoe
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 :)

  • Locked thread