Coding Bash vs. Perl vs. Python (by Evan Liu)


Topics about help

Help: how to use help

In perl, use the below three commands to get helps on inner varaibles,inner functions, and keywords
perldoc -v PerlVar
perldoc -f PerlFunc
perldoc -q FAQKeywords

Tasks:

		  
Bash Perl Python
  1. #todo
  2. -------output----------
  1. #todo
  1. #run in linux shell
  2. #1. perldoc -v PerlVar
  3. perldoc -T -v '$_' |sed -n '/$_ =~/p' |sed 's/^[[:space:]]*//'
  4. #2. perldoc -f PerlFunc
  5. perldoc -T -f split | egrep 'split\(' |sed 's/^[[:space:]]*//'
  6. #3. perldoc -q FAQKeywords
  7. perldoc -T -q debug | egrep 'Dumper' |sed 's/^[[:space:]]*//'
  8. -------output----------
  1. #run in linux shell
  2. #1. perldoc -v PerlVar
  3. $_ =~ /^Subject:/
  4. $_ =~ tr/a-z/A-Z/
  5. #2. perldoc -f PerlFunc
  6. print join(':', split('b', 'abc')), "\n";
  7. print join(':', split('', 'abc')), "\n";
  8. print join(':', split(//, 'abc', 1)), "\n";
  9. print join(':', split(//, 'abc', 2)), "\n";
  10. print join(':', split(//, 'abc', 3)), "\n";
  11. print join(':', split(//, 'abc', 4)), "\n";
  12. print join(':', split(',', 'a,b,c,,,')), "\n";
  13. print join(':', split(',', 'a,b,c,,,', -1)), "\n";
  14. ($login, $passwd) = split(/:/);
  15. print join(':', split(/ /, ' abc')), "\n";
  16. print join(':', split(//, ' abc'));
  17. print join(':', split(//, ' abc', -1)), "\n";
  18. split(/-|,/, "1-10,20", 3)
  19. split(/(-|,)/, "1-10,20", 3)
  20. split(/-|(,)/, "1-10,20", 3)
  21. split(/(-)|,/, "1-10,20", 3)
  22. split(/(-)|(,)/, "1-10,20", 3)
  23. #3. perldoc -q FAQKeywords
  24. The Data::Dumper module can pretty-print Perl data structures:
  25. use Data::Dumper qw( Dumper );
  26. print STDERR "The hash is " . Dumper( \%hash ) . "\n";
  1. #run in linux shell
  2. #1. list all modules
  3. echo "pydoc modules"
  4. echo "python -c \"help('modules')\""
  5. #2. list installed packages
  6. echo "pip list"
  7. #3. help about 'sys' module
  8. echo "pydoc sys"
  9. echo "python -c \"help('sys')\""
  10. -------output----------
  1. #run in linux shell
  2. #1. list all modules
  3. pydoc modules
  4. python -c "help('modules')"
  5. #2. list installed packages
  6. pip list
  7. #3. help about 'sys' module
  8. pydoc sys
  9. python -c "help('sys')"

Help: get the module path

 

Tasks:
1.1. print the path of a module
		  
Bash Perl Python
  1. # todo
  2. -------output----------
  1. # todo
  1. # todo
  2. -------output----------
  1. # todo
  1. import os; os.__file__
  2. import re; print re.__file__
  3. import logging; logging.__file__
  4. import bpython; print bpython.__file__
  5. -------output----------
  1. /usr/lib/python2.7/re.pyc
  2. /usr/lib/python2.7/dist-packages/bpython/__init__.pyc

Help: get all functions of a module


		  
		  

Tasks:

		  
Bash Perl Python
  1. #todo
  2. -------output----------
  1. #todo
  1. #todo
  2. -------output----------
  1. #todo
  1. import re
  2. import inspect
  3. print dir(re)
  4. #or use inspect, which is better
  5. print inspect.getmembers(re, inspect.isfunction)
  6. -------output----------
  1. ['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_alphanum', '_cache', '_cache_repl', '_compile', '_compile_repl', '_expand', '_pattern_type', '_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'sys', 'template']
  2. #or use inspect, which is better
  3. [('_compile', <function _compile at 0x7f5e2eced1b8>), ('_compile_repl', <function _compile_repl at 0x7f5e2eced230>), ('_expand', <function _expand at 0x7f5e2eced2a8>), ('_pickle', <function _pickle at 0x7f5e2eced398>), ('_subx', <function _subx at 0x7f5e2eced320>), ('compile', <function compile at 0x7f5e2ecf2f50>), ('escape', <function escape at 0x7f5e2eced140>), ('findall', <function findall at 0x7f5e2ecf2e60>), ('finditer', <function finditer at 0x7f5e2ecf2ed8>), ('match', <function match at 0x7f5e2ecf2c08>), ('purge', <function purge at 0x7f5e2eced050>), ('search', <function search at 0x7f5e2ecf2c80>), ('split', <function split at 0x7f5e2ecf2de8>), ('sub', <function sub at 0x7f5e2ecf2cf8>), ('subn', <function subn at 0x7f5e2ecf2d70>), ('template', <function template at 0x7f5e2eced0c8>)]