Skip to content

Instantly share code, notes, and snippets.

View Przemek625's full-sized avatar

Przemysław Spychalski Przemek625

  • Gdansk, Poland
View GitHub Profile
@Przemek625
Przemek625 / gist:19aaecd14d454b22377c2a2dfc13c43b
Created May 18, 2021 00:22 — forked from grapo/gist:2319638
Customizable serialization

Introduction:

If we want to serialize objects we must consider two questions:

  1. What to serialize? Which fields of objects, how deep to serialize related objects.
  2. How output should looks like? Which format (xml, json, yaml), fields renaming, some format specific options (like attributes in xml - nothing similar in json), order of fields, some fields in other place in structure tree than others.

This questions lead as to two phases of serialization:

  1. First phase "dehydration": Change class instances (generally Python class, particular Django Model class) to dictionary contains data(python native types) of all interesting us fields. In this stage we are not interested in specific format.
@Przemek625
Przemek625 / gist:7fee537900c44e08ec0c954fe36e93a4
Created May 18, 2021 00:22 — forked from grapo/gist:2319638
Customizable serialization

Introduction:

If we want to serialize objects we must consider two questions:

  1. What to serialize? Which fields of objects, how deep to serialize related objects.
  2. How output should looks like? Which format (xml, json, yaml), fields renaming, some format specific options (like attributes in xml - nothing similar in json), order of fields, some fields in other place in structure tree than others.

This questions lead as to two phases of serialization:

  1. First phase "dehydration": Change class instances (generally Python class, particular Django Model class) to dictionary contains data(python native types) of all interesting us fields. In this stage we are not interested in specific format.
@Przemek625
Przemek625 / cachedecorator.py
Created April 4, 2021 13:37 — forked from mminer/cachedecorator.py
An example of a Python decorator to simplify caching a function's result.
"""An example of a cache decorator."""
import json
from functools import wraps
from redis import StrictRedis
redis = StrictRedis()
def cached(func):
@Przemek625
Przemek625 / django-shell-notebook-setup.md
Created April 3, 2021 16:56 — forked from madyasiwi/django-shell-notebook-setup.md
Guide to setup Django-shell Jupyter notebook running in Docker container

Setting Up Django-Shell Jupyter Notebook in Docker Container

This guide assumes that your django application already running in the container.

1. Install required python packages

You will have to make sure that django-extensions and jupyter packages are installed in the container's python virtual environment.

If you need to shell into the container as root user to do this, you can do so from the host PC:

<form [formGroup]="form" ngSubmit="postForm()">
<input type="file" formControlName="file" (change)="onFileChange($event)">
<button type="submit">Upload</button>
component:
import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http';
import { FormBuilder, Validators } from '@angular/forms';
import { Component } from '@angular/core';
@Component({
<form [formGroup]="form" ngSubmit="postForm()">
<input type="file" formControlName="file" (change)="onFileChange($event)">
<button type="submit">Upload</button>
component:
import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http';
import { FormBuilder, Validators } from '@angular/forms';
import { Component } from '@angular/core';
@Component({
@Przemek625
Przemek625 / dates.py
Created September 11, 2018 13:31 — forked from delfick/dates.py
Something to group django orm models by month
from itertools import groupby, izip
from django.db import connections
from monthdelta import MonthDelta
import operator
import datetime
def months(startdate):
"""Yield all months since start"""
date = datetime.date(startdate.year, startdate.month, 1)
while True:
@Przemek625
Przemek625 / gist:b207904b18f80ae13f3d521bbcf216f8
Created September 5, 2018 13:40 — forked from ChrisLTD/gist:957014
Django Nested Regroup Example (Group by category foreign key, then month of start date)
#views.py
def events_index(request, year):
selected_year = Year.objects.get(title=year)
events_list = Event.objects.filter(year = selected_year.id).order_by('category','start_date')
return render_to_response('events_list.html', {"events_list": events_list})
#events_list.html
{% regroup events_list by category.title as events_list_by_category %}
@Przemek625
Przemek625 / settings.py
Created September 5, 2018 10:12 — forked from alexmorozov/settings.py
Production raven config
INSTALLED_APPS += (
'raven.contrib.django.raven_compat',
)
MIDDLEWARE_CLASSES += (
'raven.contrib.django.raven_compat.middleware.Sentry404CatchMiddleware',
)
RAVEN_CONFIG = {
'dsn': 'http://x:[email protected]/2', # NOQA
@Przemek625
Przemek625 / python-closures.py
Created March 24, 2018 23:21 — forked from DmitrySoshnikov/python-closures.py
Understanding Python's closures
# "Understanding Python's closures".
#
# Tested in Python 3.1.2
#
# General points:
#
# 1. Closured lexical environments are stored
# in the property __closure__ of a function
#
# 2. If a function does not use free variables