Skip to content

2. general topics

jetxeberria edited this page Dec 28, 2018 · 3 revisions

Index


2. General topics

2.1 GPU programming

2.3 Text processing

2.4 System interaction

2.4.1 subprocess

The subprocess library allows to manage operative system commands. There are several ways to use it. See here

import subprocess
p = subprocess.Popen(["ls", "-l", "/etc/resolv.conf"], stdout=subprocess.PIPE)
output, err = p.communicate()
print "*** Running ls -l command ***\n", output
Output:
> *** Running ls -l command ***
> -rw-r--r-- 1 root root 157 Nov  7 15:06 /etc/resolv.conf

2.4.2 sys

  • sys.path is a variable of module sys that contains the list of directories which will be searched for modules at runtime
import sys
sys.path
  • Execute it from linux terminal without joining the interactive python by typing:
python -c 'import sys; print "\n".join(sys.path)'
  • Check which modules are built in by typing in Python or Ipython:
import sys
sys.builtin_module_names

2.5 Printing

2.5.1 Printing Hacks

To print special unicode characters, find the code in internet and it can be printed directly

text = u'2 \u00B1 1'
Output:
2 (+-) 1

But if it pass through the str() function, it needs to be explicitly advised the encoding

text = '2 {simbol} 1'.format(simbol=u'\u00B1'.encode('utf-8'))
Output:
2 (+-) 1

Go Up