Skip to content

Instantly share code, notes, and snippets.

View sankalpjonn's full-sized avatar

Sankalp Jonna sankalpjonn

View GitHub Profile
@sankalpjonn
sankalpjonn / target.lua
Created September 22, 2017 15:14
Golang redis example for targeting
local m = redis.call("ZREVRANGE", ARGV[1], "0", "0", "WITHSCORES");
if m ~= nil and m[2] ~= nil then
local max_priority = m[2]
local a = redis.call("ZRANGEBYSCORE", ARGV[1], max_priority, max_priority);
if a ~= nil and #a > 0 then
math.randomseed(ARGV[2])
local ad_id = math.random(#a)
return redis.call("HGET", "ad:" .. a[ad_id] .. ":attrs", "ad_response");
end
end
@sankalpjonn
sankalpjonn / sleep.py
Created October 29, 2018 11:24
Periodic task using time.sleep
import time
def foo():
print time.ctime()
while True:
foo()
time.sleep(1)
#Mon Oct 29 16:54:07 2018
@sankalpjonn
sankalpjonn / timer.py
Created October 29, 2018 11:28
Periodic task using threading.Timer
import time, threading
WAIT_SECONDS = 1
def foo():
print(time.ctime())
threading.Timer(WAIT_SECONDS, foo).start()
foo()
import threading, time
def foo():
print time.ctime()
WAIT_TIME_SECONDS = 2
ticker = threading.Event()
while not ticker.wait(WAIT_TIME_SECONDS):
foo()
@sankalpjonn
sankalpjonn / timeloop_mvp.py
Last active October 5, 2021 11:19
Predecessor to timeloop
import threading, time, signal
from datetime import timedelta
WAIT_TIME_SECONDS = 1
class ProgramKilled(Exception):
pass
def foo():
import time
from timeloop import Timeloop
from datetime import timedelta
tl = Timeloop()
@tl.job(interval=timedelta(seconds=2))
def sample_job_every_2s():
print "2s job current time : {}".format(time.ctime())
from rest_framework.permissions import BasePermission
from rest_framework.view import APIView
class IsStaff(BasePermission):
def has_permission(self, request, view):
return request.user and request.user.is_staff
class TestAPIView(APIView):
permission_classes = [IsStaff]
# user for the SDK to authenticate
sdk_user_email = "sdk@{}.sf".format(business.id)
sdk_user = User.objects.create_user(sdk_user_email, sdk_user_email, str(uuid.uuid4()))
business.api_key = Token.objects.create(user=sdk_user).key
business.save()
BusinessTeamMember.objects.create(
business= business,
user= sdk_user,
## validated_data contains the request data to the invite api
user, _ = helpers.get_or_create_user(email=validated_data['email'])
user.is_active = False
user.save()
## business is the object of the business model that is retrieved using the auth token in the header
business_team_member, _ = BusinessTeamMember.objects.get_or_create(
user=user,
client_user_email = validated_data['email']
try:
client_user = User.objects.get(username=client_user_email)
except User.DoesNotExist:
raise Exception("this email was not invited")
client_user.is_active=True
client_user.set_password(validated_data['password1'])
client_user.save()