This question already has answers here:
Map list item to function with arguments
(4 answers)
Closed 1 year ago.
I have a following function:
def calculate_frequent_itemset(fractional_data, support):
"""Function that calculated the frequent dataset parallely"""
return apriori(fractional_data, min_support=support, use_colnames=True)
I would like to call it in a map function using:
frequent_itemsets=p.map(calculate_frequent_itemset,(dataNew1,dataNew2,dataNew3,dataNew4,dataNew5), 200)
In other words I want to set 200 as support argument. But I got an error TypeError: calculate_frequent_itemset() missing 1 required positional argument: 'support'. How can I fix it please?
You could use a wrapper function (or a lambda):
support=200
def my_calculate_frequent_itemset(fractional_data):
return calculate_frequent_itemset(fractional_data, support)
frequent_itemsets=p.map(my_calculate_frequent_itemset,(dataNew1,dataNew2,dataNew3,dataNew4,dataNew5))
It's pretty horrible.
I rediscovered this - rather perhaps see https://stackoverflow.com/a/31995624/1021819 which gives the same answer
Related
This question already has answers here:
PyQt TypeError connect()
(1 answer)
Passing extra arguments through connect
(1 answer)
Closed 1 year ago.
I created a calculator using PyQt5 designer, in the logic part written in Python each button call a class to add the corresponding symbol like this:
self.button_0.clicked.connect(self.push0)
self.button_1.clicked.connect(self.push1)
def push0(self):
t=self.display.toPlainText()
self.display.setText(t+"0")
def push1(self):
t=self.display.toPlainText()
self.display.setText(t+"1")
But if i try to write one function for all of them like:
self.button_0.clicked.connect(self.push(self, '0'))
self.button_0.clicked.connect(self.push(self, '1'))
def push(self, c):
t=self.display.toPlainText()
self.display.setText(t+c)
I get the message "TypeError: push() takes 2 positional arguments but 3 were given"
What's the problem?
Thans to everyone already
This question already has answers here:
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
Closed 2 years ago.
I am searching for a tip/technique to dynamically build function argument names when calling a method:
I have a function which is using dynamic arguments that later are posted to a Webservice using http:
def create_case(**fields):
...
Currently, I call the function like this:
create_case(field54=first_name,
field_1003=last_name,
field_948=street)
Since I have multiple instances of the Webservices, which have different field ID's I try to put those argument names into a configuration file and build them dynamically. All my current tries were not successful and I ran out of ideas on how to approach this.
What I tried:
config.py:
FIELD_FIRST_NAME=54
FIELD_LAST_NAME=1003
FIELD_STREET=948
client.py:
create_case(field_+config.FIELD_FIRST_NAME=first_name,
field_+config.FIELD_LAST_NAME=last_name,
field_+config.FIELD_STREET=street)
It seems, it's not possible to just concat the arguments together. Does anyone have a suggestion on how I could go on this ?
Best regards
You can create a dictionary of the arguments:
kwargs_dict = {
"field_"+str(config.FIELD_FIRST_NAME): first_name,
"field_"+str(config.FIELD_LAST_NAME): last_name,
"field_"+str(config.FIELD_STREET): street
}
and then pass this to the function as:
create_case(**kwargs_dict)
This question already has answers here:
What is the difference between calling function with parentheses and without in python? [duplicate]
(5 answers)
Closed 2 years ago.
I've got following simple script, which gets text from some site:
from urllib.request import urlopen
def fetch_words():
contentdownload = urlopen('https://wolnelektury.pl/media/book/txt/treny-tren-viii.txt')
decluttered = []
for line in contentdownload:
decltr_line = line.decode('utf8').split(" ")
for word in decltr_line:
decluttered.append(word)
contentdownload.close()
return decluttered
When adding: print(fetch_words) at the end, the program returns: <function fetch_words at 0x7fa440feb200>, but on the other hand, when I replace it with: print(fetch_words()) it returns the content of the website, that a function downloads.
I have following question: why it works like this, what's the difference: function with () or without...
All help appreciated!
When you call print(fetch_words) you get the representation of the function as an object.
def fetch_words():
pass
isinstance(fetch_words,object)
return True. Indeed, functions in Python are objects.
So when you type print(fetch_words) you actually get the result of fetch_words.__str__(), a special method which is called when you print object.
And when you type print(fetch_words()) you get the result of the function (the value the function returns). Because, the () execute the function
So fetch_words is an object and fetch_words() execute the function and its value is the value the function returns.
This question already has answers here:
Argument 1 has unexpected type 'NoneType'?
(2 answers)
Closed 3 years ago.
How can I pass the name of ObjectName like self.lineEdit_260 as a parameter in a method using python 3.4?
self.Open_Fan_J104_A_stage_2(no=self.lineEdit_260.text()))
def Open_Fan_J104_A_stage_2(self,no):
if no=='' and self.lineEdit_247.text()=='':
print(type(self.lineEdit_260.text()))
self.start_time=datetime.datetime.now()
self.lineEdit_260.setText(str(self.start_time))
elif self.lineEdit_247.text()=='':
QMessageBox.information(self, 'x')
else:
self.lineEdit_260.setText('')
self.start_time = datetime.datetime.now()
self.lineEdit_260.setText(str(self.start_time))
self.lineEdit_247.setText('')
self.lineEdit_241.setText('')
When I run this code:
self.pushButton_123.clicked.connect( self.Open_Fan_J104_A_stage_2(no=self.lineEdit_260.text()))
TypeError: argument 1 has unexpected type 'NoneType'
I think, the Open_Fan_J104_A_stage_2 expects a callable and since there was a lack of information. So try,
self.Open_Fan_J104_A_stage_2(lambda: self.lineEdit_260.text()))
This question already has answers here:
Python Tkinter Return
(2 answers)
Closed 8 months ago.
I'm currently working on a GUI project on Python (3.6) using tkinter (8.6).
Following this question I'm wondering how to get back the result of someFunction :
def someFunction(event):
do stuff ..
return(otherStuff)
canvas.bind('<Button-1>',lambda event: someFunction(event))
Thank you in advance :) !
The return values of callback functions like your someFunction are ignored. Rather than using return, have the callback save the value somewhere (in a global variable or an attribute of some kind of object). Or have your function pass the computed value as an argument to some other function that will do something with it.