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
tripwire
Nov 19, 2004

        ghost flow

darknife posted:

Yea, I didn't really know how it worked either.

How does yours work?
What part are you confused about? The use of the ternary operator or recursive functions in general?

When you are writing a recursive functions you first have to think about what your base case is, and then figure out how you can break down everything else into your base case through repeated calls to itself.

So on the first line hlfrk414 defines a function binary which takes on argument, num. The base case here is when the argument is equivalent to the integers 0 or 1, in which case the same value is returned as a string. If the input is not in the base case then we proceed. We know that even numbers in binary end in zero and odd end in one so hlfrk414 checks if the input is divisible by two and stores that in the variable "lowdigit". Finally the recursive part: the function evaluates and returns the expression "binary(num // 2) + lowdigit", which means calling itself again and again and again until it finally reaches the base-case and cascades back up again. The rest of it is just printing a comparison of this function with pythons built in bin() function which returns the binary form of an integer.

Adbot
ADBOT LOVES YOU

jupo
Jun 12, 2007

Time flies like an arrow, fruit flies like a banana.

Janin posted:

Python 3 rules, just finished testing the port of a library I wrote and it's about 25% faster. I was also able to simplify the code immensely.

code:
$ python3 speedtest.py 
Reading data
old jsonlib:  1.103366
new jsonlib:  0.867395
stdlib json: 71.427396

Writing data
old jsonlib:  1.293511
new jsonlib:  0.828123
stdlib json: 29.775200
Feeling :smug: as gently caress

You make it sound like Python 3 was responsible for the speedup alone yet you also mention it giving you the ability to immensely simplify. Did the speedup come before or after the simplification? Which new language features led to IMMENSE simplifications?

Sylink
Apr 17, 2004

Anyone have any experience with google appengine and webapp?

I'm tinkering around and enjoying the fact I can just make raw python appear on my browser. If I wanted to make a serious app is webapp worth anything or should I settle for Django?

I'm tempted to write my own, is output basically down to just printing something so if I had an entire page to load I could simple print the html ?

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"

jupo posted:

You make it sound like Python 3 was responsible for the speedup alone yet you also mention it giving you the ability to immensely simplify. Did the speedup come before or after the simplification? Which new language features led to IMMENSE simplifications?

The str/unicode unification means that I got to remove about half of the string-handling code, and the per-interpreter module initialization allows logic to be split easily across the Python/C boundary. I don't know which came first: once I finished porting it, which involved removing various workarounds for Python 2.x behavior, I ran the performance tests and noticed that it had improved without any particular effort.

Peao
Dec 1, 2004
If you can read this, remind me of why I shouldn't post.
Not to sound like a dick, but could the OP perhaps provide more details? It links to a lot of resources, but there's so much going on at the listed sites, rife with programming vocabulary, that I feel it would scare off newbies. The me of a year ago who knew nothing about programming would've been confused by the information overload. Since we're recommending Python as a first language to people that ask, let's try to make the megathread as newbie-friendly as possible.

Make it like one of the threads in the Games forum, where the OP gives information, storyline, screenshots, trailers, etc. A one-stop information station, with additional links.

I say champion a specific IDE (one with code completion, debugger, GUI designer). Don't even call it an IDE, just give a list of stuff saying "this is what you need to make a Python program."

Also, I seem to remember an interactive online Python tutorial, similar to a Flash game. I can't look for it now because my connection is too slow, but if anyone has it, we should it add it to the OP.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Peao posted:

... Since we're recommending Python as a first language to people that ask, let's try to make the megathread as newbie-friendly as possible. ...

I don't think that you're being a dick, but I don't think that the OP necessarily needs to singly cater to "My First Programming Language" crowd. Many people that write great software in Python start with something else and move to Python.

Additionally, I must wholeheartedly champion *NO* IDE for beginning programmers. Environments that obfuscate the tool chain drastically stunt the learning process.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
Since the almost univeral answer to "I want to learn how to program what should I learn?" on these forums is Python, it might not be a bad idea to at least have a section of first-time friendly links.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Peao posted:

Not to sound like a dick, but could the OP perhaps provide more details? It links to a lot of resources, but there's so much going on at the listed sites, rife with programming vocabulary, that I feel it would scare off newbies. The me of a year ago who knew nothing about programming would've been confused by the information overload.

The Python documentation goes out of its way not to mention jargon. Hell, the standard for the docs actually prohibits the use of "CPU" without first defining it.

Peao posted:

Since we're recommending Python as a first language to people that ask, let's try to make the megathread as newbie-friendly as possible.

Let's not. I'm sure most of the programmers in this forum learned to program on their own with nothing more than a compiler (or interpreter), a text editor, and online documentation (and believe me, Python has some of the easiest-to-read newbie documentation on the planet).

If you're so easily confused by some of the best and most newbie-friendly documentation out there, then you in no way have the commitment required to progress beyond "Hello, world!" Seriously, programming requires initiative on the part of the student to learn (just like anything else). About the only thing the official Python tutorial expects of you is basic computer literacy. If you know what a command prompt is, you can use the tutorial. (If you don't know what a command prompt is, you'd have one hell of a wake-up call when you wrote "Hello world".)

Peao posted:

Make it like one of the threads in the Games forum, where the OP gives information, storyline, screenshots, trailers, etc. A one-stop information station, with additional links.

It is a one-stop shop. Hell, if all it contained was a link to the Python documentation, it'd be a one-stop shop. And the Games threads are a terrible thing to emulate. They're massive walls of text that no one actually reads.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Lonely Wolf posted:

Since the almost univeral answer to "I want to learn how to program what should I learn?" on these forums is Python, it might not be a bad idea to at least have a section of first-time friendly links.

I would support this. A section for the "my first programming language" peeps is a solid proposal.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

tbradshaw posted:

I would support this. A section for the "my first programming language" peeps is a solid proposal.

I'll work on adding this. Been so long since I did the OP I've forgotten what I put there.

Polygynous
Dec 13, 2006
welp
I'm using project euler as an excuse to learn python but this has me losing my mind.
code:
f=[0]
g=[1]
h=[1]
for i in range(3,7):
  print h, g
  c=0
  for j in range(len(g)):
    if j > len(h):
      s=g[j]+c
    else:
      s=g[j]+h[j]+c
    f[j]=s%10
    c=s/10
  print h, g
  while c:
    f.append(c%10)
    c=c/10
  h=g
  g=f
  print f
somehow the values of g and h change between the two print statements, I get this:
[1] [1]
[1] [1]
[2]
[1] [2]
[1] [3]
[3]
[3] [3]
[6] [6]
[6]
[6] [6]
[2] [2]
[2, 1]

what the hell is going on

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
array_1 = array_2 just makes array_1 refer to the same data as array_2.

code:
>>> arr = [1,2,3]
>>> arr2 = arr
>>> arr[1] = 50
>>> arr2
[1, 50, 3]
You can take a slice of the whole array to make a copy:
code:
>>> arr = [1,2,3]
>>> arr2 = arr[:]
>>> arr[1] = 50
>>> arr2
[1, 2, 3]

Avenging Dentist fucked around with this message at 20:59 on May 14, 2009

spankweasel
Jan 4, 2006


Huge props to Avenging Dentist for figuring out what the code was trying to do. I looked at it and promptly :psypop:

edit: Avenging Dentist updated his post with the same thing I said.

spankweasel fucked around with this message at 21:07 on May 14, 2009

Lurchington
Jan 2, 2003

Forums Dragoon
this can come up with things like dictionaries too, but instead of the slice trick, they provide the use of a copy() function for making a shallow copy.

http://docs.python.org/library/stdtypes.html#typesmapping

Polygynous
Dec 13, 2006
welp
o_O

Thanks. Do they bother to mention that in the tutorial anywhere? I may have missed it since I've just kind of been skimming it but that seems like it would be kind of important.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

spoon0042 posted:

o_O

Thanks. Do they bother to mention that in the tutorial anywhere? I may have missed it since I've just kind of been skimming it but that seems like it would be kind of important.

This isn't particular to Python. Assignment by reference vs. assignment by value is very consistent through most languages. (The same for all mainstream programming languages?)

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"

tbradshaw posted:

This isn't particular to Python. Assignment by reference vs. assignment by value is very consistent through most languages. (The same for all mainstream programming languages?)

C++ has a bizarre mixture of both, but C (everything is by-value) and Java/C#/Ruby/Python/etc (everything by-reference) are consistent.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Janin posted:

C++ has a bizarre mixture of both, but C (everything is by-value) and Java/C#/Ruby/Python/etc (everything by-reference) are consistent.

I'm pretty sure they are all consistent. Scalars are by value, structures are by reference...

dancavallaro
Sep 10, 2006
My title sucks
I used py2exe to make an executable of a Python script I wrote. It works great on the Windows XP computer that I created it on, and it works great on my Windows Vista laptop at home. But it doesn't work on my boss's Vista computer, and fails with the error "the application has failed to start because its side-by-side configuration is incorrect. Please see application event log for more details". Any ideas what could be wrong?

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...

tbradshaw posted:

This isn't particular to Python. Assignment by reference vs. assignment by value is very consistent through most languages. (The same for all mainstream programming languages?)

No, but the concept of the "name" in Python is largely peculiar. All variables are effectively pointers / references, it's just that the non-mutable data obsfucates this. Looked at another way, Python's variable / data model is just like that in other languages but what has to be made explicit in other languages (passing a pointer or a reference) is implicit in Python. It can be confusing, especially if you've learnt other languages previously and it's something that most teaching material neglects or mentions later.

Modern Pragmatist
Aug 20, 2008
I was wondering if anyone had any experience in dealing with Spreadsheet processing using xlrd and xlwt. What I want to do is open a sheet, modify the content of a few of the cells, then save it.

code:
import xlrd
import xlwt

sheet = xlrd.open_workbook('filename.xls').sheet_by_index(0)

sheet.put_number_cell(0,0,1,0)

newWB = xlwt.Workbook()

# Somehow copy sheet data from "sheet" into a new sheet in newWB

newWB.save('filename.xls')
It seems fairly straight-forward. Maybe I am just missing it since the documentation for either module isn't all that great.

It would also be nice, but not necessary, to be able to preserve macros, etc. that are embedded in the initial spreadsheet.

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

outlier posted:

No, but the concept of the "name" in Python is largely peculiar. All variables are effectively pointers / references, it's just that the non-mutable data obsfucates this. Looked at another way, Python's variable / data model is just like that in other languages but what has to be made explicit in other languages (passing a pointer or a reference) is implicit in Python. It can be confusing, especially if you've learnt other languages previously and it's something that most teaching material neglects or mentions later.

Could you give any examples? I can't picture this as something atypical about the labels variable system in Python. I only found it notable in regard to garbage collection. I also can't find anything that is implicit that is normally explicit. Do you mean "everyone treats the collection objects in Python as if they are scalar datatypes, and are surprised to find out they are objects?"

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.

dancavallaro posted:

I used py2exe to make an executable of a Python script I wrote. It works great on the Windows XP computer that I created it on, and it works great on my Windows Vista laptop at home. But it doesn't work on my boss's Vista computer, and fails with the error "the application has failed to start because its side-by-side configuration is incorrect. Please see application event log for more details". Any ideas what could be wrong?

Did you check the log?

dancavallaro
Sep 10, 2006
My title sucks

Lonely Wolf posted:

Did you check the log?

No, never got a chance to, my boss just told me that it was crashing with that error. Strangely, I tried running it on *another* Windows XP computer, and it didn't work, but this time the error was something like "Windows failed to start the executable" or "the program failed to start" or something like that.

Scaevolus
Apr 16, 2007

Janin posted:

C++ has a bizarre mixture of both, but C (everything is by-value) and Java/C#/Ruby/Python/etc (everything by-reference) are consistent.
Java passes references by value though.

dancavallaro
Sep 10, 2006
My title sucks

dancavallaro posted:

I used py2exe to make an executable of a Python script I wrote. It works great on the Windows XP computer that I created it on, and it works great on my Windows Vista laptop at home. But it doesn't work on my boss's Vista computer, and fails with the error "the application has failed to start because its side-by-side configuration is incorrect. Please see application event log for more details". Any ideas what could be wrong?

Ok, different approach. I'm gonna make this a server-side app instead, so my boss doesn't have to worry about running it. Basically what I'm doing is some parsing of log files, and my results are split up into about 20 different .CSV files. Basically what I want to do is to set it up so that my boss goes to the page, the script generates the .CSV files, writes them to a .zip file, deletes the .CSV files, and then sends the .zip file to the end-user to download. Right now I have it running on my laptop so that it creates the .zip file and deletes the .CSV files. How can I now make this a server-side app that takes that .zip file and sends it to the browser?

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"

Scaevolus posted:

Java passes references by value though.
As opposed to what?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Janin posted:

As opposed to what?

As opposed to passing references by reference.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
"Java/C#/Ruby/Python/etc (everything by-reference)" is an overstatement, too. C# structs have value semantics, Java has the whole primitives fiasco, and not everything is references in Python, either.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

Avenging Dentist posted:

As opposed to passing references by reference.
This. Java (and most other languages) are strictly pass-by-value, you just have a reference to most things and those references are passed by value.

Python is also strictly a pass-reference-by-value language, but it's important to understand that immutable objects (e.g., tuple, frozenset) have pass-by-value characteristics because when you make a change to a immutable object a copy is made and reassigned.

I am not 100% confident about this as I am relatively new to Python, but I am pretty sure it is correct.

Benji the Blade
Jun 22, 2004
Plate of shrimp.

Mustach posted:

not everything is references in Python, either.
I don't understand how this piece of Python shows what you indicate it shows. For the people who didn't follow the link:

quote:

code:
x = 1
y = x
x = 2
print x, y
Output:
code:
2 1
The way I read this, x is set to refer to the immutable object int(1), then y is also set to refer to the immutable object int(1), then x is set to refer to the immutable object int(2). I don't see anything about copy or pass-by-value semantics here. Am I mistaken somehow?

Edit:

supster posted:

Python is also strictly a pass-reference-by-value language, but it's important to understand that immutable objects (e.g., tuple, frozenset) have pass-by-value characteristics because when you make a change to a immutable object a copy is made and reassigned.

How can you make a change to an immutable object? That's a contradiction in terms. You can only come up with a new immutable object and potentially assign it the same name as the old value. If there's "pass-by-value characteristics," I'd like to see 'em. Maybe a code example?

Benji the Blade fucked around with this message at 01:52 on May 15, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Benji the Blade posted:

The way I read this, x is set to refer to the immutable object int(1), then y is also set to refer to the immutable object int(1), then x is set to refer to the immutable object int(2). I don't see anything about copy or pass-by-value semantics here. Am I mistaken somehow?

This is actually what happens under the hood too. Small ints in the range [-5,256] (I think that's the range) are preallocated by Python and x = 1 just sets x to refer to the global instance of 1 and increments the reference count appropriately. This has the amusing side effect that you can change the value of, say, 4 if you use the C API.

Benji the Blade
Jun 22, 2004
Plate of shrimp.

Avenging Dentist posted:

This is actually what happens under the hood too. Small ints in the range [-5,256] (I think that's the range) are preallocated by Python and x = 1 just sets x to refer to the global instance of 1 and increments the reference count appropriately.

Yeah. This has the unfortunate side-effect that comparing ints with "is" instead of "==" magically works for small, mostly positive integers, and then your code (usually) breaks on larger numbers or floats. Obviously, you shouldn't be using "is" for that anyway, but it got me when I was new to the language.

Avenging Dentist posted:

This has the amusing side effect that you can change the value of, say, 4 if you use the C API.

Okay, when I said changing an immutable value is a contradiction in terms earlier, that didn't involve breaking the language/runtime. That is hilarious.

dancavallaro
Sep 10, 2006
My title sucks

dancavallaro posted:

Ok, different approach. I'm gonna make this a server-side app instead, so my boss doesn't have to worry about running it. Basically what I'm doing is some parsing of log files, and my results are split up into about 20 different .CSV files. Basically what I want to do is to set it up so that my boss goes to the page, the script generates the .CSV files, writes them to a .zip file, deletes the .CSV files, and then sends the .zip file to the end-user to download. Right now I have it running on my laptop so that it creates the .zip file and deletes the .CSV files. How can I now make this a server-side app that takes that .zip file and sends it to the browser?

Figured it out. Nevermind.

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"

Avenging Dentist posted:

As opposed to passing references by reference.
Somewhere there's a copy involved. A "reference" is just a pointer, even C++'s weird references. It's always going to be pass [...] by value.

supster posted:

Python is also strictly a pass-reference-by-value language, but it's important to understand that immutable objects (e.g., tuple, frozenset) have pass-by-value characteristics because when you make a change to a immutable object a copy is made and reassigned.

I am not 100% confident about this as I am relatively new to Python, but I am pretty sure it is correct.
That's not pass-by-value. Pass-by-value means that every time the object is provided as a parameter to a function, it's copied. In languages that support variable assignment, it also indicates that assignment involves a full copy.

Mustach posted:

"Java/C#/Ruby/Python/etc (everything by-reference)" is an overstatement, too. C# structs have value semantics, Java has the whole primitives fiasco, and not everything is references in Python, either.
Whoops, forgot C# structs were mutable.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Janin posted:

Somewhere there's a copy involved. A "reference" is just a pointer, even C++'s weird references. It's always going to be pass [...] by value.

There is a practical difference, you know. Reassigning a reference in a pass by reference by value system doesn't affect the original, whereas it does in pass by reference by reference.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.

Avenging Dentist posted:

This is actually what happens under the hood too. Small ints in the range [-5,256] (I think that's the range) are preallocated by Python and x = 1 just sets x to refer to the global instance of 1 and increments the reference count appropriately. This has the amusing side effect that you can change the value of, say, 4 if you use the C API.
My life was so much brighter before reading this. I was going to change my example to x += 2 but I can already guess that the implementation is really a pointer increment (maybe). Why would -5 be the lower bound? This is all both freakish and fascinating. Time to start reading the documentation from page 1…

king_kilr
May 25, 2007

Mustach posted:

My life was so much brighter before reading this. I was going to change my example to x += 2 but I can already guess that the implementation is really a pointer increment (maybe). Why would -5 be the lower bound? This is all both freakish and fascinating. Time to start reading the documentation from page 1…

Integers aren't mutable so, += 2 isn't even an interesting operation, it sets x to a new integer who's value is 2 greater than before. I doubt the integer behavior is documented anywhere other than the source anyways.

dorkanoid
Dec 21, 2004

dancavallaro posted:

I used py2exe to make an executable of a Python script I wrote. It works great on the Windows XP computer that I created it on, and it works great on my Windows Vista laptop at home. But it doesn't work on my boss's Vista computer, and fails with the error "the application has failed to start because its side-by-side configuration is incorrect. Please see application event log for more details". Any ideas what could be wrong?

For the record (even if this is an old post, and seems resolved), this is most likely because the target computer needs the Visual C++ 2008 SP1 Redistributable Package (I'm guessing you use Python 2.6.0-2.6.1?)

I think this was fixed in 2.6.2, and it's related to issue #4566 (downgrading to Python 2.5 was not an option for me - I've used a lot of time getting stuff to work on 2.6).

Adbot
ADBOT LOVES YOU

tbradshaw
Jan 15, 2008

First one must nail at least two overdrive phrases and activate the tilt sensor to ROCK OUT!

Mustach posted:

My life was so much brighter before reading this. I was going to change my example to x += 2 but I can already guess that the implementation is really a pointer increment (maybe). Why would -5 be the lower bound? This is all both freakish and fascinating. Time to start reading the documentation from page 1…

This is just an optimization. The range is -5 to 256 because those are apparently the most commonly used integers and the CPython implementers found that range useful. It's a neat optimization, and really only ever comes up in situations where someone used "is" to compare integers instead of "=". A novel bit of trivia, and I only call it "just an optimization" because there's no zen-like understanding at the end of the this issue, it doesn't generalize or anything.

  • Locked thread