Questions tagged [functools]

functools is a module for the Python language which provides support for working with higher-order functions: functions that act on or return other functions

functools is a module for the Python language which provides support for working with higher-order functions: functions that act on or return other functions.

The official documentation can be found here: here

382 questions
0
votes
1 answer

python 3 functools.reduce gives 0 if not literal list given

I am trying reduce in python and getting 0 when expecting other values: In [1]: from functools import reduce In [2]: reduce( (lambda x, y: x * y), [1, 2, 3, 4] ) Out[2]: 24 In [3]: def multiply(x, y): return x * y In [4]: reduce( multiply, [1, 2,…
Idaho06
  • 5
  • 3
0
votes
3 answers

Python Decorators - object has no attribute '__name__'

I have a tornado method like below and I tried to decorate method to cache stuff. I have the following setup def request_cacher(x): def wrapper(funca): @functools.wraps(funca) @asynchronous @coroutine def…
tuna
  • 6,211
  • 11
  • 43
  • 63
0
votes
1 answer

Apply a function from a groupby transform

My pandas looks like this Date Ticker Open High Low Adj Close Adj_Close Volume 2016-04-18 vws.co 445.0 449.2 441.7 447.3 447.3 945300 2016-04-19 vws.co 449.0 455.8 448.3 450.9 450.9 907700 2016-04-20 vws.co …
Excaliburst
  • 143
  • 1
  • 4
  • 15
0
votes
2 answers

Putting the same button with the same command in multiple places, but having unique functions

I'm not entirely sure how to phrase my question in one sentence. I am using python to create a calendar with the ability to log mileage for athletes. I have a calendar made that contains a 7x4 grid containing the number of the month in the upper…
0
votes
1 answer

How to use python collections for custom classes

Still somewhat perplexed by python and it's magic functional programming, so I tend to find myself writing code that is more towards the Java paradigm of programming as opposed to Idiomatic Python. My question is somewhat related to: How do I make a…
0
votes
0 answers

How to use PyQt slot and signal with parameter in a loop?

I am trying to create insert a remove tool button dynamically inside a lineedit and also associate slot to the remove tool. What complicates the issue is the slot has parameters that is, lineedit and label of rows to be removed. So as a result, I…
wondim
  • 697
  • 15
  • 29
0
votes
1 answer

Benefits to importing functools over own pure-Python impl?

I found this piece of code and started wondering, why not simply replace the import with own implementation? Is there any (performance) benefit to using functools.partial? Is it implemented in pure Python or native code? try: from functools…
ArekBulski
  • 4,520
  • 4
  • 39
  • 61
0
votes
1 answer

What is the correct way to define a python decorator in a class?

What I'd like to achieve is that the following code out puts the following: Here1 Here2 Here3 argOne argTwo I'm wondering if my use of __call__ is somehow clobbering functools.wraps; it also appears that the arguments are lost at some point. Is…
SMTF
  • 705
  • 10
  • 16
0
votes
2 answers

Partially apply a string equality function

From what I understand I can compare strings with is and ==. Is there a way I can partially apply these functions? For example: xs = ["hello", "world"] functools.filter(functools.partial(is, "hello"), xs) Gives…
0
votes
1 answer

How deal with TypeError: 'functools.partial' object has no attribute '__getitem__'

I have a function called 'generate_wind_cap_new_data' which returns a pandas dataframe. I want to cut this one by a specific timerange. My code is: generate_wind_cap = partial(generate_wind_cap_new_data,freq='1T')['2011-1-1':'2011-12-31'] But I get…
EP1986
  • 853
  • 1
  • 7
  • 14
0
votes
0 answers

Where is _functools.py located?

I was looking at how the wrap decorator was implemented in functools.py and it used partial(), which I then wanted to see how that was implemented. It was imported from _functools so I looked for _functools.py but couldn't find it. I grepped the…
Jpaji Rajnish
  • 1,491
  • 4
  • 17
  • 35
0
votes
3 answers

Monkeypatch with instance method

I'm trying to monkeypatch how pandas Panel's slicing (__getitem__). This is straightforward to do with a basic function, foo. from pandas import Panel Panel.__getitem__ = ORIGINAL_getitem def newgetitem(panel, *args, **kwargs): """ Append a…
Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
0
votes
0 answers

functools partial error : AttributeError: 'functools.partial' object has no attribute '__module__'

I've noticed this happens sometimes, usually I switch to lambda, but was wondering what I am doing that is causing it: AttributeError: 'functools.partial' object has no attribute '__module__' Appears with: def f(div, x): total = 0 for row…
Cenoc
  • 11,172
  • 21
  • 58
  • 92
0
votes
1 answer

Python: Compositions of functions of multiple arguments /without/ functools

Working on writing a deceptively simple function that finds the accumulation of things. It is quite abstract, and its signature is this: def accumulate(combiner, start, n, term): """Return the result of combining the first n terms in a…
Alex Walczak
  • 1,276
  • 1
  • 12
  • 27
0
votes
2 answers

How do I modify parameters stored by functools.wraps?

I have a decorator that validates some parameters and passes an validated key to various functions: from functools import wraps ref validate(f): @wraps(f) # This is to ensure docstrings are passed through the decorated function def…
dragonx
  • 14,963
  • 27
  • 44