Roy Tang

Programmer, engineer, scientist, critic, gamer, dreamer, and kid-at-heart.

Blog Notes Photos Links Archives About Now

All entries tagged python.

You can subscribe to an RSS feed of this list.

Jan 2020

Flask vs Django


Posted by under post at / Tags: #software development #python #flask #django / Syndicated: twitter / 0 / 533 words

Aug 2019

Python: Markov Chains


Back when I was still learning Python in 2008, one of the first “fun” scripts I wrote was a text generator using Markov chains. I’d run it against all the chat logs I had with people at work and serve the results from a webserver on my computer. THe results were often amusing and sometimes hilarious. Since I’ve been going through my old scripts lately, I thought I’d update that script to Python 3 (read: add parentheses around print params and use pathlib) and run it against all the posts on this here site.

read more (162 words)

Posted by under post at / Tags: #software development #python / Syndicated: twitter / 0 / 162 words

Devnotes: Python Pathlib


Ever since I started learning Python back in 2008ish, I’ve been using it as my primary scripting language for various tasks such as processing log files, organizing my own file system, processing stuff on this blog, and so on. A lot of it is basically moving files around. In the days of Python 2, that involved a lot of imports of different libraries like os, shutil and glob. It can become a bit messy with so many imports, and I often can’t remember which import I need for a particular case and end up having to search for the documentation (or stackoverflow, let’s not kid ourselves here).

read more (316 words)

Posted by under post at / Tags: #software development #devnotes #python / Syndicated: twitter / 0 / 316 words

Feb 2019

A while back I started a Twitter trivia bot as a weekend project. That bot is still up and running on Twitter, you can check it out there! But today, I thought I’d write about the answer-checking mechanism used by the bot. It was a bit interesting to me because it was the first nontrivial use I had for Django’s unit testing framework. I’m not too keen on unit testing web functionality (something I still have to learn), but this seemed an appropriate first use of a unit test framework for several reasons:

read more (579 words)

Oct 2018

Django Blog Application


Ten years ago this month, I started studying Django by trying to build my own blog application. I found the code lying around while I was going through some backups lately. It’s way out of date, it uses an early version of django. I thought of bringing it up to speed, but that didn’t seem practical. Instead, for archival purposes, I cleaned it up a bit and uploaded the code to a github repo.

read more (176 words)

Mar 2017

I’ve been hesitant to try Python 3.x because it’s not backward compatible with Python 2.x which I’ve been using for scripting since forever. But recently I found out that since Python 3.3, they’ve included a launcher in the Windows version that supports having both versions installed. You can use the launcher to specify the Python version to use at the command line (it defaults to whichever version was installed first):

read more (108 words)

Posted by under post at / Tags: #python #Software Development / Syndicated: tumblr twitter / 0 / 108 words

Feb 2017

Weekend Project: Twitter Trivia Bot


I had been meaning to try writing a Twitter bot for a while now. I figured a trivia bot would be pretty easy to implement, so I spent some time a couple of weekends to rig one together. It’s (mostly) working now, the bot is active as triviastorm on Twitter, with a supporting webapp deployed on http://trivia.roytang.net/. The bot tweets out a trivia question once every hour. It will then award points to the first five people who gave the correct answer.

read more (264 words)

Posted by under post at / Tags: #projects #python #triviastorm #Software Development / Syndicated: tumblr twitter / 0 / 264 words

Jan 2017

So the other day I was reworking a Python script that I had been using for years on my home PC to manage and categorize some downloaded files for me. This time I wanted to add some smarter behavior to make it more able to figure out when to group files into folders without constantly needing manual intervention from me. To do this, I needed to persist some data between runs – so that the script remembers how it categorized previous files and is able to group similar files together.

read more (322 words)

Posted by under post at / Tags: #python #Software Development / Syndicated: tumblr twitter / 0 / 322 words

Aug 2011

Oct 2010

Jul 2010

Jun 2010

Is there any Python library that allows me to parse an HTML document similar to what jQuery does?

i.e. I’d like to be able to use CSS selector syntax to grab an arbitrary set of nodes from the document, read their content/attributes, etc.

The only Python HTML parsing lib I’ve used before was BeautifulSoup, and even though it’s fine I keep thinking it would be faster to do my parsing if I had jQuery syntax available. :D

Jan 2010

urlpatterns = patterns('',     
    (r'^salaries/employee/$', list_detail.object_list, 'employee_info'),
)

The third item in the tuple needs to be a dictionary, not a string. Try removing the single quotes around employee_info:

urlpatterns = patterns('',     
    (r'^salaries/employee/$', list_detail.object_list, employee_info),
)
Posted by under notes at / Tags: #python #django / Imported from stackexchange / Syndicated: stackexchange / 0

Dec 2009

Sep 2009

Apr 2009

Feb 2009

Stylesheet Switcher using Django


You may have noticed the new color scheme and new “Theme Switcher” widget in the sidebar. I had done some CSS work during the past month in the office and it made me want to tweak the stylesheets on this site a bit. I figured I might as well make it easy to switch stylesheets, so I wrote a small Theme Switcher django app. (Well, it’s more of a stylesheet switcher I guess)

read more (543 words)

Jan 2009

Using Django Pingback


I actually had some trouble using django-pingback on my custom blog engine; the django-pingback documentation is mostly fine, but there were some caveats that I had to discover myself through a bit of debugging: The URL specified for the XML-RPC endpoint in the HTML head needs to be a full absolute url including domain, i.e. http://roytang.net/xmlrpc/, which gave me trouble when I was trying to test using localhost pinging to an online server.

read more (410 words)

Dec 2008

Colophon 2008


Frontend: All page templates are valid (X)HTML. However, I choose not to claim valid XHTML (and no doc type declaration) since I can’t guarantee that blog posts I write are compliant! The site uses standard CSS and uses the Blueprint CSS Framework for the grid layout of the page. The site design is entirely original (if not simple and bland – I’m not very good with website design yet!). The site has minor usage of JQuery Javascript library in some parts.

read more (283 words)

Fixing up the comments


I played around with the built-in comments app today, trying to clean it up. Some findings: Some of the moderation views, such as flagging a post or deleting a post, accept a next parameter that determines where the view will redirect to after the operation. However, the way the url’s are set up, there’s no easy way to pass this parameter normally, even through query strings. THis lovely bug is documented in http://code.

read more (435 words)

Posted by under post at / Tags: #royondjango #django #python #software development / Imported from royondjango / Syndicated: blogger / 0 / 435 words

Nov 2008

I’ve been busy at work so fell a bit behind with Django. Last night I worked on a WordPress importer, so that I could migrate posts from my current blog(s) into the Django-powered blog that I’m coding. I’m using BeautifulSoup to parse the WordPress export file and insert them as Django objects. Since I was running the script repeatedly, I had to figure out how to easily run it from the command line, without having to run it from inside manage.

read more (232 words)

Oct 2008

django-tagging


I wanted to add some basic tagging to my blog app so I tried out django-tagging. Unfortunately, the featured downloads on the Google Code site are quite out-of-date and would not work with Django 1.0, so I did a subversion checkout instead. If you’re getting an error like “ImportError: cannot import name parse_lookup”, then you need to get the source code from SVN. Adding the tagging to the blog was pretty easy:

read more (164 words)

Posted by under post at / Tags: #royondjango #django #python #software development / Imported from royondjango / Syndicated: blogger / 0 / 164 words

Deployment Problems


So I got a basic blog app up and running. Posting, paged archives, etc. Comments implemented using the django.contrib.comments. No problems here, the only caveat being most of the current documentation found by Google searches refer to the pre-1.0 version. Need to peruse the official docs for 1.0 stuff. RSS feeds implemented using django.contrib.syndication, this one seems fine. I tested it and it’s running fine on localhost. I also have a free django hosting account at http://bells-n-whistles.

read more (143 words)

Posted by under post at / Tags: #royondjango #django #python #software development / Imported from royondjango / Syndicated: blogger / 0 / 143 words

Playing with Generic Views and URLs


“when redirecting, how can I make the redirect URL decoupled from the urls.py of the parent app?” -> It turns out that HttpResponseRedirect supports relative paths, so this was fine. return HttpResponseRedirect("../” + str(post.id) + “/") I got the basic posting structure up. /post/new/ -> To make new posts /post// -> To view a single post /post/all/ -> To view all posts I should probably start thinking of a better url scheme.

read more (167 words)

Posted by under post at / Tags: #royondjango #django #python #software development / Imported from royondjango / Syndicated: blogger / 0 / 167 words

Starting out


The quintessential app to learn from is of course a blog. Started using a simple Post model. Added the new post form and view. Can now successfully insert posts into the DB. Next: – create the detail page that will show the post after saving Figure out: – when redirecting, how can I make the redirect URL decoupled from the urls.py of the parent app? i.e. if the parent app has the following mapping:

read more (127 words)

Posted by under post at / Tags: #royondjango #django #python #software development / Imported from royondjango / Syndicated: blogger / 1 / 127 words

Jul 2008