Skip to content

Instantly share code, notes, and snippets.

View dmiro's full-sized avatar

David Miró dmiro

View GitHub Profile
@dmiro
dmiro / Extraer fecha de una cadena
Created December 13, 2013 17:30
Extraer fecha de una cadena Toma un bloque arbitrario del texto, buscar algo que se parece a una cadena de fecha y construye un objeto DateTime fuera de ella
from dateutil.parser import parse
parse('hola que tal 25 October 1990 ya esta', fuzzy=True)
# datetime.datetime(1990, 10, 25, 0, 0)
text = (u'%s' % 'voil\u00e0').encode('utf-8')
# voilà
@dmiro
dmiro / split
Created December 13, 2013 18:38
split a lista en python
"hola esto es un split a lista en python".split()
# ['hola', 'esto', 'es', 'un', 'split', 'a', 'lista', 'en', 'python']
"""
CHEAT 1: a class with person property using @property decorator
There are two ways to specify a property:
(1) property class
(2) @property decorator
https://docs.python.org/2/library/functions.html#property
"""
@dmiro
dmiro / cheat_2.py
Created May 21, 2014 13:43
Python clausule With and Context Managers
"""
CHEAT 2: clausule With and Context Managers
In python the with keyword is used when working with unmanaged resources (like file streams).
It is similar to the using statement in VB.NET and C#
It allows you to ensure that a resource is "cleaned up" when the code that uses it finishes running,
even if exceptions are thrown. It provides 'syntactic sugar' for try/finally blocks.
https://docs.python.org/2/whatsnew/2.6.html#pep-343-the-with-statement
"""
@dmiro
dmiro / cheat_3.py
Created May 21, 2014 13:58
Python property class
"""
CHEAT 3: a class with person property using property class
There are two ways to specify a property:
(1) property class
(2) @property decorator
https://docs.python.org/2/library/functions.html#property
"""
@dmiro
dmiro / cheat_4.py
Created May 21, 2014 14:08
Python With & @contextmanager decorator
"""
CHEAT 4: with and @contextmanager decorator
Creating context managers the traditional way, by writing a class with __enter__() and __exit__() methods,
is not difficult. But sometimes it is more overhead than you need just to manage a trivial bit of context.
In those sorts of situations, you can use the contextmanager() decorator to convert a generator function
into a context manager.
http://pymotw.com/2/contextlib/#from-generator-to-context-manager
"""
@dmiro
dmiro / cheat_5.py
Last active August 29, 2015 14:01
Python Yield
"""
CHEAT 5: yield
Generators are iterators, but you can only iterate over them once. It's because they do not
store all the values in memory, they generate the values on the fly.
Yield is a keyword that is used like return, except the function will return a generator.
http://freepythontips.wordpress.com/2013/09/29/the-python-yield-keyword-explained/
http://www.alvarohurtado.es/generadores-en-python/
"""
@dmiro
dmiro / set_interval.py
Last active August 29, 2015 14:01
Python Timer & set interval snippet
"""
Timer example
"""
from threading import Timer
def hello():
print "hello, world"
#test
t = Timer(5.0, hello)
t.start() # after 5 seconds, "hello, world" will be printed
@dmiro
dmiro / cheat_6.py
Last active August 29, 2015 14:01
Python @decorator to create decorator
"""
Python @decorator to create decorator
http://librosweb.es/eventos/pycon-2014/como-crear-decorators-avanzados-en-las-aplicaciones-python/
"""
import functools
class object_proxy(object):
def __init__(self, wrapped):