Herramientas de usuario

Herramientas del sitio


wiki2:python:basic

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
wiki2:python:basic [2017/08/19 13:54]
alfred
wiki2:python:basic [2021/09/12 14:16] (actual)
Línea 48: Línea 48:
 </​code>​ </​code>​
  
 +
 +==== Decorators ====
 +
 +=== Class decorator ===
 +<​code>​
 +def decorator(cls):​
 +    class Wrapper(object):​
 +        def __init__(self,​*args,​**kwargs):​
 +            self.wrapper = cls(*args,​**kwargs)
 +
 +        """​
 +        def __getattr__(self,​ name, *args, **kwargs):
 +            print('​Getting the {} of {}, {}, {}'​.format(name,​ self.wrapped,​ repr(args), repr(kwargs)))
 +            return getattr(self.wrapped,​ name)
 +        """​
 +
 +        def __getattr__(self,​ attr):
 +            print attr
 +            def wrapper(*args,​ **kw):
 +                print('​called with %r and %r' % (args, kw))
 +                return getattr(self.wrapper,​ attr)(*args,​ **kw)
 +            return wrapper
 +
 +    return Wrapper
 +
 +@decorator
 +class C(object):
 +    def method(self,​ x, y):
 +        print '​b'​
 +
 +if __name__ == '​__main__':​
 +    c = C()
 +    c.method(1,​2)
 +
 +</​code>​
 ===== Call several Python functions ===== ===== Call several Python functions =====
 <code python> <code python>
Línea 67: Línea 102:
 cwd = os.getcwd() cwd = os.getcwd()
 </​code>​ </​code>​
-File name from a path:+ 
 +Script relative ​path:
 <​code>​ <​code>​
-os.path.basename(value)+__file__
 </​code>​ </​code>​
  
 +Get the relative path of a file (the folder where it is):
 +<​code>​
 +os.path.dirname(__file__)
 +</​code>​
 +
 +''​os.path.abspath'':​ Get the absolute path:
 +<​code>​
 +os.path.abspath(the_relative_path)
 +</​code>​
 +
 +''​os.path.dirname'':​ The parent path of the folder\file:​
 +<​code>​
 +print(root)
 +print(os.path.dirname(root))
 +/​home/​alfred/​Desktop/​presentesNOW/​ReDesktop-SUBJECTS/​Kibbutz
 +/​home/​alfred/​Desktop/​presentesNOW/​ReDesktop-SUBJECTS
 +</​code>​
 +
 +''​os.path.basename'':​ The file\folder name:
 +<​code>​
 +print(root)
 +print(os.path.basename(root))
 +/​home/​alfred/​Desktop/​presentesNOW/​ReDesktop-SUBJECTS/​Kibbutz
 +Kibbutz
 +</​code>​
 +===== Implementar tu propio Context Manager =====
 +<​code>​
 +class Saved():
 +    def __init__(self,​ cr):
 +        self.cr = cr
 +    def __enter__(self):​
 +        self.cr.save()
 +        return self.cr
 +    def __exit__(self,​ type, value, traceback):
 +        self.cr.restore()
 +
 +cr.translate(68,​ 68)
 +for i in xrange(6):
 +    with Saved(cr):
 +        cr.rotate(2 * math.pi * i / 6)
 +        cr.rectangle(-25,​ -60, 50, 40)
 +        cr.stroke()
 +</​code>​
 +
 +
 +===== Ejecutar comandos y leer =====
 +
 +Ejecuta el comando, mostrando su salida, y devuelve un int.
 +<​code>​
 +import os
 +os.system('​ls -l')
 +</​code>​
 +<​code>​
 +import os
 +stream = os.popen('​echo Returned output'​)
 +output = stream.read()
 +output
 +</​code>​
 +===== Crear procesos =====
 +Crear un proceso y leer lo que sale por pantalla:
 +<​code>​
 +>>>​ a = subprocess.check_output('​ls'​)
 +>>>​ a
 +b'​alpine.tar\napps\nbin\nbuild\ncbf_git.txt\ncoma_git.txt\ndbcompare.venv\nDesktop\ndist\nDocuments\nDownloads\nexample.retry\nexample.yml\nfabfile.py\nflowRoot884.png\ngitea.dockerfiles\ngitea.tar\njenkins_home\nlogo2.png\nlogo2_small.png\nmanage.py\nMusic\nnew-repo\nPictures\n-proxy\nPublic\nREADME.md\nrema\nrequirements.txt\nresources\nscript.py\nTemplates\ntmp\nVideos\nvoid\nworkspace\nWorkspace\n'​
 +</​code>​
 +
 +The same but not using a bytestring:
 +<​code>​
 +>>>​ print(subprocess.check_output('​ls',​ universal_newlines=True))
 +alpine.tar
 +apps
 +bin
 +build
 +cbf_git.txt
 +coma_git.txt
 +dbcompare.venv
 +Desktop
 +dist
 +...
 +</​code>​
 +
 +Since running ''​ls -l''​ is that ''​ls''​ is the command, and ''​-l''​ is a flag to that command.
 +
 +<​code>​
 +>>>​ print(subprocess.check_output(['​ls',​ '​-l'​],​ universal_newlines=True))
 +total 103160
 +-rw------- ​ 1 alfred alfred ​ 5801472 Apr  8 14:46 alpine.tar
 +drwxrwxr-x ​ 9 alfred alfred ​    4096 Apr 29 15:49 apps
 +drwxrwxr-x ​ 3 alfred alfred ​    4096 Dec 18 14:33 bin
 +drwxrwxr-x ​ 4 alfred alfred ​    4096 Apr 29 15:49 build
 +-rw-rw-r-- ​ 1 alfred alfred ​     880 Jan 14 10:34 cbf_git.txt
 +...
 +</​code>​
 +
 +To tell Python to pass the parameters through the UNIX shell so it accepts special characters:
 +<​code>​
 +print(subprocess.check_output(['​ls',​ '​-l',​ '​*.txt'​],​ shell=True, universal_newlines=True))
 +</​code>​
 +
 +''​subprocess.run''​ takes many similar arguments to subprocess.check_output. But what's different is that it doesn'​t return a string, even when universal_newlines is set to True. Instead, it returns an instance of subprocess.CompletedProcess,​ which contains all sorts of information about the process that ran.
 +
 +
 +<​code>​
 +>>>​ subprocess.run(['/​bin/​ls',​ '​-l'​],​ universal_newlines=True,​ stdout=subprocess.PIPE)
 +>>>​ vars(cp)
 +{'​args':​ ['/​bin/​ls',​ '​-l'​],​ '​returncode':​ 0, '​stderr':​ None,
 +'​stdout':​ None}
 +</​code>​
 +
 +You also can assign stderr to subprocess.PIPE in order to receive it. Note that in the case of both stdout and stderr, you can assign not just subprocess.PIPE,​ which lets you grab and work with the program'​s output, but also an open (writable) file object. This means you can invoke an external process and put its output into an arbitrary file. I'd argue that most of the time, the reason you would be executing an external process in Python is that you want to do something to the text, but this will work.
 +
 +You might be wondering whether you can not only write to stderr and stdout, but also read from stdin. And the answer is definitely. Just provide a file object, and subprocess.run will do the rest. For example:
 +
 +<​code>​
 +>>>​ cp = subprocess.run(['/​bin/​cat',​ '​-n'​],​ stdin=open('/​etc/​passwd'​),​ stdout=subprocess.PIPE,​ universal_newlines=True)
 +</​code>​
 +
 +
 +===== SETS =====
 +<code python>
 +>>>​ my_set = set("​patata"​)
 +>>>​ my_set
 +{'​p',​ '​t',​ '​a'​}
 +</​code>​
 +<code python>
 +>>>​ united_set = first_set.union(second_set)
 +>>>​ united_set
 +{'​two',​ '​banana',​ '​three',​ '​peach',​ '​orange',​ '​one'​}
 +...
 +>>>​ first_set = {'​one',​ '​two',​ '​three'​}
 +>>>​ second_set = {'​orange',​ '​banana',​ '​peach',​ '​one'​}
 +>>>​ first_set.intersection(second_set)
 +{'​one'​}
 +...
 +>>>​ first_set = {'​one',​ '​two',​ '​three'​}
 +>>>​ second_set = {'​three',​ '​four',​ '​one'​}
 +>>>​ first_set.difference(second_set)
 +{'​two'​}
 +>>>​ second_set.difference(first_set)
 +{'​four'​}
 +</​code>​
wiki2/python/basic.1503150847.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)