Skip to content

Instantly share code, notes, and snippets.

View cstrap's full-sized avatar
🐍

Christian Strappazzon cstrap

🐍
View GitHub Profile
@cstrap
cstrap / validate-form.py
Created April 15, 2015 15:35
Validate form django with a function that add the errors. Useful when one form data depends on other data field.
class MyForm(forms.Form):
# ...
def clean(self):
def set_field_error(field, message):
if field in self._errors:
self._errors[field].append(message)
else:
self._errors[field] = self.error_class([message])
if field in cleaned_data.keys():
del cleaned_data[field]
@cstrap
cstrap / django-model-log-history.py
Created March 25, 2015 13:34
Django model log history. A sample that uses LogEntry (django default) and reversion.
from django.contrib.admin.models import LogEntry, CHANGE, ContentType
from reversion.revisions import default_revision_manager as revision_manager
from app.models import MyModel
revision_manager.register(MyModel)
LogUser, created = User.objects.get_or_create(username='LogUser')
if created:
ApplicationUser.first_name = 'Log'
@cstrap
cstrap / mailtrap.py
Created March 17, 2015 15:34
Integrate mailtrap.io - plain python
import smtplib
from email.mime.text import MIMEText
msg = MIMEText('There was a terrible error that occured and I wanted you to know!')
msg['Subject'] = 'Hello world'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
username = 'mailtrap.io username'
password = 'mailtrap.io password'
@cstrap
cstrap / enable_spill_vertica
Created October 6, 2014 09:46
HP Vertica - Server with low memory - Enable Spill
When: ERROR: Join inner did not fit in memory ...
$ vsql
dbadmin=> select set_vertica_options('EE','ENABLE_JOIN_SPILL');
set_vertica_options
--------------------------------------------------------------
EE Vertica Options
--------------------
@cstrap
cstrap / add_subnet_vertica
Created October 6, 2014 09:43
Adding a HP Vertica node outside main cluster
-- First setup Vertica node.
-- When it's up'n'running launch from vsql:
CREATE SUBNET my_subnet with '192.168.0.0'; -- IP of your subnet may differs
-- The above statement creates a subnet between Vertica main clusters and the Vertica node.
-- NOTE: they must be in the same network
ALTER DATABASE VERTICA_DB EXPORT ON my_subnet;
@cstrap
cstrap / gist:d7cd401203ee680292ad
Created September 19, 2014 12:18
Get data from JSON with a regex
/*
* (value) could be (value|tag|name) or whatever is the key on json string
*/
public static String getValueFromJson(String json) {
String value = "";
Pattern p = Pattern.compile("\"(value)\":\"((\\\\\"|[^\"])*)\"");
Matcher m = p.matcher(json);
if (m.find() && m.groupCount() > 1) {
@cstrap
cstrap / RESTClient.java
Created September 19, 2014 12:16
Example of REST client with POST data
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RESTClient {
@cstrap
cstrap / JavaGzipFile
Last active August 29, 2015 14:04
Gzip file in java
public static void gzipFile(String source, String destination, boolean delete) {
try {
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(format("%s.gz", destination));
GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
byte[] buffer = new byte[1024];
int len;
@cstrap
cstrap / JavaMergeListFiles
Last active August 29, 2015 14:04
Merge a list of files into another one
/**
* Merge a list of files into another one
*
* @param files
* @param mergedFile
* @throws IOException
*/
public static void mergeFiles(List<File> files, File mergedFile) {
if (mergedFile.exists()) {
#!/usr/bin/env python
#
# Converts any integer into a base [BASE] number. I have chosen 62
# as it is meant to represent the integers using all the alphanumeric
# characters, [no special characters] = {0..9}, {A..Z}, {a..z}
#
# I plan on using this to shorten the representation of possibly long ids,
# a la url shortenters
#