Skip to content

Instantly share code, notes, and snippets.

View lsauer's full-sized avatar
🎯
Focusing

Lorenz Lo Sauer lsauer

🎯
Focusing
View GitHub Profile
@lsauer
lsauer / gist:1210023
Created September 11, 2011 19:45 — forked from timbroder/gist:1209944
insert update url rewrite
#INSERT INTO `core_url_rewrite` (`store_id`, `category_id`, `product_id`, `id_path`, `request_path`, `target_path`, `is_system`)
SELECT 1 AS `store_id`,
`sub`.`category_id`,
`sub`.`entity_id` AS `product_id`,
CONCAT('product', '/', `entity_id`, '/', `category_id`) AS `id_path`,
`value` AS `request_path`,
CONCAT('catalog/product/view/id/', `entity_id`, '/category/', `category_id`) AS `target_path`,
1 AS `is_system`
FROM
(SELECT
@lsauer
lsauer / gist:1213403
Created September 13, 2011 08:20
Submitting a browser-like HTTP request
#L sauer 2011, public domain
def main():
import urllib2
furl = urllib2.Request('http://www.mypacm.us.gov/bin/www_bget?one=two')
furl.add_header('Referer', 'http://www.mypacm.us.gov/')
furl.add_header('User-agent', 'Mozilla/5.0 AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.35 Safari/535.1')
furl.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
furl.add_header('Accept-Encoding', 'gzip,deflate,sdch')
furl.add_header('Accept-Language', 'en-US,en;q=0.8')
#furl.add_header('Origin', 'http://www.mypacm.us.gov/bin/')
@lsauer
lsauer / gist:1238036
Created September 23, 2011 18:04
Social Box for websites with Facebook, Twitter and Google plus one - HTML 4 / 5
<style type="text/css">
.socialBoxV, .socialBoxH { {
float: right;
margin: 15px 20px 0 0;
padding: 7px 7px 7px 14px;
background-color: #FCF6C4;
border-radius: 5px;
-o-transition: all 0.7s linear;
-moz-transition: all 0.7s linear;
@lsauer
lsauer / gist:1238371
Created September 23, 2011 20:26
Color and grey palette in PHP and Javascript
//author: unknown/ extended by Lo Sauer (2000); Codebase yr2000
//descr: Color and grey palette in PHP with Javascript interaction
<script language="JavaScript">
var coRed=127;coGreen=127;coBlue=255; // Original color
var Hex=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
function rgb2hex(cRed,cGreen,cBlue){
cColor=Hex[Math.floor(cRed/16)]+Hex[cRed%16]+Hex[Math.floor(cGreen/16)]+Hex[cGreen%16]+Hex[Math.floor(cBlue/16)]+Hex[cBlue%16];
@lsauer
lsauer / gist:1247582
Created September 28, 2011 10:19
Employee list - anonymized for statistical evaluation
//author: lsauer.com
//Data is in the form
//[ ["JOEBRA","Doeduardo","9.9.1999","Midwaystreet 12","1160 Vienna", "Austria" ],...]
//if you need the name fully anonymized shuffle the name before substr or calculate a Soundex of it
for(i=0; i<x.autpolData.length; i++){
tmp[i] = [].concat(x.autpolData[i][1].substr(0,4), x.autpolData[i][2].split('.')[2], x.autpolData[i].slice(3) )
}
//["Doed", "1948", "Midwaystreet 12", "1160 Vienna", "Austria"
//the address is resolved to GPS lat/long -> see my scripts_n_snips folder
//often times JS is faster and more powerful than an editor with RegExp
@lsauer
lsauer / gist:1260006
Created October 3, 2011 19:32
PHP's trim(chars) in JS and mapping RegExp matches
//author: lo sauer 2011 - lsauer.com
//implementation of PHP's trim
//see http://php.net/manual/en/function.trim.php
//bug: doesn't work with ']' in the charlist
//@param: charlist <string>
String.prototype.phptrim = function(s)
{
if(0==arguments.length)
return this.valueOf().trim();
@lsauer
lsauer / gist:1259821
Created October 3, 2011 18:15
RegEx - matching numbers only in select regions (JS RegEx vs Python PCRE)
#author: lo sauer 2011 - lsauer.com
#demonstrating the difference in RegEx engines
Python:
import re
a="123 test 42 <!-- comment 345 commentEnd --> 678 test2 348"
filter(None, re.findall('(?:\<\!--.+\d+.+--\>)|(\d+)',a))
#With RegEx lookbehinds; for demonstration only - not very practical (slow!)
re.findall('(\d+)(?!(?:[^<]+|<(?!!--))*-->)',a))
@lsauer
lsauer / gist:1259865
Created October 3, 2011 18:36
Filter empty elements from an Array or csv string/file
//lo sauer 2011 - lsauer.com
//join() is by default: join(',')
[1,2,,3,,3,,,0,,,4,,4,,5,,6,,,,,8,,,-2].filter(String).join()
>>>"1,2,3,3,0,4,4,5,6,8,-2"
//same result for: filter(function(){return 1});
[1,2,,3,,3,,,0,,,4,,4,,5,,6,,,,,8,,,-2].filter(function(){return 1}).join()
>>>"1,2,3,3,0,4,4,5,6,8,-2"
//to conveniently eliminate null-elements...
@lsauer
lsauer / gist:1264847
Created October 5, 2011 16:09
JSON.stringify: Forcing Array notation for JavaScript's serialized function arguments
//author: lo sauer 2011, lsauer.com
//description: Forcing JSON array notation on array-like object (i.e. arguments, DOMCollection)
//application: e.g. JSON-RPC
//Take for instance this simple function, which returns a pure JS object notation
function x(){console.log(JSON.stringify(arguments))}
x(1,2,3,"sfdsf", [1,2,3], {1:2})
>>>[{"0":1,"1":2,"2":3,"3":"sfdsf","4":[1,2,3],"5":{"1":2}}]
//There is no straightforward Array typecasting in JS; Array() will nest the object in an array
@lsauer
lsauer / gist:1278258
Created October 11, 2011 14:38
Importing a SQL database file without DATABASE CREATE information
//instructions are especially for mysql:
//lo sauer, 2011
1. create the database: 'CREATE DATABASE `enzymes`;'
2. now either select the database with 'USE `enzymes`;' or put the same command at the beginning of your sql-file.
in the latter case run sqlimport which is a shortcut for the more powerful command LOAD DATA INFILE http://dev.mysql.com/doc/refman/5.1/en/load-data.html
' mysqlimport -u root -p --local enzymes enzymes.sql'
Or in mysql (e.g. mysql -u root -p) you can use the LOAD DATA INFILE Command