"AttributeError: 'ArgumentParser' object has no attribute 'parse_intermixed_args'" - python

I am attempting to develop an application using argparse module in Python3. I am getting the above error on when attempting to call the function parse._intermixed_args(some_string).
parser = argparse.ArgumentParser(prog='string',description='description')
parser.add_argument('--optional', nargs='+', type=int, help='help text')
parser.add_argument('name', help='help text')
if len(commands) > 0:
args=parser.parse_intermixed_args(commands)

According to the docs parse_intermixed_args was introduced in Python 3.7. You must be using an earlier version.

Related

Creating a template for DataFlow throws the error AttributeError: 'RuntimeValueProvider' object has no attribute 'tableId'

I have made a pipeline with Apache Beam which runs successfully with the DataFlow runner. I'm trying to create a template, but when using a RuntimeValueProvider for the apache_beam.io.gcp.bigquery.WriteToBigQuery transformer, the following error is thrown:
AttributeError: 'RuntimeValueProvider' object has no attribute 'tableId'.
The code (excerpt) looks as follows:
class ProcessOptions(PipelineOptions):
#classmethod
def _add_argparse_args(cls, parser):
parser.add_value_provider_argument(
'--output_gcs',
dest='output_gcs',
default='gs://nlp-text-classification/results/stackoverflow_template',
type=str,
required=False,
help='Google Cloud Storage Path.')
parser.add_value_provider_argument(
'--output_bq_table',
dest='output_bq_table',
default='nlp-text-classification:stackoverflow.template_test',
type=str,
required=False,
help='BigQuery table.')
process_options = options.view_as(ProcessOptions)
with beam.Pipeline(options=options) as p:
[...]
"Write Posts to BigQuery" >> beam.io.WriteToBigQuery(table=process_options.output_bq_table,
schema=table_schema)
[...]
Is this a bug or am I doing something wrong?
Currently Dataflow uses a runner-native source that does not support templates by default. We do have a Beam source that supports templates. To use that, you have to enable an experiment using following flag.
--experiment=use_beam_bq_sink
Instead of 'Write To BigQuery' >> beam.io.Write(beam.io.BigQuerySink(..)) I used beam.io.WriteToBigQuery(..) and also used --experiment=use_beam_bq_sink which worked for me and I am not getting the below error anymore AttributeError: 'RuntimeValueProvider' object has no attribute 'datasetId

Python: "AttributeError: 'Namespace' object has no attribute" argparse

My code looks like this:
parser.add_argument('-i', '--input', help='Input path/to/file.csv', required=True)
parser.add_argument('-oh', '--output-html', help='Output path/to/confusion_matrix.html', required=True)
parser.add_argument('-oc', '--output-csv', help='Output path/to/confusion_matrix.csv', required=True)
args = parser.parse_args()
....
y_true = pd.Series(true_data, name="Actual")
y_pred = pd.Series(pred_data, name="Predicted")
df_confusion = pd.crosstab(y_true, y_pred)
df_confusion.to_html(args.output-html)
df_confusion.to_csv(args.output-csv)
When i try to run it, it gives me this error:
df_confusion.to_html(args.output-html)
AttributeError: 'Namespace' object has no attribute 'output'
However, if i change from
df_confusion.to_html(args.output-html)
To
df_confusion.to_html(args.output)
It works as it should. Can anyone explain why it doesn't work, and how can i make it work with args.output-html?
By default (ie if you don't provide dest kwarg to add_argument) it changes - to _ when creating the attribute since Python attributes can't contain the character - (as a matter of fact they can, but then they are only accessible by using getattr).
It means that you should change args.output-html to args.output_html, and args.output-csv to args.output_csv.

ArgumentParser object has no attribute 'add_option' on passing flags

Day three of learning python.
I'm attempting to understand how to pass flags from the command line and call a function with that flag. However, I'm getting the following error:
Traceback (most recent call last):
File "main.py", line 18, in <module>
parser.add_option("-l", action="callback", callback=printLogs)
AttributeError: 'ArgumentParser' object has no attribute 'add_option'
The code is here:
import argparse
def printLogs():
print("logs!")
parser = argparse.ArgumentParser()
parser.add_argument('-e','--entry', type=str, help='New entry',required=False)
parser.add_option("-l", action="callback", callback=printLogs)
args = parser.parse_args()
I can understand that parser.add_option doesn't exist for parser. This much is clear. I can also see that the OptionParser has been deprecated as per this link. So, OptionParser is out.
The question being: How do I parse the -l argument such that the printLogs function is called when its passed?
The way I would implement this is:
import argparse
def printLogs():
print("logs!")
parser = argparse.ArgumentParser()
parser.add_argument('-e','--entry', type=str, help='New entry')
parser.add_argument("-l", action="store_true", help='print logs')
args = parser.parse_args()
if args.l:
printLogs()
The primary purpose of argparse is to parse the input (sys.argv), and give you a set of argument values (args is a simple namespace object). callbacks is a optparse concept that have not been included in argparse.
The FooAction example in the docs, http://docs.python.org/3.4/library/argparse.html#action, does something like this optparse callback. It prints some information when called, and then does the important thing - set a value in the namespace.

Can't solve Python argparse error 'object has no attribute'

When I run this code I get
AttributeError: 'ArgumentParser' object has no attribute 'max_seed'
Here's the code
import argparse
import ConfigParser
CFG_FILE='/my.cfg'
# Get command line arguments
args = argparse.ArgumentParser()
args.add_argument('verb', choices=['new'])
args.add_argument('--max_seed', type=int, default=1000)
args.add_argument('--cmdline')
args.parse_args()
if args.max_seed:
pass
if args.cmdline:
pass
My source file is called "fuzz.py"
You should first initialize the parser and arguments and only then get the actual arguments from parse_args() (see example from the docs):
import argparse
import ConfigParser
CFG_FILE='/my.cfg'
# Get command line arguments
parser = argparse.ArgumentParser()
parser.add_argument('verb', choices=['new'])
parser.add_argument('--max_seed', type=int, default=1000)
parser.add_argument('--cmdline')
args = parser.parse_args()
if args.max_seed:
pass
if args.cmdline:
pass
Hope that helps.
If you use argparse parsed arguments inside another class (somewhere you do self.args = parser.parse_args() ), you might need to explicitly tell your lint parser to ignore Namespace type checking. As told by #frans at Avoid Pylint warning E1101: 'Instance of .. has no .. member' for class with dynamic attributes
:
Just to provide the answer that works for me now - as [The
Compiler][1] suggested you can add a rule for the problematic class in
your projects .pylintrc:
[TYPECHECK]
ignored-classes=Namespace
[1]: https://stackoverflow.com/users/2085149/the-compiler

Mercurial Commit Hook with Python main function

I'm trying to create a complex mercurial commit hook in python. I want to also be allowed to pass parameters using OptionParser. Here is the gist of what I have so far:
.hg/hgrc config:
[hooks]
commit = python:/mydir/pythonFile.py:main
# using python:/mydir/pythonFile.py doesn't work for some reason either
pythonFile.py:
def main(ui, repo, **kwargs):
from optparse import OptionParser
parser = OptionParser()
parser.add_option('--test-dir', action='store', type="string",
dest='test_dir', default='otherdir/',
help='help info')
(options, args) = parser.parse_args()
# do some stuff here
someFunc(options.test_dir)
if __name__ == '__main__':
import sys
main(sys.argv[0], sys.argv[1], sys.argv[2:])
When I run hg commit -m 'message' I get an error: "Usage: hg [options] hg: error: no such option: -m". When I try hg commit --test-dir '/somedir' I get an error: "hg commit: option --test-dir not recognized".
Lastly I tried specifying commit = python:/mydir/pythonFile.py:main --test-dir '/somedir' in the hgrc config and I got this error: "AttributeError: 'module' object has no attribute 'main --test-dir '/somedir''"
Thank you for your help.
I think your problem may be in trying to import something that isn't part of the python packaged with mercurial.
If what you need is to pass additional information to the hook such that you can configure it differently for different repos/branches etc, you could use
param_value= ui.config('ini_section', 'param_key', default='', untrusted=False)
where ini_section is the bit in [] in the mercurial.ini / .hgrc file and param_key is the name of the entry
so something like
[my_hook_params]
test-dir=/somedir
then use
test_dir = ui.config('my_hook_params', 'test-dir', default='otherdir/', untrusted=False)

Categories

Resources