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
Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Sailor_Spoon posted:

Depends on your user, I guess. I'd much rather get an error then have the function return None, which could mean a lot of things.

Also, does a file that's not seekable still have the seek method? I assumed it does, so an error on seek would presumably not get caught by that except.

Not having a seek method would be an AttributeError.

I caused an IOError when I was loving around testing seek on different things I didn't think would be seekable, but, now that you mention it, it might not be universal across implementations/types of unseekable files.

tef posted:

:v:
code:
temp_file = open(tempfile.mkstemp(), 'rw')
for line in input_file:
   temp_file.write(line)
temp_file.seek(0, os.SEEK_SET)
for line in temp_file:
   yield line
Eureka! :toot:

Adbot
ADBOT LOVES YOU

tripwire
Nov 19, 2004

        ghost flow

tef posted:

code:

with open(...) as fh:
     lines = reversed(fh.readlines())

:v:


Yeah but in python 2.X that has to read the whole file into memory at once, no? I've written a grungy generator before for an environment where I'm forced to use python 2.4; it would spit the file out backwards using seek, while bundling it up into chunks delimited by the line seperator. I'm not really proud of that, and god knows how it would behave on file-like objects which don't seek like a file does.

Rohaq
Aug 11, 2006
So, what are my alternatives to using gigantic lists in Pyth 2.7? I'm trying to use matplotlib to produce a pretty graph the results generated by a diff tool I created for large binary files, which produces a text file with the start/end byte of differences, but some as far as I can see, I'd have to create some goddamned massive lists in memory in order for matplotlib to do it's thing. Is there any way to make it use ranges when generating graphs, or am I stuck?

tripwire
Nov 19, 2004

        ghost flow

Rohaq posted:

So, what are my alternatives to using gigantic lists in Pyth 2.7? I'm trying to use matplotlib to produce a pretty graph the results generated by a diff tool I created for large binary files, which produces a text file with the start/end byte of differences, but some as far as I can see, I'd have to create some goddamned massive lists in memory in order for matplotlib to do it's thing. Is there any way to make it use ranges when generating graphs, or am I stuck?

Use numpy arrays.

Rohaq
Aug 11, 2006

tripwire posted:

Use numpy arrays.
I've not tried those before; I'll give them a look, thanks!

DirtyDiaperMask
Aug 11, 2003

by Ozmaugh

tripwire posted:

Yeah but in python 2.X that has to read the whole file into memory at once, no? I've written a grungy generator before for an environment where I'm forced to use python 2.4; it would spit the file out backwards using seek, while bundling it up into chunks delimited by the line seperator. I'm not really proud of that, and god knows how it would behave on file-like objects which don't seek like a file does.

Wouldn't f.readlines() still read the entire file into memory even in Python 3?

DirtyDiaperMask fucked around with this message at 05:41 on Dec 16, 2010

Lurchington
Jan 2, 2003

Forums Dragoon
http://docs.python.org/library/stdtypes.html#file.readlines says list, as does http://docs.python.org/py3k/library/io.html#io.IOBase.readlines

so, I think since it's a list, it's all in memory. Now, if you subclasses the FileObject type to supper __reversed__ then reversed(your_subclassed_file) would work

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.
EDIT: I deemed this threadworthy, due to not fitting into any existing megathread.

Sinestro fucked around with this message at 11:20 on Dec 17, 2010

SirPablo
May 1, 2004

Pillbug

tripwire posted:

Use numpy arrays.

Seconded. Numpy is the poo poo for working with large->huge datasets. I am working on a project now using weather model data that gets dumped into a 181x360x50 array and it works sexy fast.

tripwire
Nov 19, 2004

        ghost flow

DirtyDiaperMask posted:

Wouldn't f.readlines() still read the entire file into memory even in Python 3?

Woops, I guess I was mistaken; I thought that python 3 had changed readlines to act like xreadlines. Looking closer, you're just supposed to use the file object itself as an iterable.

Jam2
Jan 15, 2008

With Energy For Mayhem
In Mathematica, it's simple to test whether an element, n, is an integer. I can type Element[n,Integers] and get a true or false. How do I do a similar test in python?

ErIog
Jul 11, 2001

:nsacloud:

Jam2 posted:

In Mathematica, it's simple to test whether an element, n, is an integer. I can type Element[n,Integers] and get a true or false. How do I do a similar test in python?

What type are the elements you're wanting to test?

Lurchington
Jan 2, 2003

Forums Dragoon

Jam2 posted:

In Mathematica, it's simple to test whether an element, n, is an integer. I can type Element[n,Integers] and get a true or false. How do I do a similar test in python?

Seems pretty spot-on for isinstance()

MaberMK
Feb 1, 2008

BFFs

Jam2 posted:

In Mathematica, it's simple to test whether an element, n, is an integer. I can type Element[n,Integers] and get a true or false. How do I do a similar test in python?

code:
>>> x = 0
>>> type(x) == int
True

Jam2
Jan 15, 2008

With Energy For Mayhem

MaberMK posted:

code:
>>> x = 0
>>> type(x) == int
True

This is what I need. Now, how would I structure this to get the following logic:

If x is an integer, then do ...

In Mathematica, the only lang in which I have ANY experience, it would go:

code:
If[Element[n/15,Integers]==true,*then whatever I want to do*]
I image in py it's something along the lines of:
code:
If type(x) == int == True:
...
This doesn't seem right for Python though.

ATLbeer
Sep 26, 2004
Über nerd

Jam2 posted:

code:
If type(x) == int == True:
...
This doesn't seem right for Python though.

you are close.. The true is just implied though

code:
if type(x) == int:
    #do stuff here if true
else:
    #do stuff here if false

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
== : any X any -> bool. You don't need == True. Also If and if are different things as far as Python knows. Are you trying to find out if the type of a variable's value is int or are you trying to determine if a value is integral? Those are two very different things and everyone is answering the former question.

Jam2
Jan 15, 2008

With Energy For Mayhem
Why is Python turning x = n/15 into a float when n = 15. 1.0 is a goddamn integer Stop making my life difficult.

Lonely Wolf posted:

== : any X any -> bool. You don't need == True. Also If and if are different things as far as Python knows. Are you trying to find out if the type of a variable's value is int or are you trying to determine if a value is integral? Those are two very different things and everyone is answering the former question.
Thanks for the pitfall prevention.

I need to be able to tell when a number(a) is cleanly divisible by another number(b). Previously, I would test whether a/b == int. Is there a better way to do this in Python?

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"

Jam2 posted:

Why is Python turning x = n/15 into a float when n = 15. 1.0 is a goddamn integer Stop making my life difficult.

Thanks for the pitfall prevention.

I need to be able to tell when a number(a) is cleanly divisible by another number(b). Previously, I would test whether a/b == int. Is there a better way to do this in Python?

Python has three relevant numeric types: 'int', for arbitrary-size integers, 'float', for 64-bit floating-point, and 'decimal.Decimal', for arbitrary-precision rationals.

If you want to test whether a value is of type 'int', use type() or isinstance().

If you want to test whether a value is integral, use the modulus operator %:

code:
def integral(x):
  return x % 1 == 0

print integral(1) # True
print integral(1.0) # True
print integral(1.5) # False

Jam2
Jan 15, 2008

With Energy For Mayhem

Janin posted:

Python has three relevant numeric types: 'int', for arbitrary-size integers, 'float', for 64-bit floating-point, and 'decimal.Decimal', for arbitrary-precision rationals.

If you want to test whether a value is of type 'int', use type() or isinstance().

If you want to test whether a value is integral, use the modulus operator %:

code:
def integral(x):
  return x % 1 == 0

print integral(1) # True
print integral(1.0) # True
print integral(1.5) # False

Documentation says...

quote:

For (plain or long) integer division, the result is an integer. The result is always rounded towards minus infinity: 1/2 is 0, (-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0. Note that the result is a long integer if either operand is a long integer, regardless of the numeric value.

Why is 15/15 = 1.0 ?

hlfrk414
Dec 31, 2008
Are you using Python 3? Division always returns a float in 3. If you need integer division, you are supposed to use //, which works in both 2 and 3.

DirtyDiaperMask
Aug 11, 2003

by Ozmaugh

Jam2 posted:

Why is Python turning x = n/15 into a float when n = 15. 1.0 is a goddamn integer Stop making my life difficult.

Thanks for the pitfall prevention.

I need to be able to tell when a number(a) is cleanly divisible by another number(b). Previously, I would test whether a/b == int. Is there a better way to do this in Python?

This
code:
def isdiv(a, b): return not bool(a % b)
will work.

Or just "not a % b". a/b == int is equivalent to a/b == True, since the boolean value of builtins(and most other stuff) is True.

Jam2
Jan 15, 2008

With Energy For Mayhem

hlfrk414 posted:

Are you using Python 3? Division always returns a float in 3. If you need integer division, you are supposed to use //, which works in both 2 and 3.

Yeah, I'm using 3. If I use //, it turns all results into int's which invalidates my logic test. I want to know when a division operation results in an int naturally. What's a good way to get around this?

I'm solving the Hoppity Hop Facebook puzzle. I already solved it using Mathematica and now I'm trying my hand in Python. I've never used this lang before. It's all really new to me.

Tennis Ball
Jan 29, 2009
So I just finished project Euler #3, What is the largest prime factor of the number 600851475143 ?:

code:
import math
i = 1
k = 1
j = 1
t = 0
list = []
factors = []

while i < math.sqrt(600851475143):
        if 600851475143%i==0:
                list.append(i)
        i = i + 1
while list!=[]:      
        k = list.pop()
        j = 1
        t = 0
        while j<k:
                if k%j==0:
                        t = t + 1
                j = j + 1
        if t > 2:
                print 'skipped'
        if t < 2:
                print k 

print 'finished'
It gives the right answer, but is there anything in there that is bad practice? I want to avoid these sorts of things before I form bad habits.

Stabby McDamage
Dec 11, 2005

Doctor Rope

Jam2 posted:

Yeah, I'm using 3. If I use //, it turns all results into int's which invalidates my logic test. I want to know when a division operation results in an int naturally. What's a good way to get around this?

I'm solving the Hoppity Hop Facebook puzzle. I already solved it using Mathematica and now I'm trying my hand in Python. I've never used this lang before. It's all really new to me.

You may need rethink how you're approaching a lot of things going from Mathematica to Python. Mathematica is designed to hide all the computery aspects of the system and emulate pure math and logic, so you can say things like "does this division naturally produce an integer?".

That doesn't translate directly to Python, because Python is more in the vein of traditional programming languages, where there are separate numerical types for different tasks (float vs. integer), and a given operation will produce a result of a given type, regardless of if it "needs" to use a float vs. an int.

Basically, Mathematica does a ton of magic behind the scenes involving floating point conversion, arbitrary precision, rounding, rational representation, etc. Python doesn't (by default).

Getting back to your question, the traditional way to ask if two numbers are evenly divisible is to ask if their remainder is zero. The modulus operator ('%') in Python gives the remainder, so you can do your check with:

code:
if a % b == 0:
    print "%d is divisible by %d" % (a,b)

tef
May 30, 2004

-> some l-system crap ->

Tennis Ball posted:

So I just finished project Euler #3, What is the largest prime factor of the number 600851475143 ?:

It gives the right answer, but is there anything in there that is bad practice? I want to avoid these sorts of things before I form bad habits.


Read PEP8

Use real variable names. ijk is for fortran. factors is unused. list is a terrible name for a list.

Don't put computations in while conditions. (i.e take that sqrt out because it gets executed every loop)

Stabby McDamage
Dec 11, 2005

Doctor Rope

Tennis Ball posted:

So I just finished project Euler #3, What is the largest prime factor of the number 600851475143 ?:

code:
import math
i = 1
k = 1
j = 1
t = 0
list = []
factors = []

while i < math.sqrt(600851475143):
        if 600851475143%i==0:
                list.append(i)
        i = i + 1
while list!=[]:      
        k = list.pop()
        j = 1
        t = 0
        while j<k:
                if k%j==0:
                        t = t + 1
                j = j + 1
        if t > 2:
                print 'skipped'
        if t < 2:
                print k 

print 'finished'
It gives the right answer, but is there anything in there that is bad practice? I want to avoid these sorts of things before I form bad habits.

A few notes:

1. You should define any value you use more than once in a variable, instead of writing the literal number in multiple places. So put "value=600851475143" near the top, and reference that from then on. This makes the code more readable, and lets you adapt the code to take a value as an input later on.

2. Minor issue: putting the math.sqrt call in the while loop means that you're calculating the square root EVERY trip through the loop, even though it doesn't change. (I don't think python is smart enough to realize it's constant.)

3. "while list!=[]" → "while list"

4. Don't use a variable called "list", because list is the name of the type, and you may need to use that name later on.

5. Don't use a while loop when you can use a for. For example, change the first loop to a "for i in xrange(math.sqrt(value))".

5a. Better yet, for simple loops, use a list comprehension instead. For example, you can drop the whole first loop and replace it with "factors = [i for i in xrange(math.sqrt(value)) if value%i==0]"

6. You don't need to pre-define all your variables at the top if you're going to redefine them later. That goes for k, j, and t.

7. The algorithm of the second loop could be improved in a ton of ways. Most simply, the prime checker is very inefficient -- why check for divisibility by 4 when you've already checked 2? Why check 9 when you've checked 3? What's more, you could actually rewrite the whole program to be vastly more efficient by doing prime factorization to begin with on the number...how many small factors could you divide out of that number with very little work?

I'm sure there's a very clever two-line solution to this, but it's 2AM here, so I'm too tired to think about it.

Jam2
Jan 15, 2008

With Energy For Mayhem
Finally.

code:
def Hoppity(n):
	for i in range(1,n+1):
		x = i % 15
		y = i % 5
		z = i % 3
		if x==0:
			print('Hop')
		elif y==0:
			print('Hophop')
		elif z==0:
			print('Hoppity')

Tennis Ball
Jan 29, 2009

Stabby McDamage posted:

A few notes:

1. You should define any value you use more than once in a variable, instead of writing the literal number in multiple places. So put "value=600851475143" near the top, and reference that from then on. This makes the code more readable, and lets you adapt the code to take a value as an input later on.

2. Minor issue: putting the math.sqrt call in the while loop means that you're calculating the square root EVERY trip through the loop, even though it doesn't change. (I don't think python is smart enough to realize it's constant.)

3. "while list!=[]" → "while list"

4. Don't use a variable called "list", because list is the name of the type, and you may need to use that name later on.

5. Don't use a while loop when you can use a for. For example, change the first loop to a "for i in xrange(math.sqrt(value))".

5a. Better yet, for simple loops, use a list comprehension instead. For example, you can drop the whole first loop and replace it with "factors = [i for i in xrange(math.sqrt(value)) if value%i==0]"

6. You don't need to pre-define all your variables at the top if you're going to redefine them later. That goes for k, j, and t.

7. The algorithm of the second loop could be improved in a ton of ways. Most simply, the prime checker is very inefficient -- why check for divisibility by 4 when you've already checked 2? Why check 9 when you've checked 3? What's more, you could actually rewrite the whole program to be vastly more efficient by doing prime factorization to begin with on the number...how many small factors could you divide out of that number with very little work?

I'm sure there's a very clever two-line solution to this, but it's 2AM here, so I'm too tired to think about it.

Thanks for the tips! The last part though "clever two-line solution" doesn't interest me right now. I am intentionally more complicated ways so that I can get feedback on the things you mentioned above.

tef
May 30, 2004

-> some l-system crap ->
Here is a one-link solution: http://www.wolframalpha.com/input/?i=+largest+prime+factor+of+600851475143 :v:

ErIog
Jul 11, 2001

:nsacloud:

Jam2 posted:

Finally.

code:
def Hoppity(n):
	for i in range(1,n+1):
		x = i % 15
		y = i % 5
		z = i % 3
		if x==0:
			print('Hop')
		elif y==0:
			print('Hophop')
		elif z==0:
			print('Hoppity')

It's funny to find out after all your questions that you're doing a FizzBuzz problem.

Also, there's still weirdness in your code. There's no reason to store the result of your modulus operations if you're going to use them once three lines below. This is making you execute more modulus operations than you need. In theory it's inefficient, but in practice this probably doesn't make your code faster.

code:
def Hoppity(n):
	for i in range(1,n+1):
		if not i % 15:
			print('Hop')
		elif not i % 5:
			print('Hophop')
		elif not i % 3:
			print('Hoppity')

Jam2
Jan 15, 2008

With Energy For Mayhem
Makes sense that they'd have a fizzbuzz problem then. Your link confirms that these puzzles are things companies expect hires to be able to solve quickly in order to be of any use.

I'm just learning. So why not start with "important" programs? My ideas are too big for my current skillet, or lack thereof. I want to create intelligent subscription based music software. Presently, I wouldn't know where to start. I'm better off tackling many small challenges and developing an understanding. Learning is an Iterative process. By the time I work my way through to the most difficult puzzles, I will have encountered pretty much every aspect of programming and utilized useful techniques along the way, no?

So far I've dealt with a lot of mathematical logic when programming. The next puzzle I want to try requires some new skills. I need to parse a text file, assign strings to values, create and append lists of strings, and do logic tests with strings. I figure Python should have a cleanly-written sentence or two on each of these tasks, right?

tef
May 30, 2004

-> some l-system crap ->
the purpose of that puzzle is to get people comfortable with the submission system, beyond hello world.

Jam2
Jan 15, 2008

With Energy For Mayhem

tef posted:

the purpose of that puzzle is to get people comfortable with the submission system, beyond hello world.

:arghfist: I've never used Python before okay! :arghfist:

ErIog
Jul 11, 2001

:nsacloud:
Yeah, my comment about FizzBuzz wasn't meant to be a knock against you. I was just assuming that since you said you were so used to Mathematica that you must be trying to do something in Python more complicated than FizzBuzz. I completely understand doing problems like those to help you learn a language. I spend a lot of time doing project euler problems.

tripwire
Nov 19, 2004

        ghost flow
Jam2, if you are coming from mathematica or matlab, you may want to check out numpy/scipy and matplotlib. They are python libraries for numerical/scientific computation, and graphing/plotting, respectively.

Jam2
Jan 15, 2008

With Energy For Mayhem

tripwire posted:

Jam2, if you are coming from mathematica or matlab, you may want to check out numpy/scipy and matplotlib. They are python libraries for numerical/scientific computation, and graphing/plotting, respectively.

Thanks

Phayray
Feb 16, 2004
This has probably been answered in the last 130 pages or so, but is there a way to get the python3 (command line) shell to syntax highlight on the fly via a plugin, or do I have to make scripts via vim/emacs/IDLE to get syntax highlighting? It's not a big deal, it would just be nice.

Bodhi Tea
Oct 2, 2006

seconds are secular, moments are mine, self is illusion, music's divine.
I'm trying to do form based authentication with urllib2, I'm following the code given here: http://personalpages.tds.net/~kent37/kk/00010.html

So basically I have something that looks like this (taken from the site):

code:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)
params = urllib.urlencode(dict(username='joe', password='secret'))
f = opener.open('http://coolsite.example.com/login/', params)
The problem is that it seems that even if I provide bogus credentials, the login page is retrieved without a problem, so no error message is raised (that I'm aware of). How can I handle the case where I provide invalid username and password?

Adbot
ADBOT LOVES YOU

Jam2
Jan 15, 2008

With Energy For Mayhem
In what sort of environment do you guys code Python? I've been juggling notepad++ and the Python shell, pasting code across and evaluating it to see when it breaks. Is there a better way? I watched a Google lecture on Python. The speaker utilized Terminal and executed Python in that way. How would I do this in Windows? Should I work in OS X?

How do I import a text file and go about parsing the contents? Do I need to import modules to do this? I want to be able to locate certain patterns of data, create lists, strings, and assign values to variables.

I've been using Python 3. Is this a bad idea? Should I stick with a version that's been in production for a while? The Facebook puzzle robot accepts Python 2.5.2. A visit to the Python 2.5.2 page informs that this version has been replaced by 2.5.5. What is a newbie to do?

Jam2 fucked around with this message at 00:56 on Dec 20, 2010

  • Locked thread