This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Input: URI -- something like content://com.example.app.provider/table2/dataset1 | |
Output: PATH -- something like /sdcard/DCIM/123242-image.jpg | |
*/ | |
public String convertMediaUriToPath(Uri uri) { | |
String [] proj={MediaStore.Images.Media.DATA}; | |
Cursor cursor = getContentResolver().query(uri, proj, null, null, null); | |
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); | |
cursor.moveToFirst(); | |
String path = cursor.getString(column_index); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
cat /var/log/apache2/access_log | awk '{print $1}' > ips.txt | |
uniq ips.txt > uniqips.txt | |
IPS=`cat uniqips.txt` | |
for i in $IPS | |
do | |
echo "$i,`geoiplookup $i | cut -d "," -f2 | sed -e 's/^[\t]//'`" >> ipinfo.csv | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
context_processors.py | |
def baseurl(request): | |
""" | |
Return a BASE_URL template context for the current request. | |
""" | |
if request.is_secure(): | |
scheme = 'https://' | |
else: | |
scheme = 'http://' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bash command to uninstall all pip installed packages | |
sudo pip uninstall -y $(pip freeze | sed 's;==.*;;g' | tr '\n' ' ') | |
# bash command to list top 5 large files/directories | |
du -a / | sort -n -r | head -n 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
num = 100 | |
people = list(range(1,num+1)) | |
index = 0 | |
while len(people) > 1: | |
people.pop((index+1) % len(people)) | |
index = 0 if (index >= len(people)-1) else (index+1) | |
print people[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def monthdelta(date, delta): | |
m, y = (date.month+delta) % 12, date.year + ((date.month)+delta-1) // 12 | |
if not m: m = 12 | |
d = min(date.day, [31, | |
29 if y%4==0 and not y%400==0 else 28,31,30,31,30,31,31,30,31,30,31][m-1]) | |
return date.replace(day=d,month=m, year=y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Set isolation level to READ-COMMITTED | |
# https://dev.mysql.com/doc/refman/5.6/en/set-transaction.html | |
## | |
SELECT @@GLOBAL.tx_isolation, @@tx_isolation; | |
SET GLOBAL tx_isolation='READ-COMMITTED'; | |
SET SESSION tx_isolation='READ-COMMITTED'; | |
## | |
# Set binlog to MIXED format |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def days_period_to_datetime(dd): | |
now = datetime.datetime.utcnow() | |
return now + datetime.timedelta(days=dd) | |
def months_period_to_datetime(mm): | |
now = datetime.datetime.utcnow() | |
new_day = now.day |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timedelta, date | |
def get_first_day(dt, d_years=0, d_months=0): | |
# d_years, d_months are "deltas" to apply to dt | |
y, m = dt.year + d_years, dt.month + d_months | |
a, m = divmod(m - 1, 12) | |
dt = date(y + a, m + 1, 1) | |
return datetime(dt.year, dt.month, dt.day, 0, 0, 0) | |
def get_last_day(dt): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file: /etc/nginx/sites-available/example.com | |
# nginx configuration for example.com | |
server { | |
listen 80; | |
server_name example.com; | |
access_log /srv/www/example.com/logs/access.log; | |
error_log /srv/www/example.com/logs/error.log; | |
# pass root to django |
NewerOlder