Re: Rép : Why is str(None) == 'None' and not an empty string?
On Thursday, August 29, 2013 12:55:36 PM UTC+2, Ian wrote: > On Wed, Aug 28, 2013 at 5:42 AM, Fabrice POMBET wrote: > > > > > > On 8/28/2013 4:57 AM, Piotr Dobrogost wrote: > > > > > >> Having repr(None) == 'None' is sure the right thing but why does str(None) > >> == 'None'? Wouldn't it be more correct if it was an empty string? > > > > > > the point of str(obj) is to return a string containing the obj (a sequence > > of characters if it is unbound or not built-in, etc.)... > > > > > > If you set the rule str(None)=="", then you will cause plenty of problems. > > > > > > For instance, if you want to build a string like request="SELECT X"+"IN > > Y"+"WHERE B="+String(B) > > > to prepare a sequel request, and the field B happens to be sometimes > > "None", you would automatically end up with """SELECT X IN Y WHERE B=''""" > > instead of """SELECT X IN Y WHERE B='None'""", > > > and your sql request will fall into limbos... > > > > The proper way to pass values into a SQL query is by using bind > > parameters. Inserting them into the query string by concatenation is > > error-prone and an excellent way to write code that is vulnerable to > > SQL injection attacks. > > > > The DB API guarantees that the object None will map to the database > > value NULL when passed directly as a parameter. The value returned by > > str(None) is irrelevant in this context. I could not agree more with you. The purpose of my post, however, was only to give a simple illustration of how such a generic change would make everything awkward, not to give any proper, precise or general directions on how to code a safe SQL request for a DB when you are online. Thank you however for your corrections. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a function that applies list of functions to a value?
On Wednesday, August 28, 2013 2:52:46 PM UTC+2, AdamKal wrote: > Hi, > > > > From time to time I have to apply a series of functions to a value in such a > way: > > > > func4(func3(func2(func1(myval > > > > I was wondering if there is a function in standard library that would take a > list of functions and a initial value and do the above like this: > > > > func_im_looking_for([func1, func2, func3, func4], myval) > > > > I looked in itertools but nothing seamed to do the job. This seams like > something vary obvious that was needed many times elsewhere so maybe you > could help me? My way is so obvious that it may not be that interesting... def func4(f1,f2,f3,f4): def anon(x): f1(f2(f3(f4(x return anon -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a function that applies list of functions to a value?
On Thursday, August 29, 2013 11:05:38 PM UTC+2, Chris Angelico wrote: > On Fri, Aug 30, 2013 at 6:50 AM, wrote: > > > My way is so obvious that it may not be that interesting... > > > > > > def func4(f1,f2,f3,f4): > > > def anon(x): > > > f1(f2(f3(f4(x > > > return anon > > > > Or have it return the result of f1. And then, since it's an anonymous > > function that simply returns an expression, I'd write it as: > > > > def func4(f1,f2,f3,f4): > > return lambda x: f1(f2(f3(f4(x > > > > Of course, that's still restricted to precisely four args. Extending > > this concept to a variable number of arguments is, uhh, left as an > > exercise to the reader. Which will probably end up going back to > > reduce(). :) > > > > ChrisA Chris, call me a snob, but I resent using lambdas (aren't they usually considered odd/bad practice in python?) -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a function that applies list of functions to a value?
On Thursday, August 29, 2013 11:05:38 PM UTC+2, Chris Angelico wrote: > On Fri, Aug 30, 2013 at 6:50 AM, wrote: > > > My way is so obvious that it may not be that interesting... > > > > > > def func4(f1,f2,f3,f4): > > > def anon(x): > > > f1(f2(f3(f4(x > > > return anon > > > > Or have it return the result of f1. And then, since it's an anonymous > > function that simply returns an expression, I'd write it as: > > > > def func4(f1,f2,f3,f4): > > return lambda x: f1(f2(f3(f4(x > > > > Of course, that's still restricted to precisely four args. Extending > > this concept to a variable number of arguments is, uhh, left as an > > exercise to the reader. Which will probably end up going back to > > reduce(). :) > > > > ChrisA Here is the generalisable version: def comp(*func): def anon(x): res=x for f in func: res=f(res) return res return anon you can then compose your function in __main__: f4=comp(f5,f6,f7) and call it: print(x) ... (I hope that Chris is happy now...) -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a function that applies list of functions to a value?
On Wednesday, August 28, 2013 3:10:49 PM UTC+2, Jussi Piitulainen wrote:
> AdamKal writes:
>
>
>
> > Hi,
>
> >
>
> > From time to time I have to apply a series of functions to a value
>
> > in such a way:
>
> >
>
> > func4(func3(func2(func1(myval
>
> >
>
> > I was wondering if there is a function in standard library that
>
> > would take a list of functions and a initial value and do the above
>
> > like this:
>
> >
>
> > func_im_looking_for([func1, func2, func3, func4], myval)
>
> >
>
> > I looked in itertools but nothing seamed to do the job. This seams
>
> > like something vary obvious that was needed many times elsewhere so
>
> > maybe you could help me?
>
>
>
> If you can have things backwards, or have func_im_looking_for reverse
>
> things first, you can do it this way:
>
>
>
> from functools import reduce
>
>
>
> def funcall(arg, fun): return fun(arg)
>
>
>
> def func1(arg): return 'f1({})'.format(arg)
>
> def func2(arg): return 'f2({})'.format(arg)
>
> def func3(arg): return 'f3({})'.format(arg)
>
>
>
> # prints: f1(f2(f3(3.14)))
>
> print(reduce(funcall, (func3, func2, func1), 3.14))
I had never thought about the "f({0})".format(arg) trick, that is excellent,
thank you!!!
--
http://mail.python.org/mailman/listinfo/python-list
Re: Is there a function that applies list of functions to a value?
On Friday, August 30, 2013 4:09:45 AM UTC+2, Steven D'Aprano wrote: > On Thu, 29 Aug 2013 13:50:39 -0700, fp2161 wrote: > > > > > My way is so obvious that it may not be that interesting... > > > > > > def func4(f1,f2,f3,f4): > > > def anon(x): > > > f1(f2(f3(f4(x > > > return anon > I don't think "obvious" is quite the right description. Well, perhaps > > "obviously wrong" :-) > You also need to define func1 (trivial), func2, func3, func5, func6, > > func7, func8, ..., func2147483647, plus another master function to choose > > between them, depending on the number of functions provided as argument. > I assume that the maximum number of arguments given is 2**31-1. Python > > may not actually have that limitation, in which case you would need to > > define additional functions. > Or... you would have to come up with an implementation which doesn't hard- > > code the number of functions used. > > Steven I got the generalisation criticism before yours, and generalised it accordingly. Unfortunately it was wrong essentially because it was so obvious that Josh Englsih posted essentially the same one before me... -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a function that applies list of functions to a value?
On Thursday, August 29, 2013 11:35:39 PM UTC+2, Chris Angelico wrote: > On Fri, Aug 30, 2013 at 7:27 AM, wrote: > > > Chris, call me a snob, but I resent using lambdas (aren't they usually > > considered odd/bad practice in python?) > > > > They're not bad practice; all they are is a function without a name, > > that's restricted to returning a single expression. So they're > > perfectly suited to this task, and ill-suited to some others. > > > > Like everything, lambda's a tool that can be used or misused. > > > > ChrisA For this purpose however, I suspect that a named function with a proper docstring that can be imported and reused over and over again is probably more appropriate than a lambda (the first post is telling us about something happening from time to time...) -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a function that applies list of functions to a value?
On Wednesday, August 28, 2013 8:50:53 PM UTC+2, Josh English wrote:
> Reduce tricks are nice, but I prefer clarity sometimes:
>
>
>
> def double(x):
>
> return x*2
>
>
>
> def add3(x):
>
> return x+3
>
>
>
>
>
> def compose(*funcs):
>
> for func in funcs:
>
> if not callable(func):
>
> raise ValueError('Must pass callable functions')
>
>
>
> def inner(value):
>
> for func in funcs:
>
> value = func(value)
>
> return value
>
>
>
> return inner
>
>
>
>
>
> add_then_double = compose(add3, double)
>
> double_then_add = compose(double, add3)
>
>
>
> print add_then_double(1) # prints 8
>
> print double_then_add(1) # prints 5
This is my favourite design, simple, clear, straightforward, very pythonic
imho. So great that I actually dod not notice it, and wrote it again after
you! Imho still, the ValueError you are raising is not that important in this
context, it would raise an Error anyway.
--
http://mail.python.org/mailman/listinfo/python-list
