Skip to content

Instantly share code, notes, and snippets.

@miratcan
miratcan / fix_database_to_utf8.py
Last active August 31, 2023 00:06
Small python script that converts character sets to utf8 in all databases and tables. My solution for "Illegal mix of collations" errors. (http://stackoverflow.com/questions/3029321/how-to-solve-illegal-mix-of-collations-in-mysql)
from MySQLdb import connect
conn = connect(user="[USER]", passwd= "[PASSWORD]")
cur = conn.cursor()
cur.execute("show databases;")
dbs_to_update = filter(
lambda db: db not in ('information_schema', 'mysql', 'performance_schema'),
[dbname[0] for dbname in cur.fetchall()])
from __future__ import print_function
from os import mkdir
from os import walk
from os import popen
from os.path import join
from os.path import exists
from os.path import getsize
from os.path import basename
@miratcan
miratcan / db_copy.py
Created January 31, 2013 20:40
Database copier function built on lurker
from __future__ import print_function
class DbNotFound(Exception):
pass
def copy(src_conn, src_dbname, dst_conn, dst_dbname=None, drop_table=True,
drop_db=True, silent=False, tables_to_copy=None):
""" (<lurker connection>, str, <lurker connection>, dst_dbname=str, drop_table=bool
drop_db=bool, silent=bool, tables_to_copy=list) -> bool
@miratcan
miratcan / gist:5546928
Created May 9, 2013 11:23
Convert tuple groups (that has no constant length) into dict object.
def list_to_dict(l):
"""
This method converts tuple groups (that has no constant length) into
dict object.
>>> l = ((1, 'foo', 'bar'), (2, 'ta', 'ran', 'ti', 'no'))
>>> list_to_dict(l)
..: {'1': (foo, bar), 2: ('ta', 'ran', 'ti', 'no')}
@miratcan
miratcan / gist:5630638
Last active December 17, 2015 15:19
rotate your web page 90 degree.
var s = document.createElement('style');s.innerHTML = 'body {transform:rotate(90deg);-webkit-transform:rotate(90deg);}';document.getElementsByTagName('head')[0].appendChild(s);
from django.conf import settings
from django.http.request import validate_host
from django.middleware.csrf import _sanitize_token, constant_time_compare
from tastypie.authorization import ReadOnlyAuthorization
from tastypie.authentication import Authentication
from urlparse import urlparse
class InternalResourceAuthentication(Authentication):
def is_authenticated(self, request, **kwargs):
@miratcan
miratcan / gist:40ec6a75940ea358e2a6
Last active August 29, 2015 14:03
Dump users and related data as json.
from django.db.models import (get_models, ManyToManyField, ForeignKey, Count)
from django.contrib.contenttypes.generic import GenericRelation
from django.core import serializers
from django.contrib.auth.models import User
from sets import Set
def foreign_keys_to(model_class):
models = {}
for model in get_models():
@miratcan
miratcan / invalid_chars.py
Last active February 29, 2016 06:38
A function to find non Turkish characters in text.
alpha = 'ABC\xc3\x87DEFG\xc4\x9eHI\xc4\xb0JKLMNO\xc3\x96PRS\xc5\x9eTU' \
'\xc3\x9cVYZabc\xc3\xa7defg\xc4\x9fh\xc4\xb1ijklmno\xc3' \
'\xb6prs\xc5\x9ftu\xc3\xbcvyz'.decode('utf-8')
def invalid_chars(text, charset=alpha):
return set(filter(lambda c: c not in charset, list(text)))
invalid_chars(u'üğüğüp0*2')
@miratcan
miratcan / issubset.py
Created February 10, 2015 09:48
Find if a string present at list of strings.
def issubset(text, list_of_text):
"""
>>> msgs = ["abc", "def", "efg"]
>>> issubset("ab", msgs) # is subset of first item in msgs
True
>>> issubset("az", msgs)
False
>>> issubset("ef", msgs)
True
"""
@miratcan
miratcan / filika
Last active February 29, 2016 06:39
Friendfeed backup tool without any package dependencies. Just download and run like: ./filika bret
#!/usr/bin/env python
__author__ = "Mirat Can Bayrak"
__email__ = "[email protected]"
__copyright__ = "Copyright 2015, Planet Earth"
ENTRIES_PER_PAGE = 100
import re
import logging