Skip to content

Instantly share code, notes, and snippets.

View unpluggedcoder's full-sized avatar

UnpluggedCoder unpluggedcoder

View GitHub Profile
@unpluggedcoder
unpluggedcoder / config.py
Last active February 11, 2024 19:01
[Python Config Parser Compatible With Environment Var] Get config from config file or environment variables. The priority order of config is: Config file > Environment > default_value. #python #configparser #env
import os
import configparser
class Config:
""" Get config from config file or environment variables.
The priority order of config is: Config file > Environment > default_value.
The env var is composed by: [SECTION]_[OPTION]
For example:
get_or_else('smtp', 'BIND_ADDRESS', '127.0.0.1')
__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 / 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;
@unpluggedcoder
unpluggedcoder / models.py
Created December 22, 2017 03:05
[limit_choices_to example for Django GenericForeignKey] limit model choice to specified model #Django #Python #GenericForeignKey
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class Poll(models.Model):
question = models.CharField(max_length=500)
answers = # ...
# ...
limit = models.Q(app_label='your_app', model='page') | \
@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 / 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 / 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 / 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)