Content tagged with "python"

Flex Django and AMF > Post > Dec 15, 2009 10 p.m.

Been working back in Flex again a bit, using Vim in Linux for a while, but I been really impressed with the short compile times in FlashDevelop for Windows.

Besides the point

Having become increasingly frustrated with the shortcomings of Django-amf, I've decided to give pyamf another try. I tried it the first time around, and got hung up on an error that made me bail out on it:

Cannot find a view for the path ['/gateway/cardservice/echo'], 'DjangoGateway' object has no attribute '__name__'

Some poking around in the source didn't help any, but the fix turned out to be easy. I stumbled on this post on StackOverflow, the author answered his own question in his comment. Turns out, leaving the django-amf middleware turned on in the settings.py file messed up the operation.

I don't have enough clout on StackOverflow to tell the author thanks there, so I'll do it here...

Aaron

Dreamhost, Django, Premature end of script headers: dispatch.fcgi > Post > Oct 28, 2009 03 p.m.

This problem comes up from time to time on my Dreamhost account, I get a long page load, finally ending with a 500 error in the browser. Tail'ing the apache error_log shows:

...
[Wed Oct 28 13:48:37 2009] [error] [client 198.53.10.99] Premature end of script headers: dispatch.fcgi
[Wed Oct 28 13:48:37 2009] [error] [client 198.53.10.99] Premature end of script headers: dispatch.fcgi
[Wed Oct 28 13:48:40 2009] [error] [client 198.53.10.99] Premature end of script headers: dispatch.fcgi
[Wed Oct 28 13:49:18 2009] [error] [client 198.53.10.99] Premature end of script headers: dispatch.fcgi
[Wed Oct 28 13:49:21 2009] [error] [client 198.53.10.99] Premature end of script headers: dispatch.fcgi
[Wed Oct 28 13:49:58 2009] [error] [client 198.53.10.99] Premature end of script headers: dispatch.fcgi
[Wed Oct 28 13:50:39 2009] [error] [client 198.53.10.99] Premature end of script headers: dispatch.fcgi
...

But where's the real error?

A little trick for you: go to the folder where your dispatch.fcgi file is and type:

./dispatch.fcgi

Basically you are executing the script, similar to an incoming request. You should then see the original traceback.

Hth,

Aaron

Awesome Python Tutorial > Link > Nov 24, 2008 03 p.m.

http://www.poromenos.org/tutorials/python

Why Python > Link > Nov 24, 2008 03 p.m.

http://www.linuxjournal.com/article/3882

Lightroom -like histogram in Python > Post > Nov 19, 2008 02 p.m.

I just added a dynamically-generated histogram to the image items on my site. It's not really usefull information in the context of viewing a photograph, it's more functional when editing, but being the uber-geek that I am, I thought it would still be interesting information to have on my site, right along with the exif data (aperature, focal length, etc).

But not just any histo

I didn't just want a black and white representation of all the image histogram, I wanted something like the pretty histogram in Adobe Photoshop Lightroom:

Lightroom Histogram

All in all the final version took me about 1.5 hours to write in Python using PIL. The semi-tricky part was getting the overlapping colors. It turned out to be about 70 lines of code, and could probably be done much better. On my local Ubuntu install, a typical image histogram is generated in about 5-10 ms, not bad for a skinny white guy :)

Check out an image or two to see the pretty histogram in action :) Next step, drop shadows on the layers...

Cheers, Aaron

Automatic moderation with Django 1.0 Comments > Post > Oct 16, 2008 02 p.m.

This one was actually a lot easier than I thought it would be. The Django comments framework was totally revamped for the Django 1.0 release, and it is now way mo betta :)

But! Even with all the new anti-spam stuff built in, I needed all comments to go into a moderation queue for a client site. At first I tried the pre_save hook to change the is_public flag on the comment, but there's no way (that I could tell) how to distinguish when the comment was submitted, and when it was moderated.

The solution

The Django folks are always thinking :) Then put in a sweet little hook called comment_will_be_posted that gets called when the user submits the comment. Hook in and change the 'is_public' flag to 'False', and the comments automatically go into the moderation queue.

The Code

from django.contrib.comments.models import Comment
from django.core.mail import send_mail
from django.contrib.sites.models import Site
from django.contrib.comments.signals import comment_will_be_posted
from django.conf import settings

def comment_set_public(sender, comment, request, **kwargs):
    # set the flag to false so it hits moderation
    comment.is_public = False
    
    # send an email
    subject = '%s > New Comment on %s' % (Site.objects.get_current(), comment.content_object.title)
    msg = """
You have a new comment on the item:\n%s\n\n
Comment text:\n%s\n\n
To moderate comments, visit the link below:\n
http://%s/comments/moderate/
          """ % (comment.content_object.title, comment.comment, Site.objects.get_current())
    send_mail(subject, msg, settings.COMMENT_FROM_EMAIL, [settings.COMMENT_TO_EMAIL])
    
    # from the docs: "If any receiver returns False the comment will be discarded..."
    # so there's much more potential for spam checking/protection
    return True

# the hook
comment_will_be_posted.connect(comment_set_public, sender=Comment)

Other goodies

Just for good measure, we put email notification in the same place which is handy (otherwise, how would you know comments were added?). The other super handy goodie with Comments 1.0 is they have added views for moderation also. Just point your browser to:

http://mysite.com/comments/moderate/

and you get a nice little view that looks just like the admin interface with which to moderate your comments. Obviously, if you're not logged in, it will prompt you to do so :)

Cheers, Af

PS: Here's a little screen to give you a taste: Django 1.0 Comments Moderation Screen

Django TimeSelect widget > Snippet > Oct 15, 2008 10 a.m.

# based on the DateSelect widget from django, tested with newforms-admin, need to test with 1.0
class SelectTimeWidget(Widget):
    """
    A Widget that splits time input into two <select> boxes.

    This also serves as an example of a Widget that has more than one HTML
    element and hence implements value_from_datadict.
    """
    hour_field = '%s_hour'
    minute_field = '%s_minute'
    apm_field = '%s_apm'

    def __init__(self, attrs=None):
        # years is an optional list/tuple of years to use in the "year" select box.
        self.attrs = attrs or {}


    def render(self, name, value, attrs=None):
        hour_val = minute_val = apm_val = None
        try:
            value = datetime.time(*map(int, value.split(':')))
            #value = datetime.date(*map(int, value.split('-')))
            #pdb.set_trace()
            #year_val, month_val, day_val = value.year, value.month, value.day
            if value.hour > 12:
                value.hour -= 12
                apm_val = 'pm'
            else:
                hour_val = value.hour
                apm_val = 'am'
            minute_val = value.minute
            
        except (AttributeError, TypeError, ValueError):
            hour_val = '8'
            minute_val = apm_val = None

        output = []

        hour_choices = (
            ('1','1'), 
            ('2','2'),  
            ('3','3'),  
            ('4','4'),  
            ('5','5'),  
            ('6','6'), 
            ('7','7'),  
            ('8','8'),  
            ('9','9'),  
            ('10','10'),  
            ('11','11'),  
            ('12','12'), 
        )

        select_html = Select(choices=hour_choices).render(self.hour_field % name, hour_val)
        output.append(select_html)

        minute_choices = (('00','00'), ('15','15'), ('30','30'), ('45','45'))
        select_html = Select(choices=minute_choices).render(self.minute_field % name, minute_val)
        output.append(select_html)

        apm_choices = (('am','AM'), ('pm','PM'))
        select_html = Select(choices=apm_choices).render(self.apm_field % name, apm_val)
        output.append(select_html)

        return mark_safe(u'\n'.join(output))

    def value_from_datadict(self, data, files, name):
        #y, m, d = data.get(self.year_field % name), data.get(self.month_field % name), data.get(self.day_field % name)
        h, m, a = data.get(self.hour_field % name), data.get(self.minute_field % name), data.get(self.apm_field % name)
        if a == 'pm':
            h = int(h) + 12
        if h and m:
            return '%s:%s:%s' % (h,m,'00')
        #if y and m and d:
           # return '%s-%s-%s' % (y, m, d)
        return data.get(name, None)
</select>

Ubuntu and PyQt > Post > Oct 14, 2008 01 p.m.

I just went through the nightmare of trying to install PyQt and all its dependancies. I had originally searched for an ubuntu package (coz we loves packages, doesn't we precious?) I couldn't seem to find one.

Turns out (after failing 'configure's and 'make's and missing dependancies), there is a package (a couple, actually).

To install PyQt on Ubuntu:

sudo apt-get install libqt4-core libqt4-dev libqt4-gui qt4-dev-tools python-qt4 pyqt4-dev-tools

I'm not sure if all the packages before the last one are necessary, but I didn't think it would hurt to have them.

Thanks again, Ubuntu Forums.

af

Installing Pygments on Webfaction > Post > Oct 10, 2008 12 p.m.

I just wanted to share the command, it took a little tinkering to get it right:

easy_install --always-unzip --install-dir=$HOME/lib/python --script-dir=$HOME/bin Pygments

Cheers, Aaron

Categories

Tag Cloud

'dark 'why 50mm actionscript adbusters amf aquaplex band blaire bowen brian burntstick butterfly cache calaway calaway park calgary caroline cat causeway chrome clearwater comments dan design django dos double time dreamhost drivers easy_install emacs email extra facebook family reunion favorite fcgi film firefox firewire flash flex flood fluxbox fossils funny gedit george green google grep hack hardy heron hdr init james join keys laserjet 1600 lightroom linux marye memory moderation mount mxml nelson noah olympus outlook park pil pillar pool potty pygments python raven recommend sata select send_mail server shortcut signals signs skye skype smtp soup spirit staged right sucks sucks' sundre sunset svn trac ubuntu vafcs vim virtualbox virus vista vnc water webcam webfaction white balance wide angle wireless work xl2 xmbc xmlrpc xp xp'

Random Images

pups BLr sunsuet Raven flood 2 James River flood Rabbit parts road Indi Cover The Longhouse Family get-together

About this blog

This blog is built on an experimental engine conceived by Aaron Fay. The system used to power this site is running on the awesome Django framework. As the site becomes more complete, and the functionality becomes streamlined, I will reveal more about the inner workings and may also release it open source one day. The most prominent feature at this point is all the different content types use the same model :)

My Comments Elsewhere

Darren's Developer Diary: Controlling file associations in Gnome Installing and configuring lighttpd webserver - HOWTO Import RSS feeds into Facebook without relinquishing content control | bylr.net Grogler » Blog Archive » After Effects Keyboard Shortcuts - Next Frame / Previous Frame Django snippets: Template Query Debug Django snippets: EditInline for GenericForiegnKey II I quit; goodbye cruel facebook « don’t mind me, just talking to myself