Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, July 30, 2012

Handy trick for using django runserver/testmaker on remote server

Thanks Bart for showing me how to have a local version of django running on my VPS.

On the VPS start django as required, eg

python manage.py runserver
python manage.py testmaker -a web

Now do this from a command line where 1.2.3.4 is your vps ip:

ssh root@1.2.3.4 -L 5984:localhost:8000 -N

enter the password and it should not come back with anything.

Now go to this URL and your website should start

http://localhost:5984/

Sunday, February 7, 2010

Installing OpenCV 2 on OSX with Python

Starting with instructions on the Willow Garage site, I fell at the first hurdle because thought I didn't need to install svn. Briefly, this is what I did and the more detailed explanation follows.

The short route:

1. sudo fink install svn-ssl (which takes a long time!)
2. sudo port install cmake
3. svn co https://code.ros.org/svn/opencv/trunk opencv
4. cd opencv
5. mkdir opencv/build
6. cd opencv/build
7. cmake ..
8. ccmake . (option c then option g)
9. make
10. sudo make install

On my osx 10.5 it copied the shared objects file to:

/usr/local/lib/python2.5/site-packages/cv.so

so I needed to move it to the site-packages I was using

11. cp /usr/local/lib/python2.5/site-packages/cv.so /Users/phoebebr/Development/site-packages

Now I can do:

python
>>> import cv

no error

and I can run the tests in my downloaded opencv folder,

cd opencv/opencv/tests/python
python test.py

This did report a few problems but most stuff seemed to run. Tried to run some tests calling opencv from Aptana but this caused the following error: " cp /usr/local/lib/python2.5/site-packages/cv.so /Users/phoebebr/Development/site-packages". Having installed successfully (I hope!) will have a go a writing a few programs next week....




The long route:

Already had svn so did:
svn co https://code.ros.org/svn/opencv/trunk opencv
and got
svn: SSL is not supported
so thought, I don't need https
svn co http://code.ros.org/svn/opencv/trunk opencv

and got

svn: PROPFIND request failed on '/svn/opencv/trunk'
svn: PROPFIND of '/svn/opencv/trunk': 301 Moved Permanently (http://code.ros.org)


So tried the instructions from the top:

The-Black-Book-2:site-packages phoebebr$ sudo port install subversion
Password:
---> Fetching apr
---> Attempting to fetch apr-1.3.5.tar.bz2 from http://www.mirrorservice.org/sites/ftp.apache.org/apr
---> Attempting to fetch apr-1.3.5.tar.bz2 from http://apache.multidist.com/apr
---> Attempting to fetch apr-1.3.5.tar.bz2 from http://apache.mirroring.de/apr
---> Attempting to fetch apr-1.3.5.tar.bz2 from http://archive.apache.org/dist/apr
---> Verifying checksum(s) for apr
---> Extracting apr
---> Configuring apr
---> Building apr
---> Staging apr into destroot
---> Installing apr @1.3.5_0
---> Activating apr @1.3.5_0
Error: Target org.macports.activate returned: Image error: /opt/local/bin/apr-1-config already exists and does not belong to a registered port. Unable to activate port apr.
Error: The following dependencies failed to build: apr apr-util expat libiconv gperf sqlite3 ncurses ncursesw readline cyrus-sasl2 openssl zlib gettext neon serf
Error: Status 1 encountered during processing.

So found this URL:
http://project-tigershark.com/people/rob/blog/2006/08/21/svn-on-os-x-with-fink-part-i-installation/
which explained that I needed the server version of svn and I only had the client version installed.

and did

sudo fink install svn-ssl


and that install svn so could continue with instructions above, missing the first step. Phew....

Monday, January 25, 2010

Using Pyparsing to extract dates from text block


This is very much work in progress, but I thought I'd post in case it helps anyone.

I'm currently using two bits of code, one to handle relative dates like "today", "2 days ago" and the other to handle specific dates like "3rd November 2009". These bits of code are building on work already done by others. I've tweeked them and added tests for parsing using parseString - expecting whole text to be a date, eg. "13th December 2009" and scanning using scanString where the date or dates is buried in the text, eg. "projects starts on 12th Nov 09 and finishes 3/2/10".


Relative dates:

Original code is in the Examples - In development on the pyparsing site: http://pyparsing.wikispaces.com/UnderDevelopment

Actual dates:



Saturday, January 23, 2010

Getting started with Pyparsing

Pyparsing is especially useful for those of us who don't use regexp enough to get any good at it! It is a deceptively simple parser for all kinds of text.

Here are a few resources and tutorials I found useful.

And here is my first program. The requirement is to decode an instruction into two chunks. An opening "o" or "-", followed by a mix of "?", "!", "&","^".

from pyparsing import *


def matching(list1, list2):

if len(list1) != len(list2):
return False

for key in list1.keys():
if (not list2.has_key(key)) or (list2.has_key(key) and list2[key] != list1[key]):
return False
return True

tests = [
('o?',{'status':'o', 'stype':'?'}),
('-!',{'status':'-', 'stype':'!'}),
('-!!!',{'status':'-', 'stype':'!'}),
('o?!',{'status':'o', 'stype':'?'}),
]


def handleStuff(string, location, tokens):

print 'string',string
print 'tokens', tokens, tokens[0][0]
return tokens[0][0]


status = Word("-o")
stype = Word("!?&^").setParseAction(handleStuff)


search = ZeroOrMore(status("status"))+ZeroOrMore(stype("stype"))


for (test, result) in tests:
print '---------------'
parsed = search.parseString(test,parseAll=True)
return_value = {'status':parsed.status, 'stype':parsed.stype}
print test, return_value, result, matching(return_value, result)

The bits I had to google a while for:

How to return named tokens.

If you did:

search = ZeroOrMore(status("myvar"))

and this is recommended above using setResultsName I believe.

then to get the return value:

parsed = search.parseString("My test string")
print parsed.myvar


The other question was what does setParseAction return and the answer is an updated token. See example above which returns the first character in the first token.


Results of the program are:

---------------
string o?
tokens ['?'] ?
o? {'status': 'o', 'stype': '?'} {'status': 'o', 'stype': '?'} True
---------------
string -!
tokens ['!'] !
-! {'status': '-', 'stype': '!'} {'status': '-', 'stype': '!'} True
---------------
string -!!!
tokens ['!!!'] !
-!!! {'status': '-', 'stype': '!'} {'status': '-', 'stype': '!'} True
---------------
string o?!
tokens ['?!'] ?
o?! {'status': 'o', 'stype': '?'} {'status': 'o', 'stype': '?'} True

Wednesday, December 30, 2009

Git and Unfuddle for python project - using .ignore

This is a reminder to me in case I forget how I got this working.

Main development environment is local laptop
Master reponsitory on Unfuddle.com
Live environment on remote server

There are a few files, like settings.py, that I don't want to be synced and as these files were already in the repository I'd created, git kept trying to sync them.

To resolve:
Assuming already have everything setup - remote server setup with clone from unfuddle.

Create .gitignore file as part of repository:

settings.py
*.pyc
*~
site_media/avatars/*

Remove settings.py from git without deleting on both local and remote environments.

In Local environment:
git rm --cached settings.py
git commit -a -m "remove settings.py"
git push unfuddle master
In remote live environment
git rm --cached settings.py
git commit -a -m "remove settings.py"
git push

That should do it. Now commits can be exchanged without settings.py being updated.