Skip to content

Instantly share code, notes, and snippets.

View unpluggedcoder's full-sized avatar

UnpluggedCoder unpluggedcoder

View GitHub Profile
@unpluggedcoder
unpluggedcoder / models.py
Last active September 14, 2017 10:42
Use corresponding serializer class for different request method in Django Rest Framework - Part 1
# models.py
import uuid
from django.db import models
class Organization(models.Model):
org_uuid = models.UUIDField(
primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=40)
@unpluggedcoder
unpluggedcoder / serializers.py
Last active September 14, 2017 10:42
Use corresponding serializer class for different request method in Django Rest Framework - Part 2
# serializers.py
from rest_framework import serializers
from myapp import models
class OrganizationListViewSerializer(serializers.ModelSerializer):
class Meta:
model = models.Organization
fields = '__all__'
@unpluggedcoder
unpluggedcoder / kill_pg_seesion.sql
Created October 11, 2017 02:33
[Kill postgresql session/connection] #sql #postgresql
-- You can use pg_terminate_backend() to kill a connection. You have to be superuser to use this function. This works on all operating systems the same.
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
-- don't kill my own connection!
pid <> pg_backend_pid()
-- don't kill the connections to other databases
@unpluggedcoder
unpluggedcoder / generate_convert_query.sql
Created February 11, 2018 03:09
[MySQL CONVERT CHARACTER SET to utf8 for all tables] Generate query for convert character set to utf8 for all tables #mysql #characterset
SELECT CONCAT("ALTER TABLE `", TABLE_NAME,"` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;") AS ExecuteTheString
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="[YOUR DATABASE NAME]"
AND TABLE_TYPE="BASE TABLE";
SELECT @@character_set_database, @@collation_database;
__version__ = "0.1.6.8"
if __name__ == "__main__":
import sys
import argparse
def increment_line(args):
vi = [int(i) for i in __version__.split('.')]
print('current version: %s' % __version__)
@unpluggedcoder
unpluggedcoder / asynclog_using_celery.py
Last active May 2, 2018 17:47
[Python Asynclog Examples] Examples of using asynclog module #python #asynchronous #logging #threading #celery
from celery import shared_task
@shared_task
def write_task(msg):
# Write log in Network IO
print(msg)
celery_handler = AsyncLogDispatcher(write_task, use_thread=False, use_celery=True)
celery_handler.setLevel(logging.INFO)
logger.addHandler(celery_handler)
@unpluggedcoder
unpluggedcoder / cool_argparse_stuff.py
Created November 2, 2018 13:17 — forked from brantfaircloth/cool_argparse_stuff.py
Some cool argparse stuff
class FullPaths(argparse.Action):
"""Expand user- and relative-paths"""
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, os.path.abspath(os.path.expanduser(values)))
def is_dir(dirname):
"""Checks if a path is an actual directory"""
if not os.path.isdir(dirname):
msg = "{0} is not a directory".format(dirname)
raise argparse.ArgumentTypeError(msg)
@unpluggedcoder
unpluggedcoder / views.py
Last active January 16, 2020 10:57
Use corresponding serializer class for different request method in Django Rest Framework - Part 3
# views.py
from rest_framework import exceptions
from rest_framework import generics
from myapp import models
from myapp import serializers as ser
class MethodSerializerView(object):
'''
@unpluggedcoder
unpluggedcoder / r0010_regex_matching.rs
Created February 6, 2020 15:20
Leetcode snippets
#![feature(slice_patterns)]
impl Solution {
pub fn is_match(s: String, p: String) -> bool {
is_match(s.as_bytes(), p.as_bytes())
}
}
fn is_match(s: &[u8], p: &[u8]) -> bool {
match (p, s) {
@unpluggedcoder
unpluggedcoder / dev_setup.sh
Last active March 29, 2020 16:57
Ubuntu Vim development environment setup
#!/usr/bin/env bash
# Ubuntu 18.04
# Rust + Postgresql 10 + redis development environment
# Zsh
sudo apt install zsh
# oh-my-zsh
# using curl
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# or using wget