Skip to content

Instantly share code, notes, and snippets.

@Guilouf
Guilouf / migration.py
Created June 5, 2024 12:40
Django database generated slug
"""Register a custom SLQ function to convert a string to a slug
https://gist.github.com/abn/779166b0c766ce67351c588489831852
This example is using Postgresql
"""
from django.db import migrations
SLUGIFY_FUNCTION = """
CREATE OR REPLACE FUNCTION public.slugify(
v TEXT
) RETURNS TEXT
@Guilouf
Guilouf / python_list_index.py
Created January 31, 2019 17:45
Access list dict pass via a tuple of indexes
def search_index(obj, itero):
try:
return search_index(obj[next(itero)], itero)
except StopIteration:
return obj
a = [{"name":"someone", "age":23}]
s = (0, 'name')
result = search_index(a, iter(s))
@Guilouf
Guilouf / models.py
Last active December 12, 2018 14:14
Django ordered many to many field
class Film(models.Model):
name = models.CharField(max_length=30)
def __str__(self):
return self.name
class Jury(models.Model):
name = models.CharField(max_length=30)
many_films = models.ManyToManyField(Film, through='Classement')
@Guilouf
Guilouf / instance_inheritance.py
Last active October 25, 2018 08:33
Instance inheritance trial
class Agro:
def __init__(self, parent, prop1=None, prop2=None):
self.parent = parent
self._prop1 = prop1
self._prop2 = prop2
@property
def prop1(self):
try:
if self._prop1 is None:
@Guilouf
Guilouf / fourberie.py
Last active May 28, 2018 17:46
Fourberie python
from multiprocessing import Pool
class Modudule:
def __init__(self, param):
self.param = param
print("modudu: ", self.param.bouboul, self.param.nom,
self.param.metoclass())
# via multiproc sur windows: false
@Guilouf
Guilouf / asynchrone.py
Created May 23, 2018 17:11
A l'arrache
import asyncio
async def lent(ind, string):
a = 1
# for i in range(ind): #en fait vu que c'est single thread, le calcul se fait à la queulelle
# a **= 2
await asyncio.sleep(ind)
print("funlent", string)
@Guilouf
Guilouf / class_attr.py
Created May 16, 2018 18:18
"Inherit" class attributes to instance attributes
class Params:
def __init__(self):
self.attr1 = self.attr1
@classmethod
def ret_attr1(cls):
return cls.attr1
class MetaRace(type):
def __new__(mcs, name, bases, dct):
dct['genre'] = 'M' # ecrasé après
dct['espece'] = 'YAGARA' # pas ecrasé
return super().__new__(mcs, name, bases, dct)
class Parent:
nom = "daron"
import sys
# Classes créée dynamiquement
AutreNomYolo = type('Yolo', (), {'__str__': lambda: 'salut'})
AutreNomYolo2 = type('Yolo', (), {'__str__': lambda: 'degage'})
type('Yolo', (), {'__str__': lambda: 'salut'}) # garbage collector direct ? comment appeler la classe par son __name__
# car je pourrais stocker l'objet dans une liste etc..
list_obj = [None, None]
list_obj[1] = type('Yolo', (), {'__str__': lambda: 'salut'})
print(AutreNomYolo.__str__())
ll = ['ya', 'bor']
point_visrgul = ';'.join
print(point_visrgul(ll))
class bidule:
pass
inst = bidule()