Skip to content

Instantly share code, notes, and snippets.

View openrijal's full-sized avatar
🇳🇵
<code />

Nitesh Rijal openrijal

🇳🇵
<code />
View GitHub Profile
@openrijal
openrijal / media_to_uri.java
Last active April 21, 2021 03:26
Get Path from URI in Android.
/*
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);
@openrijal
openrijal / ip2geo.sh
Created April 3, 2013 18:33
Bash Script to find Geolocation from IP Addresses in apache's access log. You need to have "geoiplookup" package from GeoLite package. The excerpts are taken from http://danielmiessler.com/blog/a-simple-script-for-harvesting-dns-country-state-and-city-information-from-a-list-of-ip-addresses
#!/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
@openrijal
openrijal / gist:8120686
Created December 25, 2013 06:30
BASE_URL for Django from A BASE_URL Template Variable in Django http://www.micahcarrick.com/base-url-in-django.html via @MicahCarrick
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://'
# 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
@openrijal
openrijal / circular_deaths.py
Created May 25, 2017 12:16
circular deaths probelm
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]
@openrijal
openrijal / months_delta.py
Created April 27, 2017 05:22
substract month from python datetime
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)
@openrijal
openrijal / mysql_transaction_iso
Created January 11, 2017 05:50
MySQL Transaction Isolation Level
##
# 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
@openrijal
openrijal / periods_delta.py
Last active November 10, 2016 10:24
methods to calculate datetime from days, months and years delta in python
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
@openrijal
openrijal / first_last_day.py
Last active October 3, 2016 07:57
return first and last day of a month (in datetime)
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):
@openrijal
openrijal / nginx.conf
Created September 9, 2016 13:51 — forked from asmallteapot/nginx.conf
My default Nginx configuration for serving Django projects.
# 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