Trying to visualize the result of various sensor data in python
I can see it on raspberry pi terminal like a linux command python Sensortag2650.py -n 5 -t 1 -T -A -H -M -B -G -K -L -P 54:6C:0E:52:F8:FC
now i want to see the same result on python shell with same code
def main():
import time
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('host', action='store',help='MAC of BT device')
parser.add_argument('-n', action='store', dest='count', default=0,
type=int, help="Number of times to loop data")
parser.add_argument('-t',action='store',type=float, default=5.0, help='time between polling')
parser.add_argument('-T','--temperature', action="store_true",default=False)
parser.add_argument('-A','--accelerometer', action='store_true',
default=False)
parser.add_argument('-H','--humidity', action='store_true', default=False)
parser.add_argument('-M','--magnetometer', action='store_true',
default=False)
parser.add_argument('-B','--barometer', action='store_true', default=False)
parser.add_argument('-G','--gyroscope', action='store_true', default=False)
parser.add_argument('-K','--keypress', action='store_true', default=False)
parser.add_argument('-L','--light', action='store_true', default=False)
parser.add_argument('-P','--battery', action='store_true', default=False)
parser.add_argument('--all', action='store_true', default=False)
arg = parser.parse_args(sys.argv[1:])
print('Connecting to ' + arg.host)
tag = SensorTag(arg.host)
Output :
Sensortag2650.py [-h] [-n COUNT] [-t T] [-T] [-A] [-H] [-M] [-B] [-G]
[-K] [-L] [-P] [--all]
host
Sensortag2650.py: `error: the following arguments are required: host`
Related
I have tried the below code
def parse_args():
"""Get user command line parameters"""
parser = argparse.ArgumentParser(description="Available Options")
parser.add_argument('-i', '--input-path', type=is_valid_path,
required=True, help="Enter the path of the file or the folder to process")
parser.add_argument('-a', '--action', choices=[
'Highlight', 'Redact'], type=str, help="Choose to highlight or to redact")
parser.add_argument('-s', '--search-str', dest='search_str',
type=str, help="Enter a valid search string")
parser.add_argument('-p', '--pages', dest='pages', type=tuple,
help="Enter the pages to consider in the PDF file, e.g. (0,1)")
parser.add_argument("-g", "--generate-output", action="store_true", help="Generate text content in a CSV file")
path = parser.parse_known_args()[0].input_path
if os.path.isfile(path):
parser.add_argument('-o', '--output_file', dest='output_file',
type=str, help="Enter a valid output file")
parser.add_argument("-t", "--highlight-readable-text", action="store_true", help="Highlight readable text in the generated image")
parser.add_argument("-c", "--show-comparison", action="store_true", help="Show comparison between captured image and the generated image")
if os.path.isdir(path):
parser.add_argument("-r", "--recursive", action="store_true", help="Whether to process the directory recursively")
# To Porse The Command Line Arguments
args = vars(parser.parse_args())
# To Display The Command Line Arguments
print("## Command Arguments #################################################")
print("\n".join("{}:{}".format(i, j) for i, j in args.items()))
print("######################################################################")
return args
if name == 'main':
args = parse_args()
# If File Path
if os.path.isfile(args['input_path']):
# Process a file
if filetype.is_image(args['input_path']):
ocr_img(
# if 'search_str' in (args.keys()) else None
img=None, input_file=args['input_path'], search_str=args['search_str'], highlight_readable_text=args['highlight_readable_text'], action=args['action'], show_comparison=args['show_comparison'], generate_output=args['generate_output']
)
else:
ocr_file(
input_file=args['input_path'], output_file=args['output_file'], search_str=args['search_str'] if 'search_str' in (args.keys()) else None, pages=args['pages'], highlight_readable_text=args['highlight_readable_text'], action=args['action'], show_comparison=args['show_comparison'], generate_output=args['generate_output']
)
# If Folder Path
elif os.path.isdir(args['input_path']):
# Process a folder
ocr_folder(
input_folder=args['input_path'], recursive=args['recursive'], search_str=args['search_str'] if 'search_str' in (args.keys()) else None, pages=args['pages'], action=args['action'], generate_output=args['generate_output']
)
Getting this error
usage: ipykernel_launcher.py [-h] -i INPUT_PATH [-a {Highlight,Redact}]
[-s SEARCH_STR] [-p PAGES] [-g]
ipykernel_launcher.py: error: the following arguments are required: -i/--input-path
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
C:\Users\poddaral\AppData\Roaming\Python\Python310\site-packages\IPython\core\interactiveshell.py:3386: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
You need to paas input path as an argument while running the code. Let's say file name is abc.py, then in command line run
python abc.py --input_path Path
Here is the code.
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="infomedia"
)
parser.add_argument("file", help="path to file")
parser.add_argument(
"-i",
"--info",
type=str,
default="False",
help="get information about",
)
cli_args = parser.parse_args()
worker = Worker(
cli_args.input,
cli_args.info,
)
worker._application()
When the program is running with -h / --help it shows the default values.
positional arguments:
file path to file
optional arguments:
-h, --help show this help message and exit
-i INFO, --info INFO get information about (default: False)
How to avoid printing the default values? Or is there a way to define the default values of this code in a different way?
You can create new class inheriting from argparse.ArgumentDefaultsHelpFormatter and override _get_help_string method and pass your newly created class which is MyHelpFormatter in the below example as formatter_class in ArgumentParser constructor. Here is the example code which can help you:
import argparse
class MyHelpFormatter(argparse.ArgumentDefaultsHelpFormatter):
def _get_help_string(self, action):
return action.help
def main():
parser = argparse.ArgumentParser(
formatter_class=MyHelpFormatter,
description="infomedia",
)
parser.add_argument("file", help="path to file")
parser.add_argument(
"-i",
"--info",
type=str,
default="False",
help="get information about",
)
cli_args = parser.parse_args()
if __name__ == "__main__":
main()
I think you want more like:
parser.add_argument("--info", help="get information", action="store_true")
I want to implement a python3 command line by argparse, which supports positional arguments and flag options.
For example:
usage: mycmd [-v] [-h] [-o] text
mycmd text print text
mycmd -h show help message and exit
mycmd -v show version info
mycmd -o text print text and dump text to file mycmd.txt
Here's my implementation:
import argparse
class MyCmd:
def __init__(self):
self.parser = argparse.ArgumentParser()
self.parser.add_argument('text', help='print text') # mycmd text
self.parser.add_argument('-v', action='store_true', dest='version', help='show version info') # mycmd -v
self.parser.add_argument('-o', dest='output', help='print text and dump text to file mycmd.txt')
def parse(self):
return self.parser.parse_args()
def text(self, text):
print(text)
def version(self):
print('version info: mycmd-0.0.1')
def output(self, text):
print(text)
fp = open('mycmd.txt', 'w')
fp.write(text + '\n')
fp.close()
if __name__=='__main__':
mycmd = MyCmd()
(options, args) = mycmd.parse()
if options.text is not None:
mycmd.text(text)
elif options.version is not None:
mycmd.version()
elif options.output is not None:
mycmd.output(options.output)
When I test it with:
$ ./mycmd -v
Gives me error:
usage: mycmd [-h] [-o OUTPUT]
[-v]
text
mycmd: error: the following arguments are required: text
Why mycmd cannot consume mycmd -v ?
What your original implementation is missing is the step of including a specific subparse to the text processing flow.
Here's a more simplified version (without a class):
import argparse
def version():
return 'v0.0.1'
def print_and_save(text, file_path):
print(text)
if file_path:
with open(file_path, 'w') as fp:
fp.write(text + '\n')
fp.close()
if __name__=='__main__':
# Root (rt)
root_parser = argparse.ArgumentParser()
root_parser.add_argument('-v', '--version', action='store_true', help='show version info')
root_sbparsers = root_parser.add_subparsers(help='sub-command help')
# Adding sub-commands
text_parser = root_sbparsers.add_parser('process', help='print text')
text_parser.add_argument('text', type=str, help='text you want to work with')
text_parser.add_argument('-o', type=str, default=None, help='print text and dump text to file mycmd.txt')
args = root_parser.parse_args()
if args.version:
print(version())
elif args.text:
print_and_save(args.text, args.o)
I'm not sure why you have to embed it on a whole class but here's the version with it:
import argparse
class MyCmd(object):
def __init__(self):
# Root (rt)
self.parser = argparse.ArgumentParser()
self.parser.add_argument('-v', '--version', action='store_true', help='show version info')
root_sbparsers = self.parser.add_subparsers(help='sub-command help')
# Adding sub-commands
text_parser = root_sbparsers.add_parser('process', help='print text')
text_parser.add_argument('text', type=str, help='text you want to work with')
text_parser.add_argument('-o', type=str, default=None, help='print text and dump text to file mycmd.txt')
def parse(self):
return self.parser.parse_args()
def text(self, text):
print(text)
def version(self):
print('version info: mycmd-0.0.1')
def output(self, text, file_path):
with open(file_path, 'w') as fp:
fp.write(text + '\n')
fp.close()
if __name__=='__main__':
mycmd = MyCmd()
args = mycmd.parse()
if args.version:
mycmd.version()
elif args.text:
mycmd.text(args.text)
if args.o:
mycmd.output(args.text, args.o)
When I run the following code:
import argparse
from bottle import Bottle, run
app = Bottle()
#app.get('/')
def get_fn():
return 'Testing'
def command_line():
parser = argparse.ArgumentParser()
parser.add_argument('-k', '--sslkey', nargs='?', default=None,
help='SSL key file')
parser.add_argument('-c', '--sslcrt', nargs='?', default=None,
help='SSL certificate file')
return parser.parse_args()
if __name__ == "__main__":
args = command_line()
run(app=app, server='gunicorn',
host='0.0.0.0', port=8888,
debug=False, quiet=True,
keyfile=args.sslkey, certfile=args.sslcrt)
I'm getting the unrecognized argument error:
python3 ./tst.py --sslcrt ''
usage: tst.py [-h] [-v] [-c CONFIG] [-b ADDRESS] [--backlog INT] [-w INT]
[-k STRING] [--threads INT] [--worker-connections INT]
[--max-requests INT] [--max-requests-jitter INT] [-t INT]
[--graceful-timeout INT] [--keep-alive INT]
[--limit-request-line INT] [--limit-request-fields INT]
[--limit-request-field_size INT] [--reload]
[--reload-engine STRING] [--reload-extra-file FILES] [--spew]
[--check-config] [--preload] [--no-sendfile] [--reuse-port]
[--chdir CHDIR] [-D] [-e ENV] [-p FILE] [--worker-tmp-dir DIR]
[-u USER] [-g GROUP] [-m INT] [--initgroups]
[--forwarded-allow-ips STRING] [--access-logfile FILE]
[--disable-redirect-access-to-syslog]
[--access-logformat STRING] [--error-logfile FILE]
[--log-level LEVEL] [--capture-output] [--logger-class STRING]
[--log-config FILE] [--log-config-dict LOGCONFIG_DICT]
[--log-syslog-to SYSLOG_ADDR] [--log-syslog]
[--log-syslog-prefix SYSLOG_PREFIX]
[--log-syslog-facility SYSLOG_FACILITY] [-R]
[--statsd-host STATSD_ADDR] [--statsd-prefix STATSD_PREFIX]
[-n STRING] [--pythonpath STRING] [--paste STRING]
[--proxy-protocol] [--proxy-allow-from PROXY_ALLOW_IPS]
[--keyfile FILE] [--certfile FILE] [--ssl-version SSL_VERSION]
[--cert-reqs CERT_REQS] [--ca-certs FILE]
[--suppress-ragged-eofs] [--do-handshake-on-connect]
[--ciphers CIPHERS] [--paste-global CONF]
tst.py: error: unrecognized arguments: --sslcrt
If I run the same script without any arguments, it works just fine.
Currently my workaround is to reset the args right before calling run():
import argparse, sys
from copy import copy
from bottle import Bottle, run
app = Bottle()
#app.get('/')
def get_fn():
return 'Testing'
def command_line():
parser = argparse.ArgumentParser()
parser.add_argument('-k', '--sslkey', nargs='?', default=None,
help='SSL key file')
parser.add_argument('-c', '--sslcrt', nargs='?', default=None,
help='SSL certificate file')
return parser.parse_args()
if __name__ == "__main__":
args = copy(command_line())
# must reset the args, otherwise gunicorn will go nuts
sys.argv = sys.argv[:1]
run(app=app, server='gunicorn',
host='0.0.0.0', port=8888,
debug=False, quiet=True,
keyfile=args.sslkey, certfile=args.sslcrt)
With this workaround my script does work as expected:
python3 ./tst.py --sslcrt ''
[2019-05-01 11:01:01 +0100] [29198] [INFO] Starting gunicorn 19.9.0
[2019-05-01 11:01:01 +0100] [29198] [INFO] Listening at: http://0.0.0.0:8888 (29198)
[2019-05-01 11:01:01 +0100] [29198] [INFO] Using worker: sync
[2019-05-01 11:01:01 +0100] [29239] [INFO] Booting worker with pid: 29239
Is there a more sensible way of doing this?
I need to rewrite an arguments parser.The application has two modes: console and graphics. We can choose only one mode. Lately, the console version received two arguments: name of file with text and name of file with words. It looked so:
def parse_args():
parser = argparse.ArgumentParser(description='Fuzzy search in text')
parser.add_argument('-g', '--graphics', help='graphical version',
action='store_true')
parser.add_argument('-c', '--console', help='console version', nargs=2,
type=argparse.FileType('r'),
metavar=('TEXTFILE', 'WORDSFILE'))
return parser.parse_args()
usage: fuzzy_search.py [-h] [-g] [-c TEXTFILE WORDSFILE]
Fuzzy search in text
optional arguments:
-h, --help show this help message and exit
-g, --graphics graphical version
-c TEXTFILE WORDSFILE, --console TEXTFILE WORDSFILE
console version
Now I need to rewrite the accepting of arguments in the console version. I want to add two optional arguments that will store true for register-sensitivity (-r) and special view(-v). I know how to do it but I can't understand what to do with the next thing. I want to make two variants: 1)obligatory two files with text and words like it was initially and 2) obligatory file with text and not obligatory file with words, if it's not written than default - sys.stdin. Graphics shouldn't accept any arguments. So it should look like this
usage: fuzzy_search.py [-h] [-g] [-c [-r] [-v] TEXTFILE WORDSFILE(if not written then sys.stdin)]
I tried this variant:
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='versions')
parser_console = subparsers.add_parser('console', help='console version')
parser_console.add_argument('-r', '--register-check', action='store_true', help='register sensitivity')
parser_console.add_argument('-v', '--row-view', action='store_true', help='row view')
parser_console.add_argument('TEXTFILE', type=argparse.FileType('r'), help='text file')
parser_console.add_argument('--WORDSFILE', type=argparse.FileType('r'), default=sys.stdin, help='words file')
parser_graphics = subparsers.add_parser('graphics', help='graphics version')
But when I tried this
print(parser.parse_args(['console', '-r', '-v', 'text.txt','words.txt']))
there was an error:
unrecognized arguments: words.txt
with this
print(parser.parse_args(['console', '-r', '-v', 'text.txt']))
everything is ok:
Namespace(TEXTFILE=<_io.TextIOWrapper name='text.txt' mode='r' encoding='cp1251'>, WORDSFILE=<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>, register_check=True, row_view=True)
So problem somewhere in optional argument WORDSFILE and its default value. How to correct it?
Use -t for text and -w for the words file. The latter with default:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='versions')
parser_console = subparsers.add_parser('console',
help='console version')
parser_console.add_argument('-r', '--register-check',
action='store_true', help='register sensitivity')
parser_console.add_argument('-v', '--row-view',
action='store_true', help='row view')
parser_console.add_argument('-t', '--textfile',
type=argparse.FileType('r'),
help='text file')
parser_console.add_argument('-w', '--wordsfile', type=argparse.FileType('r'),
default=sys.stdin, help='words file')
print(parser.parse_args(['console', '-r', '-v', '-t', 'text.txt',
'-w', 'words.txt']))
parser.parse_args(['console', '-r', '-v', '-t', 'text.txt'])
Output:
Namespace(register_check=True, row_view=True,
textfile=<_io.TextIOWrapper name='text.txt' mode='r' encoding='UTF-8'>,
wordsfile=<_io.TextIOWrapper name='words.txt' mode='r' encoding='UTF-8'>)
Namespace(register_check=True, row_view=True,
textfile=<_io.TextIOWrapper name='text.txt' mode='r' encoding='UTF-8'>,
wordsfile=<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>)