Skip to content

Instantly share code, notes, and snippets.

View bebosudo's full-sized avatar
💾

Alberto Chiusole bebosudo

💾
View GitHub Profile
@bebosudo
bebosudo / allocation_examples.c
Last active October 9, 2017 21:46
Allocations example and differences between static and dynamic allocation (in stack and heap). Italian.
/*
Da compilare su linux con:
gcc -o allocation_examples allocation_examples.c
oppure se si vuole usare il vecchio standard del C dell'89:
gcc --std=c89 -o allocation_examples allocation_examples.c
Si puo' poi eseguire facendo:
./allocation_examples
*/
@bebosudo
bebosudo / virtualenvwrapper_setup.md
Last active November 20, 2018 11:25
Setup virtualenvwrapper on Ubuntu 16.04 or Fedora 25+

Setup virtualenvwrapper on Ubuntu 16.04 or Fedora 25+

Install virtualenvwrapper, which eases the handling of Python virtualenvs (sort of Virtual Machines for Python packages).

$ pip3 install --user virtualenvwrapper

Add to your ~/.bashrc, or better, to your ~/.bash_profile:

@bebosudo
bebosudo / gist:7b1c0650e2f94d26afe830530764de65
Created April 15, 2018 14:57
Compile python 3.6 locally, with openssl and sqlite3 support
Create a dir to build things into:
$ mkdir ~/stack && cd ~/stack
Download the sources (sqlite 3230100 was broken, 3220000 was used instead):
$ wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz
$ wget https://sqlite.org/2018/sqlite-src-3220000.zip
$ wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
@bebosudo
bebosudo / django_widget_html5_date.py
Created August 20, 2018 19:22
A HTML5 date django widget to apply to date form fields.
from django import forms
# https://github.com/django/django/blob/master/django/forms/widgets.py
class HTML5DateWidget(forms.widgets.Input):
input_type = 'date'
template_name = 'django/forms/widgets/date.html'
# Example:
@bebosudo
bebosudo / django_csv_row_iterator.py
Last active September 1, 2018 17:33
Example of how to iterate over an uploaded file in django (2.1) using generic base classes.
# --- forms.py ---
class CSVImportForm(forms.Form):
file = forms.FileField()
comment = forms.CharField(max_length=50, required=False)
# --- utils.py ---
@bebosudo
bebosudo / gist:0c91f943013fa3c2dab86290d62de7b5
Created September 15, 2018 18:27
Django doesn't provide id in forms or serializers.
Manually provide the id with:
# forms.py
class SomethingForm(...):
id = forms.IntegerField(min_value=0)
# serializers.py
class SetIDFieldFromModel:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@bebosudo
bebosudo / django_custom_widget.md
Last active January 25, 2024 02:48
Custom django form widget with no pain.

First of all, edit your_app_settings/settings.py and add:

INSTALLED_APPS = [
    'django.forms',  # to let django discover the built-in widgets
    ...
]

FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
@bebosudo
bebosudo / django_signals_setup.md
Created September 20, 2018 22:40
Setup signals in django

Inside your application directory, create a file your_app/signals.py.

The default project and app creator should have created a file your_app/apps.py. Inside the class used for your app, add the ready method:

class YourAppConfig(AppConfig):
    name = 'your_app'

 def ready(self):

If we have a model with some fields that have default values (e.g. a cost = DecimalField(default=0, null=false)), and we receive a form with that field set as None, django will complain with a NOT NULL constraint failed. We can manually set the fields of interest in the save() method of the models, or simply add this class to all the django Models, which will automatically discover and set the null fields having a default value.

from django.db.models.fields import NOT_PROVIDED

class SetModelDefaultsOnSave:
    def save(self):
        for field in self._meta.get_fields():
 if (not field.auto_created and getattr(self, field.name) is None and
24046 /usr/bin/ld: /opt/cluster/spack/opt/spack/linux-centos7-x86_64/gcc-8.2.0/boost-1.68.0-xkyf5icz7yn62yjvduwrs3mfru4merj4/lib/libboost_serialization-mt.a(archive_exception.o): re
location R_X86_64_32S against symbol `_ZTVN5boost7archive17archive_exceptionE' can not be used when making a shared object; recompile with -fPIC
24047 /usr/bin/ld: /opt/cluster/spack/opt/spack/linux-centos7-x86_64/gcc-8.2.0/boost-1.68.0-xkyf5icz7yn62yjvduwrs3mfru4merj4/lib/libboost_serialization-mt.a(basic_archive.o): reloca
tion R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
24048 /usr/bin/ld: /opt/cluster/spack/opt/spack/linux-centos7-x86_64/gcc-8.2.0/boost-1.68.0-xkyf5icz7yn62yjvduwrs3mfru4merj4/lib/libboost_filesystem-mt.a(operations.o): relocation R
_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
24049 /usr/bin/ld: /opt/cluster/spack/opt/spack/l