Archive for the ‘Uncategorized’ Category

Testing User Sessions with Flask Elegantly

Thursday, August 17th, 2017

There is excellent information available on testing Flask e.g.

One thing I ran into was the need to set up a user session for most of my test cases.
The documentation suggests something like:

with app.test_client() as c:
    with c.session_transaction() as sess:
        sess['a_key'] = 'a value'

but repeating that in every test case (especially with a bit more code to set up the session) is not really DRY.

Now the same documentation suggests a very elegant trick using context managers for setting up users on the global object:

from contextlib import contextmanager
from flask import appcontext_pushed, g

def user_set(app, user):
    def handler(sender, **kwargs):
        g.user = user
    with appcontext_pushed.connected_to(handler, app):

Inspired by this, I figured that they can be combined into:

def user_set(app, user):
   with app.test_client() as c:
       with c.session_transaction() as sess:
           start_user_session(sess, user)
       yield c

and then use it in a test like:

user = ...
with user_set(app, user) as client:

Fix your PayPal PHP IPN script

Thursday, January 23rd, 2014

In October 2013  PayPal changed a bit of their server infrastructure. They announced it well, with multiple warnings about requiring the Host: header to be sent with verification requests.

Now suddenly my PHP IPN scripts did not work anymore, data was coming in but nothing got verified.

Checking the response quickly points out that PayPal is now redirecting requests to use HTTPS, but our old scripts are still based on years old example code using fsockopen on port 80.  fsockopen doesn’t know about HTTP redirects.

So, to get your PHP PayPal IPN notifications working again, you should replace the fsockopen line with:

$fp = fsockopen('ssl://', 443, $errno, $errstr, 30);

Update: A few days later things stopped working again… it seems PayPal now adds a newline character behind the VERIFIED response, messing up the sample script. This can be resolved by replacing:

$res = fgets ($fp, 1024);


$res = trim(fgets ($fp, 1024));

Freemium and Bootstrapping

Thursday, January 6th, 2011

These last few years the Freemium business model has gotten lot of attention and examples.  Basically it’s to offer free accounts to your service with an option to upgrade to a paid pro account. Key is to create enough benefit and goodwill to get those upgraded members.

Another interesting development is the notion of ‘Bootstrapping a company’. Which comes down to  starting a company self-funded, with minimum means. This is nothing new, most businesses in the past centuries have probably been started this way.

What I am interested in is the combination of using a freemium model for a bootstrapped (online) business.  It seems natural to create a free site first, as a side project, and add the paying part later, when it gets some traction. You would think that’s perfect for bootstrapping, as at first you can just focus on the idea itself, instead of building the whole business. It’s just going to cost you some time as there is no need to invest heavily in sales: free sells itself.

However, I believe this is generally a bad idea. First of all, free accounts create volume. This means your costs will grow and development will become more complicated with the larger scale.  Furthermore, it’s hard to pay for promotion,  when you are unable to determine returns on the extra users you gain. What if those expensive clicks just lead to more free money-draining signups?  This disconnection between scale and revenue involves risk, that you may not be able to take without an outside investment. It could take a while before you find the right balance to turn a freemium business into a profitable one.

A perceived advantage of a free version is that lots of users will get you a lot of feedback to improve your product.  However, feedback from users that don’t pay, does not necessarily say anything about the reasons users are not upgrading.

If you are going with a freemium model, I do have some advice:

  • Put very strict limits on the free account from the very start. If you have to put up limits afterwards, this will only get you angry users;
  • Add new features to your paid version first. Make sure that those members that are paying feel valued;
  • Think very hard about the cost of a free user, especially over time. For example, starting a freemium video site is probably not a very good idea without a stack of money;
  • If possible, make sure free accounts generate content  (unique content can potentially get you traffic)
  • Develop the paid version first, make sure it is worth the money and only after that add a free version to help promotion and to convince new customers.

All in all, using a freemium model complicates things in a way that makes it hard for a bootstrapped company to really follow it through.  In the end you are partly creating your own competitor, available for free.

Posting Regularly

Tuesday, December 28th, 2010

As you have seen,  I’m way better than before in posting regularly on this blog.  I’m still doing the same things, so there is not more to write about. However, I do follow a new system for posting.    In the past I would post when I was inspired to do so.  After a while inspiration dries up and you run into the ‘I have to write something…. ‘ problem and it will start to go downhill soon from there.

My new approach is to decouple inspiration,  writing and posting.   Instead of  going from idea to post in say 30 minutes.  I now just write down everything that could potentially be a blog post, without putting the burden of actually writing it on myself.  So I just create a draft post with the title and whatever flows easily from the mind.   Sometimes it is just the title,  at other times it is almost a full post.Because I do not force myself to write the whole post, I’ve collected a lot of drafts that could grow into a post.

At other times I might not feel inspired at all, but I do feel like getting something done. This the time to finish posts. Maybe find some relevant links or creating an illustration. When done, I either schedule the post  or let it rest for a bit until I feel like it is time to post again.

To make sure that I don’t forget about posting, I’ve also set up a repeating item in my Google calendar.  With the drafts already there, it takes no effort at all to actually get something out there if I didn’t already.

Using this approach I have gone from a single idea to post action  to a buffered approach that’s can cope much better with prolonged lack of inspiration or plain laziness.  It does take a bit of discipline to not immediately post what has been written, but it does lead to a much more consistent post rate.

Analyzing the Pomodoro Method

Thursday, December 23rd, 2010

In earlier posts I summarized the Pomodoro Method for time management.  Since than, I have to admit,  I wasn’ t able to persist in using it on a daily basis.  However, I did find it to be very useful to prevent me from diving into lengthy spans of procrastination and low-productivity.   Even one or two days of it, give back the feeling of actually getting stuff done. This is often enough to get everything back on track.

An important part of the Pomodoro Method is to have dedicated time assigned for a specific task, ensuring your full attention and focus. However, I believe this method mainly works for me because it decouples your breaks from your natural breaks in attention. On a normal working day you need to take breaks. Usually you will take them at times you mind starts to wander, energy is low or things start to get painful. You stopped working because you don’t feel like working anymore. Making it very hard to find motivation to start up again: it wasn’t going well when you started this break and now you don’t feel any better.

With the Pomodoro method, taking a break is fixed. Because you are forced to take it when you are in the middle of something, the feeling is entirely different.  At first I  felt ridiculous to take the break now, instead of just finishing your task.  However, after a while, I learned that this feeling ensures that you can easily start up again after a break. As you were making good progress before taking the break, you are eager to continue and finish that task at hand.

A New Office Chair

Sunday, September 19th, 2010

I’ve decided that I no longer like my  10 year old Ikea office chair.  So now I’m looking for something new and better. Unfortunately there is no clear winner,  for all chairs there can be found very positive and very negative reviews. Furthermore,  just sitting in a chair for a few minutes probably won’t tell me whether it is any good in the long term. And a pleasant chair, might not even mean it is actually good for you.  Anyway, I’m still doing research, which I’m publishing on a separate blog: High Back Office Chair .   Some chairs I’m currently investigating:

  • Aeron Mirra
  • RH Logic 400
  • Humanscale Freedom
  • Vitra Headline
  • Ikea Verksam  (as the reasonable and cheap base choice)

Suggestions are more than welcome.

SEOmoz Pro Account Review

Sunday, July 26th, 2009

SEOmoz seemed to have some nice tools,  so  we decided to give it’s PRO account a try.     And indeed the tools are quite nice.   I especially like the idea of Linkscape and  Ranktracker.    Linkscape shows you exactly what pages are linking to your site.  And better: you can easily filter them to see which do not come from your own website, which ones have  nofollow on them, etc.     Ranktracker does what you expect from the name:   track  search engine positions for different keywords over time.    There is a bunch of other tools as well, but those are not really impressive and maybe even a bit outdated.

However,  the site and tools are SLOW,   a significant portion of my requests  fails or take ages.     I’ve tested it during multiple days, so it’s not a one-time thing.    In my opinion  as a programmer I’m better of reproducing the tools with some small scripts,  because unresponsive sites really annoy me, especially if I pay $79 a month for them.     I do not think  I lost that first payment though, because there is also some great paid content over there.  But that’s not nearly enough to justify paying  $79 every month.

An Idea I do like it that I can take a questionnaire after I cancel  and get another week for free.   Because on our sites people usually don’t want to take the effort to tell us why they cancel.

Blank Canvas

Thursday, February 26th, 2009

Almost two years ago  I got some paint and a canvas for my birthday, but I never got around doing something with it.  Until today:

I think it came out a bit weird, but I’m happy with it for a first (and quick) effort.

Business Card

Tuesday, February 24th, 2009

I had a design for our business card lying around for more than a year.   There was a nice offer from VistaPrint so I thought I’d give it a go:

It’s quite ok, but I think having the blue printed as a single color, instead of using CMYK would give it a better look.  Also I would really like to put the illustration we have on our website  on the back, to make it a bit less formal.

Anyway,  I don’t really need business cards,  so probably the next edition will not be printed soon 🙂

Twitter, I like it

Tuesday, February 10th, 2009

What I never expected a few months ago: I actually like twitter.    Although it seems like an overflow of non-interesting information at first sight,  it’s actually pretty useful.  For me it’s especially keeping up to date with new developments without having to read through tons of lengthy blog posts.      For myself I try to keep a schedule of at least one twit a day. That should not be to difficult to stick to.  Finally I’ve added the feed to this blog (bottom right).