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
| 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. |
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
| # 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 |
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 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") |
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
| 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 |
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
| """ | |
| Tkinter: listbox + scrollbar example | |
| """ | |
| #!/usr/bin/env python | |
| # -*- coding: cp1252 -*- | |
| import Tkinter as tk | |
| class Application(tk.Frame): | |
| def __init__(self, master=None): |
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
| """ | |
| 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 |
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
| """ | |
| *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/ | |
| """ |
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 simple decorator example with function | |
| """ | |
| import functools | |
| def function_wrapper(wrapped): # function closure | |
| @functools.wraps(wrapped) | |
| def _wrapper(*args, **kwargs): | |
| print "enter wrapper" |
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): |
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 |