Critic my module

Devyn Collier Johnson devyncjohnson at gmail.com
Fri Jul 26 06:59:15 EDT 2013


On 07/25/2013 10:09 AM, Alain Ketterlin wrote:
> Devyn Collier Johnson <devyncjohnson at gmail.com> writes:
>
>>     I made a Python3 module that allows users to use certain Linux
>> shell commands from Python3 more easily than using os.system(),
>> subprocess.Popen(), or subprocess.getoutput(). This module (once
>> placed with the other modules) can be used like this
> Good, but I doubt it's really useful: I think nobody is going to add a
> dependency on your module for, basically, one-line wrappers...
>
> Here are a few comments:
>
>> def ls():
>> 	version = '0.3'
>> 	print(subprocess.getoutput('ls'))
> version is local here, so basically your first statement is useless
> (search for "global" in python's language ref).
>
>> def uname():
>> 	version = '0.3'
>> 	print(platform.uname())
> I once learned: "never print anything in a library function". This is a
> bad thing to do, for a variety of reasons. For instance, stdout may be
> redirected during this call...
>
>> def man(x):
>> 	version = '0.3'
>> 	print(subprocess.getoutput('man' + x))
> getoutput is (essentially) Popen(...,shell=True), and the doc says:
>
> "the use of shell=True is strongly discouraged in cases where the
> command string is constructed from external input"
>
> (for very good reasons)
>
>> def clear_bash_history():
>> 	version = '0.3'
>> 	print(subprocess.getoutput('history -c'))
> Who told you subprocess will use bash? Again, the doc:
>
> "On Unix with shell=True, the shell defaults to /bin/sh."
>
> All your uses of bash-isms may break (esp. "!!")
>
>> def firefox():
>> 	version = '0.3'
>> 	print(subprocess.Popen('(firefox &)'))
> See section "Replacing the os.spawn family" in... the doc.
>
>> def go_back():
>> 	version = '0.3'
>> 	print(subprocess.Popen('cd !!:1'))
> Hopeless. Have you tried this?
>
>> def reboot():
>> 	version = '0.3'
>> 	print(subprocess.Popen('shutdown -r now'))
> What do you expect this to print? I mean, after shutdown/reboot.
>
>> version = '0.6b'
> So, what's the version? 0.3 or 0.6b
>
> (btw, are you sure this "version" is the same as the one you use in all
> functions?).
>
> -- Alain.

The version in each function is the version of that function if users 
want to know what version they are using. The last version is for the 
whole module. The module overall is version 0.6b. The module started 
with a few functions and as I increased the number of functions, I 
increased the module version number. It is a coincidence that all of the 
modules happen to have the same version number. I increase the version 
number after I work on a function. I cannot remember the command to 
print a module's/function's version number, but with that command, you 
could see the version of a particular function or module. No, I have not 
tried go_back(), thank you for catching that.

The main point of this is for shell users that are using Python and do 
not know some of the Python commands. This module would make Python more 
like a Linux shell. For instance, a shell user would type boash.uname() 
because they may not know they can type "import platform; platform.uname()".

I know that printing is not really the best of ideas, but how else can I 
make the output be displayed without quotes or newline marks?

Thank you very much Alain Ketterlin for your feedback!

Mahalo,

DCJ



More information about the Python-list mailing list