Skip to content

Instantly share code, notes, and snippets.

@miratcan
miratcan / get_first_or_create.py
Last active August 29, 2015 14:20
When you need to create a record of a model and delete duplicates same time you can use this method.
def create_and_delete_others(cls, **kwargs):
"""
1. Get first record of model cls from db with given kwargs.
2. If found, update that record with update_params.
2. If not found, create instance with create_params.
3. If more than one found, delete others.
"""
create_params = kwargs.pop('create_params', {})
update_params = kwargs.pop('update_params', {})
save_params = kwargs.pop('save_params', {})
@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__ = "miratcanbayrak@gmail.com"
__copyright__ = "Copyright 2015, Planet Earth"
ENTRIES_PER_PAGE = 100
import re
import logging
@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 / 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 / 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():
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: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);
@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 / 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
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