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


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

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

Started byUnix SA <d.joshi84@gmail.com>
First post2014-02-26 19:45 +0545
Last post2014-02-26 09:37 -0500
Articles 2 — 2 participants

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


Contents

  Need help in writing some code so i can re-use it in every module or class Unix SA <d.joshi84@gmail.com> - 2014-02-26 19:45 +0545
    Re: Need help in writing some code so i can re-use it in every module or class Roy Smith <roy@panix.com> - 2014-02-26 09:37 -0500

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

FromUnix SA <d.joshi84@gmail.com>
Date2014-02-26 19:45 +0545
SubjectNeed help in writing some code so i can re-use it in every module or class
Message-ID<mailman.7395.1393423618.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

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

[toc] | [next] | [standalone]


#67361

FromRoy Smith <roy@panix.com>
Date2014-02-26 09:37 -0500
Message-ID<roy-7FA3DD.09372026022014@news.panix.com>
In reply to#67097
In article <mailman.7395.1393423618.18130.python-list@python.org>,
 Unix SA <d.joshi84@gmail.com> wrote:

> 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 ?

What you are talking about is a very common technique.  You write some 
code which abstracts the intent of an operation, but hides the 
implementation details.  Then, you can change the implementation without 
all the places it's called from having to be aware of the change.

So, it sounds like you want to write a function which accepts the two 
variable parts of the command, namely the username and password.  That 
function will then use call(), or one of its variants, from the 
subprocess module (http://docs.python.org/2/library/subprocess.html) to 
execute the actual command, inserting all the other boilerplate.

If, at some point in the future, you change which command you're 
calling, nothing but your function needs to change.

[toc] | [prev] | [standalone]


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


csiph-web