Skip to content

Instantly share code, notes, and snippets.

@Perlence
Perlence / fixcsv.py
Last active December 15, 2015 13:08
Set column number of CSV file to column number of first row moving latter items
import sys
import itertools
import csv
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
def chainfix(lines):
@Perlence
Perlence / imgren.py
Last active December 22, 2015 04:29
Rename JPEG images with EXIF data according to date
import sys
import os
from datetime import datetime
from PIL import Image
# Keys of items that may contain date
items = [36868, 36867, 306]
# Output date format
@Perlence
Perlence / mrename.py
Created September 2, 2013 22:02
Open filenames in the text editor for further changes
import sys
import os
import glob
import subprocess
import tempfile
EDITOR = 'c:/Program Files/Sublime Text 2/sublime_text.exe'
files = sys.argv[1:]
@Perlence
Perlence / albums.py
Last active July 9, 2016 19:07
Generate playlist from random albums in your collection
from __future__ import print_function, unicode_literals
import codecs
import os
from path import path
import random
if os.name == 'nt':
import win_unicode_console
win_unicode_console.enable()
@Perlence
Perlence / jsonconfig.py
Created September 3, 2013 07:37
Acquire access to JSON configs via ConfigParser interface
import json
import ConfigParser
class JSONConfig(ConfigParser.RawConfigParser):
def write(self, fp):
output = self._dict()
if self._defaults:
for (key, value) in self._defaults.items():
output[key] = value
for section in self._sections:
@Perlence
Perlence / GuitarProMouse.ahk
Created November 29, 2013 20:07
Useful mouse button binds for Guitar Pro 5
MouseIsOver(WinTitle) {
MouseGetPos,,, Win
return WinExist(WinTitle . " ahk_id " . Win)
}
#If WinActive("Guitar Pro 5") and MouseIsOver("Guitar Pro 5")
RButton::
Send {LButton}
Send {Del}
Return
@Perlence
Perlence / csv2sqlite.py
Created January 24, 2014 12:24
Convert CSV file to SQLite table
#!/usr/bin/python
import re
import csv
import sqlite3
from contextlib import closing
TYPES = [
('INTEGER', re.compile('^[-+]?(\d+,)*\d+$')),
('REAL', re.compile('^[-+]?((\d+,)*\d+)?\.?[0-9]+([eE][-+]?[0-9]+)?$')),
@Perlence
Perlence / vpnbook.py
Created February 17, 2014 12:57
Update VPNBook password
#!/usr/bin/python
import os
import re
import urllib2
AUTH_FILE = '~/vpnbook-openvpn-auth'
resp = urllib2.urlopen('http://www.vpnbook.com/freevpn')
content = resp.read()
match = re.search(r'(?:<\w+>)?Password(?:</\w+>)?: (?:<\w+>)?([a-zA-Z0-9]{8})(?:</\w+>)?', content)
@Perlence
Perlence / fancyconfig.py
Last active August 29, 2015 14:00
Fancy config
"""Fancy config.
This module reads data from ``default.cfg`` and ``config.cfg`` using
:class:`RawConfigParser` and stores sections in the module's namespace.
Let's take the following ``config.cfg`` for example::
[section1]
key1 = 1
key2 = 2
@Perlence
Perlence / simple_argparse.py
Created April 25, 2014 10:17
Parse positional and keyword arguments from command line
"""Parse positional and keyword arguments from `sys.argv`.
We all like :class:`argparser.ArgumentParser`. It is a great tool to build
command-line interfaces with many nice features and automatic coercing.
But sometimes it's just too verbose. At times all we need is to pass arguments
to a function from CLI. And I will help you.
Consider this ``square.py`` program::