Skip to content

Instantly share code, notes, and snippets.

@mazlum
mazlum / pydantic_like_rest_framework.py
Last active May 6, 2021 14:55
pydantic_like_rest_framework.py
from typing import Optional, Any, Dict, TYPE_CHECKING
from pydantic import BaseModel, validator, validate_model, PrivateAttr
from pydantic.error_wrappers import ValidationError
object_setattr = object.__setattr__
class Foobar(BaseModel):
_errors = PrivateAttr()
@mazlum
mazlum / async-example.vue
Last active April 3, 2020 16:06
async example
<template>
<div id="app">
<button @click="load">Load Component right here.</button>
<async-component
v-if="loadComponent"
:component="Example"
example="Example Props"
@listen="listen"/>
</div>
</template>
@mazlum
mazlum / asyncomponent.vue
Last active April 3, 2020 16:01
Vue async component
<template>
<transition name="fade" mode="out-in">
<component
ref="asyncComponent"
:is="asyncComponent"
v-bind.sync="$attrs"
@reload="getComponent"
v-on="$listeners"
/>
</transition>
from django.db.models import Count, Q
q2 = Exam.objects.aggregate(
success=Count("pk", filter=Q(score__gte=85)),
)
print(q)
# {"success": 5}
from django.db.models import Case, IntegerField, Sum, Value, When
from django.db.models.functions import Coalesce
q = Exam.objects.aggregate(
success=Coalesce(
Sum(
Case(
When(score__gte=85, then=Value(1)),
default=Value(0),
output_field=IntegerField(),
@mazlum
mazlum / making_queries_models.py
Last active June 3, 2019 14:28
Django making queries
from django.db import models
class Exam(models.Model):
studen_name = models.CharField(max_lenght=50)
point = models.PositiveIntegerField(default=0)
@mazlum
mazlum / gcd.py
Created March 18, 2019 11:36
greatest common divisor
def gcd(x, y):
while y != 0:
(x, y) = (y, x % y)
return x
from sys import getsizeof
import json
import timeit
j = '''{
"first_name": "Mazlum",
"last_name": "Ağar"
}'''
# Memory
# views.py
def product_list(request):
f = ProductFilter(request.GET, queryset=Product.objects.all())
return render(request, 'my_app/template.html', {'filter': f})
# urls.py
from django.conf.urls import url
from django_filters.views import FilterView
from myapp.models import Product
class ContentTypesConfig(AppConfig):
name = 'django.contrib.contenttypes'
verbose_name = _("Content Types")
def ready(self):
pre_migrate.connect(inject_rename_contenttypes_operations, sender=self)
post_migrate.connect(create_contenttypes)
checks.register(check_generic_foreign_keys, checks.Tags.models)
checks.register(check_model_name_lengths, checks.Tags.models)