functools

Functools.update_wrapper() doesn't work properly

不打扰是莪最后的温柔 提交于 2019-12-05 22:17:13
问题 I use Functools.update_wrapper() in my decorator, but It seems like update_wrapper rewrites only function attributes (such as __doc__ , __name__ ), but does not affect on help() function. I aware of these answers, but they don't work with decorator-class. Here is my function. import functools class memoized(object): def __init__(self, func): self.func = func functools.update_wrapper(self, func) def __call__(self, *args): self.func(*args) @memoized def printer(arg): "This is my function" print

itertools.accumulate() versus functools.reduce()

扶醉桌前 提交于 2019-12-03 01:53:21
In Python 3.3, itertools.accumulate() , which normally repeatedly applies an addition operation to the supplied iterable, can now take a function argument as a parameter; this means it now overlaps with functools.reduce() . With a cursory look, the main differences between the two now would seem to be: accumulate() defaults to summing but doesn't let you supply an extra initial condition explicitly while reduce() doesn't default to any method but does let you supply an initial condition for use with 1/0-element sequences, and accumulate() takes the iterable first while reduce() takes the

Dynamically created method and decorator, got error 'functools.partial' object has no attribute '__module__'

六眼飞鱼酱① 提交于 2019-11-30 08:14:57
I am currently using EndpointsModel to create a RESTful API for all my models on AppEngine. Since it is RESTful, these api have a lot of repeat code which I want to avoid. For example: class Reducer(EndpointsModel): name = ndb.StringProperty(indexed=False) @endpoints.api( name="bigdata", version="v1", description="""The BigData API""", allowed_client_ids=ALLOWED_CLIENT_IDS, ) class BigDataApi(remote.Service): @Reducer.method( path="reducer", http_method="POST", name="reducer.insert", user_required=True, ) def ReducerInsert(self, obj): pass ## and GET, POST, PUT, DELETE ## REPEATED for each

python equivalent of functools 'partial' for a class / constructor

妖精的绣舞 提交于 2019-11-30 01:53:12
问题 I want to create a class that behaves like collections.defaultdict, without having the usage code specify the factory. EG: instead of class Config(collections.defaultdict): pass this: Config = functools.partial(collections.defaultdict, list) This almost works, but isinstance(Config(), Config) fails. I am betting this clue means there are more devious problems deeper in also. So is there a way to actually achieve this? I also tried: class Config(Object): __init__ = functools.partial

Python functools lru_cache with class methods: release object

耗尽温柔 提交于 2019-11-28 18:42:50
问题 How can I use functools' lru_cache inside classes without leaking memory? In the following minimal example the foo instance won't be released although going out of scope and having no referrer (other than the lru_cache). from functools import lru_cache class BigClass: pass class Foo: def __init__(self): self.big = BigClass() @lru_cache(maxsize=16) def cached_method(self, x): return x + 5 def fun(): foo = Foo() print(foo.cached_method(10)) print(foo.cached_method(10)) # use cache return

Differences between functools.partial and a similar lambda?

a 夏天 提交于 2019-11-28 09:13:43
In Python, suppose I have a function f that I want to pass around with some secondary arguments (assume for simplicity that it's just the first argument that remains variable). What are the differences between doing it these two ways (if any)? # Assume secondary_args and secondary_kwargs have been defined import functools g1 = functools.partial(f, *secondary_args, **secondary_kwargs) g2 = lambda x: f(x, *secondary_args, **secondary_kwargs) In the doc page for partial , for example, there is this quote: partial objects defined in classes behave like static methods and do not transform into

Python functools partial efficiency

有些话、适合烂在心里 提交于 2019-11-28 09:04:28
I have been working with Python and I set up the following code situation: import timeit setting = """ import functools def f(a,b,c): pass g = functools.partial(f,c=3) h = functools.partial(f,b=5,c=3) i = functools.partial(f,a=4,b=5,c=3) """ print timeit.timeit('f(4,5,3)', setup = setting, number=100000) print timeit.timeit('g(4,5)', setup = setting, number=100000) print timeit.timeit('h(4)', setup = setting, number=100000) print timeit.timeit('i()', setup = setting, number=100000) I get the following as a result: f: 0.181384086609 g: 0.39066195488 h: 0.425783157349 i: 0.391901016235 Why do

Python multiprocessing - Why is using functools.partial slower than default arguments?

狂风中的少年 提交于 2019-11-28 01:51:12
Consider the following function: def f(x, dummy=list(range(10000000))): return x If I use multiprocessing.Pool.imap , I get the following timings: import time import os from multiprocessing import Pool def f(x, dummy=list(range(10000000))): return x start = time.time() pool = Pool(2) for x in pool.imap(f, range(10)): print("parent process, x=%s, elapsed=%s" % (x, int(time.time() - start))) parent process, x=0, elapsed=0 parent process, x=1, elapsed=0 parent process, x=2, elapsed=0 parent process, x=3, elapsed=0 parent process, x=4, elapsed=0 parent process, x=5, elapsed=0 parent process, x=6,

Python multiprocessing - Why is using functools.partial slower than default arguments?

心不动则不痛 提交于 2019-11-27 19:12:33
问题 Consider the following function: def f(x, dummy=list(range(10000000))): return x If I use multiprocessing.Pool.imap , I get the following timings: import time import os from multiprocessing import Pool def f(x, dummy=list(range(10000000))): return x start = time.time() pool = Pool(2) for x in pool.imap(f, range(10)): print("parent process, x=%s, elapsed=%s" % (x, int(time.time() - start))) parent process, x=0, elapsed=0 parent process, x=1, elapsed=0 parent process, x=2, elapsed=0 parent

Python functools partial efficiency

巧了我就是萌 提交于 2019-11-27 02:37:20
问题 I have been working with Python and I set up the following code situation: import timeit setting = """ import functools def f(a,b,c): pass g = functools.partial(f,c=3) h = functools.partial(f,b=5,c=3) i = functools.partial(f,a=4,b=5,c=3) """ print timeit.timeit('f(4,5,3)', setup = setting, number=100000) print timeit.timeit('g(4,5)', setup = setting, number=100000) print timeit.timeit('h(4)', setup = setting, number=100000) print timeit.timeit('i()', setup = setting, number=100000) I get the