Rails Style Params in Python and Flask
Ruby on Rails does a great job of parsing arrays in form data and populating a params hash, for quick and easy access in your controllers.
For instance
Gives you
params[:item] # => ["item 1", "item 2"]
Flask doesn't do this work f...
Written by Sean Behan on 11/04/2018
How to Just Get SQL Statement Error with SQLAlchemy Python Database Wrapper
If you're working with SQLAlchemy, the best database driver for Python, and want to see only SQL syntax errors, you need to use the StatementError exception class. On it, is an attribute `orig`, that contains just the SQL syntax error... and not any data...
Written by Sean Behan on 05/10/2018
Reshape an Array of Form Inputs for Flask with getlist()
This is how to reshape an array using Python without Numpy.
This is a fairly simple task but it can be a little confusing to wrap your head around. I ran into this problem while working with Flask, working with array from form fields.
I wanted to us...
Written by Sean Behan on 03/07/2017
Trigrams, Bigrams and Ngrams in Python for Text Analysis
Creating trigrams in Python is very simple
trigrams = lambda a: zip(a, a[1:], a[2:])
trigrams(('a', 'b', 'c', 'd', 'e', 'f'))
# => [('a', 'b', 'c'), ('b', 'c', 'd'), ('c', 'd', 'e'), ('d', 'e', 'f')]
You can generalize this a little bit more
...
Written by Sean Behan on 03/06/2017
How to Decorate Imported Libs in Python for Jinja Template Filters in Flask
To decorate an imported function in Python you would do something like this
# in ./lib.py
def function_name():
# function body
And then in your program you could decorate it like this
from lib import function_name
function_name = decorat...
Written by Sean Behan on 03/04/2017
Get Method Name as String in Python
Here is how to get the string representation of a method in Python
def my_method_name():
print "Hello World"
my_method_name.__name__
# => 'my_method_name'
Short and sweet!
Written by Sean Behan on 03/04/2017
How To Enable IFrame Support on Heroku with Ruby on Rails and Sinatra
This actually has more to do with Rails and/or Sinatra than it does with Heroku. You have to enable IFrame support in your headers explicitly.
With Rails you can add this to your config/application.rb file
config.action_dispatch.default_headers = ...
Written by Sean Behan on 08/25/2013
How to Merge a YAML File Into a Single Hash in Ruby
require 'yaml'
# Nested YAML structure from something-nested.yml
#
# nested:
# key: value
# key_two: value_two
# Procedural Approach to building a single Hash from nested YAML data structure.
@yaml = YAML.load_file("something-nested...
Written by Sean Behan on 06/17/2012
How Many Gigs of RAM Are On My Server?
How much memory is on my linux server? Run the free command
free -g
total used free shared buffers cached
Mem: 2 1 0 0 0 1
-/+ buffers/cache: 0 1
Swap: ...
Written by Sean Behan on 06/17/2012
Programmatically Turn Off Comments in Wordpress with Filter
To turn comments off programmatically with a filter in Wordpress, you can set the post object's comment_status variable to "closed". Call the filter at some point after the post is loaded but before the comments are rendered. This is a hack, but I haven't...
Written by Sean Behan on 06/17/2012
Thematic Function Reference
I could not find a listing of all thematic theme functions for Wordpress online. So the following is just a recursive grep of the thematic directory.
grep -rh "function thematic_" *
Each function needs to prepended with "thematic_" when adding as a actio...
Written by Sean Behan on 06/17/2012
Hacking Rails Plugins
Using the Acts as Taggable On plugin to add categories to a model class, I wanted to override the to_param method and place the name attribute in the url. The plugin, installed as a gem, source shouldn't need to be hacked in order to accomplish this. The ...
Written by Sean Behan on 06/17/2012
Mod_Python and Web.py on Ubuntu
Download
First install mod_python for Apache and then restart/reload the server.
apt-get install libapache2-mod-python
/etc/init.d/apache2 force-reload
apache2ctl restart
Next grab the web.py framework from webpy.org. You can grab the tar or use easy_in...
Written by Sean Behan on 06/17/2012
Override to_param method in model to get pseudo permalinks without any work
There are a number of permalink plugins for Rails, http://www.seoonrails.com/even-better-looking-urls-with-permalink_fu, is a good one that I've used before. However, this involves informing the model class (has_permalink :title), adding a route, using t...
Written by Sean Behan on 06/17/2012