Open Side Menu Go to the Top
Register
** Python Support Thread ** ** Python Support Thread **

07-13-2012 , 07:21 PM
Quote:
Originally Posted by tyler_cracker
hippo,

done with these yet? i can suggest more when you're ready!
i'm jumping around but would love more suggestions. appreciated
** Python Support Thread ** Quote
07-13-2012 , 09:29 PM
Quote:
Originally Posted by daveT
$530 i5 Lenovo:

Manual copy: 10.962864498256714
List comprehension: 3.4187096686791882
My MBP w/ Retina:

Manual copy: 9.90991711617
List comprehension: 3.24251699448

Suck it!
** Python Support Thread ** Quote
07-13-2012 , 09:40 PM
You got better than the base model! Nice!

Last edited by kerowo; 07-13-2012 at 09:40 PM. Reason: or I got a lemon...
** Python Support Thread ** Quote
07-13-2012 , 10:05 PM
I got the upgraded model. 'Cause that's how I role!*





* When work pays for it...
** Python Support Thread ** Quote
07-13-2012 , 11:49 PM
Imagine what would happen if I paid $600, huh?

(Yeah, go ahead and pick on my 45pt font and limited color pallet)
** Python Support Thread ** Quote
07-14-2012 , 12:00 AM
Quote:
Originally Posted by tyler_cracker
Unittest

Functools(!)

Good luck!
That looks juicy, but why is it better than lambda, if that's your -- and not Guido's -- opinion?
** Python Support Thread ** Quote
07-14-2012 , 04:24 AM
Quote:
Originally Posted by daveT
That looks juicy, but why is it better than lambda, if that's your -- and not Guido's -- opinion?
I don't understand this question. It sounds to me like "This itertools module looks great, but why is it better than for, in your opinion?"
** Python Support Thread ** Quote
07-14-2012 , 02:25 PM
dave,

xhad's answer is on the money; your question is kinda weird because functools and lambda are largely orthogonal.

i have used functools.partial to get rid of a closure which existed solely to bind an argument from a model (python class) so that the view (template) could call that function without the argument being exposed unnecessarily. the real example is quite a bit more involved (and included some horrific and unreadable lambdas!) but the punch line is the resulting code is a lot easier to read and understand.

functool.wraps is also cool. at work we use a lot of python decorators, which is kind of mind-bending at first, but ultimately reduces a ton of code duplication and makes things more readable.

you can sort of use lambdas to do these things, but at the expense of readability.

Last edited by tyler_cracker; 07-14-2012 at 02:26 PM. Reason: i have no idea of guido's opinions on any of this :)
** Python Support Thread ** Quote
07-14-2012 , 02:29 PM
Guido hates lambda and would have removed it from Python 3 if not for the massive amounts of outcry when he announced it.
** Python Support Thread ** Quote
07-15-2012 , 12:33 PM
What are some good projects to do for a beginner?

In school I had classes in Matlab and C++ but I don't really feel that I learned that much. I've started trying to become competent in a programming language. I'm almost finished with a python book + doing some stuff on the internet. However I have no idea how to apply what I know, I don't really have a certain project that I want to do. Does anyone have some suggestions? Reading a book and doing exercises is only fun for so long.
** Python Support Thread ** Quote
07-15-2012 , 01:01 PM
klior,

necessity is the mother of invention. find an itch and scratch it!
** Python Support Thread ** Quote
07-15-2012 , 06:43 PM
Quote:
Originally Posted by tyler_cracker
dave,

xhad's answer is on the money; your question is kinda weird because functools and lambda are largely orthogonal.

i have used functools.partial to get rid of a closure which existed solely to bind an argument from a model (python class) so that the view (template) could call that function without the argument being exposed unnecessarily. the real example is quite a bit more involved (and included some horrific and unreadable lambdas!) but the punch line is the resulting code is a lot easier to read and understand.

functool.wraps is also cool. at work we use a lot of python decorators, which is kind of mind-bending at first, but ultimately reduces a ton of code duplication and makes things more readable.

you can sort of use lambdas to do these things, but at the expense of readability.
This is the chosen answer.

I looked it up a bit and found a few threads on Stack Overflow. They seemed pretty divided on this. One case stood out that lambda allows for un-ordered arguments, though I'm not sure why the person believed this is a good thing.
** Python Support Thread ** Quote
07-15-2012 , 10:22 PM
Quote:
Originally Posted by daveT
This is the chosen answer.


Quote:
I looked it up a bit and found a few threads on Stack Overflow. They seemed pretty divided on this. One case stood out that lambda allows for un-ordered arguments, though I'm not sure why the person believed this is a good thing.
not entirely sure what you mean, but there are a few ways "un-ordered arguments" can be useful:

- being able to just throw a dictionary of name-value pairs at a function instead of remembering "ok it wants foo, bar, baz... or is it foo, baz, bar?"

- being able to leave out args you don't care about/don't know about. handy for lots of things, though i'll admit i don't have a great use case for doing this with a lambda -- probably because i don't do a lot of functional programming in python.
** Python Support Thread ** Quote
07-16-2012 , 12:06 AM
What does lambda have to do with unordered arguments?

Code:
def foo(a, b):
    return (a,b)

>> foo(b=3, a=4)
(4, 3)
>> foo(**{'b': 4, 'a': 3})
(3, 4)
** Python Support Thread ** Quote
07-16-2012 , 03:05 AM
Quote:
Originally Posted by tyler_cracker
not entirely sure what you mean, but there are a few ways "un-ordered arguments" can be useful:

- being able to just throw a dictionary of name-value pairs at a function instead of remembering "ok it wants foo, bar, baz... or is it foo, baz, bar?"

- being able to leave out args you don't care about/don't know about. handy for lots of things, though i'll admit i don't have a great use case for doing this with a lambda -- probably because i don't do a lot of functional programming in python.
And here I thought functools suggested functional programming tools. <-- Tail call recursion fail, obv?

This is all good answers, but I guess I'm too amateur to see how it is helpful outside of command-line usage, whether you are talking about a named or anon function. Just seems sloppy to think you would need a function that takes so many arguments you can't conceivably run a program without making something out-of-order. Inside a program, I wouldn't want to do this trick, since I can already see it turning into a massive bug unless there is a comment that says: "hey, these are out of order, go look at file xyz to see what the issue is..." or maybe you have the offending function copy/pasted in the comment area, but that's just my initial impression.

When I read "out of order arguments," it sounds like there is an issue with a function trying to take way too many arguments to work on many different things and perhaps the function arguments aren't semantic, which I would think is going to be necessary if you have a function with so many arguments it can become confusing. I'm not saying there should be maximum amount of arguments as a rule per se, and there are times I would be able to appreciate this feature during debugging.

I'll admit that there a few issues with my logic.

don't get me wrong, I do use stuff like:

def myFun(one=1, two=2, three=3): pass

myFun(one=4)

Well, not THAT, of course...

Quote:
Originally Posted by Xhad
What does lambda have to do with unordered arguments?

Code:
def foo(a, b):
    return (a,b)

>> foo(b=3, a=4)
(4, 3)
>> foo(**{'b': 4, 'a': 3})
(3, 4)
Sorry, I wasn't clear. According to this, functools.partial does not accept out-of-order arguments, whereas lambda does, but that should be true when you consider lambda an anonymous function:

http://stackoverflow.com/questions/5...tion-vs-lambda

Some more links I found on front page of [everyone's favorite search engine]:

http://stackoverflow.com/questions/3...tial-necessary

funny how this one seques into itertools and then how to use lambda and functools:

http://stackoverflow.com/questions/2...ilter-function

Anyways, all of this makes me exited to look into functools.
** Python Support Thread ** Quote
07-16-2012 , 03:06 AM
Quote:
Originally Posted by fluorescenthippo
So I'm getting a good feel for python now. I would still like to be more familiar with all the popular packages and modules. What should I be getting familiar with? sys,os,itertools,etc...
What about decimal and some database module for that job?
** Python Support Thread ** Quote
07-16-2012 , 06:09 AM
i've started learning python and for the most part it's been rewarding. i can now do useless tasks with 4 or 5 lines of code and that makes me feel like i am learning.

i became very frustrated over the past hour or so trying to install easy_install. if i did it right, then i should be able to run from python cmd line (i think? a lot of the language/terms such as 'shell' are confusing for me...maybe i'm supposed to run from windows cmd line? not that that works either...):

>>>>easy_install mypackage

but i keep getting SyntaxError: invalid syntax

i'm using windows 7 64 bit python 2.7

most tilting part is that it's called easy_install and yet i ended up just installing my packages manually by pasting the right directory in the right folder. the packages are now working like a charm yet after googling and testing several possible solutions, easy_install remains a mystery to me.
** Python Support Thread ** Quote
07-16-2012 , 12:34 PM
you are meant to use the "windows cmd line".
** Python Support Thread ** Quote
07-16-2012 , 12:49 PM
ok thanks tyler cracker. i got a different error message using windows command line and i tried a few different solutions. it involved typing in "cd" which is probably one of the most basic things but i've never done before which probly illustrates the extent of my cluelessness.

but now it works! so glad i figured it out

now to figure out how to use my new packages
** Python Support Thread ** Quote
07-16-2012 , 08:02 PM
I'm relatively new to python, but not to programming, and I'm certainly new to pypoker-eval which I'm trying to use.

It all works well, except I was getting suspciously low equity, and discovered that summing all the ['ev'] entries in each hand consistently had the total equity at around 997.

I couldn't find any known bugs related to this, could it be a symptom of my implementation rather than a bug in pypoker-eval?
** Python Support Thread ** Quote
07-16-2012 , 10:14 PM
rounding error? chop equity being left out?
** Python Support Thread ** Quote
07-16-2012 , 11:27 PM
Quote:
Originally Posted by daveT
And here I thought functools suggested functional programming tools. <-- Tail call recursion fail, obv?
well it does, in the sense that it's about programming with functions as opposed to, say, purely declarative programming with BASIC [1].

Quote:
This is all good answers, but I guess I'm too amateur to see how it is helpful outside of command-line usage, whether you are talking about a named or anon function. Just seems sloppy to think you would need a function that takes so many arguments you can't conceivably run a program without making something out-of-order. Inside a program, I wouldn't want to do this trick, since I can already see it turning into a massive bug unless there is a comment that says: "hey, these are out of order, go look at file xyz to see what the issue is..." or maybe you have the offending function copy/pasted in the comment area, but that's just my initial impression.

When I read "out of order arguments," it sounds like there is an issue with a function trying to take way too many arguments to work on many different things and perhaps the function arguments aren't semantic, which I would think is going to be necessary if you have a function with so many arguments it can become confusing. I'm not saying there should be maximum amount of arguments as a rule per se, and there are times I would be able to appreciate this feature during debugging.
i agree that wanting to do crazy things with args is a smell indicating you need to rethink your design.

sometimes you need to do weird things because you're stuck interoperating with a component you can't control and want to do something it wasn't strictly designed for. unit/integration testing is one place where this can happen.

heh, i know amber, author of the accepted answer.

i think it's no coincidence that in all three of your SO links, someone points out that a list comprehension or generator expression is way shorter and easier to read. functional programming in python is like commuting on a unicycle: every once in a while it will be the most elegant solution, but mostly you look ridiculous and there are far easier ways to get the job done.


[1] in before debate about whether GOSUB or some kind of GOTO pattern constitute "functional programming". also in before "my programs are functional... barely :rimshot:".
** Python Support Thread ** Quote
07-17-2012 , 12:19 AM
trying to learn about the IDLE debugger. trying to watch videos like this:http://www.youtube.com/watch?v=bZZTeKPRSLQ
but it looks like they are on linux. cant even get the first line import buggy to run.

any good ways to learn? also i heard WinPBD is pretty good so i might look into that over pdb
** Python Support Thread ** Quote
07-17-2012 , 12:51 AM
we use ipdb at work. that's all i can tell ya.
** Python Support Thread ** Quote
07-17-2012 , 03:05 AM
Quote:
Originally Posted by tyler_cracker
i agree that wanting to do crazy things with args is a smell indicating you need to rethink your design.

sometimes you need to do weird things because you're stuck interoperating with a component you can't control and want to do something it wasn't strictly designed for. unit/integration testing is one place where this can happen.
I see. For a second there I was thinking I was diving off a cliff into water far beyond my depth and I was going to start embarrassing myself really bad and you were going to knowledge own me so hard I was just going to have a massive brain spark.

Thanks for these answers. They're really interesting a excellent food for thought. The decorators stick out for me as well. I've been meaning to see how they function, but I haven't gotten around to it.

Quote:
heh, i know amber, author of the accepted answer.

i think it's no coincidence that in all three of your SO links, someone points out that a list comprehension or generator expression is way shorter and easier to read. functional programming in python is like commuting on a unicycle: every once in a while it will be the most elegant solution, but mostly you look ridiculous and there are far easier ways to get the job done.
I would be an interesting Python indeed if it was somehow capable of purely imperative, object-oriented, and functional at the same time!

Funny quote I found in some not-python wiki on some not-python functional programming language:

Quote:
How to write x = x+ 1
user=> [code]

Or, a more idiomatic way would be to use inc:

user=> [code]

The most idiomatic way is not do this at all. Why would you want to do x=x+1 in [not python, obv]?
** Python Support Thread ** Quote

      
m