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
m0nk3yz
Mar 13, 2002

Behold the power of cheese!

And Q1 doesn't reflect current benchmarks - and more patches, which have not been pushed upstream have been applied to unladen swallow trunk. This quarter's release has focused on LLVM support, and next quarter's will be an optimization free-for-all.

Again - run pypy through real world benchmarks - which unladen-swallow's are (they range from Django ones, to others) and you'll get a better idea of pypy's performance. I'm not denying it could/can be faster than CPython, or even unladen swallow - I'm saying that the benchmarks they've done have been very selective, and not representative of real applications.

Adbot
ADBOT LOVES YOU

buttcheekwarrior
May 17, 2008
I am looking to start learning Python and have had a book for years on it. The book was written when Python 2.0 was around. Is it too outdated to use? I just saw that a new 3.1 release came out not too long ago.

What is considered the most used/versatile Python release? Should I start fresh with the newest 3.1?

ShizCakes
Jul 16, 2001
BANNED

buttcheekwarrior posted:

I am looking to start learning Python and have had a book for years on it. The book was written when Python 2.0 was around. Is it too outdated to use? I just saw that a new 3.1 release came out not too long ago.

What is considered the most used/versatile Python release? Should I start fresh with the newest 3.1?

I really, REALLY wanted to just start with 3.0 / 3.1, but if you intend on using anything outside the included libraries within at least the next year, you should use 2.6.

Python's download page posted:

The current production versions are Python 2.6.2 and Python 3.1. You should start here if you want to learn Python or if you want the most stable versions. Note that both Python 2.6 and 3.1 are considered stable production releases, but if you don't know which version to use, start with Python 2.6 since more existing third party software is compatible with Python 2 than Python 3 right now. Here are some quick download links.

Scaevolus
Apr 16, 2007

buttcheekwarrior posted:

The book was written when Python 2.0 was around. Is it too outdated to use?
Pretty much. Try the official tutorial, it's really good.

king_kilr
May 25, 2007

Scaevolus posted:

Pretty much. Try the official tutorial, it's really good.

As is http://diveintopython.org/

Ferg
May 6, 2007

Lipstick Apathy

king_kilr posted:

As is http://diveintopython.org/

I read this book cover-to-cover electronically, and then purchased a copy. Definitely a fantastic resource to learn the language.

Buffis
Apr 29, 2006

I paid for this
Fallen Rib

Ferg posted:

I read this book cover-to-cover electronically, and then purchased a copy. Definitely a fantastic resource to learn the language.

Seconded. It is a really great book if you have some prior development knowledge in any other language.

Balam
Dec 12, 2006
Hey, more newbness here.

I am trying to make a little program that will do some phonological analysis. Basically, get the phonetic representation of a word and return the phonological representation.

So, to keep it simple just work with assimilation right now.

So the user gives something like "umty" and return "umdy". so if "t", "p" or "k" appear right after "m" or "n" they turn into "d", "b", and "k" respectively.

so we set up some string sets and a dictionary (ignoring uppercase for now)

nasal = "mn"
stops = {"t":"d","p":"b","k":"g"}

what I am trouble figuring out is how to say if a string has a series of nasal immediately followed by a stop, then replace the stop with the associated value.

MononcQc
May 29, 2007

Balam posted:

Hey, more newbness here.

I am trying to make a little program that will do some phonological analysis. Basically, get the phonetic representation of a word and return the phonological representation.

So, to keep it simple just work with assimilation right now.

So the user gives something like "umty" and return "umdy". so if "t", "p" or "k" appear right after "m" or "n" they turn into "d", "b", and "k" respectively.

so we set up some string sets and a dictionary (ignoring uppercase for now)

nasal = "mn"
stops = {"t":"d","p":"b","k":"g"}

what I am trouble figuring out is how to say if a string has a series of nasal immediately followed by a stop, then replace the stop with the associated value.
http://en.wikipedia.org/wiki/Soundex to not reinvent the wheel
and here's a simple implementation of it:
http://code.activestate.com/recipes/52213/

Zombywuf
Mar 29, 2008

MononcQc posted:

http://en.wikipedia.org/wiki/Soundex to not reinvent the wheel
and here's a simple implementation of it:
http://code.activestate.com/recipes/52213/

You can do a lot better than Soundex.

tef
May 30, 2004

-> some l-system crap ->
python + natural language process = http://www.nltk.org/code

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

tef posted:

python + natural language process = http://www.nltk.org/code

I tech-reviewed that book, but they forgot me in the credits. *sad face*

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!
if you want simple then just do double metaphone.

MononcQc
May 29, 2007

Zombywuf posted:

You can do a lot better than Soundex.

Compared to what he was attempting to do (comparing 'p' and 'b', seeing for more general pronounciation), soundex seems to be alright.

If he wanted to dig deeper, wikipedia had the links to do so (ie.: http://en.wikipedia.org/wiki/Phonetic_algorithm). If he takes the time to read the entry and needs a more advanced solution, I'm sure he'll be able to dig through the first 2 lines of the article and follow the links. I'm pretty sure the guy is more than 4 years old and I believe he can do some searching. Sometimes a first term is all one needs to be able to go forward.

Otherwise, providing links yourself would have been way more helpful (like the one tef posted, which looks really nice).

His Neutralness
Oct 1, 2007

Live Free or Don't
How would I keep printing on the same line (just using the shell), like if I was printing the speed something was downloading at?

Scaevolus
Apr 16, 2007

His Neutralness posted:

How would I keep printing on the same line (just using the shell), like if I was printing the speed something was downloading at?

print "\r" # carriage return

king_kilr
May 25, 2007
Finish a print statement with a comma

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
I think he means in the same location, not just on the same line.

In that case you will probably want to look at curses.

king_kilr
May 25, 2007
Oh, I just use sys.stdout.write(); sys.stdout.flush()

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

king_kilr posted:

Oh, I just use sys.stdout.write(); sys.stdout.flush()

Stupid sys.stdout!!11 http://bugs.python.org/issue5313 :ughh:

Thermopyle
Jul 1, 2003

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

His Neutralness posted:

How would I keep printing on the same line (just using the shell), like if I was printing the speed something was downloading at?

code:
import time
for i in range(20):
    print str(i) + '\r',
    time.sleep(1)
The '\r' returns to beginning of line and the ',' suppresses the new line.

Alcohol Kills
Feb 29, 2008

by Peatpot
another nubby question:

let's say i have a dictionary, filled up with int:string lit pairs. I would like to iterate through this dictionary and use the strings as identifers for a series of new lists

the problem, obviously, is that string lits can't be used as identifiers

i've been reading through the docs for the built-ins for a couple hours, but the best i've got so far is 'cannot assign to operator'

is tearing the quotes off a string and using it as an identifier realistic? Possible? Should i just stick to pumping out dictas and lists by hand? Any help appreciated, sorry for the capitalization, phone post

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
I'm not sure if I understand you, but you can do this:
code:
>>> globals()["foo"] = "bar"
>>> foo
'bar'
That said, you probably shouldn't be doing this unless you have a very good reason to. Post more details about what you're trying to do for a better method if you want.


edit: If you are doing this inside a class, look at __setattr__.

supster fucked around with this message at 00:05 on Jul 1, 2009

Alcohol Kills
Feb 29, 2008

by Peatpot

supster posted:

I'm not sure if I understand you, but you can do this:
code:
>>> globals()["foo"] = "bar"
>>> foo
'bar'
That said, you probably shouldn't be doing this unless you have a very good reason to. Post more details about what you're trying to do for a better method if you want.


edit: If you are doing this inside a class, look at __setattr__.

i should have mentioned that this wll be in a class, i'm not out to flood main with a bunch of garbage. I need a method that i can throw a number and class instance at and have it add a series of uniquely-named attribute-dicts to said class instance, representing entities or locations or w/e.

setattr appears to be precisely what i needed, thaks mate!

i feel like 'your skill in python has increased(10)!' exciting :)

edit:

just wanted to show off what i made :coal:

code:
	def eachcell(self):
		iter1 = 0
		while iter1 < len(self.griddic):
			if not hasattr(self, 'a'+str(self.griddic[iter1][0])
				       +str(self.griddic[iter1][1])):
				setattr(self, 'a'+str(self.griddic[iter1][0])
					+str(self.griddic[iter1][1]),{})
				iter1 += 1
	def showlocattr(self):
		self.matchstr = re.findall(r"""\w\d\d""",str(dir(self)))
		print self.matchstr
Am i doing anything ridiculously stupid? is this way more code than i need?



What it produces given an 8x8 grid from the class it's a part of:
['a00', 'a01', 'a02', 'a03', 'a04', 'a05', 'a06', 'a07', 'a10', 'a11', 'a12', 'a13', 'a14', 'a15', 'a16', 'a17', 'a20', 'a21', 'a22', 'a23', 'a24', 'a25', 'a26', 'a27', 'a30', 'a31', 'a32', 'a33', 'a34', 'a35', 'a36', 'a37', 'a40', 'a41', 'a42', 'a43', 'a44', 'a45', 'a46', 'a47', 'a50', 'a51', 'a52', 'a53', 'a54', 'a55', 'a56', 'a57', 'a60', 'a61', 'a62', 'a63', 'a64', 'a65', 'a66', 'a67', 'a70', 'a71', 'a72', 'a73', 'a74', 'a75', 'a76', 'a77']

64 uniquely-named dictionaries, hopefully ripe for stuffing full of references to other things.

The end-goal is a text-based adventure of sorts, ideally generic enough that I can drop some sprite graphics on top without too much wrangling. I'd like to take it to the internet at some point, although I've only just started looking at Django and TurboGears.

Let me know if I'm going in a wrong direction here, or if you'd like to see the class that this method is a part of.

Alcohol Kills fucked around with this message at 02:38 on Jul 1, 2009

Alcohol Kills
Feb 29, 2008

by Peatpot
Bumping because I'm :2bong: and Python has the most amazing morphyness I have yet to encounter in a programming language.

Granted, all I've used is C++ and VB, but the rate of learning that python bestows through the magic of the interpreter is just incredible, for one. I didn't learn in a full semester of C++ or VB class to do the things I have learned on my own in Python in the last few days.

edit: I totally forgot the best part. Python loads and reloads modules on the loving fly wowwwww it's like I'm playing in twelve dimensional space or something. Am I being silly for being blown away by the implications of that? I mean, the way I'm perceiving it, I can literally have a baby module make a copy of a module it is using as daddy, edit the copy of that module according to whatever parameters, rename the copy to overwrite the original, and then have the baby module reload the daddy module and change behaviors accordingly? Without a break in execution?

That's like, fffffffff what? Do I understand that right?

Alcohol Kills fucked around with this message at 05:17 on Jul 1, 2009

yippee cahier
Mar 28, 2005

Alcohol Kills posted:

Am i doing anything ridiculously stupid? is this way more code than i need?

Is there any real reason your program needs to see these as attributes?

Could this be accomplished with a simple 2D array?

cells = [[{} for x in range(8)]]*8

Scaevolus
Apr 16, 2007

sund posted:

Is there any real reason your program needs to see these as attributes?

Could this be accomplished with a simple 2D array?

cells = [[{} for x in range(8)]]*8
that might give you some unexpected behavior
code:
>>> a = [[0]] * 8
>>> a
[[0], [0], [0], [0], [0], [0], [0], [0]]
>>> a[2] = 4
>>> a
[[0], [0], 4, [0], [0], [0], [0], [0]]
>>> a[3][0] = 7
>>> a
[[7], [7], 4, [7], [7], [7], [7], [7]]

Zombywuf
Mar 29, 2008

Alcohol Kills posted:

Bumping because I'm :2bong: and Python has the most amazing morphyness I have yet to encounter in a programming language.

Granted, all I've used is C++ and VB, but the rate of learning that python bestows through the magic of the interpreter is just incredible, for one. I didn't learn in a full semester of C++ or VB class to do the things I have learned on my own in Python in the last few days.

edit: I totally forgot the best part. Python loads and reloads modules on the loving fly wowwwww it's like I'm playing in twelve dimensional space or something. Am I being silly for being blown away by the implications of that? I mean, the way I'm perceiving it, I can literally have a baby module make a copy of a module it is using as daddy, edit the copy of that module according to whatever parameters, rename the copy to overwrite the original, and then have the baby module reload the daddy module and change behaviors accordingly? Without a break in execution?

That's like, fffffffff what? Do I understand that right?

Now go play some Javascript, here's a starter (untested):
code:
function A() {}
A.prototype.foo() { return 1; }
function B() {}
B.prototype = new A();
b = new B();
b.foo(); // returns 1
function C() {}
C.prototype.foo() { return 2; }
B.prototype = new C();
b.foo(); // returns 2

Zombywuf fucked around with this message at 18:06 on Jul 1, 2009

yippee cahier
Mar 28, 2005

Scaevolus posted:

that might give you some unexpected behavior
Whoops... I just made sure it returned the right groupings of lists and never thought about that. You learn something every day.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
Is there a good way I can see a summary of changes between Python 2.3 and 2.6? I need to evaluate the effort required to make a script targetted to 2.6 work under 2.3.

No Safe Word
Feb 26, 2005

supster posted:

Is there a good way I can see a summary of changes between Python 2.3 and 2.6? I need to evaluate the effort required to make a script targetted to 2.6 work under 2.3.


2.3 -> 2.4 = http://docs.python.org/whatsnew/2.4.html
2.4 -> 2.5 = http://docs.python.org/whatsnew/2.5.html
2.5 -> 2.6 = http://docs.python.org/whatsnew/2.6.html

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
I was hoping to avoid going over the entire documents, but I did. If anyone needs this in the future, here's a reference of changes likely to affect day-to-day development:

I'm sure I skipped over some things.


edit: \/ fixed, thanks.

supster fucked around with this message at 23:52 on Jul 2, 2009

Swed
Sep 25, 2003
the voices sayeth

supster posted:

I was hoping to avoid going over the entire documents, but I did. If anyone needs this in the future, here's a reference of changes likely to affect day-to-day development:


I'm sure I skipped over some things.

I think you got those two pointing at the same place. I think this is what you wanted? http://docs.python.org/whatsnew/2.6.html#pep-371-the-multiprocessing-package

Swed fucked around with this message at 05:25 on Jul 2, 2009

darknife
Feb 23, 2009
So, I shortened some code I wrote in a tkinter program from this:

code:
    l1x1 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l1x1.grid(row=1,column=1)
    l1x2 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l1x2.grid(row=1,column=2)
    l1x3 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l1x3.grid(row=1,column=3)
    l1x4 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l1x4.grid(row=1,column=4)
    l2x1 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l2x1.grid(row=2,column=1)
    l2x2 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l2x2.grid(row=2,column=2)
    l2x3 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l2x3.grid(row=2,column=3)
    l2x4 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l2x4.grid(row=2,column=4)
    l3x1 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l3x1.grid(row=3,column=1)
    l3x2 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l3x2.grid(row=3,column=2)
    l3x3 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l3x3.grid(row=3,column=3)
    l3x4 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l3x4.grid(row=3,column=4)
    l4x1 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l4x1.grid(row=4,column=1)
    l4x2 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l4x2.grid(row=4,column=2)
    l4x3 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l4x3.grid(row=4,column=3)
    l4x4 = Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40)
    l4x4.grid(row=4,column=4)
(I didn't type this by hand. I wrote a quick program to do it which pretty much resembles the shortened code.)

to:
code:
for x in range(4):
        for y in range(4):
            Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40).grid(row=x,column=y)
Is there anymore I can do to simplify and shorten it? Maybe make it just one loop?

darknife fucked around with this message at 01:32 on Jul 3, 2009

hey mom its 420
May 12, 2007

k = [Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40).grid(row=x,column=y) for x in range(4) for y in range(4)]

darknife
Feb 23, 2009

Bonus posted:

k = [Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40).grid(row=x,column=y) for x in range(4) for y in range(4)]

List comprehension, right?

hey mom its 420
May 12, 2007

Yes indeed!

darknife
Feb 23, 2009
Thank you, sir, you reminded me. I didn't get to that in my reading so now I shall learn about it.

darknife fucked around with this message at 02:04 on Jul 3, 2009

king_kilr
May 25, 2007

Bonus posted:

k = [Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40).grid(row=x,column=y) for x in range(4) for y in range(4)]

code:
f = lambda x,y : Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40).grid(row=x,column=y)
[f(x, y) for x in xrange(4) for y in xrange(4)
:colbert:

Adbot
ADBOT LOVES YOU

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"
If you're not saving the result of a list comprehension, you probably shouldn't be using one. They don't exist just to win rounds of Python golf, but to provide an elegant alternative to the map(lambda:) pattern. In fundamentally procedural code such as was posted, for-loops are easiest to read. The original poster had it correct: just use two loops.

If you're using 2.6 there's a built-in function to separate the loop and index generation:
code:
from itertools import product
for x, y in product(range(4), range(4)):
    Label(fGrid, text=randice(Dice), font=('Arial Black',80), pady = 10, padx=40).grid(row=x,column=y)

  • Locked thread