Skip to content

Instantly share code, notes, and snippets.

@yevrah
yevrah / new_file0
Created September 17, 2014 23:02
Generate sqlAlchemy classes from existing db
pip install sqlacodegen
pip install MySQL-python
sqlacodegen mysql://www:www@localhost/db_name
@yevrah
yevrah / new_file0
Last active August 29, 2015 14:06
MySQL: Load data fast!
# More information can be found;
# - http://dev.mysql.com/doc/refman/5.1/en/load-data.html
# - http://dev.mysql.com/doc/refman/5.5/en/optimizing-innodb-bulk-data-loading.html
SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;
LOAD DATA INFILE IGNORE 'my_data.csv' INTO TABLE my_table
COMMIT;SET unique_checks=1;SET foreign_key_checks=1;
@yevrah
yevrah / parse_log_date.pl
Last active August 29, 2015 14:07
Logfile Date Filtering
#!/usr/bin/env perl
use strict 'vars';
use feature qw/say/;
my $date_from = &date_to_int( 'Oct', '14', '20', '00', '00');
my $date_to = &date_to_int( 'Oct', '15', '21', '00', '00');
while ( <DATA> ) {
chomp;
my ($month, $day, $hour, $min, $sec, $server, $msg) = $_ =~ m/(\w+) (\d+) (\d+):(\d+):(\d+) (\w+) (.*)/;
@yevrah
yevrah / token_sort.sh
Last active August 29, 2015 14:08
Sort classes, methods or other tokens in a file
#!/bin/sh
# sort on perl subs, can be changed to any other token for any other language
TOKEN=sub
gsed -e ':a;N;$!ba;s/\n/__newline__/g' "$1" > "$1.out"
gsed -i "s/__newline__\\s*$TOKEN\W/\\nsub /g" "$1.out"
sort $1.out -o $1.out
gsed -i 's/__newline__/\n/g' $1.out
@yevrah
yevrah / time.pl
Created November 5, 2014 04:52
Perl: Getting various times, GMT, Local, and Custom Time Zone
use strict;
use feature qw/say/;
use POSIX qw(strftime tzset);
say strftime("%Y %d %m %H:%M:%S GMT", gmtime(time)); # GMT
say strftime("%Y %d %m %H:%M:%S %Z", localtime(time)); # Local Time
# Set to custom timezone
$ENV{TZ} = 'America/Los_Angeles';
tzset;
@yevrah
yevrah / flock.pl
Created November 11, 2014 04:01
Perl: File locking
#!/usr/bin/env perl
use strict;
# import LOCK_* and SEEK_END constants
use Fcntl qw(:flock SEEK_END);
# Open the file
open(my $xml, ">>", "sample.xml") or die "Can't open xml $!";
# Lock the file
@yevrah
yevrah / context.pl
Created November 11, 2014 05:16
Perl: Determine application context
#!/usr/bin/env perl
use strict;
use warnings;
# Determine Context
use constant IS_MOD_PERL => exists $ENV{'MOD_PERL'};
use constant IS_PSGI => exists $ENV{'psgi.version'};
use constant IS_CGI => IS_MOD_PERL || IS_PSGI || exists $ENV{'GATEWAY_INTERFACE'};
@yevrah
yevrah / fixnames.sh
Created November 12, 2014 04:10
Bash: Repair case in source files when migrating from windows to linux
#!/bin/sh
# Replace filenames found in code and create a backup of original
find . -type f \( -name "*.php" -or -name "*.jpg" \) -exec bash -c 'gsed -i.bak "s/(\W`basename {}`\W/)`basename {}`/Ig"/src/ *' \;
########################
# Explanation
# find
# . : Recursively from current directory,
# -type f : files only - not folders,
@yevrah
yevrah / print_env.pl
Created November 21, 2014 00:07
Perl: print environment variables
#!/usr/bin/env perl
foreach $key (keys(%ENV)) {
printf("%-30.30s: $ENV{$key}\n", $key);
}
@yevrah
yevrah / mysql_processlist.sh
Last active August 29, 2015 14:10
MySQL Watch Processlist
#!/bin/bash
watch -n 1 --differences "mysql -u mon -e 'show processlist' | grep -v Sleep"