1

So, I am trying to pass a code to main(), the original code is command line based (It takes arguments from command line interface) but I want to pass it to main() directly in program. Here is the original code.

if __name__ == '__main__':
    log_fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(level=logging.INFO, format=log_fmt)

    parser = argparse.ArgumentParser()
    parser.add_argument('json_in')
    parser.add_argument('graph_out')
    parser.add_argument('text_out')
    parser.add_argument('spm_model')
    parser.add_argument('--sequence', action='store_true')
    parser.add_argument('--simple-preprocessing',
                        action='store_false', dest='extended_preprocessing')
    parser.add_argument('--bpe-dropout', type=float, default=None)
    parser.add_argument('--sample-factor', type=int, default=1)

    args = parser.parse_args()
    main(args) 

Now I understand how to convert each line except 11th one, line including the --sequence.

I don't understand what to pass for dest in program.

I am actually working on colab and it throws as error SystemExit: 2 with argparse, thats why I dont want to use it anyway.

1 Answer 1

1

The flags with a - prefix, are accessed by removing the - prefix and making the non-prefix - underscores _, if there is no dest defined.

parser.add_argument('--sequence', action='store_true')
parser.add_argument('--simple-preprocessing',
                    action='store_false', dest='extended_preprocessing')
parser.add_argument('--bpe-dropout', type=float, default=None)
parser.add_argument('--sample-factor', type=int, default=1)

args = parser.parse_args()

args.sequence 
args.extended_preprocessing     # use dest
args.bpe_dropout
args.sample_factory
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks and I appreciate your quick answer, but the problem is I dont want to use arguments(args). Like I want to use pass these values in main directly!! as an example: json_in = 'link to file' I am trying to run this code on Colab actually.
you could make an object that works the same as the args object and use that instead, but maybe rewriting the signature to main makes more sense. ``` from dataclasses import dataclass @dataclass class Args: sequence = False extended_preprocessing = False bpe_dropout = None sample_factor = 1 args = Args(sequence=True, sample_factor = 3) main(args) ``` but maybe you want to rewrite main to take the arguments separately. ``` def main(sequence: bool, extend_preprocessing: bool, bpe_dropout: float, sample_factor: int) - > None: .... ```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.