Skip to content

Instantly share code, notes, and snippets.

View goutomroy's full-sized avatar

Goutom Roy goutomroy

View GitHub Profile
@goutomroy
goutomroy / chat.py
Last active August 29, 2015 14:18 — forked from gregvish/chat.py
from socket import socket, SO_REUSEADDR, SOL_SOCKET
from asyncio import Task, coroutine, get_event_loop
class Peer(object):
def __init__(self, server, sock, name):
self.loop = server.loop
self.name = name
self._sock = sock
self._server = server
Task(self._peer_handler())
@goutomroy
goutomroy / DpToPxAndPxToDp
Created May 19, 2016 23:40 — forked from laaptu/DpToPxAndPxToDp
Android convert dp to px and vice versa
public static float convertPixelsToDp(float px){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return Math.round(dp);
}
public static float convertDpToPixel(float dp){
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return Math.round(px);
@goutomroy
goutomroy / EndlessRecyclerOnScrollListener.java
Created March 24, 2017 22:57 — forked from ssinss/EndlessRecyclerOnScrollListener.java
Endless RecyclerView OnScrollListener
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName();
private int previousTotal = 0; // The total number of items in the dataset after the last load
private boolean loading = true; // True if we are still waiting for the last set of data to load.
private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;
@goutomroy
goutomroy / django_session_middleware.py
Last active May 21, 2019 16:56
django.contrib.sessions.middleware
import time
from importlib import import_module
from django.conf import settings
from django.contrib.sessions.backends.base import UpdateError
from django.core.exceptions import SuspiciousOperation
from django.utils.cache import patch_vary_headers
from django.utils.deprecation import MiddlewareMixin
from django.utils.http import http_date
@goutomroy
goutomroy / measure_time.py
Last active October 9, 2023 22:46
A Python decorator which will calculate function execution time in seconds and prints it in console.
import functools
import time
def timer(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = time.perf_counter()
value = func(*args, **kwargs)
@goutomroy
goutomroy / creat_circle.py
Created May 26, 2019 12:35
Uses example of commonly used python decorators.
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
"""Get value of radius"""
return self._radius
@radius.setter
@goutomroy
goutomroy / singleton.py
Last active October 11, 2020 23:13
A singleton decorator for python classes.
class Singleton:
def __init__(self, cls):
self._cls = cls
def Instance(self):
try:
return self._instance
except AttributeError:
self._instance = self._cls()
@goutomroy
goutomroy / medium_queryset_evaluation_caching_model.py
Last active June 7, 2019 18:22
Model example to understand queryset operations
from django.db import models
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def __str__(self):
return self.name
class Entry(models.Model):
@query_debugger
def book_list():
queryset = Book.objects.all()
books = []
for book in queryset:
books.append({'id': book.id, 'name': book.name, 'publisher': book.publisher.name})
return books