Skip to content

Instantly share code, notes, and snippets.

@mdipierro
mdipierro / get_time_slices
Created December 4, 2012 17:47
Given a start_date/stop_date and start_time/stop_date and a set of weekdays, returns all hourly time intervals in that range
def get_time_slices(vars):
slices = []
nd = (vars.stop_date - vars.start_date).days
weekdays = [k for k,name in enumerate('monday|tuesday|wednesday|thusrday|friday|saturday|sunday'.split('|')) if vars.get(name)]
hours = vars.stop_time.hour - vars.start_time.hour
for d in range(nd):
day = vars.start_date + timedelta(days=d)
if day.weekday() in weekdays:
zero = datetime(day.year, day.month, day.day,
vars.start_time.hour,
@mdipierro
mdipierro / fileio.js
Created February 2, 2013 04:11
HTML5 JS that allows saving and loading client side of any textarea. The example assume jQuery but fileio.js does not need jQuery.
/* example:
<script src='fileio.js'>
<script>FileIO.check_browser()</script>
<textarea id="code">
hello world
</textarea>
<a href="#" id="load-button">Load</a>
<a href="#" id="save-button" onclick="FileIO.save(jQuery('#code').val())">Save</a>
<script>FileIO.load('load-button',function(f){jQuery('#code').val(f.result)})</script>
@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
"""
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
"""
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
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;');
@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:
@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)
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)