Hey Salvatore,
   Have you tried the `key` attribute for `@cached_function` and/or 
`@cached_method`?

@cached_method(key=tuple)
def foo(x):
    return tuple(x)*2

The only downside is that there will be some redundancy (you have to do the 
cast twice, once for the key, once in the method).

I think a useful extension of this concept would be similar to 
UniqueRepresentation, where we preprocess the input and cache the result...

Best,
Travis


On Wednesday, August 12, 2015 at 2:56:05 AM UTC-7, Salvatore Stella wrote:
>
> Dear All, 
> I have several functions that take as input a tuple of integers. In many 
> of 
> these functions I check if this tuple belongs to some given list so its 
> type 
> is important. On the other hand I would like to allow users to pass a 
> lists or 
> a vector instead of a tuple. 
>
> One obvious way to deal with this is to make a coercion at the beginning 
> of 
> each function but this would lead to code duplication. Moreover it would 
> also 
> prevent me from using memoization. Is there a decorator in sage that I 
> could 
> use instead? 
>
> If I were coding in python 3.4 I would write a decorator like this: 
>
> def make_hashable(func): 
>     @wraps(func) 
>     def wrapper(*args, **kwargs): 
>         hashable_args = [] 
>         for x in args: 
>             try: 
>                 hashable_args.append(tuple(x)) 
>             except: 
>                 hashable_args.append(x) 
>   
>         hashable_kwargs = {} 
>         for x in kwargs: 
>             try: 
>                 hashable_kwargs[x] = tuple(kwargs[x]) 
>             except: 
>                 hashable_kwargs[x] = kwargs[x] 
>   
>         return func(*hashable_args, **hashable_kwargs) 
>     return wrapper 
>
> This works almost as well in earlier version of python except that @wraps 
> messes up the function signature. Is there any elegant solution to this 
> problem? 
> Thanks 
> S. 
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-combinat-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-combinat-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-combinat-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-combinat-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to