Skip to content

Instantly share code, notes, and snippets.

View ulgens's full-sized avatar
⚙️
In Progress

Ülgen Sarıkavak ulgens

⚙️
In Progress
View GitHub Profile
@ulgens
ulgens / Gezgin Piyon
Last active October 16, 2016 11:35
Interview solution for sahibinden.com, from 2014
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Ülgen Sarıkavak
# ulgensrkvk@gmail.com
# İstanbul - 2014
# Class definitions
class Point(object):
@ulgens
ulgens / Xmodmap
Last active August 29, 2015 14:00
Convert Mac modifier keys layout to PC keyboard format
!mod1 = Alt_L
!mod4 = Super_L Super_R
!mod5 = ISO_Level3_Shift (Alt_gr)
! Control_L Alt_L Super_L Super_R ISO_Level3_Shift // Old keys
! 37 64 133 134 108 // Keycodes
! Control_L Super_L Alt_L ISO_Level3_Shift Control_R I // New keys
keycode 37 = Control_L
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ulgens
ulgens / brown-taggedwords.py
Last active October 16, 2016 11:34
Export Brown Corpus tagged words by categories using NLTK. Based on: https://gist.github.com/JonathanReeve/ac543e9541d1647c1c3b
from nltk.corpus import brown
for category in brown.categories():
words = brown.tagged_words(categories=category)
text = '\n '.join('%s, %s' % word for word in words)
filename = category + '.txt'
with open(filename, 'w') as outfile:
outfile.write(text)
@ulgens
ulgens / gist:3f4a5e33d6bbf414a2e6ac18c6f4ee2f
Created January 21, 2017 20:15 — forked from datagrok/gist:2199506
Virtualenv's `bin/activate` is Doing It Wrong

Virtualenv's bin/activate is Doing It Wrong

I'm a Python programmer and frequently work with the excellent [virtualenv][] tool by Ian Bicking.

Virtualenv is a great tool on the whole but there is one glaring problem: the activate script that virtualenv provides as a convenience to enable its functionality requires you to source it with your shell to invoke it. The activate script sets some environment variables in your current environment and defines for you a deactivate shell function which will (attempt to) help you to undo those changes later.

This pattern is abhorrently wrong and un-unix-y. activate should instead do what ssh-agent does, and launch a sub-shell or sub-command with a modified environment.

Problems

@ulgens
ulgens / middlewares.py
Created June 12, 2018 15:41
Profile non HTML responses with debug_toolbar
class NonHtmlDebugToolbarMiddleware(MiddlewareMixin):
"""
Converts non-HTML responses to HTML,
for being able to profile them with django debug toolbar.
Usage: /<endpoint>/?format=json&profile=1
"""
def process_response(self, request, response):
profile = request.GET.get('profile')
@ulgens
ulgens / humble_bundle_download.py
Last active February 2, 2019 01:11
Humble Bundle Book Download
# pip install wget
# Save purchase/download page to index.html first
from lxml import html
import wget
book_list_xpath = """//*[@id="papers-content"]/div[11]/div[4]/div/div/div/div"""
book_name_xpath = """//*[@id="papers-content"]/div[11]/div[4]/div/div/div/div[{index}]/div/div[2]/div[1]/a/text()"""
download_links_path = """//*[@id="papers-content"]/div[11]/div[4]/div/div/div/div[{index}]/div/div[3]/div/div/div/div[1]/a/@href"""
@ulgens
ulgens / debug_toolbar_middleware.py
Created February 29, 2020 08:39
GQL Debug Toolbar Fix
import json
from debug_toolbar.middleware import DebugToolbarMiddleware as BaseMiddleware
from debug_toolbar.middleware import get_show_toolbar
from debug_toolbar.toolbar import DebugToolbar
from django.template.loader import render_to_string
from graphiql_debug_toolbar.middleware import get_payload, set_content_length
from graphiql_debug_toolbar.serializers import CallableJSONEncoder
__all__ = ['DebugToolbarMiddleware']
@ulgens
ulgens / jwt_auth_stack.py
Created March 5, 2020 10:00
JWT (headers) auth stack for django-channels
import jwt
from channels.auth import AuthMiddlewareStack
from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.db import close_old_connections
from rest_framework.authtoken.models import Token
from members.models import MemberProfile
@ulgens
ulgens / instance_count.py
Created September 11, 2020 06:42
Get counts from all models in Django
import django.apps
models = django.apps.apps.get_models()
for model in models:
print(model, model.objects.count())