Python decorator basics, part II
This is a continuation of my previous post: Python decorator basics. Here I’ll talk about a decorator with optional arguments. Let’s say we want to pass an optional argument to the same debug decorator:
def debug(msg=None):
def actual_decorator(f):
def wrapper(*args):
if msg:
print msg
return f(*args)
return wrapper
return actual_decorator
@debug("Let's multiply!")
def mul(x, y):
return x*yCalling mul:
mul(5, 2)
Let's multiply!
10Excellent. Now let’s decorate without a msg and call mul:
@debug
def mul(x, y):
return x*y
mul(5, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: actual_decorator() takes exactly 1 argument (2 given)
</module></stdin>Oh oh. Let’s see what happens at time of decoration:
mul = debug(mul)Hmmm, mul gets passed to debug as it’s argument and then the arguments (5, 2) are passed to actual_decorator, since debug returns actual_decorator. To resolve this we need to always call the decorator as a function:
@debug()
def mul(x, y):
return x*y
mul(5, 2)
10Assuming that we always expect the msg …
python
DBD::Pg 3.0.0 and the utf8 flag
One of the major changes in the recently released 3.0 version of DBD::Pg (the Perl driver for PostgreSQL) was the handling of UTF-8 strings. Previously, you had to make sure to always set the mysterious “pg_enable_utf8” attribute. Now, everything should simply work as expected without any adjustments.
When using an older DBD::Pg (version 2.x), any data coming back from the database was treated as a plain old string. Perl strings have an internal flag called “utf8” that tells Perl that the string should be treated as containing UTF-8. The only way to get this flag turned on was to set the pg_enable_utf8 attribute to true before fetching your data from the database. When this flag was on, each returned string was scanned for high bit characters, and if found, the utf8 flag was set on the string. The Postgres server_encoding and client_encoding values were never consulted, so this one attribute was the only knob available. Here is a sample program we will use to examine the returned strings. The handy Data::Peek module will help us see if the string has the utf8 flag enabled.
#!perl
use strict;
use warnings;
use utf8;
use charnames ':full';
use DBI;
use Data::Peek;
use lib …dbdpg perl postgres unicode
A Git and symlink mistake
A couple of times, I’ve accidentally created an infinite symlink loop and lost files from that directory from a single Git commit. Here’s how it happened:
- First, on the production app, images tied to products in a database were uploaded to the server. Let’s say I was working on a Rails app, so these images were uploaded to the RAILS_ROOT/public/system/ directory. I added “public/system/” to my .gitignore file, and all appeared to be good.
- Next on a camps instance, I created a symlink from CAMP_ROOT/public/system pointing to the production app public/system directory. This is common practice in End Point’s camps setup because we often don’t need the redundancy of uploaded files on our dev camp instance, and we don’t want the extra disk space used up for these types of files. The make camp script is designed to allow a user to toggle symlink functionality on and off for various directories during the make camp process.
- Next, I accidentally committed and push the public/system symlink from my development instance.
- Finally, I pulled the commit onto my production instance. The pull resulted in public/system symlinking to itself, and all of the files vanished (poof). Since they were …
git linux
Java Web app error: “Your security settings have blocked a self-signed application from running”
There’s a growing number people complaining that Java does not seem secure enough and that they feel vulnerable every time they open (and confirm to “Trust”) Java Web applications.
Since after the last update Java had at the end of January, this shouldn’t be a problem anymore as you can read in the Java Changelog for the latest release there has been many efforts toward making “the Web” a safer place for everyone.
Unfortunately is also quite known that security and usability often fight one against each other, so it’s fairly possible that after installing the last Java Update and when trying to use Web Apps that worked until a few minutes earlier, you found yourselves facing the following error: “Your security settings have blocked a self-signed application from running”.
What happened is that Oracle changed the behavior your Java browser plugin will have when dealing with self signed Web applications by actually denying the execution of those since considered harmful by default.
In order to fix this situation you’ll need to launch the command jcontrol. Most Linux distributions will install it under /usr/bin/jcontrol while others will place that binary in different places, as an …
java linux security update
Spree Active Shipping Gem “We are unable to calculate shipping rates for the selected items.” Error
I was recently working on a Spree site and setting up the Spree Active Shipping Gem along with the UPS API. For those not familiar, the Spree Active Shipping Gem interacts with various shipping APIs like UPS, USPS, and FedEx. Due to the nature of Spree—where it does so much for you, and the interaction between the Active Shipping Gem and a shipping API also being “auto-magic”, it is often difficult to debug. As I was recently undertaking the task of setting this up I found a few “gotchas” that I hope, through this blog post, may be able to save others a lot of time.
I have found that there wasn’t a lot of instruction for setting up the Active Shipping Gem and a shipping carrier API like the UPS Shipping API. Ostensibly, there isn’t much to it—the Active Shipping Gem handles much of the interaction between the shipping API of choice and Spree.
First, you’re going to go the Spree Active Shipping Gem GitHub repo and follow the instructions for installing the Active Shipping Gem. It is very straightforward, but do proceed in the order mentioned in the Spree Active Shipping Gem documentation as some steps depend on the successful completion of others.
Second, you’re going to go to the …
ecommerce api rails shipping spree
A Brief Retrospective of Spree
SpreeConf NYC 2014 starts on the 26th and its hard to believe that Spree is almost 7 years old! Here’s a retrospective showing some notable Spree moments and major releases.
July 15, 2007 RailsCart, the precursor to Spree, is created by Sean Schofield and gets its first commit as a Google Code project.
February 1, 2008 Sean is hired by End Point Corporation to work on RailsCart along with its other Rails developers. End Point sponsors Spree for the next year and a half.
February 15, 2008 RailsCart project moves from Google Code to GitHub and is renamed Spree.
June 1, 2008 Spree 0.0.9 released: sophisticated data model for inventory.
June 3, 2008 Product Properties added.
June 5, 2008 Spree 0.2.0 released: adds Spree extensions.
July 4, 2008 Zones introduced.
Sept 8, 2008 Refactored to REST routing added state machine to order.
October 1, 2008 New Taxonomy system introduced.
October 2, 2008 Spree 0.4.0 released: Taxonomy, and VAT-inclusive pricing.
November 26, 2008 Volume pricing introduced as an extension.
November 24, 2008 Spree 0.5.0 released: new shipping framework.
December 3, 2008 SEO friendly URLs.
December 4, 2008 Switched from attachment_fu to paperclip for image …
ecommerce rails spree open-source
Long-term Benefits from RailsAdmin
Sometimes setting up an admin tool with user authorization at the onset of a new Rails project can take a bit of work, and it’s not until later that long-term benefits of this framework can be realized. This came up recently when I was asked to write an export method to allow website admins to export customer and order data (for a specific date range) on a platform that was already using RailsAdmin in combination with CanCan for user authorization.
A normal Rails development process for this (from scratch) might look like this:
- Create controller.
- Create route to map to controller.
- Create method in controller to render initial view and send data.
- Create view which allows user to select date range.
- Add user authorization to allow specific users to access functionality.
This isn’t that much work, but here’s how the same functionality was built using RailsAdmin:
RailsAdmin Action
First, I create Rails Admin action, which inherits from RailsAdmin::Config::Actions. Inheriting from RailsAdmin::Config::Actions includes many class methods such as defining the actions http_methods (get, post, etc.), defining if the action is applicable to a single instance or a set of instances (which …
rails
Perl PostgreSQL driver DBD::Pg 3.0.0 released
I am happy to announce that version 3.0.0 of DBD::Pg, the Perl interface to Postgres, was released on February 3, 2014. This represents a major release, mostly due to the way it now handles UTF-8. I will try to blog soon with more details about that and some other major changes in this version.
The new version is available from CPAN. Please make sure that this is the latest version, as new versions may have come out since this post was written.
Checksums for 3.0.0:
58c2613bcb241279aca4c111ba16db48 DBD-Pg-3.0.0.tar.gz
03ded628d453718cbceaea906da3412df5a7137a DBD-Pg-3.0.0.tar.gz
The complete list of changes is below. Thank you to everyone who sent in patches, helped debug, wrote bug reports, and helped me get this version out the door!
Version 3.0.0 Released February 3, 2014 (git commit 9725314f27a8d65fc05bdeda3da8ce9c251f79bd)
- Major change in UTF-8 handling. If client_encoding is set to UTF-8,
always mark returned Perl strings as utf8. See the pg_enable_utf8 docs
for more information.
[Greg Sabino Mullane, David E. Wheeler, David Christensen]
- Bump DBI requirement to 1.614
- Bump Perl requirement to 5.8.1
- …dbdpg perl postgres
