Skip to content

Instantly share code, notes, and snippets.

View dmiro's full-sized avatar

David Miró dmiro

View GitHub Profile
@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 / 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_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_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
"""
"""
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 / 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']
text = (u'%s' % 'voil\u00e0').encode('utf-8')
# voilà
@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)
@dmiro
dmiro / RoundDecimals.js
Created May 27, 2013 13:39
Round decimals
var original=28.453
//round "original" to two decimals
var result=Math.round(original*100)/100 //returns 28.45
// round "original" to 1 decimal
var result=Math.round(original*10)/10 //returns 28.5
//round 8.111111 to 3 decimals
var result=Math.round(8.111111*1000)/1000 //returns 8.111
@dmiro
dmiro / hasOwnProperty_correct_use.js
Last active August 8, 2017 19:53
hasOwnProperty correct use
var foo = {
hasOwnProperty: function() {
return false;
},
bar: 'Here be dragons'
};
foo.hasOwnProperty('bar'); // siempre devolverá false
// Utilice otro objeto con hasOwnProperty y llamelo con 'this' para asignarlo a foo
({}).hasOwnProperty.call(foo, 'bar'); // true