|
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. 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:code:
|
# ? Dec 14, 2010 21:06 |
|
|
# ? May 9, 2024 07:14 |
|
tef 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.
|
# ? Dec 14, 2010 23:40 |
|
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?
|
# ? Dec 15, 2010 05:46 |
|
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.
|
# ? Dec 15, 2010 06:06 |
|
tripwire posted:Use numpy arrays.
|
# ? Dec 15, 2010 07:05 |
|
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 |
# ? Dec 16, 2010 05:39 |
|
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
|
# ? Dec 16, 2010 17:31 |
|
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 |
# ? Dec 17, 2010 09:22 |
|
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.
|
# ? Dec 17, 2010 14:11 |
|
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.
|
# ? Dec 18, 2010 02:35 |
|
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?
|
# ? Dec 19, 2010 04:01 |
|
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?
|
# ? Dec 19, 2010 04:31 |
|
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()
|
# ? Dec 19, 2010 05:07 |
|
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:
|
# ? Dec 19, 2010 05:19 |
|
MaberMK posted:
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:
code:
|
# ? Dec 19, 2010 05:31 |
|
Jam2 posted:
you are close.. The true is just implied though code:
|
# ? Dec 19, 2010 05:40 |
|
== : 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.
|
# ? Dec 19, 2010 05:43 |
|
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. 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?
|
# ? Dec 19, 2010 05:54 |
|
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. 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:
|
# ? Dec 19, 2010 06:05 |
|
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. 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 ?
|
# ? Dec 19, 2010 06:40 |
|
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.
|
# ? Dec 19, 2010 06:44 |
|
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. This code:
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.
|
# ? Dec 19, 2010 07:10 |
|
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.
|
# ? Dec 19, 2010 07:14 |
|
So I just finished project Euler #3, What is the largest prime factor of the number 600851475143 ?:code:
|
# ? Dec 19, 2010 08:02 |
|
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? 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:
|
# ? Dec 19, 2010 08:10 |
|
Tennis Ball posted:So I just finished project Euler #3, What is the largest prime factor of the number 600851475143 ?: 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)
|
# ? Dec 19, 2010 08:13 |
|
Tennis Ball posted:So I just finished project Euler #3, What is the largest prime factor of the number 600851475143 ?: 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.
|
# ? Dec 19, 2010 08:24 |
|
Finally.code:
|
# ? Dec 19, 2010 08:30 |
|
Stabby McDamage posted:A few notes: 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.
|
# ? Dec 19, 2010 08:53 |
|
Here is a one-link solution: http://www.wolframalpha.com/input/?i=+largest+prime+factor+of+600851475143
|
# ? Dec 19, 2010 09:21 |
|
Jam2 posted:Finally. 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:
|
# ? Dec 19, 2010 15:19 |
|
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?
|
# ? Dec 19, 2010 18:23 |
|
the purpose of that puzzle is to get people comfortable with the submission system, beyond hello world.
|
# ? Dec 19, 2010 18:55 |
|
tef posted:the purpose of that puzzle is to get people comfortable with the submission system, beyond hello world. I've never used Python before okay!
|
# ? Dec 19, 2010 19:05 |
|
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.
|
# ? Dec 19, 2010 19:08 |
|
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.
|
# ? Dec 19, 2010 21:22 |
|
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
|
# ? Dec 19, 2010 21:40 |
|
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.
|
# ? Dec 20, 2010 00:37 |
|
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:
|
# ? Dec 20, 2010 00:39 |
|
|
# ? May 9, 2024 07:14 |
|
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 |
# ? Dec 20, 2010 00:53 |