|
Ah, I just remembered. Classmethods are not really pythonic. They're kind of a kludge for implementing something like a static class method in Java/C++. The pythonic way to do static class methods is to make them module methods. So in this case you'd have a Record module with a read method. Inside the Record module there would be a Record class. Objects of the Record class type would be returned by the Record module's read method.
|
# ? Aug 23, 2012 18:32 |
|
|
# ? May 9, 2024 01:24 |
|
classmethods are very Pythonic, especially as factory methods. staticmethods are not.
|
# ? Aug 23, 2012 18:37 |
|
Dren posted:
Excellent. Thanks for your help with this. This helps me have a better grasp on the way to tackle this. One design question though, would it make more sense to subclass Record with something else and have the decode() method there? I have things like integers and lists that won't necessarily be able to be decoded. The other option would be to keep decode() as a method of Record and just return the original value if it is of a datatype that doesn't need to be decoded, right? The second option seems simpler, but I'm not sure if it's confusing/worse to have a decode method for an integer-containing record.
|
# ? Aug 23, 2012 18:44 |
|
Have you considered using something like pydicom? I've never used it and can't vouch for its functionality, but I get the feeling you're working on a solved problem (at least for most intents and purposes).
|
# ? Aug 23, 2012 18:46 |
|
Modern Pragmatist posted:Excellent. Thanks for your help with this. This helps me have a better grasp on the way to tackle this. It certainly makes sense to subclass Record and overload the decode operator. That said, I wouldn't do it. It sounds like for most of your data you don't need to do the decode in a second pass, you can decode it as it is unpacked because its decoding depends on its datatype. You could set up the factory method to return two types of records, UnknownEncRecords and KnownRecords. KnownRecords would have their data decoded right in the factory method. UnknownEncRecords would store binary data that is decoded after the encoding is determined. You could do something like not give KnownRecords a decode method but give UnknownEncRecords a decode method. Then, after all records have been read from the file, you could loop through them all and if hasattr(rec, 'decode') do the decode. There are a dozen ways to deal with this design problem in Python. My least favorite, in this case, would be to subclass for each of your datatypes. It'd just be a ton of code and for what? A bunch of decode methods that mostly do nothing? Bleh. Something I am given to is to create dictionary for the enum and do something like this: code:
|
# ? Aug 23, 2012 19:41 |
|
Just wanted to toss my two cents, as I've been heavily programming in Python at work. Holy poo poo blasting god drat! I mean, I knew about the power of interpreted/scripting languages, but you never can really appreciate that power until you actually program in it. Something like reading a file is a complete , coming from a heavy Java, C, and C# background. Two lines to open a file, loop through and read each line, AND close it? No,
|
# ? Aug 25, 2012 10:54 |
|
pliable posted:Just wanted to toss my two cents, as I've been heavily programming in Python at work. Python is as close to pseudocode as it gets.
|
# ? Aug 25, 2012 13:03 |
|
pliable posted:Just wanted to toss my two cents, as I've been heavily programming in Python at work. Don't know if you've come across this pattern yet. Say you have a file with two columns of numbers, and you want to read them into a list: Python code:
|
# ? Aug 25, 2012 14:51 |
|
ufarn posted:On the other hand, it really spoils you, when you suddenly start reading pseudocode in C in algorithm books. I completely agree. I'm going to have to return to C#/Java land soon, so it'll be interesting to make that switch. I've already broken the habit of putting semi-colons at the end of my statements Emacs Headroom posted:Don't know if you've come across this pattern yet. Say you have a file with two columns of numbers, and you want to read them into a list: That is precisely what I was talking about . Minus the append line and list assignment, it's a mere two lines. When I learned you don't even have to check for EOF, my head exploded.
|
# ? Aug 25, 2012 22:08 |
|
I recently stumbled upon this and while quite simple I can't help but marvel at the results.Python code:
And some high-resolution results:
|
# ? Aug 26, 2012 00:14 |
|
I've just started watching the MIT lectures found here: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008 The videos are not clear enough to view the code. Is there some better quality video or do they realize their error and get a better quality video camera or something? Not trying to decipher blurry code.
|
# ? Aug 26, 2012 00:50 |
|
huhu posted:I've just started watching the MIT lectures found here: You can look at the course readings here which has most of the material for the course.
|
# ? Aug 26, 2012 00:56 |
|
Captain Capacitor posted:I recently stumbled upon this and while quite simple I can't help but marvel at the results. What in the mother gently caress? I want to try this but, 18 minutes to process...
|
# ? Aug 26, 2012 01:04 |
|
Captain Capacitor posted:You can look at the course readings here which has most of the material for the course. Just realized that they actually zoom in to the coding later in the video so all is well. Have another question though. In the video they do operations such as 9/5 equals 1 and 'a' < 3 equals false. I understand the reason for this but I'm using Python 2.0 which gives answers of 1.8 and "TypeError: unorderable types: str() < int()" Are they using Python 1? If not what is the difference between their setup and mine?
|
# ? Aug 26, 2012 01:48 |
|
pliable posted:What in the mother gently caress? Python code:
Edit: Also, PyPy really does make a difference. Captain Capacitor fucked around with this message at 02:13 on Aug 26, 2012 |
# ? Aug 26, 2012 02:11 |
|
Too bad PyPy developers are sick of seeing mandelbrots already.
|
# ? Aug 26, 2012 02:53 |
|
Another small question. I see that 25**.5==9**.5+16**.5 is false and I understand why. What is the correct way to work around this?
|
# ? Aug 26, 2012 06:21 |
|
huhu posted:Another small question. I see that 25**.5==9**.5+16**.5 is false and I understand why. What is the correct way to work around this? 25 ** .5 == 5 9 ** .5 == 3 16 ** .5 == 4 3 + 4 != 5 not sure what you mean by a work around
|
# ? Aug 26, 2012 06:27 |
|
how!! posted:25 ** .5 == 5 Oh gently caress, I think it's time for me to stop coding for the night I'm starting to lose it. Thanks for clarifying. Edit:I swear I have a real question now. The problem is: quote:A Pythagorean triplet is a set of three natural numbers, a b c, for which, Here is my code: code:
huhu fucked around with this message at 18:04 on Aug 26, 2012 |
# ? Aug 26, 2012 06:32 |
|
Captain Capacitor posted:I recently stumbled upon this and while quite simple I can't help but marvel at the results. This is beautiful
|
# ? Aug 26, 2012 21:20 |
|
huhu posted:Oh gently caress, I think it's time for me to stop coding for the night I'm starting to lose it. Thanks for clarifying. These are small suggestions, but: Don't use the extra variables of a,b,c. Just use the x,y,z from the loops, i.e just write if x+y+z==1000. Put a quit() under the print. Right now it finds the correct number, prints it, and then just keeps on loopin'. You might be finding the the right number in the first 10 seconds, and then waiting the other 20 seconds for some needless looping to finish.
|
# ? Aug 27, 2012 06:45 |
|
huhu posted:Oh gently caress, I think it's time for me to stop coding for the night I'm starting to lose it. Thanks for clarifying. There is no reason for the third, innermost loop. Once you have chosen values for a and b, the value of c is fixed (namely, it is 1000 - a - b). You are looping a factor of 1000 times more than you should be. Since you are requiring that a < b, you could also build that requirement into the second range(). (i.e. have the range for b / y be dependent on the current value of a.)
|
# ? Aug 27, 2012 06:54 |
|
By the way as well, its much cleaner to use itertools.product for nested for loops like that. This is an example with two lists, but it can do more.Python code:
|
# ? Aug 27, 2012 07:42 |
|
pliable posted:What in the mother gently caress? That guy was on a slower Core 2. Took 10 minutes in regular Python and 3 minutes on IronPython on my i5.
|
# ? Aug 27, 2012 16:03 |
|
My solution to problem 9 takes 49.5 ms. It's written in python. However, it is not in the spirit of project euler to give you the answer. Instead I'll give you some hints: 1) You only need two loops. 2) You can greatly constrain the numbers that you have to search. For instance, the answer will obviously not be a = 1000, b = 1000, c = 1000 since a + b + c would be 3000, not 1000.
|
# ? Aug 27, 2012 19:09 |
|
Thanks everyone for your input, I'm going to take a look at your suggestions now. Have another question. If I have a def such as prime_test(#) and I have it setup to print 'nprime' if it's not prime and 'prime' if it's prime. Another part of my program has if prime_test(#)=='prime' then do something else. If I type prime_test(5) I get prime but if I type prime_test(5)=='prime' I get false. How do I make these two things interact with each other? Here is my code so far. code:
huhu fucked around with this message at 20:56 on Aug 27, 2012 |
# ? Aug 27, 2012 20:45 |
|
huhu posted:Thanks everyone for your input, I'm going to take a look at your suggestions now. Have another question. If I have a def such as prime_test(#) and I have it setup to print 'nprime' if it's not prime and 'prime' if it's prime. Another part of my program has if prime_test(#)=='prime' then do something else. If I type prime_test(5) I get prime but if I type prime_test(5)=='prime' I get false. How do I make these two things interact with each other? Here is my code so far. VV Much better explanation jusion fucked around with this message at 20:56 on Aug 27, 2012 |
# ? Aug 27, 2012 20:51 |
|
huhu posted:How do I make these two things interact with each other? You want to return a value, not print it. And since you're just testing whether something is true/false, then "return true" if you find a prime, and "return false" at the end of your loop if you don't; no need to do string comparisons. Returning will also implicitly break the loop; it jumps you out of your function when used.
|
# ? Aug 27, 2012 20:53 |
|
Played around with my code and got:code:
Also, forgot to include a counter for my while loop and took me ten minutes to figure out that was the problem. huhu fucked around with this message at 21:08 on Aug 27, 2012 |
# ? Aug 27, 2012 20:58 |
|
huhu posted:Also another question about the line if x<y<z and x**2+y**2==z**2: if it finds that x<y<z is false will it break the loop and go back to the beginning or will it still try to compute x**2+y**2==z**2? No, it won't compute x**2+y**2==z**2. Python will see the and, which means both sides need to be True, then see that the first part is False. Now it knows the whole thing will end up being False and it can short circuit the if evaluation and proceed on to the next value in the y loop, as you'd expect.
|
# ? Aug 27, 2012 21:25 |
|
huhu posted:Also another question about the line if x<y<z and x**2+y**2==z**2: if it finds that x<y<z is false will it break the loop and go back to the beginning or will it still try to compute x**2+y**2==z**2? I feel that this operation could slow down the code. When you write 'A and B' then if A is false then B will not be evaluated (this is called short circuiting, and a similar thing happens with 'or'). Really though, you don't need the 'if x < y < z' check at all if you set up the bounds of your loops to make it so this is always true.
|
# ? Aug 27, 2012 21:30 |
|
How do I return a numpy.float32 value using the C-API? I can return a Python builtin float withC++ code:
|
# ? Aug 28, 2012 01:38 |
|
xPanda posted:How do I return a numpy.float32 value using the C-API? Try these: http://docs.scipy.org/doc/numpy/reference/c-api.array.html#array-scalars
|
# ? Aug 28, 2012 02:23 |
|
I've got a question. Is it just me, or is del redundant? I can't seem to find anything worth doing with it that can't be done another way. I know that, for example, you could do: code:
code:
|
# ? Aug 28, 2012 03:16 |
|
What if you want to del a?
|
# ? Aug 28, 2012 03:24 |
|
super_chair posted:I'm sure that I'm missing something obvious here, could anyone tell me what it is? You can't replace it when deleting local names, and it's more convenient for lists when you delete from the middle, or by range: Python code:
Also used for dicts, so you get consistency: Python code:
|
# ? Aug 28, 2012 03:35 |
|
Lysidas posted:What if you want to del a? That's very rare. The only case I can imagine is if you litter the module namespace and want to clean up afterwards. super_chair posted:I've got a question. Python code:
Python code:
|
# ? Aug 28, 2012 03:45 |
|
Suspicious Dish posted:
Well, you could do a[:] = a[:2] there.
|
# ? Aug 28, 2012 03:52 |
|
super_chair posted:I've got a question. I used it just today to delete a key,value pair from a dictionary. I liked it better than using pop or popitem.
|
# ? Aug 28, 2012 07:54 |
|
|
# ? May 9, 2024 01:24 |
|
huhu posted:Played around with my code and got: You can create better constraints for the ranges you search.
|
# ? Aug 28, 2012 15:53 |