Skip to content

Commit faf8ae2

Browse files
committed
Adding python examples
1 parent cc563bd commit faf8ae2

97 files changed

Lines changed: 9902 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

argparse/completer_extractor.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import argparse
2+
from string import Template
3+
4+
5+
6+
FILENAME = "completer_template"
7+
8+
def completer_extractor(parser):
9+
completer_dict = {"opts": [],
10+
"subparser":{}}
11+
12+
print "parser: %s" % str(parser)
13+
for a in parser._actions:
14+
if type(a) is argparse._SubParsersAction:
15+
subparser_dict = a.choices
16+
for sarg in subparser_dict:
17+
sub_argument = subparser_dict[sarg]
18+
19+
completer_dict["subparser"][sarg] = {"opts":[]}
20+
for sa in sub_argument._actions:
21+
completer_dict["subparser"][sarg]["opts"].extend(sa.option_strings)
22+
continue
23+
24+
25+
if "option_strings" in dir(a):
26+
completer_dict["opts"].extend(a.option_strings)
27+
28+
29+
name = parser.prog
30+
if "." in name:
31+
name = name.partition(".")[0]
32+
generate_completer(completer_dict, name)
33+
print "parser dict: %s" % str(completer_dict)
34+
35+
def generate_completer(completer_dict, name):
36+
print "name: %s" % name
37+
completer_file = FILENAME
38+
f = open(completer_file)
39+
completer = Template(f.read())
40+
f.close()
41+
42+
43+
completer = completer.safe_substitute(NAME = name)
44+
completer = Template(completer).safe_substitute(GLOBAL_OPTS = generate_opt_string(completer_dict))
45+
completer = Template(completer).safe_substitute(CMDS = generate_sub_commands(completer_dict["subparser"]))
46+
47+
print "completer:\n%s" % completer
48+
f = open("%s_bash_completer" % name, "w")
49+
f.write(completer)
50+
f.close()
51+
52+
53+
54+
def generate_sub_commands(command_dict):
55+
subcommands = ""
56+
subcommands_opts = False
57+
subcommands += "\t\tcase \"${prev}\" in\n"
58+
for cmd in command_dict:
59+
if len(command_dict[cmd]["opts"]) == 0:
60+
continue
61+
subcommands_opts = True
62+
subcommands += "\t\t\t%s)\n" % cmd
63+
subcommands += "\t\t\t\tlocal %s_opts = \"%s\"\n" % (cmd, generate_opt_string(command_dict[cmd]))
64+
subcommands += "\t\t\t\tCOMPREPLY=( $(compgen -W ${%s_opts} -- ${curr}) )\n" % (cmd)
65+
subcommands += "\t\t\t\treturn 0\n"
66+
subcommands += "\t\t\t\t;;\n"
67+
68+
subcommands += "\t\t\t*)\n"
69+
subcommands += "\t\t\t;;\n"
70+
subcommands += "\t\tesac\n"
71+
72+
if not subcommands_opts:
73+
return ""
74+
return subcommands
75+
76+
def generate_opt_string(completer_dict):
77+
opts = completer_dict["opts"]
78+
opt_string = ""
79+
if "subparser" in completer_dict.keys():
80+
sub_commands = completer_dict["subparser"].keys()
81+
82+
for sub_command in sub_commands:
83+
opt_string += sub_command
84+
opt_string += " "
85+
86+
for i in range(len(opts)):
87+
opt = opts[i]
88+
opt_string += opt
89+
if i < (len(opts) - 1):
90+
opt_string += " "
91+
92+
93+
return opt_string

argparse/completer_template

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
_${NAME}(
3+
local cur prev opts
4+
#Get the current commands line string
5+
COMPREPLY = ()
6+
7+
#COMP_WORDS: List of items on the command line
8+
#COMP_CWORD: Index of the current word in the list
9+
cur = "${COMP_WORDS[COMP_CWORD]}"
10+
prev = "${COMP_WORDS[COMP_CWORD - 1]}"
11+
12+
opts = "${GLOBAL_OPTS}"
13+
14+
#Check to see if the previous command is part of our list of sub commands
15+
${CMDS}
16+
17+
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
18+
return 0
19+
)
20+
21+
complete -F _${NAME} ${NAME}

argparse/example_subparser.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#! /usr/bin/python
2+
3+
import sys
4+
import argparse
5+
import completer_extractor
6+
7+
DESCRIPTION = "\n" \
8+
"Test Tool\n"
9+
10+
EPILOG = "\n" \
11+
"ppllloooggg\n" + \
12+
"\n"
13+
14+
15+
if __name__ == "__main__":
16+
parser = argparse.ArgumentParser()
17+
subparsers = parser.add_subparsers( title = "Tools Title",
18+
description = "Description of Commands",
19+
dest = "tool",
20+
help = 'Sub Tools',
21+
metavar = None)
22+
23+
parser.add_argument("-d", "--debug", action='store_true', help="Output test debug information")
24+
25+
a_parser = subparsers.add_parser("A")
26+
b_parser = subparsers.add_parser("B")
27+
28+
#print "subparsers: %s" % str(subparsers)
29+
30+
a_parser.add_argument("-t", "--test", action='store_true', help="Test something")
31+
b_parser.add_argument("-j", "--jman", action='store_true', help="Test something")
32+
33+
completer_extractor.completer_extractor(parser)
34+
35+
'''
36+
print "Parser:"
37+
for a in parser._actions:
38+
if type(a) is argparse._SubParsersAction:
39+
print "Sub parser"
40+
sub_dict = a.choices
41+
42+
for sarg in sub_dict:
43+
sub_argument = sub_dict[sarg]
44+
45+
print "\tName: %s " % sarg
46+
for sa in sub_argument._actions:
47+
print "\t\t%s" % str(sa)
48+
continue
49+
50+
print "%s" % str(a)
51+
52+
'''
53+
54+
sys.exit()
55+
args = parser.parse_args()
56+
57+
if args.debug:
58+
debug = True
59+
print ("Debug Output Enabled")
60+
61+
'''
62+
if args.verbose:
63+
print ("Verbose Output Enabled")
64+
'''
65+
66+
print "dir: %s" % str(args)
67+
print "sub command: %s" % str(args.tool)
68+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
_example_subparser(
3+
local cur prev opts
4+
#Get the current commands line string
5+
COMPREPLY = ()
6+
7+
#COMP_WORDS: List of items on the command line
8+
#COMP_CWORD: Index of the current word in the list
9+
cur = "${COMP_WORDS[COMP_CWORD]}"
10+
prev = "${COMP_WORDS[COMP_CWORD - 1]}"
11+
12+
opts = "A B -h --help -d --debug"
13+
14+
#Check to see if the previous command is part of our list of sub commands
15+
case "${prev}" in
16+
A)
17+
local A_opts = "-h --help -t --test"
18+
COMPREPLY=( $(compgen -W ${A_opts} -- ${curr}) )
19+
return 0
20+
;;
21+
B)
22+
local B_opts = "-h --help -j --jman"
23+
COMPREPLY=( $(compgen -W ${B_opts} -- ${curr}) )
24+
return 0
25+
;;
26+
*)
27+
;;
28+
esac
29+
30+
31+
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
32+
return 0
33+
)
34+
35+
complete -F _example_subparser example_subparser

autocomplete/autocomplete.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#! /usr/bin/python
2+
3+
import readline
4+
5+
commands = [
6+
"eggs",
7+
"cheese",
8+
"bread",
9+
"five"
10+
]
11+
12+
def completer(text, state):
13+
print "in completer"
14+
options = [i for i in commands if i.startswith(text)]
15+
if state < len(options):
16+
return options[state]
17+
else:
18+
return None
19+
20+
readline.parse_and_bind("tab: complete")
21+
readline.set_completer(completer)

autocomplete/command_complete

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# !/usr/bin/python
2+
3+
import cmd
4+
5+
addresses = [
6+
7+
8+
9+
]
10+
11+
class MyCmd(cmd.Cmd):
12+
def do_send(self, line):
13+
pass
14+
15+
def complete_send(self, text, line, start_index, end_index):
16+
if text:
17+
return [
18+
address for address in addresses
19+
if address.startswith(text)
20+
]
21+
else:
22+
return addresses
23+
24+
25+
if __name__ == '__main__':
26+
my_cmd = MyCmd()
27+
my_cmd.cmdloop()

autocomplete/command_complete~

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import cmd
2+
3+
addresses = [
4+
5+
6+
7+
]
8+
9+
class MyCmd(cmd.Cmd):
10+
def do_send(self, line):
11+
pass
12+
13+
def complete_send(self, text, line, start_index, end_index):
14+
if text:
15+
return [
16+
address for address in addresses
17+
if address.startswith(text)
18+
]
19+
else:
20+
return addresses
21+
22+
23+
if __name__ == '__main__':
24+
my_cmd = MyCmd()
25+
my_cmd.cmdloop()

autocomplete/complete

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#! /usr/bin/python
2+
3+
import readline
4+
5+
commands = [
6+
"eggs",
7+
"cheese",
8+
"bread",
9+
"five"
10+
]
11+
12+
def completer(text, state):
13+
options = [i for i in commands if i.startswith(text)]
14+
if state < len(options):
15+
return options[state]
16+
else:
17+
return None
18+
19+
readline.parse_and_bind("tab: complete")
20+
readline.set_completer(completer)

file_watcher/file_process.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#! /usr/bin/python
2+
3+
4+
def func():
5+
print "In a function"
6+
f = open("text.txt")
7+
#print "File Contents:" + f.read()
8+
9+
lines = f.readlines()
10+
11+
print "List: " + str(lines)
12+
13+
if (len(lines) == 0):
14+
print "no lines"
15+
elif(len(lines) > 0):
16+
print "More than 0"
17+
18+
for line in lines:
19+
print "Line: " + line
20+
21+
def process_file(input_list, output_list):
22+
print "Hello"
23+
24+
25+
26+
27+
if __name__ == "__main__":
28+
print "Hello World"
29+
func()
30+
find_list = ["word1", "word2"]
31+
32+
'''
33+
filenames = sys.argv[1:]
34+
35+
input_file = filenames[0]
36+
world_list_file = filename[1]
37+
38+
print "input files: %s
39+
'''
40+
41+
s1 = "hello my name is dave"
42+
43+
if "my" in s1:
44+
print "Found my"
45+
46+
before = s1.partition("my")[0]
47+
after = s1.partition("my")[2]
48+
49+
insert_string = "your"
50+
51+
s2 = before + insert_string + after
52+
53+
print "output: " + s2
54+

0 commit comments

Comments
 (0)