Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved
Improve this question
here I want to call web service function only once throughout the program.
how to accomplish this anybody suggest me
import sys,os
def web_service(macid):
# do something
if "__name__" = "__main__" :
web_service(macid)
This is how I would to that:
i_run_once_has_been_run = False
def i_run_once(macid):
global i_run_once_has_been_run
if i_run_once_has_been_run:
return
# do something
i_run_once_has_been_run = True
#Vaulstein's decorator function would work too, and may even be a bit more pythonic - but it seems like a bit overkill to me.
Using class,
class CallOnce(object):
called = False
def web_service(cls, macid):
if cls.called:
print "already called"
return
else:
# do stuff
print "called once"
cls.called = True
return
macid = "123"
call_once_object = CallOnce()
call_once_object.web_service(macid)
call_once_object.web_service(macid)
call_once_object.web_service(macid)
Result is,
I have no name!#sla-334:~/stack_o$ python once.py
called once
already called
already called
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a simplified example. But basically, I have function within a function. I want to pass the argument from the top-level function to the embedded function, but it doesn't work. How do I solve this?
Thank you
def Check(file_name, mh_criticalval):
test_file=file_name
def eval_value(excel_sht, excel_col, mh_value):
if excel_sht.cell(row=i, column=excel_col) > mh_value:
cell_cny_b=excel_sht.cell(
row=i,
column=excel_col
).value='OK'
eval_value(sht, 9, mh_criticalval)
Check('test.xlsm',1)
Method 1: De-Nesting
The most obvious method here is to remove the nested functions. Doing this is simple:
def eval_mh(excel_sht, excel_col, mh_value):
if excel_sht.cell(row=i, column=excel_col)>mh_value:
cell_cny_b=excel_sht.cell(row=i, column=excel_col).value='OK'
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
eval_mh(sht, 9, mh_criticalval)
CheckDiag('test.xlsm',1)
All we did here was move the inner function out. However, if you still wanted them to be nested, try the following.
Method 2: Local/Global Variable Exploitation
Here, we replace all instances of mh_value with mh_criticalval
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
def eval_mh(excel_sht, excel_col):
if excel_sht.cell(row=i, column=excel_col)>mh_criticalval:
cell_cny_b=excel_sht.cell(row=i, column=excel_col).value='OK'
eval_mh(sht, 9)
CheckDiag('test.xlsm',1)
Method 3: Why 2 functions?
In this method, we remove the function entirely!
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
if sht.cell(row=i, column=9)>mh_criticalval:
cell_cny_b=sht.cell(row=i, column=9).value='OK'
eval_mh(sht, 9)
CheckDiag('test.xlsm',1)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I came across this problem. Not sure how to continue.
My dictionary:
DICT = {
"inside": "It's obviously inside",
"outside": "It went outside"
}
I have a function that returns a dictionary key. Then I have a printing function that should print the value connected to that function.
If my previous function's return line is return "inside", I tried this:
def print_location(key):
print(DICT[key])
This seems not to be working. Somehow I might need to connect the variable 'key' to the returned key but this is where I get stuck. How could I do this?
Your code seems fine; the following works as expected:
DICT = {
"inside": "It's obviously inside",
"outside": "It went outside"
}
def get_key():
return 'inside'
def print_location(key):
print(DICT[key])
# run the code
the_key = get_key()
print_location(the_key) # prints "It's obviously inside"
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
can anyone tell me how to skip a function calling if a particular condition is true for example if r[3]=='Device': if this condition is true then the next function call must be skip, and if this condition is not true then the next function call must execute.the code will be like
the first function :
def sei1(r):
if r[4]=='Device':
print(r[1],r[2],r[3])
if r[7]=='Device':
print(r[5],r[6])
if r[7]=='list':
print(r[5],r[6])
if r[8]=='Device':
print(r[5],r[6],r[7])
if r[8]=='list':
print(r[5],r[6],r[7])
def se(r):
if r[5]=='Device':
print(r[1],r[2],r[3],r[4])
if r[8]=='Device':
print(r[6],r[7])
if r[7]=='list':
print(r[6],r[7])
if r[8]=='Device':
print(r[6],r[7],r[8])
if r[8]=='list':
print(r[6],r[7],r[8])
sei1(r)
se(r)
This are the two functions and what i need is if r[4]=='Device' is true then next function call se(r) must not execute.
i am new at python every suggestions are appreciated.
Thank you
You need to simply check as follows :
if r[3]!='Device': # function_call is a function which will only execute when r[3] is not equal to 'Device'
function_call()
OR
if r[3]=='Device':
pass
else:
function_call()
By skip do you mean not executing it? In that case, the good ol' simple if should just work fine?
if r[3] != 'Device':
func1()
print('function is executed!')
else:
print('function is NOT executed!')
if r[3]=='Device':
pass
else:
function()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The snippet of code is below. I want my object name to be equal to element[0]. My scripts successfully generates the objects, but fails to 'name them' I cannot understand why.
master_inventory = []
def import_catalogue():
with open("./catalogue.txt", "r") as raw_catalogue:
for page in raw_catalogue:
page = page.split('\r')
for line in page:
element = line.split('\t\t\t')
element[0] = Antique(element[0], element[1], element[2], element[3], element[4], element[5], element[6], element[7], element[8], element[9])
master_inventory.append(element[0])
import_catalogue()
print master_inventory[1]
>>> <__main__.Antique instance at 0x10980f320>
print master_inventory[1].sku
>>> A00001
You should implement the __str__ function in the Antique class, so you choose how you want to display your object's name. e.g.:
class Antique():
…
def __str__(self):
return "Antique({},{},{})".format(self.foo, self.bar, self.foobar)
…
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
The pseudocode is like this:
def a_func(parent_node, child_node):
parent_node.add(child_node)
// check validity
return child_node
node1 = a_func(pnode, Node(attr_x = "a new node")
node2 = a_func(pnode, Node(node1.get_attr("attr_x"))
It's used in tree structure. a_func insert nodeX into a parent nodeY and return nodeX. Does anyone have ideas about a suitable name for it?
I would name the function something like enlistForParent or maybe addToParent or even setParentForNode I believe all these versions imply that the return value is the child note and also make the effect of calling the function clear.