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
vikingstrike
Sep 23, 2007

whats happening, captain
Reading this blog was really helpful in getting accustomed to the standard library:
http://www.doughellmann.com/PyMOTW/

I still use it from time to time for quick reference.

Adbot
ADBOT LOVES YOU

Suspicious Dish
Sep 24, 2011

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

Maluco Marinero posted:

https://www.diveintopython.net - Python Beginner Book

Dive Into Python was outdated and irrelevant five years ago. There is no good reason to use it now. It has terrible code. It is outright wrong in several sections. For a beginner programmer, Think Python or Learn Python the Hard Way is much, much better.

For someone who knows another programming language, but not Python, the Official Python Language Tutorial should be more than fine.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.
No worries, I was just contributing what I learned with, but that's fair enough.

Heavy_D
Feb 16, 2002

"rararararara" contains the meaning of everything, kept in simple rectangular structures
Got a style question, kind of new to python and trying to make some code that's friendly and sensible. In particular the code relies on struct.unpack to parse a binary model format (for quake FPS games), make some tweaks and then export it back. Python is just right for making the tweaks in the middle easy to script and vary from model to model, but right now I'm building the module which will do the ends.

The particular question I have is regarding the way struct.unpack returns a tuple. This is very handy for some of the bigger structures in the model format, using multiple assignment to distil the individual values into variables. When unpack only returns a single value, it still packs it into a tuple, so the naive code which simply assigns the value to a variable ends up getting a tuple containing a number, rather than a number.

Although that didn't take long to spot, I can see two ways to store the number within the tuple in my destination variable, and want to know what you think is the more readable/idiomatic:
code:
    self.type = struct.unpack("l", mdlfile.read(4))[0]
or
code:
    (self.type,) = struct.unpack("l", mdlfile.read(4))
To my inexperienced eye I think the former is a bit easier to dissect, but the latter is more consistent with how the rest of the assignments work, so I could go either way...

Boonoo
Nov 4, 2009

ASHRAKAN!
Take your Thralls and dive back into the depths! Give us the meat and GO!
Grimey Drawer
I haven’t really done any coding before, but I’m working on making a real basic touch-typing thing. It shows a word then you type that word in and so on. Right now to input the word you type it then hit Enter. Is there a way to make it so you would type the word then just hit the spacebar instead of enter?

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Boonoo posted:

I haven’t really done any coding before, but I’m working on making a real basic touch-typing thing. It shows a word then you type that word in and so on. Right now to input the word you type it then hit Enter. Is there a way to make it so you would type the word then just hit the spacebar instead of enter?

Are you using a GUI framework? If so, which one? Can you post any source code?

Boonoo
Nov 4, 2009

ASHRAKAN!
Take your Thralls and dive back into the depths! Give us the meat and GO!
Grimey Drawer
Naw, no gui framework. here's what I have so far.
code:
f = open("mobydick.txt", "r")
text = f.readlines()

for i in text:
	line = i.split(" ")
	
	for i in line:
		i = i.rstrip('\n')
		
		typed = 0
		
		while typed == 0:
			input = raw_input(i + "\n")
			
			if input == i:
				typed = 1

Hed
Mar 31, 2004

Fun Shoe

Heavy_D posted:

want to know what you think is the more readable/idiomatic:
code:
    self.type = struct.unpack("l", mdlfile.read(4))[0]
or
code:
    (self.type,) = struct.unpack("l", mdlfile.read(4))
To my inexperienced eye I think the former is a bit easier to dissect, but the latter is more consistent with how the rest of the assignments work, so I could go either way...

To my brain with all its biases it's way more obvious what the former assigns, so I would say that wins.

Scaevolus
Apr 16, 2007

Heavy_D posted:

To my inexperienced eye I think the former is a bit easier to dissect, but the latter is more consistent with how the rest of the assignments work, so I could go either way...
Make a wrapper function:
code:
    def unpack(f):
        return struct.unpack('<' + f, mdlfile.read(struct.calcsize('<' + f)))
Then your code becomes

code:
self.type, = unpack('l')

FoiledAgain
May 6, 2007

matplotlib question: any idea why the following is happening? What type do I need to make this work?

code:
range_ = np.arange(0,1.,.01)
x,y = np.meshgrid(range_, range_)
z = x+(y*(1-x))
z = 2*abs(z-.5)
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x,y,z,
                    facecolors=cm.jet(cm), #if I comment out this line everything works fine
                    linewidth=0, antialiased=True, shade=False)
plt.show()


Message	File Name	Line	Position	
Traceback				
    <module>	C:\Python27-backup\stability mapping.py	49		
    main	C:\Python27-backup\stability mapping.py	35		
    __call__	C:\Python27\lib\site-packages\matplotlib\colors.py	565		
TypeError: array cannot be safely cast to required type				

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

Boonoo posted:

Naw, no gui framework. here's what I have so far.
code:
f = open("mobydick.txt", "r")
text = f.readlines()

for i in text:
	line = i.split(" ")
	
	for i in line:
		i = i.rstrip('\n')
		
		typed = 0
		
		while typed == 0:
			input = raw_input(i + "\n")
			
			if input == i:
				typed = 1

Hmm, sorry. I'm not quite sure how to do this with raw_input, but after googling it seems like you could use msvcrt if you're using Windows.

Modern Pragmatist
Aug 20, 2008

FoiledAgain posted:

matplotlib question: any idea why the following is happening? What type do I need to make this work?

code:
range_ = np.arange(0,1.,.01)
x,y = np.meshgrid(range_, range_)
z = x+(y*(1-x))
z = 2*abs(z-.5)
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x,y,z,
                    facecolors=cm.jet(cm), #if I comment out this line everything works fine
                    linewidth=0, antialiased=True, shade=False)
plt.show()


Message	File Name	Line	Position	
Traceback				
    <module>	C:\Python27-backup\stability mapping.py	49		
    main	C:\Python27-backup\stability mapping.py	35		
    __call__	C:\Python27\lib\site-packages\matplotlib\colors.py	565		
TypeError: array cannot be safely cast to required type				


So you have two problems here.

1) cm.jet(cm) produces the error that you're getting because you're passing the module cm as an input. If you want a jet colormap just use cm.jet

2) You aren't using facecolors correctly. If you go this route, you have to specify a color for each point on the surface. You are trying to provide a colormap. You probably want to do the following:

code:
surf = ax.plot_surface(x,y,z,
                    cmap=cm.jet,
                    linewidth=0, antialiased=True, shade=False)

etcetera08
Sep 11, 2008

Boonoo posted:

Naw, no gui framework. here's what I have so far.
code:
f = open("mobydick.txt", "r")
text = f.readlines()

for i in text:
	line = i.split(" ")
	
	for i in line:
		i = i.rstrip('\n')
		
		typed = 0
		
		while typed == 0:
			input = raw_input(i + "\n")
			
			if input == i:
				typed = 1

This might help you:
http://stackoverflow.com/questions/6348952/handling-keyboard-events-in-python

FoiledAgain
May 6, 2007

Modern Pragmatist posted:

2) You aren't using facecolors correctly. If you go this route, you have to specify a color for each point on the surface. You are trying to provide a colormap. You probably want to do the following:

code:
surf = ax.plot_surface(x,y,z,
                    cmap=cm.jet,
                    linewidth=0, antialiased=True, shade=False)

Thanks, this is exactly what I wanted to do. I had copied and pasted code from another project where I was using facecolors (and properly in that case!).

Victory Yodel
Jan 28, 2005

When in Jerusalem, I highly suggest you visit the sexeteria.
Not sure if this is the right place to post this, but seeing as it is the Python thread, I'll give it a whirl.

I'm looking to learn to program in Python, and my company is willing to pay for me to take a course. As such, would anyone have any recommendations for a 3-4 day course that might get me up and running? I've done some google-fu and I've found some potential courses but I have no idea if they are good or absolute crap.

Some background, I'm not a harcore programmer but have have always been able to pick things up when I've wanted to, generally for a specific purpose which then falls away after I don't need it any more. Since the company is willing to pay for it, I was hoping to find a course that would give me a solid background. I've already starting reading some books and creating some silly little programs. I would ideally like to find a course that will not be too easy (and therefore waste my time) but not be so hard that I have no idea what's going on.

Any advise or suggestions are much appreciated.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
The official Python Tutorial isn't half bad if you already know another language.

Tan Dumplord
Mar 9, 2005

by FactsAreUseless
I'm using pyplot to create a graph of thing_count vs time, where distance between the time points are not even. I would like to create a moving average for the data set, but I don't know enough maths to know where to start.

All I know is that my MovingAverage implementation works for an evenly-spaced y-axis, but breaks (has sharp transitions) on window fall-off when uneven.

Brogeoisie
Jan 12, 2005

"Look, I'm a private citizen," he said. "One thing that I don't have to do is sit here and open my kimono as it relates to how much money I make or didn't."
Coming from someone who is a complete novice, I think learning python the hard way and dive into python are both good for a one-two punch. Learning python the hard way, while the author is clearly insane, has a lot of good examples to work with. The other one is a good reference material (for me, at least).

I'm in "writing tons of useless/ugly programs" stage but those tutorials were helpful in getting to this point.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
What the hell is with Python's standard library?

http://docs.python.org/dev/library/turtle.html
http://docs.python.org/dev/library/nis.html
http://docs.python.org/dev/library/pipes.html
http://docs.python.org/dev/library/formatter.html
http://docs.python.org/dev/library/reprlib.html
http://docs.python.org/dev/library/sched.html

Why are all of these things in Python 3? After the standard library cleanup? We don't have a sane http client, but we have turtle graphics?

I'm not mentioning asyncore and multiprocessing because they're the standard things that people pick on.

xPanda
Feb 6, 2003

Was that me or the door?
I'm trying to figure out an elegant solution to a problem I have with NumPy.

I have a large array of shape (20, 20, 7, 7, 10), and I want to create another array which has only the last element of the last axis of the previous array, and is thus of shape (20, 20, 7, 7). The mapping a -> b would be b[i,j,k,l] = a[i,j,k,l][-1].

It's easy enough to do with four nested for loops for each of the first four dimensions copying each item individually by index, but is there some command which makes this a one liner? The closest I've come is
pre:
truth_array = [False]*9 + [True]
ranked = big_array.compress(truth_array, axis=9)
This returns an array of shape (20, 20, 7, 7, 1), where the last axis elements are single member arrays with the desired value. Close, but not quite there.

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES

sliderule posted:

I'm using pyplot to create a graph of thing_count vs time, where distance between the time points are not even. I would like to create a moving average for the data set, but I don't know enough maths to know where to start.

All I know is that my MovingAverage implementation works for an evenly-spaced y-axis, but breaks (has sharp transitions) on window fall-off when uneven.

How "not even"?

In astro, we run into this sort of thing quite often. If you think about it, it makes sense because you often have measurements uneven in time. Two suggestions, depending on your situation.

Situation 1 involves doing this moving average piecewise. This would be good if you have say two data runs that are well separated but within the run are finer spaced. Just run the average for each set and then plot those averages. Be sure to propagate your error bars!!

Situation 2 involves a situation where say measurement A and B are hours apart and measurement B and C are days apart. You'll have a large timescale, but some areas are finer than others and there's no real clumping like in the first situation. My suggestion in this case is to not do a moving average, but fit a function to your data. Unfortunately, my experiences with the fitting routines in python's scipy are limited. Even then, I'm suspicious that they handle uneven error bars too well. "curve_fit" in scipy would be a decent place to start.

Alternatively, MFIT (FORTRAN77) was the greatest fitting routine ever. MPFIT in IDL apparently works very well too, and I know there's a python version of MPFIT.

geonetix
Mar 6, 2011


Suspicious Dish posted:

What the hell is with Python's standard library?


Why are all of these things in Python 3? After the standard library cleanup? We don't have a sane http client, but we have turtle graphics?

I'm not mentioning asyncore and multiprocessing because they're the standard things that people pick on.

As often said:

quote:

The standard library is where modules go to die

And it's not even just a stupid catch phrase. Some modules were put there way too early to be mature or even pythonic (oh god, ElementTree, why), but they are used so often and globally it stuck around.

I, too, wish there was something more sane available for HTTP; but it's how things go. Just use something that's not in the standard library; it's a safer bet.

BigRedDot
Mar 6, 2008

xPanda posted:

I'm trying to figure out an elegant solution to a problem I have with NumPy.

I have a large array of shape (20, 20, 7, 7, 10), and I want to create another array which has only the last element of the last axis of the previous array, and is thus of shape (20, 20, 7, 7). The mapping a -> b would be b[i,j,k,l] = a[i,j,k,l][-1].

Unless I am misunderstanding, I think a[...,-1] will do what you are asking.

xPanda
Feb 6, 2003

Was that me or the door?

BigRedDot posted:

Unless I am misunderstanding, I think a[...,-1] will do what you are asking.

Holy crap, it does. drat I love this language. I'm going to go and read up more on advanced indexing. Thanks!

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

OK, here's something I've been meaning to find out about for a while: can someone point me to a good place to learn about decorators? I literally have no idea what they do or why you'd use them, but I assume they're useful.

Optimus Prime Ribs
Jul 25, 2007

MeramJert posted:

OK, here's something I've been meaning to find out about for a while: can someone point me to a good place to learn about decorators? I literally have no idea what they do or why you'd use them, but I assume they're useful.

I was actually just reading up on them. This article was pretty informative.

spankweasel
Jan 4, 2006

MeramJert posted:

OK, here's something I've been meaning to find out about for a while: can someone point me to a good place to learn about decorators? I literally have no idea what they do or why you'd use them, but I assume they're useful.

http://stackoverflow.com/questions/739654/understanding-python-decorators

Has the single best explanation and walkthrough of decorators I've seen yet.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
If you're curious about the history of the decorator syntax, it started as a pattern like this:

code:
def hello():
    return "Hello!"

hello = makebold(hello)
hello = makeitalics(hello)
Where you had a function that took a function, and returned another function:

code:
def makebold(fn):
    def inner(*a, **kw):
        return "<b>%s</b>" % fn(*a, **kw)
    return inner
It was a very useful pattern. Tired of typing or something, the mastermind of terribleness, Philip J Eby, decided to give it a special syntax, at runtime:

code:
[ makebold ]
def hello():
    return "Hello!"
I leave it to the imagination to determine how that was implemented. Bytecode hacks, looking for a LOADGLOBAL/LOADFAST, BUILDLIST 1 pair.

When I see something like this:

code:
@foo(1, 2)('bar')
def butts():
    return "wizardry"
I transform it in my head to:

code:
def butts():
    return "wizardry"
butts = foo(1, 2)('bar')(butts)

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES

BigRedDot posted:

Unless I am misunderstanding, I think a[...,-1] will do what you are asking.

Oh goddamnit, I was working on a solution, thought of this, and just assumed I was misunderstanding the question. :doh:

IAmKale
Jun 7, 2007

やらないか

Fun Shoe
What's the best way to sort a dictionary? I read somewhere that you can convert it into a list and sort the list, but I haven't been able to make it work.

Reformed Pissboy
Nov 6, 2003

Karthe posted:

What's the best way to sort a dictionary? I read somewhere that you can convert it into a list and sort the list, but I haven't been able to make it work.

A dictionary in itself is unsorted by nature (being primarily for 'random' lookups, you shouldn't have to care about the order of what's inside). You can feed it into sorted() if you're iterating over it, but there isn't a way to sort a dictionary in place unless you convert it into something else.

code:
>>> n
# presented in no particular order (well, technically the hash() value of each key)
{11: 'eleven', 1: 'one', 3: 'three', 5: 'five', 6: 'six'}

>>> n.items()
# returns keys and values as tuples in a list, still in no particular order
[(11, 'eleven'), (1, 'one'), (3, 'three'), (5, 'five'), (6, 'six')]

>>> sorted(n.items())
# now our items are in sorted order -- this can get weird if your types aren't easily comparable
[(1, 'one'), (3, 'three'), (5, 'five'), (6, 'six'), (11, 'eleven')]

>>> sorted(n.items(), key=lambda (k,v): v)
# just showin' off `key` argument to sorted,
# here's our dict sorted by value instead of key...
[(11, 'eleven'), (5, 'five'), (1, 'one'), (6, 'six'), (3, 'three')]
>>> sorted(n.items(), key=lambda (k,v): len(v))
# ...and here's our dict sorted by shortest to longest word in values >8)
# (this is stupid)
[(1, 'one'), (6, 'six'), (5, 'five'), (3, 'three'), (11, 'eleven')]

>>> l = sorted(n.items())
# converting our sorted list back into a dictionary...

>>> dict(l)
# order is discarded and we're back where we were to begin with
{11: 'eleven', 1: 'one', 3: 'three', 5: 'five', 6: 'six'}

Reformed Pissboy fucked around with this message at 18:11 on May 17, 2012

IAmKale
Jun 7, 2007

やらないか

Fun Shoe

Harm Barn Gumshoe posted:

A dictionary in itself is unsorted by nature (being primarily for 'random' lookups, you shouldn't have to care about the order of what's inside). You can feed it into sorted() if you're iterating over it, but there isn't a way to sort a dictionary in place unless you convert it into something else.
Thanks for the guide, it turned out to be much more simple than I thought.

River
Apr 22, 2012
Nothin' but the rain
So I've decided to finally start on Python. I've been meaning to learn it for some time, guess I just never got around to it. Anyway, as a sort of 'learning venture' I want to make a simple IRC bot, one that greets users who enter a channel or something like that, and eventually have a 'google' function, I.E "!google somethingawful" and display the first result's url and description.

Is this something a beginner should take up as a 'first project?' What libraries are best for this? I was asking around in #python on freenode and got directed to 'twisted.' Can anyone give a second opinion on using that?

Thanks.

Red Mike
Jul 11, 2011

River posted:

So I've decided to finally start on Python. I've been meaning to learn it for some time, guess I just never got around to it. Anyway, as a sort of 'learning venture' I want to make a simple IRC bot, one that greets users who enter a channel or something like that, and eventually have a 'google' function, I.E "!google somethingawful" and display the first result's url and description.

Is this something a beginner should take up as a 'first project?' What libraries are best for this? I was asking around in #python on freenode and got directed to 'twisted.' Can anyone give a second opinion on using that?

Thanks.

If you don't care about implementing the actual protocol (but rather want to focus on the actual bot functionality), I've made a small Python IRC library.

Otherwise, Twisted is good, although a bit much for just beginning programming in Python. It's quite bloated and not newbie-friendly, from my experience.

e: Also, for starting out with a language, anything you'd find interesting is a good 'first project'. Interest is key to motivating yourself to keep learning.

Thermopyle
Jul 1, 2003

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

I've used Eventlet to make an IRC bot.

It was pleasant to use and was a fun project.

As a beginner, I wouldn't use Twisted.

deimos
Nov 30, 2006

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

Karthe posted:

Thanks for the guide, it turned out to be much more simple than I thought.

Depending on your usage you might want to use collections.OrderedDict to store the dictionary once sorted (for example if the keys don't change very often), avoiding the cruft of managing a list and a dict at the same time.

tef
May 30, 2004

-> some l-system crap ->

Thermopyle posted:

As a beginner, I wouldn't use Twisted.

As someone who has been using python for a while, I wouldn't use Twisted. Twisted is a bit Did you just tell me to go gently caress myself?. Undocumented at the best of times, out of date documentation at the worst of times.

Captain Capacitor
Jan 21, 2008

The code you say?
As someone who has to maintain a Twisted application on a daily basis I concur. Although for a bot I modified for a game here on the forums I just forked CloudBot

Boblonious
Jan 14, 2007

I HATE PYTHON IT'S THE SHITTIEST LANGUAGE EVER PHP IS WAY BETTER BECAUSE IT IS MADE FOR THE WEB AND IT USES MYSQL WHICH IS ALSO USED BY GOOGLE AND THATS WHY ITS SO MUCH BETTER THAN PYTHON EVEN HTML IS A BETTER PROGRAMMING LANGUAGE THAN PYTHON
Having just written an IRC bot using Twisted, I don't disagree with the above points, but it wasn't really that bad. The documentation could be better, and some parts of Twisted plain don't work, but a lot of Twisted code self-documents and there's very little "magic" under the hood; I found it easy to figure out.

I guess what I'm trying to say is, my Twisted experience wasn't completely negative. This was the first I've used Twisted.

Adbot
ADBOT LOVES YOU

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES
Ok, here's a really dumb question. Let's say I wanted to collect all the stats from a page, for example: http://www.nhl.com/ice/playerstats.htm?season=20112012&gameType=2&team=&position=S&country=&status=&viewName=summary#?navid=nav-sts-indiv

Right now, the only thing I can think of is setting up a python script to save each page (in this case, 1 through 30), and then just having to figure out how to distill the HTML by hand. I guess "beautifulsoup" would work, but I am looking for tips as to how to do this cleanly and reliably.

  • Locked thread