Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67102 > unrolled thread

Re: Need help in writing some code so i can re-use it in every module or class

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2014-02-26 19:50 +0100
Last post2014-02-26 19:50 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Re: Need help in writing some code so i can re-use it in every module or class Jean-Michel Pichavant <jeanmichel@sequans.com> - 2014-02-26 19:50 +0100

#67102 — Re: Need help in writing some code so i can re-use it in every module or class

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2014-02-26 19:50 +0100
SubjectRe: Need help in writing some code so i can re-use it in every module or class
Message-ID<mailman.7398.1393440635.18130.python-list@python.org>
----- Original Message ----- 

> Hello Experts,

> I have requirement, like i want to use below command in python
> script.

> <command> --username <username> --password <password> <Command line
> arguments>

> now my requirement is i want to write some class so i can re-use
> "<command> --username <username> --password <password>" part via
> importing as module or class .. and re-use that in other module or
> classes .. so i dont have to write that in every module or classes
> ..

> Now why i wan to do this is ... currently <command> we are using is
> going to change in near future to <command1>, so i dont have go to
> every module and change that command if i have written single module
> or class and re-use it in other ?

> Hope i am clear enough to describe my issue?

> any suggestions ?

> Regards,
> DJ

Hi,

Have a look at http://docs.python.org/2/library/argparse.html

Then create a module, for instance myParser, that defines the common paser, and in each of your scripts import that module and use the parser.

*Untested code*

myParser.py

import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
#then define the parser


then in a script:

script.py

import myParser

if __name__ == '__main__':
    args = myParser.parser.parse_args()
    # and so on...

Cheers,

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web