Skip to content

Instantly share code, notes, and snippets.

@ptomato
ptomato / Makefile.am
Created October 27, 2012 12:43
Test case for strange libpeas behavior
plugindir = $(libdir)/plugins
dist_plugin_DATA = confplugin.plugin
plugin_LTLIBRARIES = libconfplugin.la
libconfplugin_la_SOURCES = confplugin.c
libconfplugin_la_LIBADD = $(PLUGIN_LIBS)
libconfplugin_la_LDFLAGS = -module -avoid-version -shared
AM_CFLAGS = $(PLUGIN_CFLAGS)
dist_noinst_SCRIPTS = testconfplugin.py
@ptomato
ptomato / ashcroft.py
Created March 28, 2013 11:34
# Ashcroft-Sturm model # (Chapter 7 of my PhD thesis, "Two-dimensional optics")
import numpy as N
from scipy import constants as Const
from crystal_plane import CrystalPlane
"""
Parallel-band conductivity (Ashcroft & Sturm, 1971) using measured data and
fits (Mathewson & Myers, 1972)
"""
# sigma_a: Ashcroft & Sturm's "convenient" unit of conductivity
sigma_a = Const.e ** 2 / (24 * N.pi * Const.value('Bohr radius') * Const.hbar)
@ptomato
ptomato / gist:6516878
Created September 10, 2013 22:51
Exceptions are treated differently depending on where they occur in GJS
const Gtk = imports.gi.Gtk;
Gtk.init(null);
let w = new Gtk.Window();
let b = new Gtk.Button({ label: "Click me for an exception" });
w.add(b);
b.connect('clicked', function() {
throw("This exception is not caught, only printed");
});
const Gtk = imports.gi.Gtk;
Gtk.init(null);
let w = new Gtk.Window({
default_width: 800,
default_height: 600
});
let g = new Gtk.Grid();
let frame = new Gtk.Frame({ expand: true });
let label = new Gtk.Label({
import math
from gi.repository import Gdk, Gtk, Pango
WIDTH = 150
HEIGHT = 207
CSS = '''
.frame { border: 1px solid red; }
'''
class FixedSize(Gtk.Frame):
@ptomato
ptomato / gist:0e70598951f1ce809d5e9625a0b0dd8e
Last active May 16, 2016 00:08
Localize using gettext

How to localize using gettext

This document does not try to explain how to enable gettext support in an application. There are other documents that describe that process better. (Need links!)

The PO format and the PO files

Even as a developer, knowing some elementary stuff about the PO format, the format of the actual translations, is very useful.

The PO format is a really simple format, which probably at least partly explains its success and widespread use. The format is basically a hash list consisting of ''msgid'' and ''msgstr'' pairs, with the msgid being the original English string and key, and the msgstr being the translated value of it. As the English string is the key, all instances of the exact same English string in the code will be represented by exactly only one key/value pair, referred to as a ''message'', in the PO file. Usually this is not a problem, but instead a benefit of the format, as the exact same string won't have to be translated more than once by the translator. Below is

#include <gtk/gtk.h>
#include <stdio.h>
static GtkWidget *mainWindow = NULL;
void
prompt_window (void)
{
GtkWidget *dialog = gtk_message_dialog_new (GTK_WINDOW (mainWindow),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
// compile me with:
// g++ -g -O0 -o testconv testconv.cpp `pkg-config --cflags --libs mozjs-24` -std=c++11
// output:
// JS_ValueToInt32: did not convert
// JS::ToInt32: converted, value is 0
#include <iostream>
#include <jsapi.h>
static JSClass global_class = {
@ptomato
ptomato / callargs.cpp
Last active October 9, 2016 23:10
Templated scanf-like function
#include <iostream>
struct SomeStruct {
int val;
};
template<typename T>
bool
assign(const char c, T& ref) {
return false;
#include "jsapi.h"
int main(int argc, const char *argv[])
{
JS_Init();
JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024, JS_USE_HELPER_THREADS);
if (!rt)
return 1;