Skip to content

Instantly share code, notes, and snippets.

@mdipierro
mdipierro / example.py
Created January 23, 2024 06:54
Python script that makes its own nix-shell
#! /usr/bin/bash
"""":
# if Nix not installed, install it, then rerun this script with nix-shell
[ -f /etc/nix/nix.conf ] || curl -L https://nixos.org/nix/install | sh -s -- --daemon
exec nix-shell --command "python $0 $@" \
-p python311 \
-p python3Packages.numpy
# add any package you want above and they will be added to the shell
exit 1
""" #"
import jwt
jwt_data = jwt.decode(some_data, secret)
form = Form(.... hidden=dict(mydata=jwt_data))
if form.accepted:
some_data = jwt.decode(request.forms['mydata'], secret)
...
class Injector(Fixture):
def __init__(self, **objects):
self.objects = objects
def transform(self, output, shared_data=None):
if isinstance(output, dict):
output.update(**self.objects)
return output
injector = Injector(menu)
@mdipierro
mdipierro / gist:ffd127c83cf081daff73c7546d39bbe5
Created September 26, 2020 17:38
pydal reference examples
from pydal import DAL, Field
db=DAL()
db.define_table('person', Field('name'))
id = db.person.insert(name="Max")
print("%(name)s" % id)
db.define_table('dog', Field('name'), Field('owner', db.person))
db.dog.insert(name="Snoopy", owner=id)
dog = db(db.dog).select().first()
print("%(name)s" % dog.owner)
@mdipierro
mdipierro / gist:f95dab0f869f5504482e
Created July 16, 2015 09:15
contact form in web2py
# append to scaffoling models/db.py
db.define_table('contact',
Field('your_name',requires=IS_NOT_EMPTY()),
Field('email',requires=IS_EMAIL()),
Field('message','text'))
# append to scaffolding controllers/default.py
def contact_us():
form = SQLFORM(db.contact)
if form.process().accepted:
jQuery('input.rating,input[name*="rating"]').each(function(){
var span = jQuery('<span style="white-space:nowrap"><span class="rate0">&#x25CE;</span><span class="rate1">&#x2606;</span><span class="rate2">&#x2606;</span><span class="rate3">&#x2606;</span><span class="rate4">&#x2606;</span><span class="rate5">&#x2606;</span></span>');
var self = jQuery(this).hide().after(span);
var fill_stars = function() {
var k = parseInt(self.val()) || 0;
for(var i=1; i<6; i++) span.find('.rate'+i).html((i<=k)?'&#x2605;':'&#x2606;');
};
for(var k=0; k<6; k++) (function(k){
span.find('.rate'+k).mouseover(function(){
for(var i=1; i<6; i++) span.find('.rate'+i).html((i<=k)?'&#x2605;':'&#x2606;');
"""
Usage:
python jinja2web2py.py jinjatemplate.html > web2pytemplate.html
Disclaimer. It is not perfect. Some times minor manual tweaks may be necessary.
Notice the web2py template language was invented in 2007 and consists of pure Python code.
The opposite conversion is not possible because arbitrary Python code cannot be converted to a jinja template.
"""
import sys
"""
Usage:
python jinja2web2py.py jinjatemplate.html > web2pytemplate.html
Disclaimer. It is not perfect. Some times minor manual tweaks may be necessary.
Notice the web2py template language was invented in 2007 and consists of pure Python code.
The opposite conversion is not possible because Python code cannot be converted to a jinja template.
"""
import sys
"""
Usage:
python jinja2web2py.py jinjatemplate.html > web2pytemplate.html
Disclaimer. It is not perfect. Some times minor manual tweaks may be necessary.
Notice the web2py template language was invented in 2007 and consists of pure Python code.
The opposite conversion is not possible because Python code cannot be converted to a jinja template.
"""
import sys
@mdipierro
mdipierro / gist:5479683
Created April 29, 2013 04:19
whoosh and web2py example
def whoosh(folder):
from whoosh.index import create_in
from whoosh.fields import ID,TEXT,Schema
from whoosh.qparser import QueryParser
from whoosh.query import And
import os