Skip to content

Instantly share code, notes, and snippets.

@jcarlosroldan
jcarlosroldan / recursive-search-content.py
Last active February 13, 2018 12:21
Recursive search by content
"""
Recursive search by file content.
By Juan C. Roldán (https://juancroldan.com/)
Just change WORDS to whichever strings you are looking for.
"""
from os import listdir
from os.path import isdir
WORDS = ["this is a test", "localhost"]
@jcarlosroldan
jcarlosroldan / bypass-hotlink.php
Created March 7, 2018 14:11
Simple and safe script to bypass hotlinking protection
@jcarlosroldan
jcarlosroldan / spectrum.py
Last active October 28, 2020 12:56
Generate color spectrum using PIL
from PIL import Image
WIDTH = 700
HEIGHT = 350
img = Image.new('HSV', (WIDTH, HEIGHT))
for x in range(WIDTH):
for y in range(HEIGHT):
if y < HEIGHT / 2: # white to rainbow
@jcarlosroldan
jcarlosroldan / index.html
Last active July 31, 2019 14:17
Minimal web
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Project</title>
<link rel="stylesheet" href="style.css">
@jcarlosroldan
jcarlosroldan / where.py
Last active July 24, 2018 11:40
Geocode a place and retry everytime it fails
from geopy.exc import GeocoderTimedOut
from geopy.geocoders import Nominatim
# Choose your preferred geocoder at http://geopy.readthedocs.io/en/latest/#module-geopy.geocoders
_geo = Nominatim()
def where(place, timeout=10):
res = None
while res == None:
try:
@jcarlosroldan
jcarlosroldan / cache.py
Created July 24, 2018 11:40
Cache the result of any function
# -- the cache snippet ---------------------------------------------------------
from os import makedirs
from os.path import exists, dirname
from pickle import load as pload, dump as pdump
from re import sub
from time import time
def cache(target, args, identifier=None, cache_life=3 * 24 * 3600):
""" Run the target function with the given args, and store it to a pickled
@jcarlosroldan
jcarlosroldan / steganfind.py
Created July 24, 2018 11:43
Find compressed files hidden in images
from os import listdir
from os.path import isdir
""" Find images with hidden compressed formats """
ROOT = "path/to/a/folder/with/images"
IMAGE_EXTS = ["jpg", "png", "gif", "jpeg", "tiff", "svg", "bmp"]
EOF_SIGNATURES = {
""
}
@jcarlosroldan
jcarlosroldan / dynprog.py
Last active December 10, 2019 19:09
Abstract class to solve problems using dynamic programming
# -- the dynamic programming snippet -------------------------------------------
class DynProg:
""" Abstract class to solve problems using dynamic programming. To use it,
make a child class implementing alternatives, is_final and penalty. Then,
make one of such objects providing the initial state in the constructor, and
call to solve(), providing one search type. """
ONE_SOLUTION = 0
ONE_OPTIMAL_SOLUTION = 1
ALL_SOLUTIONS = 2
@jcarlosroldan
jcarlosroldan / mail.py
Last active July 24, 2018 11:56
Send a mail using SMTP
from smtplib import SMTP
def send_gmail(to, subject, text, username="********@gmail.com", password="********"):
""" Send a mail using simple SMTP. """
server = SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username, password)
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s"%(username, to, subject, text)
server.sendmail(username, to, msg)
server.quit()
@jcarlosroldan
jcarlosroldan / euler.py
Created July 24, 2018 12:00
A set of very optimised math auxiliar functions that I used in Project Euler.
from math import sqrt, ceil
from itertools import zip_longest, compress, chain, product, combinations
from functools import reduce
def prime_sieve(n):
"""
Returns: list of primes, 2 <= p < n
Performance: 0.5s for 10**7, 5s for 10**8, 69s for 10**9
"""
sieve = [True] * int(n / 2)