Archive for the ‘Python’ Category

Python – Connecting to FileMaker via JDBC on Linux

Monday, May 9th, 2011

Notes: This should work on FileMaker Pro 9 and 10. FileMaker Pro 11 might be different.

The trick here is you can’t use FileMaker’s ODBC library as that is only for Windows (and Mac?). You need to use the cross platform JDBC driver.

Firstly get the sljc.jar file out of the FileMaker installation media (Instructions here). I placed the sljc.jar file in /opt/drivers and I made sure /opt/drivers is owned by myself:

$ sudo chown pinerog:scicomp /opt/drivers

Now install the Python libraries you’ll need:


JPype – a library to allow Python to access Java libraries

To Install:

> cd /opt/source
> wget http://downloads.sourceforge.net/project/jpype/JPype/0.5.4/JPype-0.5.4.1.zip?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fjpype%2Ffiles%2F&ts=1304951217&use_mirror=iweb
> unzip JPype-0.5.4.1.zip
> cd JPype-0.5.4.1/
# I had to install open jdk dev
> sudo yum install java-1.6.0-openjdk-devel.x86_64
#Open up setup.py and find the function 'setupLinux' and set self.javaHome to "/usr/lib/jvm/java" or whatever your Java Home is
> sudo python setup.py install
# (Note: It still won't know where Java home is even after installed so before you use this lib, set your JAVA_HOME env variable: export JAVA_HOME=/usr/lib/jvm/java or where ever yours is)

(I got some help figuring out the Java issues here.)

JayDeBeApi – a library to let you connect Python to JDBC connections

To Install:
$ sudo easy_install JayDeBeApi
(I got help using JayDeBeApi here.)

Sample Code:

Finally you’re ready to use this stuff.

# You'll probably need to set your Java Home env var before running:
# $ export JAVA_HOME=/usr/lib/jvm/java (or where ever yours is)
import jaydebeapi
import jpype

URL = 'jdbc:sequelink://10.21.41.26:2399;serverDataSource=FLA'

jar = r'/opt/drivers/sljc.jar' #path to driver
args='-Djava.class.path=%s' % jar
jvm_path = jpype.getDefaultJVMPath()
jpype.startJVM(jvm_path, args)

conn = jaydebeapi.connect('com.ddtek.jdbc.sequelink.SequeLinkDriver',
                            URL, 'username', 'pw')
curs = conn.cursor()

curs.execute('''select * from LabHead''')
print curs.fetchall()[:100]

Update: There are a couple of changes for working with FileMaker 11.

Fixing – Django admin permissions not getting updated after adding model proxy

Friday, April 1st, 2011

Django problem:

I added a new model proxy to my models.py and then registered it on admin.py but the permissions for the new model were not showing up in the add user permissions section of the admin page even after doing a syncdb.

To fix I manually added it to the tables using the SQL below:

INSERT INTO django_content_type (name,app_label,model)
VALUES ('User Activity','VisitorLog','logentryforvisitorlogonlyproxy');
-- Use ID from insert above to fill in below
INSERT INTO auth_permission (name,content_type_id,codename)
VALUES ('Can add User Activity',12,'add_permission');
INSERT INTO auth_permission (name,content_type_id,codename)
VALUES ('Can change User Activity',12,'change_permission');
INSERT INTO auth_permission (name,content_type_id,codename)
VALUES ('Can delete User Activity',12,'delete_permission');

How to Round Up to the Nearest Quarter in Python

Sunday, August 22nd, 2010

This seems to work for me:
math.ceil(val*4)/4

XMLRPC Server Slow in Python – How to Fix

Friday, February 19th, 2010

Background:
I had set up a Python XML-RPC server on one machine. When accessing the web services from other machines it would sometimes take up to 20 seconds to get a response. The strange thing was that this only happened when accessing the web service from some machines but not others.

Problem and Solution:
It turns out that Python’s BaseHTTPRequestHandler was trying to log the fully qualified domain name of each request’s IP address. Thus when we connected from machines that didn’t have a fully qualified domain name, it would take a long time to not find the FQDN. There is actually a bug for this.

Below is a simple fix that solved the problem for me (in Python 2.4). This would probably work in future versions too.

Assume this is your basic XMLRPC Server set up in Python:

import SimpleXMLRPCServer

class FuncGroup:
    """Just hold the funcs to be used in web service"""
    def hi(self,val):
        return "hi there"

#create server and start it
server_address = ('localhost',8000) # (address, port)
server = SimpleXMLRPCServer.SimpleXMLRPCServer(server_address)
server.register_instance(FuncGroup()) #reg. functions/class instance
server.serve_forever()


Here is the fixed version. I added in an override for the trouble function:

import SimpleXMLRPCServer

#new code
import BaseHTTPServer
def not_insane_address_string(self):
    host, port = self.client_address[:2]
    return '%s (no getfqdn)' % host #used to call: socket.getfqdn(host)
BaseHTTPServer.BaseHTTPRequestHandler.address_string = \
    not_insane_address_string
#end new code

class FuncGroup:
    """Just hold the funcs to be used in web service"""
    def hi(self,val):
        return "hi there"

#create server and start it
server_address = ('localhost',8000) # (address, port)
server = SimpleXMLRPCServer.SimpleXMLRPCServer(server_address)
server.register_instance(FuncGroup()) #reg. functions/class instance
server.serve_forever()

Making py2exe Work with Paramiko

Friday, June 26th, 2009

When running py2exe with a script importing Paramiko I noticed this in the output:

The following modules appear to be missing ... paramiko ...

And then in the log file created when I run the exe I saw this:

Traceback (most recent call last):
 ...
ImportError: No module named paramiko

This has to do with py2exe not being able to handle egg files. So what I did is pulled out the paramiko directory from the egg, and placed that in my site-packages directory, and then deleted the egg. It works now though the setup function still tells me this:

The following modules appear to be missing
['Crypto.PublicKey._fastmath', 'Crypto.Util.winrandom', '_ssl', 'r_hmac']

Luckily as far as I can tell, I don’t use those modules anywhere.

This guy had the same problem and
made some sort of programmatic solution
but it didn’t work for me. Since when I ran the setup function, it didn’t throw an ImportError.