Skip to content

Instantly share code, notes, and snippets.

View shon's full-sized avatar

Shekhar shon

View GitHub Profile
@shon
shon / redis_autocomplete.py
Created November 26, 2010 02:49
Redis | Searching names
# Search usernames that begins with given phrase
#
# usernames: (username1, username2, ..)
# userscore:<username>: float
# user:obj: { id: int, username: string }
usernames_zset = "usernames"
def my_ord(c):
return "%03d" % ord(c)
@shon
shon / environment.py
Created June 19, 2014 12:27
Behaving | features/environment.py
import os
from behaving import environment as benv
from splinter import Browser
default_browser = Browser()
PERSONAS = {}
def _mkdir(path):
return os.makedirs(path) if not os.path.exists(path) else None
@shon
shon / keen.io_event_delete.py
Created February 2, 2015 14:37
It was good waste of time to figure out how to delete a keen.io event preferrably using Python. While I understand keen folks don't like deletes but wish there was better python API to do so.
import json
import requests
API_KEY = ''
PROJECT_ID = ''
COLLECTION_NAME = ''
filters = [{'property_name': 'keen.timestamp', 'operator': 'eq', 'property_value': '2015-02-02T10:20:00.000'}]
resp = requests.delete('https://api.keen.io/3.0/projects/' + PROJECT_ID + '/events/' + COLLECTION_NAME +'?api_key=' + API_KEY + '&filters=' + json.dumps(filters))
import os
import hashlib
import sys
from pathlib import Path # python < 3.4 : pip install pathlib
def md5(path):
content = path.open('br').read()
return hashlib.md5(content).hexdigest()
import json
import pickle
import msgpack
TEST_REDIS = False
if TEST_REDIS:
import redis
rconn = redis.StrictRedis()
dataset = [(('key:%d' % i), {'a': 1, 'b': list(range(100)), 'c': ('z' * 25)}) for i in range(1000000)]
@shon
shon / fa.py
Created April 3, 2019 19:02
FastAPI Python function -> API (sort of)
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
class CreateSignature(BaseModel):
id: int
@shon
shon / schematest.py
Last active April 9, 2019 11:50
Hug Bug
# - hug version: 2.4.7
# - marshmallow: 3.0.0rc4
# - Python: 3.6.7 on Ubuntu 18.10
import hug
from marshmallow import Schema, fields
class ItemSchema(Schema):
name = fields.Str()
id = fields.Int()
@shon
shon / monty_hall.py
Created August 30, 2019 17:27
Monty Hall Problem Verification
import random
from dataclasses import dataclass
NO_OF_DOORS = 3
@dataclass
class Door:
@shon
shon / pg_db_tenants.py
Last active September 20, 2019 13:48
[Postgresql multi tenant] Multiple Databases vs Single Database | Performance Comparison
import datetime
import os
import string
from random import randrange
from peewee import Model, DatabaseProxy, PostgresqlDatabase
from peewee import ForeignKeyField, BooleanField, TextField, IntegerField, CharField
NO_OF_DBS = 800
NO_OF_POSTS = 50
@shon
shon / pg_db_tenants_benchmark.py
Created November 18, 2019 09:07
Benchmarking Multi tenant | Shared Table vs Postgres Schema
import datetime
import os
import string
from random import randrange
from peewee import Model, PostgresqlDatabase
from peewee import BooleanField, TextField, CharField
from playhouse.postgres_ext import ArrayField, BinaryJSONField
NO_OF_DBS = 500