def test_one_arg_one_option(self): has_run = [False] def func(one, option=''): has_run[0] = (one, option) # Should have -o option as well as -h option parser, required_args = optfunc.func_to_optionparser(func) self.assertEqual(len(parser.option_list), 2) strs = [str(o) for o in parser.option_list] self.assert_('-h/--help' in strs) self.assert_('-o/--option' in strs) # Should have one required arg self.assertEqual(required_args, ['one']) # Should execute self.assert_(not has_run[0]) optfunc.run(func, ['the-required', '-o', 'the-option']) self.assert_(has_run[0]) self.assertEqual(has_run[0], ('the-required', 'the-option')) # Option should be optional has_run[0] = False optfunc.run(func, ['required2']) self.assert_(has_run[0]) self.assertEqual(has_run[0], ('required2', ''))
def test_run_class(self): class Class: def __init__(self, one, option=''): self.has_run = [(one, option)] class NoInitClass: pass # Should execute e = StringIO() c = optfunc.run(Class, ['the-required', '-o', 'the-option'], stderr=e) self.assertEqual(e.getvalue().strip(), '') self.assert_(c.has_run[0]) self.assertEqual(c.has_run[0], ('the-required', 'the-option')) # Option should be optional c = None e = StringIO() c = optfunc.run(Class, ['required2'], stderr=e) self.assertEqual(e.getvalue().strip(), '') self.assert_(c.has_run[0]) self.assertEqual(c.has_run[0], ('required2', '')) # Classes without init should work too c = None e = StringIO() c = optfunc.run(NoInitClass, [], stderr=e) self.assert_(c) self.assertEqual(e.getvalue().strip(), '')
def test_three_positional_args(self): has_run = [False] def func(one, two, three): has_run[0] = True # Should only have the -h help option parser, required_args = optfunc.func_to_optionparser(func) self.assertEqual(len(parser.option_list), 1) self.assertEqual(str(parser.option_list[0]), '-h/--help') # Should have three required args self.assertEqual(required_args, ['one', 'two', 'three']) # Running it with the wrong number of arguments should cause an error for argv in ( ['one'], ['one', 'two'], ['one', 'two', 'three', 'four'], ): e = StringIO() optfunc.run(func, argv, stderr=e) self.assert_('Required 3 arguments' in e.getvalue(), e.getvalue()) self.assertEqual(has_run[0], False) # Running it with the right number of arguments should be fine e = StringIO() optfunc.run(func, ['one', 'two', 'three'], stderr=e) self.assertEqual(e.getvalue(), '') self.assertEqual(has_run[0], True)
def test_multiple_invalid_subcommand(self): "With multiple subcommands, invalid first arg should raise an error" def one(arg): pass def two(arg): pass def three(arg): pass # Invalid first argument should raise an error e = StringIO() res = optfunc.run([one, two], ['three'], stderr=e) self.assertEqual(res, optfunc.ERROR_RETURN_CODE) self.assertEqual(e.getvalue().strip(), "Unknown command: try 'one' or 'two'") e = StringIO() res = optfunc.run([one, two, three], ['four'], stderr=e) self.assertEqual(res, optfunc.ERROR_RETURN_CODE) self.assertEqual(e.getvalue().strip(), "Unknown command: try 'one', 'three' or 'two'") # No argument at all should raise an error e = StringIO() res = optfunc.run([one, two, three], [], stderr=e) self.assertEqual(res, optfunc.ERROR_RETURN_CODE) self.assertEqual(e.getvalue().strip(), "Unknown command: try 'one', 'three' or 'two'")
def test_stdout_special_argument(self): def upper(stdin=None, stdout=None): stdout.write(stdin.read().upper()) stdout = FakeStdout() optfunc.run(upper, stdin=FakeStdin(), stdout=stdout) self.assertEqual(stdout.written, 'HELLO')
def test_varargs(self): def func(one, two, three, *varargs): return "foo", varargs # Should only have the -h help option num_required_args, has_varargs, parser = optfunc.func_to_optionparser(func) self.assertEqual(len(parser.option_list), 1) self.assertEqual(str(parser.option_list[0]), '-h/--help') # Should have three required args self.assertEqual(num_required_args, 3) self.assertEqual(has_varargs, True) # Running it with the wrong number of arguments should cause an error for argv in (['one'], ['one', 'two']): e = StringIO() res = optfunc.run(func, argv, stderr=e) self.assert_('Required 3 or more arguments' in e.getvalue(), e.getvalue()) self.assertEqual(res, optfunc.ERROR_RETURN_CODE) # Running it with the right number of arguments should be fine - no varargs e = StringIO() res = optfunc.run(func, ['one', 'two', 'three'], stderr=e) self.assertEqual(e.getvalue(), '') self.assertEqual(res, ("foo", ())) # Running it with the right number of arguments should be fine - with varargs e = StringIO() res = optfunc.run(func, ['one', 'two', 'three', 'four', 'five'], stderr=e) self.assertEqual(e.getvalue(), '') self.assertEqual(res, ("foo", ("four", "five")))
def test_one_arg_one_option(self): has_run = [False] def func(one, option=""): has_run[0] = (one, option) # Should have -o option as well as -h option parser, required_args = optfunc.func_to_optionparser(func) self.assertEqual(len(parser.option_list), 2) strs = [str(o) for o in parser.option_list] self.assert_("-h/--help" in strs) self.assert_("-o/--option" in strs) # Should have one required arg self.assertEqual(required_args, ["one"]) # Should execute self.assert_(not has_run[0]) optfunc.run(func, ["the-required", "-o", "the-option"]) self.assert_(has_run[0]) self.assertEqual(has_run[0], ("the-required", "the-option")) # Option should be optional has_run[0] = False optfunc.run(func, ["required2"]) self.assert_(has_run[0]) self.assertEqual(has_run[0], ("required2", ""))
def test_run_class(self): class Class: def __init__(self, one, option=""): self.has_run = [(one, option)] class NoInitClass: pass # Should execute e = StringIO() c = optfunc.run(Class, ["the-required", "-o", "the-option"], stderr=e) self.assertEqual(e.getvalue().strip(), "") self.assert_(c.has_run[0]) self.assertEqual(c.has_run[0], ("the-required", "the-option")) # Option should be optional c = None e = StringIO() c = optfunc.run(Class, ["required2"], stderr=e) self.assertEqual(e.getvalue().strip(), "") self.assert_(c.has_run[0]) self.assertEqual(c.has_run[0], ("required2", "")) # Classes without init should work too c = None e = StringIO() c = optfunc.run(NoInitClass, [], stderr=e) self.assert_(c) self.assertEqual(e.getvalue().strip(), "")
def test_stdin_special_argument(self): consumed = [] def func(stdin): consumed.append(stdin.read()) class FakeStdin(object): def read(self): return "hello" optfunc.run(func, stdin=FakeStdin()) self.assertEqual(consumed, ['hello'])
def test_short_option_can_be_named_explicitly(self): def func1(one, option='', q_verbose=False): pass num_required_args, has_varargs, parser = optfunc.func_to_optionparser(func1) strs = [str(o) for o in parser.option_list] self.assertEqual(strs, ['-h/--help', '-o/--option', '-q/--verbose']) e = StringIO() optfunc.run(func1, ['one', '-q'], stderr=e) self.assertEqual(e.getvalue().strip(), '')
def test_short_option_can_be_named_explicitly(self): def func1(one, option="", q_verbose=False): pass parser, required_args = optfunc.func_to_optionparser(func1) strs = [str(o) for o in parser.option_list] self.assertEqual(strs, ["-h/--help", "-o/--option", "-q/--verbose"]) e = StringIO() optfunc.run(func1, ["one", "-q"], stderr=e) self.assertEqual(e.getvalue().strip(), "")
def test_short_option_can_be_named_explicitly(self): def func1(one, option='', q_verbose=False): pass parser, required_args = optfunc.func_to_optionparser(func1) strs = [str(o) for o in parser.option_list] self.assertEqual(strs, ['-h/--help', '-o/--option', '-q/--verbose']) e = StringIO() optfunc.run(func1, ['one', '-q'], stderr=e) self.assertEqual(e.getvalue().strip(), '')
def test_multiple_valid_subcommand_valid_argument(self): "Subcommands with valid arguments should execute as expected" def one(arg): executed.append(('one', arg)) def two(arg): executed.append(('two', arg)) e = StringIO() executed = [] optfunc.run([one, two], ['two', 'arg!'], stderr=e) self.assertEqual(e.getvalue().strip(), '') self.assertEqual(executed, [('two', 'arg!')])
def test_stderr_special_argument(self): def upper(stderr): stderr.write('an error') class FakeStderr(object): written = '' def write(self, w): self.written = w stderr = FakeStderr() self.assertEqual(stderr.written, '') optfunc.run(upper, stderr=stderr) self.assertEqual(stderr.written, 'an error')
def test_multiple_valid_subcommand_invalid_argument(self): "Subcommands with invalid arguments should report as such" def one(arg): executed.append(("one", arg)) def two(arg): executed.append(("two", arg)) e = StringIO() executed = [] optfunc.run([one, two], ["one"], stderr=e) self.assertEqual(e.getvalue().strip(), "one: Required 1 arguments, got 0")
def test_multiple_valid_subcommand_invalid_argument(self): "Subcommands with invalid arguments should report as such" def one(arg): executed.append(('one', arg)) def two(arg): executed.append(('two', arg)) e = StringIO() executed = [] optfunc.run([one, two], ['one'], stderr=e) self.assert_( e.getvalue().strip().startswith('one: Required 1 arguments, got 0') )
def test_stderr_special_argument(self): def bad_upper(stderr): stderr.write('an error') def good_upper(stderr=None): stderr.write('an error') stderr = FakeStdout() optfunc.run(bad_upper, stderr=stderr) self.assertEqual(stderr.written.strip(), 'Required 1 arguments, got 0') stderr = FakeStdout() optfunc.run(good_upper, stderr=stderr) self.assertEqual(stderr.written, 'an error')
def test_multiple_valid_subcommand_invalid_argument(self): "Subcommands with invalid arguments should report as such" def one(arg): executed.append(('one', arg)) def two(arg): executed.append(('two', arg)) e = StringIO() executed = [] optfunc.run([one, two], ['one'], stderr=e) self.assertEqual(e.getvalue().strip(), 'one: Required 1 arguments, got 0')
def test_multiple_valid_subcommand_invalid_argument(self): "Subcommands with invalid arguments should report as such" def one(arg): executed.append(('one', arg)) def two(arg): executed.append(('two', arg)) e = StringIO() executed = [] optfunc.run([one, two], ['one'], stderr=e) self.assertEqual( e.getvalue().strip(), 'one: Required 1 arguments, got 0\nUsage: %s arg [options]' % calling_name() )
def test_notstrict(self): "@notstrict tells optfunc to tolerate missing required arguments" def strict_func(one): pass e = StringIO() optfunc.run(strict_func, [], stderr=e) self.assertEqual(e.getvalue().strip(), 'Required 1 arguments, got 0') @optfunc.notstrict def notstrict_func(one): pass e = StringIO() optfunc.run(notstrict_func, [], stderr=e) self.assertEqual(e.getvalue().strip(), '')
def test_stdout_special_argument(self): def upper(stdin, stdout): stdout.write(stdin.read().upper()) class FakeStdin(object): def read(self): return "hello" class FakeStdout(object): written = '' def write(self, w): self.written = w stdout = FakeStdout() self.assertEqual(stdout.written, '') optfunc.run(upper, stdin=FakeStdin(), stdout=stdout) self.assertEqual(stdout.written, 'HELLO')
def test_boolean_option(self): has_run = [False] def func(true=True, false=False): has_run[0] = (true, false) # Should execute self.assert_(not has_run[0]) optfunc.run(func, []) self.assert_(has_run[0]) self.assertEqual(has_run[0], (True, False)) # -f should store_false, -t should store_true has_run[0] = False optfunc.run(func, ['-f', '-t']) self.assert_(has_run[0]) self.assertEqual(has_run[0], (False, True))
def test_return_arguments(self): "Checks that return values of functions are proxied back by run()" def one(): return "one called" def two(): return "two called" e = StringIO() res = optfunc.run([one, two], ['one'], stderr=e) self.assertEqual(e.getvalue().strip(), "") self.assertEqual(res, "one called") e = StringIO() res = optfunc.run([one, two], ['three'], stderr=e) self.assertEqual(e.getvalue().strip(), "Unknown command: try 'one' or 'two'") self.assertEqual(res, optfunc.ERROR_RETURN_CODE)
def test_multiple_valid_subcommand_invalid_argument(self): "Subcommands with invalid arguments should report as such" def one(arg): pass def two(arg): pass e = StringIO() res = optfunc.run([one, two], ['one'], stderr=e) self.assertEqual(res, optfunc.ERROR_RETURN_CODE) self.assertEqual(e.getvalue().strip(), 'one: Required 1 arguments, got 0')
def test_multiple_valid_subcommand_valid_argument(self): "Subcommands with valid arguments should execute as expected" def one(arg): return "one", arg def two(arg): return "two", arg e = StringIO() res = optfunc.run([one, two], ['two', 'arg!'], stderr=e) self.assertEqual(e.getvalue().strip(), '') self.assertEqual(res, ('two', 'arg!'))
def test_one_arg_one_option(self): def func(one, option=''): return (one, option) # Should have -o option as well as -h option num_required_args, has_varargs, parser = optfunc.func_to_optionparser(func) self.assertEqual(len(parser.option_list), 2) strs = [str(o) for o in parser.option_list] self.assert_('-h/--help' in strs) self.assert_('-o/--option' in strs) # Should have one required arg self.assertEqual(num_required_args, 1) self.assertEqual(has_varargs, False) # Should execute res = optfunc.run(func, ['the-required', '-o', 'the-option']) self.assertEqual(res, ("the-required", "the-option")) # Option should be optional res = optfunc.run(func, ['required2']) self.assertEqual(res, ("required2", ""))
buffer = StringIO() curl = pycurl.Curl() curl.setopt(curl.URL, url) curl.setopt(curl.WRITEFUNCTION, buffer.write) curl.setopt(curl.VERBOSE, 0) curl.setopt(curl.USERAGENT, user_agent) curl.setopt(curl.TIMEOUT, 20) curl.perform() curl.close() buffer = buffer.getvalue().strip() html = lxml.html.parse(StringIO(buffer)) image = html.findall("//img[@name='pdf']")[0] link = image.getparent() pdf_href = link.attrib["href"] #now let's get the article title title_div = html.findall("//div[@class='articleTitle']")[0] paper_title = title_div.text paper_title = paper_title.replace("\n", "") if paper_title[-1] == " ": paper_title = paper_title[:-1] re.sub('[^a-zA-Z0-9_\-.() ]+', '', paper_title) #now fetch the document for the user os.system("wget --user-agent=\"pyscholar/blah\" --output-document=\"%s.pdf\" \"%s\"" % (paper_title, pdf_href)) print "\n\n" if __name__ == "__main__": optfunc.run(interscience)
continue elif feat[0] == "BAR": if type(feat[1]) == int: out += "_" + str(feat[1]) elif feat[0] in keptFeats: if feat[1] == 1: out += "_" + feat[0] continue return out def main(file="grammars/testRules.txt", keptFeats=["PLUR", "POSS"], removeFeats=False): gr = open(file).readlines() seen = [] for line in gr: if line == "\n" or line[0] == "#": print line, else: parsedLine = nltk.grammar.parse_fcfg(line.strip("\n")).productions() assert len(parsedLine) == 1 rule = parsedLine[0] outRule = featureRule2Rule(rule, keptFeats, removeFeats) if outRule in seen: pass else: print outRule seen.append(outRule) optfunc.run(main)
for cfg in loaded_cfg: #find the corresponding .repo file corresponding_repo_file = delrepo[i] a_repo = corresponding_repo_file if debug: print "converting .repo into a yaml representation" resulting_yaml = repo_to_yaml(repo=a_repo) if debug: print "done converting .repo into a yaml representation" base_name = os.path.basename(a_repo) if debug: print "base_name is: ", base_name base_name_scrubbed = base_name[: -5] #get rid of the .repo at the end of the filename if debug: print "base_name_scrubbed is: ", base_name_scrubbed #pkg_path = os.path.join(instances, base_name_scrubbed) pkg_path = os.path.join(instances, sanitize(cfg.name)) yaml_file_path = os.path.join(pkg_path, base_name_scrubbed + ".repo.yaml") file_handler = open(yaml_file_path, "w") file_handler.write(resulting_yaml) file_handler.close() i = i + 1 if debug: "repo_recover: ending" if __name__ == "__main__": optfunc.run(repo_recover)
#!/usr/bin/python #author: Bryan Bishop <*****@*****.**> #date: 2010-02-06 #usage: python interscience.py "http://www3.interscience.wiley.com/journal/123190541/abstract" blah.pdf import optfunc import os def interscience2pdf(id, output_file): '''retrieves a PDF from an interscience.wiley.com link''' url = "http://download.interscience.wiley.com/cgi-bin/fulltext?ID=%s&PLACEBO=IE.pdf&mode=pdf" % ( id) os.system("wget \"%s\" --output-document \"%s\"" % (url, output_file)) if __name__ == "__main__": optfunc.run(interscience2pdf)
#!/usr/bin/env python import optfunc def one(arg): print "One: %s" % arg def two(arg): print "Two: %s" % arg def three(arg): print "Three: %s" % arg if __name__ == '__main__': optfunc.run([one, two, three])
#errorTypes, allErrors, wrongCategory=analyzeErrors(chunks) #print str(wrongCategory) if sen: getSenPrec(tF) #assert allErrors+results[2]==results[1] #printErrorTypes(errorTypes) #printConfMatrix(wrongCategory) if pattern: patternCount = getChunkPatterns(tF, int(goldField), int(autoField), mode) printPatterns(patternCount) def evalInput(input, autoField=-1, goldField=-2): corp = [] for sen in input: for tok in sen: corp.append('\t'.join(tok)+'\n') corp.append('\n') chunks = getChunksFromCorp(corp, goldField, autoField, 'BI', False) chunkCounts = evaluate(chunks) tokCounts = countToks(corp, goldField, autoField) results = count(chunkCounts, tokCounts) printResults(results) if __name__ == '__main__': #runEval(sys.stdin, '-2', '-1', 'BIE1', False, True, True, False, False) optfunc.run(runEval)
link = h3.parent.parent.findAll(name="a")[0].attrs[0][1] files.append((filename, link)) output = "Which file do you want to download?\n\n" counter = 0 for file in files: filename = file[0] output = output + "\t[" + str(counter) + "] " + filename + "\n" counter = counter + 1 output = output + "\n" print output selection = raw_input("Please type a number: ") files_to_get = [int(selection)] #if selection.count(",") > 0: # files_to_get = selection.split(",") for file in files_to_get: print "Beginning download of: ", files[file][0] os.system("wget \"http://thingiverse.com" + str(files[file][1]) + "\" --output-document=\"" + files[file][0] + "\"") if __name__ == "__main__": optfunc.run(thingiverse_get) #optfunc.run(download_thingiverse_partial) #optfunc.run(tester)
if s[0]=="1" : s = "B" + s[1:] if s[0]=="E" : s = "I" + s[1:] return s def main( dontDoBIE1ToBI=False, dontDoNbarToNP=False, dontDoNplusToNbar=False, fields="-2,-1" ) : fieldList = map( int, fields.split(",") ) lengthSet = set() line = sys.stdin.readline() c=0 while line : c+=1 a = line.strip().split() if len(a)==0 : print else : assert len(a)>=2 lengthSet.add(len(a)) if len(lengthSet)>1 : raise ValueError("Column number differs from those of" + "previous lines: {0}".format(line)) for ind in fieldList : a[ind] = transform(a[ind], c, dontDoBIE1ToBI, dontDoNbarToNP, dontDoNplusToNbar) print "\t".join(a) line = sys.stdin.readline() if __name__ == '__main__': optfunc.run(main)
def x(): #if __name__=='__main__': optfunc.run(main)
#results # * results[] - The sorted list of search suggestions # * request{} - The request parameters (from the arguments above) # o q # o limit # * hits - The total number of matched suggestions # * time - The amount of time it took to process the entire request (in seconds) return simplejson.loads(response_json) def octopart_search(query): '''searches octopart with the string''' octopart = Octopart(octopart_api_key) #categories = octopart.get_categories(id=4179) #print "categories: %s" % (categories) parts = octopart.search_parts(query=query, limit=1) #print "parts: %s" % (parts) print parts.keys() class OctopartTests(unittest.TestCase): def test_octopart_get_category(self): octopart = Octopart(octopart_api_key) categories = octopart.get_category(id=4179) pass if __name__ == "__main__": optfunc.run(octopart_search)
curl.setopt(curl.URL, url) curl.setopt(curl.WRITEFUNCTION, buffer.write) curl.setopt(curl.VERBOSE, 0) curl.setopt(curl.USERAGENT, user_agent) curl.setopt(curl.TIMEOUT, 20) curl.perform() curl.close() buffer = buffer.getvalue().strip() html = lxml.html.parse(StringIO(buffer)) image = html.findall("//img[@name='pdf']")[0] link = image.getparent() pdf_href = link.attrib["href"] #now let's get the article title title_div = html.findall("//div[@class='articleTitle']")[0] paper_title = title_div.text paper_title = paper_title.replace("\n", "") if paper_title[-1] == " ": paper_title = paper_title[:-1] re.sub('[^a-zA-Z0-9_\-.() ]+', '', paper_title) #now fetch the document for the user os.system( "wget --user-agent=\"pyscholar/blah\" --output-document=\"%s.pdf\" \"%s\"" % (paper_title, pdf_href)) print "\n\n" if __name__ == "__main__": optfunc.run(interscience)
__FILENAME__ = demo #!/usr/bin/env python import optfunc def upper(filename, verbose=False): "Usage: %prog <file> [--verbose] - output file content in uppercase" s = open(filename).read() if verbose: print "Processing %s bytes..." % len(s) print s.upper() if __name__ == '__main__': optfunc.run(upper) ########NEW FILE######## __FILENAME__ = geocode #!/usr/bin/env python # Depends on geocoders from http://github.com/simonw/geocoders being on the # python path. import geocoders import optfunc import os # We use notstrict because we want to be able to trigger the list_geocoders # option without being forced to provide the normally mandatory 's' argument @optfunc.notstrict @optfunc.arghelp('list_geocoders', 'list available geocoders and exit') def geocode(s, api_key='', geocoder='google', list_geocoders=False):
#!/usr/bin/env python import optfunc def upper(filename, verbose = False): "Usage: %prog <file> [--verbose] - output file content in uppercase" s = open(filename).read() if verbose: print "Processing %s bytes..." % len(s) print s.upper() if __name__ == '__main__': optfunc.run(upper)
def test_stdin_special_argument(self): def func(stdin=None): return stdin.read() res = optfunc.run(func, stdin=FakeStdin()) self.assertEqual(res, "hello")
#for node in system.childNodes: # if node.nodeName == "Artifact": # artifact = process_artifact(node) # artifacts.append(artifact) def parse_repo(repo): system = repo.childNodes[0].nextSibling.childNodes[1] huge_thing = process_node(system) return huge_thing def main(): rval = {} if len(sys.argv) > 1 and sys.argv[1] == '-': rval['stdin'] = parse_file(sys.stdin) else: for i in sys.argv[1:]: rval[i] = parse_file(open(i)) print yaml.dump(rval, default_flow_style=False) return rval def repo_to_yaml(repo): repo_path = os.path.abspath(repo) return yaml.dump(parse_file(repo_path)) if __name__ == "__main__": optfunc.run(repo_to_yaml)
#!/usr/bin/python #author: Bryan Bishop <*****@*****.**> #date: 2010-03-04 #purpose: resolve a pesky DOI number import urllib2, httplib import optfunc httplib.HTTPConnection.debuglevel = 1 user_agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.5) Gecko/20091123 Iceweasel/3.5.5 (like Firefox/3.5.5; Debian-3.5.5-1)" def doi(number): '''resolves a DOI number, like: 10.1038/nature01036''' request = urllib2.Request("http://dx.doi.org/%s" % (number)) opener = urllib2.build_opener() f = opener.open(request) print f.url, "\n" if __name__ == "__main__": optfunc.run(doi)