Skip to content

Instantly share code, notes, and snippets.

View rmax's full-sized avatar
:octocat:
ヾ(⌐■_■)ノ♪

R Max Espinoza rmax

:octocat:
ヾ(⌐■_■)ノ♪
View GitHub Profile
@rmax
rmax / gist:1250036
Created September 29, 2011 05:20
aggreate tweets in 15-minutes slots
import sys
from disco import func
from disco.core import Job
def mapper((id, tweet), params):
import rfc822
from datetime import datetime, timedelta
from time import mktime
utc_dt = datetime.fromtimestamp(mktime(rfc822.parsedate(tweet['created_at'])))
@rmax
rmax / gist:1350162
Created November 9, 2011 02:41
bash prompt
# http://i.imgur.com/Tg068.png
PS1='\[\033[01;34m\]`if [ \$? = 0 ]; then echo \[\e[0\;32m\]\(^_^\); else echo \[\e[0\;31m\]\(0_0\); fi` ~ ${SECONDS}s\n\[\e[1;34m\][\t \u@\h:\w]\n\[\033[1;35m\]$>\[\033[00m\] '
@rmax
rmax / example usage
Created December 2, 2011 03:40
script to download mp3 files from contenidos.comteco.com.bo
bash$ python mp3box.py http://contenidos.comteco.com.bo/component/content/article/15-mp3-box/6434-top-40-usa.html
Downloading adele-rolling_in_the_deep.mp3 to /home/rolando/adele-rolling_in_the_deep.mp3
Downloading blake_shelton-honey_bee.mp3 to /home/rolando/blake_shelton-honey_bee.mp3
Downloading bruno_mars-grenade.mp3 to /home/rolando/bruno_mars-grenade.mp3
Downloading bruno_mars-just_the_way_you_are.mp3 to /home/rolando/bruno_mars-just_the_way_you_are.mp3
...
@rmax
rmax / avg.orig.py
Created December 30, 2011 00:34
non-pythonic vs pythonic code
from __future__ import division
def c_avrg(the_dict, exclude):
""" Calculate the average excluding the given element"""
i = 0
total = 0
for e in the_dict:
if e != exclude:
i += 1
total += the_dict[e]
--- a/scrapy/contrib/downloadermiddleware/httpcompression.py
+++ b/scrapy/contrib/downloadermiddleware/httpcompression.py
@@ -33,7 +33,11 @@ class HttpCompressionMiddleware(object):
def _decode(self, body, encoding):
if encoding == 'gzip' or encoding == 'x-gzip':
- body = gunzip(body)
+ try:
+ body = gunzip(body)
+ except IOError:
@requestGenerator
def parse_profile(self, response):
base_url = response.url
ul = UserLoader(response=response)
ul.add_xpath('name', '//h1[1]/text()')
ul.add_xpath('website', '//*[@rel="me" and @class="url"]/text()')
ul.add_xpath('location', '//*[@class="label adr"]/text()')
ul.add_value('url', base_url)
item = ul.load_item()
@rmax
rmax / hoja_doblada.py
Last active December 25, 2015 11:49
Calculo del área cubierta por una hoja doblada. Visualización en geogebra: http://www.geogebratube.org/student/m52908?mobile=true
"""Calculo del area cubierta por una hoja doblada."""
from __future__ import division
import math
import numpy as np
def solve(w, h, x1, x2):
"""Resuelve el calculo de area cubierta de una hoja doblada.
@rmax
rmax / 01-square-detector.py
Last active December 29, 2015 07:59
Solution to Facebook's Hacker Cup 2014 first problem: Square Detector
#!/usr/bin/env python
# encoding: utf-8
"""Square Detector
https://www.facebook.com/hackercup/problems.php?pid=318555664954399&round=598486203541358
You want to write an image detection system that is able to recognize different geometric shapes. In the first version of the system you settled with just being able to detect filled squares on a grid.
You are given a grid of NxN square cells. Each cell is either white or black. Your task is to detect whether all the black cells form a square shape.
@rmax
rmax / xpathfuncs.py
Last active August 29, 2015 14:04 — forked from shirk3y/lxml_has_class.py
"""XPath extension functions for lxml, inspired by:
https://gist.github.com/shirk3y/458224083ce5464627bc
Usage:
import xpathfuncs; xpathfuncs.setup()
"""
import string
@rmax
rmax / myspider.py
Last active April 7, 2021 18:37
An example of a Scrapy spider returning a Twisted deferred.
from scrapy import Spider, Item, Field
from twisted.internet import defer, reactor
class MyItem(Item):
url = Field()
class MySpider(Spider):