Re: how to sort a list of tuples with custom function

2017-08-01 Thread Ho Yeung Lee
how to write this distance function in sort there are the syntax error On Wednesday, August 2, 2017 at 6:03:13 AM UTC+8, Glenn Linderman wrote: > On 8/1/2017 2:10 PM, Piet van Oostrum wrote: > > Ho Yeung Lee writes: > > > >> def isneighborlocation(lo1, lo2): > >> if abs(lo1[0] - lo2[0]) <

Re: how to sort a list of tuples with custom function

2017-08-01 Thread Glenn Linderman
On 8/1/2017 2:10 PM, Piet van Oostrum wrote: Ho Yeung Lee writes: def isneighborlocation(lo1, lo2): if abs(lo1[0] - lo2[0]) < 7 and abs(lo1[1] - lo2[1]) < 7: return 1 elif abs(lo1[0] - lo2[0]) == 1 and lo1[1] == lo2[1]: return 1 elif abs(lo1[1] - lo2[1]) == 1

Re: how to group by function if one of the group has relationship with another one in the group?

2017-08-01 Thread Piet van Oostrum
Ho Yeung Lee writes: > which function should be used for this problem? > I think it is a kind if clustering, or a connectivity problem. There are special algorithms for that, not just a simple function. Maybe scikit-learn has a suitable algorithm for it. -- Piet van Oostrum WWW: http://piet.v

Re: how to sort a list of tuples with custom function

2017-08-01 Thread Piet van Oostrum
Ho Yeung Lee writes: > def isneighborlocation(lo1, lo2): > if abs(lo1[0] - lo2[0]) < 7 and abs(lo1[1] - lo2[1]) < 7: > return 1 > elif abs(lo1[0] - lo2[0]) == 1 and lo1[1] == lo2[1]: > return 1 > elif abs(lo1[1] - lo2[1]) == 1 and lo1[0] == lo2[0]: > return

Re: how to sort a list of tuples with custom function

2017-08-01 Thread Ho Yeung Lee
i tried with testing1.sort(key=lambda x: x[0]) but only first element of tuple are grouped then i expect to sort with custom function if difference between first element of tuple and another first element of tuple is less than some value and do for second element too, goal to segmentation of bla

how to sort a list of tuples with custom function

2017-08-01 Thread Ho Yeung Lee
def isneighborlocation(lo1, lo2): if abs(lo1[0] - lo2[0]) < 7 and abs(lo1[1] - lo2[1]) < 7: return 1 elif abs(lo1[0] - lo2[0]) == 1 and lo1[1] == lo2[1]: return 1 elif abs(lo1[1] - lo2[1]) == 1 and lo1[0] == lo2[0]: return 1 else: return 0 sort

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Terry Reedy
On 8/1/2017 7:06 AM, Matt Wheeler wrote: On Tue, 1 Aug 2017 at 02:32 Terry Reedy wrote: On 7/31/2017 7:31 PM, t...@tomforb.es wrote: As part of the Python 3 cleanup in Django there are a fair few uses of @functools.lru_cache on functions that take no arguments. This makes no sense to me. I

Re: @lru_cache on functions with no arguments

2017-08-01 Thread tom
> _sentinel = object() > _val = _sentinel > def val(): > if _val is _sentinel: > # Calculate _val > return _val > > seems entirely sufficient for this case. Write a custom decorator if you use > the idiom often enough to make it worth the effort. I did some timings with this as p

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Christian Heimes
On 2017-08-01 01:31, t...@tomforb.es wrote: > As part of the Python 3 cleanup in Django there are a fair few uses of > @functools.lru_cache on functions that take no arguments. A lru_cache isn't > strictly needed here, but it's convenient to just cache the result. Some > examples are here: https

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Paul Moore
On Tuesday, 1 August 2017 00:31:52 UTC+1, t...@tomforb.es wrote: > Am I right in thinking that using `maxsize=None` is best for functions that > accept no arguments? Should we even be using a `lru_cache` in such > situations, or write our own simple cache decorator instead? It seems like overki

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Matt Wheeler
On Tue, 1 Aug 2017 at 12:53 Thomas Nyberg wrote: > On 08/01/2017 01:06 PM, Matt Wheeler wrote: > > A function which is moderately expensive to run, that will always return > > the same result if run again in the same process, and which will not be > > needed in every session. > > > > What about j

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Thomas Nyberg
On 08/01/2017 02:50 PM, t...@tomforb.es wrote: > 2. Django has a long-standing no-dependencies rule, which may change in the > near future but for now it is stdlib only. We can't add a dependency on > `lazy-property`. Apologies for continuing going off-topic, but the actual code in that package I

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Chris Angelico
On Tue, Aug 1, 2017 at 10:50 PM, wrote: > And you have a simple function: > > def test(): >return object() > > I get the following numbers without much variance: > > 1. lru_cache(maxsize=None) - 870ns > > 2. lru_cache() - 1300ns > > 3. no cache - 100ns > > So, in the best case, without the C

Re: @lru_cache on functions with no arguments

2017-08-01 Thread tom
Hello all, Thank you for the replies! So, here is the context: 1. The functions Django wants to cache require Django to be initialized and the settings loaded. This means the return values are not available at definition time. (Matt Wheeler hits it on the head). 2. Django has a long-standing no

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Thomas Nyberg
On 08/01/2017 01:06 PM, Matt Wheeler wrote: > A function which is moderately expensive to run, that will always return > the same result if run again in the same process, and which will not be > needed in every session. > What about just using a lazy getter property? E.g.: https://pypi.p

Re: @lru_cache on functions with no arguments

2017-08-01 Thread Matt Wheeler
On Tue, 1 Aug 2017 at 02:32 Terry Reedy wrote: > On 7/31/2017 7:31 PM, t...@tomforb.es wrote: > > As part of the Python 3 cleanup in Django there are a fair few uses of > @functools.lru_cache on functions that take no arguments. > > This makes no sense to me. If the function is being called for