Skip to content

Instantly share code, notes, and snippets.

@safhac
safhac / snake.py
Created May 11, 2022 15:25
simple snake using pygame
import pygame
import time
import random
pygame.init()
white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80)
@safhac
safhac / SafeBaseWorker.py
Last active April 27, 2022 09:38
create a safe abstract worker
"""
class BaseWorker:
def __int__(self):
self.counter = 0
def work(self):
self.counter += 1
def get_counter(self):
@safhac
safhac / as_awaitable.py
Created February 3, 2022 10:11
awaitable descriptor
import asyncio
import time
import traceback
class Awaitable:
def __init__(self, block_fns):
self.fns: list = block_fns
def __await__(self, *args, **kwargs):
@safhac
safhac / shortest_path.py
Last active December 26, 2021 07:44
find shortest path in graph
import deque
graph2 = {
'Dov Aharon': ['Debbie Ben Zaken', 'Yaron Baberman'],
'Debbie Ben Zaken': [],
'Yaron Baberman': ['Dan Cohen'],
'Dan Cohen': ['David Dukovsky', 'Didi Eshkol'],
'David Dukovsky': [], 'Didi Eshkol': []
}
@safhac
safhac / chess_move.py
Created December 5, 2021 16:02
chess pieces
class ChessBoard:
size: int = 7
class ChessPiece:
def __init__(self):
self.dead = False
from contextlib import ExitStack
from dataclasses import dataclass
import json
@dataclass
class Node:
n_id: int
n_type: str
n_text: str
@safhac
safhac / tictactoe.py
Created September 14, 2021 12:31
my ttt solution
import numpy as np
def is_solved(board):
grid = np.array(board)
row0 = grid[0]
row1 = grid[1]
row2 = grid[2]
col0 = grid[:, 0]
col1 = grid[:, 1]
@safhac
safhac / django model manager overrides
Created September 5, 2021 09:02
django model manager overrides
class MyClassQuerySet(models.query.QuerySet):
def get(self, **kwargs):
print('hello world')
try:
return super().get(**kwargs)
except:
return None
class MyClassManager(models.Manager.from_queryset(MyClassQuerySet)):
# uncommited file to HEAD
git diff <path>
# uncommited file to before last commit
git diff HEAD^ -- <path>
#last commit to before last commit
git diff HEAD^ HEAD -- <path>
#difference between HEAD and n-th grandparent
@safhac
safhac / install-rabbitmq-centos-7.md
Created October 12, 2020 13:18 — forked from fernandoaleman/install-rabbitmq-centos-7.md
Install RabbitMQ on CentOS 7

Install RabbitMQ on CentOS 7

sudo yum -y install epel-release
sudo yum -y update

Install Erlang

Download repository