Skip to content

Instantly share code, notes, and snippets.

View amiraliakbari's full-sized avatar

AmirAli Akbari amiraliakbari

View GitHub Profile
@amiraliakbari
amiraliakbari / find_class.py
Created October 3, 2014 18:25
Find JAR files containing a specific class/package
#!/usr/bin/env python
import os
import glob
import shutil
def main():
class_name = raw_input('Which class do you want to import? ')
dir_path = raw_input('Enter the path for directory containing JAR files: [.] ') or os.getcwd()
@amiraliakbari
amiraliakbari / editor.java
Created October 28, 2014 08:05
Useful Code Snippets fot Eclipse Plugins
void addSlice(IWorkbenchWindow workbench) {
IEditorPart editor = null;
for (IWorkbenchPage p : workbench.getPages()) {
editor = p.getActiveEditor();
if (editor != null) {
System.out.println("ED " + p.getLabel());
}
}
if (editor == null) {
System.err.println("Can not find any editors, skipped.");
@amiraliakbari
amiraliakbari / upvpn.sh
Last active August 29, 2015 14:09 — forked from mjnaderi/upvpn.sh
#!/bin/bash
VPN="Sharif ID"
trap "exit 0" SIGINT SIGTERM
while true; do
if ! nmcli con status | grep "$VPN" &>/dev/null; then
echo "[`date -R`] Trying to re-connect..."
nmcli -p con up id "$VPN"
@amiraliakbari
amiraliakbari / mysql.py
Created December 7, 2014 10:56
Auto Backup
#!/usr/bin/env python
import ConfigParser
import os
import time,sys
DB_NAME = 'PNAME'
PATH = '/www/PNAME/src/backup/'
DAY_LIMIT_FOR_KEEPING_BACKUPS = 60
# On Debian, /etc/mysql/debian.cnf contains 'root' a like login and password.
config = ConfigParser.ConfigParser()
config.read("/etc/mysql/debian.cnf")
@amiraliakbari
amiraliakbari / pyc_deleter.py
Created February 6, 2015 13:41
Remove .pyc files in current directory recursively
import os
import subprocess
for dirpath, dirnames, filenames in os.walk(os.getcwd()):
for each_file in filenames:
if each_file.endswith('.pyc'):
if os.path.exists(os.path.join(dirpath, each_file)):
os.remove(os.path.join(dirpath, each_file))
@amiraliakbari
amiraliakbari / rename_traces.py
Created April 16, 2015 13:45
Fix special characters in filenames
#!/usr/bin/env python
def rr(dir):
for d in os.listdir(dir):
dd = d.replace('\xef\x80\xa2', '-')
if d != dd:
os.rename(os.path.join(dir, d), os.path.join(dir, dd))
child = os.path.join(dir, dd)
if os.path.isdir(child):
@amiraliakbari
amiraliakbari / a.py
Created October 3, 2015 07:48
Refactoring Exercise
def get_page_from_queue():
pages = Page.objects.filter(username=q_key)
if pages:
page = pages[0]
setattr(page, 'priority', q_value)
page.save()
else:
pq = PageQueue(username=q_key, priority=q_value)
pq.save()
@amiraliakbari
amiraliakbari / xlsx.py
Created January 6, 2016 13:21
ensure_elementtree_imported
ET = None
ET_has_iterparse = False
def ensure_elementtree_imported(verbosity, logfile):
global ET, ET_has_iterparse
if ET is not None:
return
if "IronPython" in sys.version:
import xml.etree.ElementTree as ET
#### 2.7.2.1: fails later with
@amiraliakbari
amiraliakbari / get_plan_score
Created April 28, 2017 13:41
Large SQL Examples
CREATE OR REPLACE FUNCTION get_plan_score(faculty_id integer, from_date date, until_date date)
RETURNS TABLE(
fdate date ,
udate date ,
score int ) AS
$BODY$
DECLARE res integer;
DECLARE unit integer;
DECLARE StartDate DATE;
DECLARE EndofYear DATE;
@amiraliakbari
amiraliakbari / a.txt
Created April 28, 2017 13:43
Bash Useful Gists
I have marked with a * those which I think are absolutely essential
Items for each section are sorted by oldest to newest. Come back soon for more!
BASH
* In bash, 'ctrl-r' searches your command history as you type
- Input from the commandline as if it were a file by replacing
'command < file.in' with 'command <<< "some input text"'
- '^' is a sed-like operator to replace chars from last command
'ls docs; ^docs^web^' is equal to 'ls web'. The second argument can be empty.
* '!!:n' selects the nth argument of the last command, and '!$' the last arg