Skip to content

Instantly share code, notes, and snippets.

View dmiro's full-sized avatar

David Miró dmiro

View GitHub Profile
@dmiro
dmiro / gist:fa05d8337b93532dff36
Created January 29, 2015 13:09
How to properly use python's isinstance() to check if a variable is a number?
You can use the types module:
>>> import types
>>> var = 1
>>> NumberTypes = (types.IntType, types.LongType, types.FloatType, types.ComplexType)
>>> isinstance(var, NumberTypes)
True
Note the use of a tuple to test against multiple types.
@dmiro
dmiro / main.py
Last active August 29, 2015 14:13
scrapy (scraping) + whoosh (indexer) example
# http://stackoverflow.com/questions/23921986/web-scraping-without-knowledge-of-page-structure
_Author = Farsheed Ashouri
import os
import sys
import re
## Spider libraries
from scrapy.spider import BaseSpider
from scrapy.selector import Selector
from main.items import MainItem
from scrapy.http import Request
@dmiro
dmiro / ytb.py
Created January 7, 2015 15:46
Ejemplo obtener 40 primeros resultados de una busqueda en youtube de un texto "la guerra del opio"
from pyquery import PyQuery as pq
from urllib import urlencode
for page in range(2):
params = urlencode({'filters':'video', 'page':page, 'search_query': 'la guerra del opio'})
jq = pq(url="http://www.youtube.com/results?%s" % params,
headers={"user-agent": "Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20140129 Firefox/24.0"})
jq.make_links_absolute("http://www.youtube.com")
for video in jq("ol.item-section").children().items():
url = video.find("a.yt-uix-tile-link").attr("href")
@dmiro
dmiro / download_youtube_pafy_example.py
Last active August 29, 2015 14:07
Download youtube video with pafy library
import pafy
# code at : https://github.com/np1/pafy
# you must copy pafy.py in your project directory
# show info about youtube video
url = "https://www.youtube.com/watch?v=oIlIVFBBbNw"
video = pafy.new(url)
print video.title
print video.duration
@dmiro
dmiro / lb.py
Created May 27, 2014 13:48
Tkinter: listbox + scrollbar example
"""
Tkinter: listbox + scrollbar example
"""
#!/usr/bin/env python
# -*- coding: cp1252 -*-
import Tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
@dmiro
dmiro / atexit.register.py
Last active August 29, 2015 14:01
Python atexit.register example
"""
atexit.register example
The atexit module defines a single function to register cleanup functions.
Functions thus registered are automatically executed upon normal interpreter termination
https://docs.python.org/2/library/atexit.html
"""
# Caution: If you work with IDLE this example doesn't work
@dmiro
dmiro / cheat_8.py
Last active August 29, 2015 14:01
Python *args and **kwargs in Python
"""
*args and **kwargs in Python (variable length argument lists in Python)
Especificar que una función puede ser llamada con un número arbitrario de argumentos.
Estos argumentos serán organizados en una lista o diccionario. Antes del número
variable de argumentos, cero o más argumentos normales pueden estar presentes
http://www.saltycrane.com/blog/2008/01/how-to-use-args-and-kwargs-in-python/
"""
@dmiro
dmiro / cheat_7.py
Last active August 29, 2015 14:01
Python simple decorator
"""
Python simple decorator example with function
"""
import functools
def function_wrapper(wrapped): # function closure
@functools.wraps(wrapped)
def _wrapper(*args, **kwargs):
print "enter wrapper"
@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):
@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