Skip to content

Instantly share code, notes, and snippets.

View fedecarg's full-sized avatar
🥄
There is no spoon

Federico Cargnelutti fedecarg

🥄
There is no spoon
View GitHub Profile
@fedecarg
fedecarg / mysql-split-string.md
Last active October 22, 2018 12:33
MySQL Split String Function

MySQL Split String Function

The following example function takes 3 parameters, performs an operation using an SQL function, and returns the result.

Function

CREATE FUNCTION SPLIT_STR(
  x VARCHAR(255),
  delim VARCHAR(12),

pos INT

@fedecarg
fedecarg / python-interceptor.md
Last active June 12, 2022 03:23
Intercepting class method invocations using metaclass programming in Python

Intercept class method calls

Here’s an example of how to use metaclass programming to intercept class method calls similar to the method_missing technique in Ruby:

class ClassMethodInterceptor(type):

    def __getattr__(cls, name):
        return lambda *args, **kwargs: \
                   cls.static_method_missing(name, *args, **kwargs)
@fedecarg
fedecarg / svnlog-ldap-extractname.sh
Created March 7, 2013 11:52
Replace LDAP entries with full names in SVN log
#!/bin/bash
# Author [email protected]
svn_url="$(svn info . | grep 'URL:' | cut -c6-)"
echo "Repository ${svn_url}"
svn log -v --xml . > templog.log
touch svnlog.log
@fedecarg
fedecarg / rgb2hex.js
Created March 7, 2013 13:08
RGB to hexadecimal converter in 3 lines of code
function rgbToHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
@fedecarg
fedecarg / nearest-colour-name.py
Last active December 14, 2015 15:29
Python script to find the nearest matching colour name given input R,G, B values (from 0-255).
def rgb_from_str(s):
# s starts with a #.
r, g, b = int(s[1:3],16), int(s[3:5], 16),int(s[5:7], 16)
return r, g, b
def find_nearest_colour(R,G, B, colorD):
mindiff = None
for d in colorD:
r, g, b = rgb_from_str(colorD[d])
@fedecarg
fedecarg / search-and-replace.md
Last active December 14, 2015 15:29
Search and replace in files

Search

grep -Irl --include=\*.txt "keyword" .

or

find . -type f -print0 | xargs -0 grep "keyword"

Search and Replace

Amazon EC2

Monitoring

ssh -i ~/.ec2/my.pem [email protected] "cat /var/log/secure | grep 'refused connect'"

When using SSH to run commands across multiple machines without exchanging your public key, you need to type in your password for each machine. A quick and simple way of setting this up is to create a public key:

ssh-keygen -t rsa

Installing Ruby with RVM

Install RVM

federico@~$ echo insecure >> ~/.curlrc
federico@~$ curl -L https://get.rvm.io | bash -s stable --ruby
federico@~$ source ~/.bash_profile

Create a Gem set (Ruby and RoR)

Get the absolute position of every link in an html document using JavaScript:

/**
 * The HTML <area> coords attribute needs the position of the left, top, right, bottom corner 
 * of the rectangle. Element.getBoundingClientRect returns a text rectangle object that encloses 
 * a group of text rectangles.
 */
function getElementAbsolutePosition(element) {
    var pos = {};

pos.left = 0;

Spike: Image-based BBC News website

  • Create list of sites to visit: arabic, hindi, russian, mundo, etc.
  • Define supported screen widths, for example: 176, 208, 240, 320, 352.
  • Define the maximum size of an image. The current average size of a mobile page is 250 KB.
  • Quality: 100% - File size: 822 KB: http://goo.gl/A4ldIF
  • Quality: 50% - File size: 272 KB:http://goo.gl/AAbrY9
  • Quality: 30% - File size: 193 KB: http://goo.gl/gOUkXK
  • Crawl sites in a distributed, scalable and efficient way (scheduling policy, async requests, politeness, message queues, path-ascending crawling).
  • Note: The app must run very well on Linux and should also be capable of test runs on OSX.