Skip to content

Instantly share code, notes, and snippets.

View RaffaeleSgarro's full-sized avatar

Raffaele Sgarro RaffaeleSgarro

View GitHub Profile
public class ArtifactsWhenUsingDialogFont {
public static void main(String... args) throws Exception {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(PrintServiceLookup.lookupDefaultPrintService());
job.setPrintable(new Printable(){
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
graphics.translate( (int) pageFormat.getImageableX(), (int) pageFormat.getImageableY() );
@RaffaeleSgarro
RaffaeleSgarro / php_to_js.php
Last active December 26, 2015 20:19
Pass PHP variables to the client Javascript. Handle untrusted input both when feeding the Javascript and when updating the DOM
<html>
<head>
<script src="jquery-2.0.3.min.js"></script>
<script src="underscore-min.js"></script>
<script src="mustache.js"></script>
<script>
$(function(){
$('.btn').click(function(e) {
var user = $.parseJSON( _.unescape( $(this).next().text() ) );
@RaffaeleSgarro
RaffaeleSgarro / Hexdump.java
Created April 7, 2013 11:03
Print a hexdump of a string
package stackoverflow;
public class Hexdump {
public static void main(String[] args) {
// This is mocked. Get the actual filename
String filename = "Qwertypsadfksadf";
// Log the hexdump
System.out.println(hexdump(filename));
}
@RaffaeleSgarro
RaffaeleSgarro / GetImageColorComponents.java
Last active December 15, 2015 21:28
Save color components of an image separately in a MySQL database
package stackoverflow;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
@RaffaeleSgarro
RaffaeleSgarro / YahooURI.java
Last active December 15, 2015 14:19
Encode/decode parameters in a format that is suitable to be sent as the encoded query string to Yahoo services with a HTTP API
package stackoverflow;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import commands, re
import pprint
class Engine:
def __init__(self):
self.data = dict()
def updateSourceEvent(self):
self.updateData()
@RaffaeleSgarro
RaffaeleSgarro / gist:4747371
Created February 9, 2013 22:23
Telnet D-Link DSL-2640B available commands
?
help
logout
reboot
adsl
atm
brctl
cat
ethctl
ddns
trait MyOption[A] { outer =>
import MyOption._
def fold[B](map: A => B, getOrElse: => B): B
}
object MyOption {
def none[A]: MyOption[A] =
@RaffaeleSgarro
RaffaeleSgarro / gist:4652027
Last active August 6, 2016 14:10
Test random generation when reseeding each iteration
# Requires python-imaging on Ubuntu
import PIL.Image, random, time
class AlexRand(random.Random):
'''This replicates how PHP does randint()'''
def randint(self, a, b):
self.seed()
return super(AlexRand, self).randint(a, b)
image = PIL.Image.new("1",(512,512)) # Creates a 512x512 bitmap
@RaffaeleSgarro
RaffaeleSgarro / gist:4635795
Created January 25, 2013 16:30
Distribute tasks among assignees
# Python 2.x
import random
import itertools
tasks = ['account', 'gestione classi', 'registro insegnanti',
'registro di classe', 'corsi di studio', 'gestione materie', 'bacheca',
'giornalino', 'pon', 'gestione circolari', 'gestione locali',
'inventario', 'viaggi', 'tasse']
assignees = ["Marco", "Gabriele", "Raffaele"]