This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dateutil.parser import parse | |
| parse('hola que tal 25 October 1990 ya esta', fuzzy=True) | |
| # datetime.datetime(1990, 10, 25, 0, 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| text = (u'%s' % 'voil\u00e0').encode('utf-8') | |
| # voilà |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "hola esto es un split a lista en python".split() | |
| # ['hola', 'esto', 'es', 'un', 'split', 'a', 'lista', 'en', 'python'] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 | |
| """ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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/ | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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): |