Issue calling another library function using command line argument in Python - python-2.7

I am trying to call different library functions based on the input command-line argument.
My main function would take the input argument and will call the function based on the parameter passed to cli. This is my main function.
from lib.updatefeed import *
def main():
......
......
parser.add_argument('-update', type=str, nargs='?', help='Update the local storage')
if args.update:
gather(args.update)
if __name__ == '__main__':
main()
Here, gather() is another function in another python library that I have already imported in the main program.
Here is the body of the imported library with gather() function
def gather(self):
if not os.path.exists('intel'):
os.mkdir('intel')
print "Starting update process"
counter = 0
ioc_list = []
timestr = time.strftime("%Y%m%d-%H%M%S")
for source in IP.iteritems():
print source
name = source + timestr
print name
file = open(name, 'w')
c = connect(source,params=url_param())
for line in c:
if line.startswith("/") or line.startswith('\n') or line.startswith("#"):
pass
else:
file.write(line+'\n')
So my gather() function will be called when the "-update" param is passed to the command line.
The function of gather() function would be to create a directory named "intel".
Then it will iterate through a list of IPs and create file names based on the IP and timestamp. It will then call the connect function to create an HTTP connection to the IP.
It will parse the contents of the IP and write it to the file.
I am not able to achieve this by using my program I have added here.
For some reason, the call from the main() function itself is not succeeding.
I tried adding "print" statements in the gather() function to see what parts are working.
Can the community help me on this please.

The solution to this problem lies in the argument characteristics. By Defining parser.add_argument('-update', type=str, nargs='?', help='Update the local storage') I am asuming that One argument will be passed to the command line along with the -update param.
But I only care about the -update arg to be present in the command line without any additional entity.
This can be overcome by changing the line of script to:
parser.add_argument('-update', action='store_true', help='Update the local storage')
This will ensure that the presence of only -update will call the function 'gather()'.

Related

How to return variables and reuse them within other functions/methods within a Python Class

I am working with a Python Class object project. One method name get_text() parses and extracts all of the text from an XML file and returns the value of text, 'text_variable' so that it can be used in another method with the Class. Within the Class object is a function called get_text. It reads the line of each XML file and then extracts all of the text. In this method, I wanted to store(return) the value of text under a variable name 'text_variable' retrieved from XML files (all of this input files are stored on my computer in a working directory) so that I can use it in another method within my program.
The'background_summary(self)' method, uses a RegEx expression, searches and extracts sentences based on a keyword/phrase search with the returned variable from the previous method, 'text_variable' as a parameter. However, under the if _name_=='_main_' statement, when I tried to print the values of the return 'text_variable', it does not give me the all of the output I am expecting. It only prints one line from one XML file. However, when I print the 'text variable' inside of the get_text(self) method, it prints out all of the output from every XML file.
So I want to know what could be the possibility as to why I am unable to reuse and call my variable successfully when I run my program. The program is unable to retrieve all of the text from each XML file when called and it only retrieves one line from the first XML file. However, when I print the output 'text_variable' within the get_text() method, I can see all of the text; however, since the value is not returned, I am unable to use it inside the other 'background_summary()' method.
CODE BELOW
files = os.listdir(os.getcwd())
class SummaryGenerator(object):
def __init__(self,files):
self.files=files
def get_text(self):
for f in files:
with open(f,"r") as fh:
for line in fh:
soup=BS(line, "lxml")
tags=soup.find_all('p')
for t in tags:
info=''.join(t.text)
buf=io.StringIO(info)
if 'NOTICE TO APPELLANT' in buf.readlines():
break
else:
words=info
text_variable="".join(words)
self.text_variable=text_variable
return self.text_variable
def background_summary(self):
bg_list=[]
background_corpora = map(lambda x: x[0], re.findall('([^.]*?(respondent|filed a petition)[^)]*\))',self.text_variable, re.I))
for b in background_corpora:
bg_list.append(b)
bkg_string = "".join(str(x)for x in bg_list)
print('Background', bkg_string)
if __name__ == "__main__":
for f in files:
Summary = SummaryGenerator(f)
print(Summary.get_text())

how to use local variable to another function in python?

def runscan(self):
p = os.popen('LD_PRELOAD=/usr/libv4l/v4l1compat.so zbarcam
/dev/video0','r')
while True :
code = p.readline().split(':')[1]
print 'Got barcode:', code
def input(self):
self.entryc.insert(END, code)
how about this? i want use local 'code' to the next function to insert the result of barcode to my Tkinter entryBox.. Thanks
just pass it as a parameter. change the definition of input to def input(self, code) and then as the last line of runscan call input(code). A side note, you shouldn't use "input" as the name of a function, because it shadows the built-in input function, which is used for getting user input from the console.

Finding out the file name in a FileUploadHandler

I am rolling my own fileupload handler in django and would like to know the file name. I am supporting more than one file format and want to do different processing in the receive_data_chunk method depending on which file format the uploaded file has. I thought I would be pragmatic and just judge file format based on file ending but I can't figure out how to get hold of the file name. If I try to extract the file name with something like the following code (before that method is called):
if request.method == 'POST':
p = re.compile('^.*\.sdf$', re.IGNORECASE)
if ( p.search(request.FILES['filecontent'].name) ) :
self.sdf = True
else:
self.sdf = False
It seems I never reach the receive_data_chunk method. I presume the call to request.FILES trigger the loading somehow and then it's already done? How can I do different processing based on file ending in my receive_data_chunk method?
Have you tried using
data=request.POST.copy()
and then working on the copy? I have used this for other things but may work in this case as well.

selenium python: what is 'lambda' in webdriverwait.until statements

I'm learning selenium webdriver with python and came across 'lambda' in following line of code. The author did not explain the use of lambda here:
search_button = WebDriverWait(self.driver, 10).until(lambda s:s.find_element_by_name("btnG"))
search_button.click()
I've read about lambda and it says lambda creates functions on the fly and some say its used to return expression. So now I'm confused and not sure exactly what difference does it make here.
In python functions are objects so you can pass them as parameters to other functions. The only thing is if you pass a function with () you call that function at the same time. So it's possible to pass functions which do not take any arguments so it can be called inside the function you passing it to later on. But if you need to pass parameters to the function while you are passing function itself you need to wrap it up in lambda so that it's called only when it's needed.
Edit
To answer the question how it gets s value. If you look into the source here doctoring explains it all:
"""Calls the method provided with the driver as an argument until the
return value is not False."""
Actual code is self explanatory as well:
def until(self, method, message=''):
screen = None
stacktrace = None
end_time = time.time() + self._timeout
while True:
try:
value = method(self._driver)
if value:
return value
except self._ignored_exceptions as exc:
screen = getattr(exc, 'screen', None)
stacktrace = getattr(exc, 'stacktrace', None)
time.sleep(self._poll)
if time.time() > end_time:
break
raise TimeoutException(message, screen, stacktrace)

Passing an argument to main that calls a function in python

I'm trying to pass arguments to my python script using argparse and consequently call functions. Any ideas where I might be going wrong?
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-d','--d', dest='action', action='store_const',const=do_comparison,
help="Diff the current and most recent map file memory information)")
options = parser.parse_args()
return options
def do_comparison(parsed_args):
# do things
def main(args):
options = parse_args()
if __name__ == '__main__':
sys.exit(main())
In my comment I missed the fact that you are using store_const and const=do_comparison. So you are trying some sort of callback.
options from parse_args is a argparse.Namespace object. This is a simple object, similar to a dictionary. In fact vars(options) returns a dictionary.
When main is run (with -d), options.action will be set to the const, a function. But remember, in Python, functions are first class objects, and can be set to variables, etc just like numbers and strings. To be used the function has to be 'called'.
options.action()
should end up calling do_comparison. Actually since that function requires an argument, you should use
options.action(options)
or some other way of providing a varible or object to the function.
Of course you'll have to be careful about the case where you don't specify -d. Then options.action will have the default value (e.g. None). If the default isn't a callable, then this call will produce an error.
The argparse documentation illustrates this kind of action in the section dealing with subparsers (subcommands). I vaguely recall a tutorial that set an argument value to functions like add and multiply, creating a simple arithmetic expression evaluator.
Usually the values in the Namespace are strings, or numbers, and to use them you test for string equality. e.g.
if options.action is None:
# default action
elif options.action == 'print':
print(options)
else:
do some other backup or error
A callback kind of action is possible, and may be convenient in some cases, but it isn't the usual arrangement.
You asked about using successfully store a string following the -d, to be used as the function arg with:
parser.add_argument('-d','--d', dest='action', dest='function_input', action='store_const', const=diff_map)
A 'store_const' action does not take an argument (in effect nargs=0). It's more like store_true. In fact store_true is just a store_const with has default=False and const=True.
What you need is another argument, whick could occur either before or after the -d. argparse tries to be order flexible.
Here's a simple script with a callable argument, and flexible positional argument.
import argparse
def action1(*args):
print 'action1',args
def action0(*args):
print 'action0',args
parser = argparse.ArgumentParser()
parser.add_argument('-d', dest='action', action='store_const', const=action1, default=action0)
parser.add_argument('args', nargs='*')
args = parser.parse_args()
args.action(args.args)
resulting runs
1238:~/mypy$ python stack32214076.py
action0 ([],)
1238:~/mypy$ python stack32214076.py one two three
action0 (['one', 'two', 'three'],)
1238:~/mypy$ python stack32214076.py one two three -d
action1 (['one', 'two', 'three'],)
1239:~/mypy$ python stack32214076.py -d one two three
action1 (['one', 'two', 'three'],)
1239:~/mypy$ python stack32214076.py -d
action1 ([],)
TO make -d value perform some action on value, try:
parser.add_argument('-d','--action')
The default action type stores one value (e.g. action='store', nargs=None)
args = parser.parse_args()
if args.action: # or is not None
do_comparison(args.action)
If -d is not given args.action will have default None value, and nothing happens here.
If -d astr is given acts.action will have the string value 'astr'. This if just calls the do_comparison function with this value. It's the present of this (nondefault) value that triggers the function call.
This is a rather straight forward use of a parser and an argument.