Mischiefblog
I make apps for other people

Python

How I code Python with tests

Posted by Chris Jones
On November 19th, 2015 at 13:25

Permalink | Trackback | Links In |

Comments Off on How I code Python with tests
Posted in Python

Setting up the project and where to put the code

  1. Project creation

    I use virtualenv to create the project. If I’m using a different version of Python than the system default, I can specify the interpreter version when creating the environment.

    $ virtualenv -p /opt/local/bin/python2.7 my_project

  2. Environment activation

    Before I can do anything with the environment, I need to activate the environment.

    $ source myproject/bin/activate

  3. Installation of modules

    By default, virtualenv installs pip to allow me to download and install Python libraries in my environment. Depending on what I’m writing, I need to pick out libraries that are needed to complete the project.

    (more…)

nginx, Python 3 pyvenv, uwsgi, Flask, and OS X with MacPorts

Posted by Chris Jones
On August 26th, 2014 at 14:53

Permalink | Trackback | Links In |

Comments Off on nginx, Python 3 pyvenv, uwsgi, Flask, and OS X with MacPorts
Posted in Python

Given a working Flask web application called RarToZip (served from the built in HTTP server), this is the configuration I used on my MacBook to get nginx working as a proxy for uWSGI and Flask.
(more…)

Configuring Apache 2 in Ubuntu for WSGI Python

Posted by Chris Jones
On September 3rd, 2010 at 12:17

Permalink | Trackback | Links In |

Comments Off on Configuring Apache 2 in Ubuntu for WSGI Python
Posted in Python

There’s a great thread on the Ubuntu forums regarding this topic. For the purpose of keeping a record of what I needed to do:

  1. Install Apache 2
  2. Install libapache2-mod-wsgi

    # apt-get install libapache2-mod-wsgi
  3. Link the project directory to /var/www

    # cd /var/www
    # ln -s /some/path/DevTools
  4. Update /etc/apache2/sites-available/default

        <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews ExecCGI
            AllowOverride None
            Order allow,deny
            allow from all
            AddHandler cgi-script .cgi
            AddHandler wsgi-script .wsgi
        </Directory>
  5. Restart Apache

    # apache2ctl restart
  6. Create the tester script, /var/www/DevTools/tester.wsgi

    def application(environ, start_response):
        status = "200 OK"
        output = "Hello World!"
     
        response_headers = [('Content-type', 'text/plain'),
                    ('Content-Length', str(len(output)))]
        start_response(status, response_headers)
     
        return [output]
  7. Verify your tester by going to http://localhost/DevTools/tester.wsgi. The output should be:

    Hello World!

If you get any 500 Server Error responses, check /var/log/apache2/error.log first. mod_python does a better job with error reporting than WSGI but is, in my experience, a more fragile framework for configuring and developing web apps.

Radar Revisited

Posted by Chris Jones
On October 14th, 2009 at 00:08

Permalink | Trackback | Links In |

Comments Off on Radar Revisited
Posted in Python

I’ve been snatching 30 and 60 minute blocks of time in the evenings and on the weekends to get back to work on my Radar test in Python with PyGame from nearly four years ago. PyGame 1.9.1 installed easily on my Windows box and OS X laptop and integrated well with Python 3.1 (on Windows) and Stackless Python 2.6 (on OS X). The only significant changes to be made in the code between Python 2 and Python 3 had to do with exception handling.
(more…)

Windows setup for my project

Posted by Chris Jones
On July 28th, 2009 at 23:31

Permalink | Trackback | Links In |

Comments (1) |
Posted in Python

Here are the steps required to set up a development environment on Windows for my Python project:

  1. Download the required programs and libraries
  2. Install Visual C++ Express
  3. Install Komodo Edit
  4. Install Stackless Python 2.6
  5. Install Berkeley DB 4.7
  6. Expand bsddb3-4.7.6.zip to a known directory
  7. Install setuptools and PyBSDDB
    1. Expand setuptools into a temporary directory
    2. Open a command line (on Vista, Windows-R and enter cmd)
    3. Add Python to the command line environment with:
      set PATH=%PATH%;C:\Python26
    4. Change directory to the temporary directory where you expanded setuptools
    5. Install setuptools with:
      python setup.py install
    6. Install PyBSDDB with:

      cd \Users\Chris\Temp\bsddb3-4.7.6 (where I expanded bsdb4)

      mkdir db
      cd db
      xcopy /e /v "C:\Program Files\Oracle\Berkeley DB 4.7.25\include" include\
      xcopy /e /v "C:\Program Files\Oracle\Berkeley DB 4.7.25\lib" lib\
      cd ..
      "C:\Program Files\Microsoft Visual Studio 9\VC\vcvarsall.bat"
      python setup.py install

. . . time for bed, but I’m having a little trouble with MSVC:

snip
Modules/_bsddb.c(8358) : warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
F:\Program Files\Microsoft Visual Studio 9.0\VC\INCLUDE\string.h(157) :
see declaration of 'strncpy'
warning: I don't know what to do with 'runtime_library_dirs': ['db/lib']
error: don't know how to set runtime library search path for MSVC++

Yeah, I’m probably nuts for trying to compile this myself. On OS X or Linux, there are still plenty of steps but no major hurdles.

Got transactions working in BDB 4.7 and Python

Posted by Chris Jones
On September 26th, 2008 at 07:44

Permalink | Trackback | Links In |

Comments Off on Got transactions working in BDB 4.7 and Python
Posted in Python

I’ve been working on a personal, side-project for about a week. This morning, I finally got transactions working. I need to learn a little more about BDB tuning and I’m not planning on releasing my code until I’m happier with it and it’s been tested more thoroughly.

Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Configure
>>> cfg = Configure.Configure()
>>> class Alpha:
... def __init__(self):
... self.field = 'alpha'
...
>>> cfg.databases['objects'][1].get('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "RefCache.py", line 110, in get
value = self.repository.get(key)
File "Repository.py", line 104, in get
raise KeyError, key
KeyError: 'a'
>>> txn = cfg.databases['objects'][1].beginTransaction()
>>> a = Alpha()
>>> cfg.databases['objects'][1].put('a', a, txn)
>>> cfg.databases['objects'][1].commitTransaction(txn)
>>> a.field
'alpha'
>>> a = None
>>> a
>>> a = cfg.databases['objects'][1].get('a')
>>> a.field
'alpha'
>>> b = Alpha()
>>> b.field = 'beta'
>>> txn = cfg.databases['objects'][1].beginTransaction()
>>> cfg.databases['objects'][1].put('b', b, txn)
>>> cfg.databases['objects'][1].rollbackTransaction(txn)
>>> cfg.shutdown()
>>> ^D
 
$ python
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Configure
>>> cfg = Configure.Configure()
>>> class Alpha:
... def __init__(self):
... self.field = 'alpha'
...
>>> b = cfg.databases['objects'][1].get('b')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "RefCache.py", line 110, in get
value = self.repository.get(key)
File "Repository.py", line 104, in get
raise KeyError, key
KeyError: 'b'
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> a = cfg.databases['objects'][1].get('a')
>>> a.field
'alpha'
>>> a = None
>>> a
>>> cfg.shutdown()
>>> ^D
 
$ db_stat -e -h db
Fri Sep 26 07:35:53 2008 Local time
0x120897 Magic number
0 Panic value
4.7.25 Environment version
...trimmed...

Working XML HTTP Update framework in Python

Posted by Chris Jones
On January 13th, 2008 at 17:55

Permalink | Trackback | Links In |

Comments Off on Working XML HTTP Update framework in Python
Posted in Python

I have a working, end-to-end XMLHTTPUpdate handling framework written in Python running as a CGI application. Ewww.

Download the framework here. It’s simple and probably has some bugs in it.

You can test it as well, but don’t expect it to work under Internet Explorer. I didn’t bother writing Javascript for IE, only Firefox (which means it will also run under Safari and on an iPhone). If you try it out, give it a couple seconds to fetch the time from the server.

There are obvious points of extension, starting from the Handlers.py file. I chose to explicitly check handler names rather than use them to dynamically call into code from a string passed through the update. It just seems safer. 🙂

. . . just 90% stupid.

Posted by Chris Jones
On January 8th, 2008 at 13:27

Permalink | Trackback | Links In |

Comments Off on . . . just 90% stupid.
Posted in Python

I figured out what I was doing wrong. Nothing except for how I chose to echo or display the XML.

Since it’s a dynamic call, I can’t view the page source to see the XML that gets returned. And I never verified that I was getting a String: I assumed that because it was inside a CDATA block that the tags would be visible, but what I actually got was the tags being evaluated, and since ^<get><time /></get> isn’t part of XHTML 1.1 . . . my XML never appeared.

So in the interest of saving some future developer a few minutes or hours or days of trouble, if you’re trying to parse POST-ed XMLHttpRequest XML DOM objects in a Python CGI web application and it looks like STDIN is empty or you’re not getting the POST contents, verify that you’re reading something by checking the length and extracting a part of the string. And convert your tags.
(more…)

I’m not entirely stupid . . .

Posted by Chris Jones
On January 7th, 2008 at 17:11

Permalink | Trackback | Links In |

Comments Off on I’m not entirely stupid . . .
Posted in Python

Now, I’m not entirely stupid. Lacking common sense? Yes. Forgetful of dates, appointments, anniversaries, bills? Yes. Easily confused? Absolutely.

But I’ve also been programming in Python for years and wrote my first CGI scripts in 1993 or 1994 or thereabouts. I’m kind of familiar with both environments, even if this is the first time I’ve actually put them together. Everything seemed to be meshing really, really well until I tried to get XML content from the web client through a DOM document passed into the XMLHttpRequest. (And yes, I am new to XMLHttpRequest dynamic app development. I’d always had the pleasure of using someone else’s library.)


function createDocGetTime() {
    docGetTime = document.implementation.createDocument("", "", null);
    var getNode = docGetTime.createElement("get");
    var getTime = docGetTime.createElement("time");
    getNode.appendChild(getTime);
    docGetTime.appendChild(getNode);
}
 
function updateTime() {
    timerClient = new XMLHttpRequest();
    timerClient.onreadystatechange = timerHandler;
    timerClient.open("POST", "xmltimer.py");
    timerClient.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
    timerClient.send(docGetTime);
    timerId = setTimeout("updateTime()", 1500);
}

Evil code (there’s no way to stop calling updateTime() :)), tutorial-simple, and pretty obvious, or at least it should be. The problem looks to be Python.
(more…)

Laying the groundwork

Posted by Chris Jones
On January 4th, 2008 at 15:37

Permalink | Trackback | Links In |

Comments Off on Laying the groundwork
Posted in Python, Tech

I’ve previously identified a few elements that are needed to make a successful web based application. Since then, I’ve been prototyping, designing, coding on the bus and while making dinner, and have a pared down, minimal proof of concept for a few of those.

  • XMLHttpRequest: I’ve verified XMLHttpRequest works with my wife’s iPhone. I expected it to, but had to make sure. This is the core Javascript call for dynamic web pages that retrieve content from servers.
  • Timer: I still need to verify this works as expected on the iPhone, but I have a demo page to verify it ready.

In addition to the front end work (of which there will be much, much more), I needed a back-end that I could develop actively. My platform choices were limited in that it needs to work on a Dreamhost shared server, work on my desktop at home, and also on my laptop for the bus or coding around the house. Tomcat had to be discarded: I’d need to set up a separate Tomcat server and Java environment at Dreamhost. Perl was discarded as I really don’t appreciate the language. 🙂 I strongly considered PHP but decided against it in the end. Ruby was also considered but I didn’t want to learn a new language while coding a new app. I toyed with using Lua. In the end, I decided on Python 2.4 so I don’t have to do any gymnastics at Dreamhost. As far as my scripts are concerned, it’s a one-line change between platforms:

#!/Python24/pythonw for Win32
#!/usr/bin/python for Linux

I have a working Python back-end that gives me a minimum of helper objects for working with HTTP requests in a CGI environment. (No WSGI here, at least not yet.) I have room to plug in different request interpreters and a complete world engine or whatever might strike my fancy as long as I keep this minimal interface in place.

Now, nobody said I had to produce anything pretty when proving this works…

Try out my first proof of concept

I’d like my next proof of concept to be an interactive “game” or other social, multi-user activity.