Skip to content

Instantly share code, notes, and snippets.

class SoftDeleteQuerySet(models.QuerySet):
@transaction.atomic
def delete(self, snapshot_id=None):
[x.delete(snapshot_id=snapshot_id) for x in self]
class SoftDeleteQueryManager(models.Manager):
def __init__(self, *args, **kwargs):
self.with_deleted = kwargs.pop('with_deleted', False)
super(SoftDeleteManager, self).__init__(*args, **kwargs)
class User(SoftDeleteModel):
name = models.CharField(null=False, blank=False)
class Cat(SoftDeleteModel):
owner = models.ForeignKey(
User,
null=True,
blank=True,
on_delete=models.SET_NULL)
class Snapshot(models.Model):
# unique identifier for snapshots
id = models.Charfield(max_length=32)
# the record which initiated the snapshot
root_id = models.CharField(max_length=32)
@transaction.atomic
def restore(self):
[x.restore() for x in self.snapshot_records.all()]
self.delete()
class Car(models.Model):
# CASCADE is the default behavior. When this car's manufacturer is deleted,
# this car will also be deleted
manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE)
# SET_NULL makes it so when this car's owner gets deleted, it will continue
# to exist without an owner.
owner = models.ForeignKey(User, on_delete=models.SET_NULL)
class SoftDeleteManager(models.Manager):
def __init__(self, *args, **kwargs):
self.with_deleted = kwargs.pop('deleted', False)
super(SoftDeleteManager, self).__init__(*args, **kwargs)
def _base_queryset(self):
return super().get_queryset().filter(deleted_at=None)
def get_queryset(self):
qs = self._base_queryset()
# horrible!
User.objects.filter(first_name='Ian', is_deleted=False).first()
class SoftDeleteModel(BaseModel):
class meta:
abstract = True
is_deleted = models.DateTimeField(null=False, default=False)
def delete(self):
self.is_deleted = True
self.save()
class BaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
# other fields / methods...
package main
import (
"errors"
"fmt"
"reflect"
)
func pipe(fns []interface{}, args ...interface{}) (interface{}, error) {
for i := range fns {
#include <stdio.h>
#include <pthread.h>
#define THREAD_COUNT 30
// Each thread will run an
// instance of this function
void *worker(void *arg) {
printf("I'm worker #%d\n", (int)arg);
return NULL;