Skip to content

Instantly share code, notes, and snippets.

View aasmpro's full-sized avatar
NewLife

Ross Amiri aasmpro

NewLife
View GitHub Profile
@aasmpro
aasmpro / is_palindrome.py
Created February 14, 2022 23:52
Python - Validate Palindrome Mathematically (numbers only of course)
def is_palindrome(number):
test_number = 0
length = math.ceil(math.log10(number + 1))
for i in range(length // 2):
test_number = (test_number * 10) + (number % 10)
number //= 10
return number == test_number or number // 10 == test_number
@aasmpro
aasmpro / rotate_marix.py
Created February 14, 2022 23:38
Python - rotate a matrix
def rotate_matrix(matrix, times):
times = times % 4
if times == 0:
# a full rotation or none
return matrix
elif times == 1:
# for 90 forward or 270 backward
return [i[::-1] for i in zip(*matrix)]
elif times == 2:
# for 180 forward or backward
@aasmpro
aasmpro / GruvBoxColors.json
Last active July 30, 2021 08:58
GruvBox colors for frontend projects
{
"black": "#000000",
"white": "#ffffff",
"dark": {
"0": "#1d2021",
"1": "#282828",
"2": "#32302f",
"3": "#3c3836",
"4": "#504945",
"5": "#665c54"
@aasmpro
aasmpro / JetBrainsMono.css
Last active October 11, 2022 17:10
JetBrains Mono css / scss / font-face / stylesheets
/* 0. extract fonts into `JetBrainsMono` folder */
/* 1. check fonts `urls` with your directory structure */
/* normal fonts */
@font-face {
font-family: JetBrainsMono;
font-style: normal;
font-weight: 100;
src: url("../fonts/JetBrainsMono/ttf/JetBrainsMono-Thin.ttf") format("truetype");
@aasmpro
aasmpro / duplicated.py
Created April 7, 2021 11:14
find duplicated objects in django
from django.db.models import Count
def get_duplicated_objects(model, field, count='id'):
dups = model.objects.values(field).annotate(Count(count)).order_by().filter(**{"{}__count__gt".format(count): 1}).values_list(field, flat=True)
return model.objects.filter(**{"{}__in".format(field): dups})
@aasmpro
aasmpro / django_annotate_queryset.py
Created June 1, 2020 07:01
django conditional annotate
class OrderQuerySet(models.QuerySet):
def annotate_sold_coupon(self):
sold_coupons = SoldCoupon.objects.filter(sell_code=OuterRef('sold_coupon__sell_code'))
return self.annotate(
sold_coupon__sell_code=Case(
When(site_id=2, then=Concat(Value('SNAPP-NEW-'), 'id')),
When(site_id=3, then=Concat(Value('TAP30-NEW-'), 'id')),
When(site_id=4, then=Concat(Value('T2BON-NEW-'), 'id'))
),
sold_coupon__exist=Exists(sold_coupons.values('is_used')),
@aasmpro
aasmpro / pattern.py
Created April 16, 2020 17:08
craete pattern with python
from PIL import Image
import sys
image = Image.open(sys.argv[1])
image = image.resize((10, 10), Image.ANTIALIAS)
image2 = Image.new('RGB', (1000, 1000))
for y in range(0, 1000, 10):
for x in range(0, 1000, 10):
image2.paste(image, (x,y))
image2.save(sys.argv[2], 'PNG', quality=100)
from time import sleep
def progress(percent=0, width=30):
left = width * percent // 100
right = width - left
print('\r[', '#' * left, ' ' * right, ']',
f' {percent:.0f}%',
sep='', end='', flush=True)
for i in range(101):
@aasmpro
aasmpro / prettify.py
Created February 13, 2019 13:16
python simple prettify
def prettify(data, indent=' ', indent_num=0):
result = ''
if isinstance(data, list) or isinstance(data, tuple):
for i in data:
if isinstance(i, list) or isinstance(i, tuple) or isinstance(i, dict):
result += "{}".format(prettify(i, indent, indent_num))
else:
result += indent * indent_num + "{}\n".format(str(i))
elif isinstance(data, dict):
for k, v in data.items():
@aasmpro
aasmpro / bankers_algorithm.py
Last active April 10, 2023 03:58
Python implementation of Banker's algorithm, written in Python 3
def main():
processes = int(input("number of processes : "))
resources = int(input("number of resources : "))
max_resources = [int(i) for i in input("maximum resources : ").split()]
print("\n-- allocated resources for each process --")
currently_allocated = [[int(i) for i in input(f"process {j + 1} : ").split()] for j in range(processes)]
print("\n-- maximum resources for each process --")
max_need = [[int(i) for i in input(f"process {j + 1} : ").split()] for j in range(processes)]