October 29th, 2009 by exhuma.twn
Problem:
$ easy_install psycopg2
Searching for psycopg2
Reading http://pypi.python.org/simple/psycopg2/
Reading http://initd.org/projects/psycopg2
Reading http://initd.org/pub/software/psycopg/
Best match: psycopg2 2.0.13
Downloading http://initd.org/pub/software/psycopg/psycopg2-2.0.13.tar.gz
Processing psycopg2-2.0.13.tar.gz
Running psycopg2-2.0.13/setup.py -q bdist_egg --dist-dir /tmp/easy_install-cHE0C_/psycopg2-2.0.13/egg-dist-tmp-x-CxRS
error: Setup script exited with error: No such file or directory
Solution:
This most likely indicates that you are missing the “libpq” headers:
sudo aptitude install libpq-dev
should solve the problem
Posted in Python | No Comments »
August 21st, 2008 by exhuma.twn
If you want command completion and a history in your python shell, export the PYTHONSTARTUP env var (export PYTHONSTARTUP=$HOME/.pystartup) in your bashrc and create a file ~/.pystartup with the following contents:
import atexit
import os
import readline
import rlcompleter
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
readline.parse_and_bind('tab: complete')
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
Posted in Python | No Comments »
December 31st, 2007 by exhuma.twn
Somethin that I need quite often is to create custom accessors and mutators for class-attributes. For example convert this:
class MyClass(object):
def __init__(self):
self.has_changes = False
self.some_attribute = False
into this:
class MyClass(object):
def __init__(self):
self.__has_changes = False
self.__some_attribute = False
def get_some_attribute(self):
"Accessor: some_attribute"
return self.__some_attribute
def set_some_attribute(self, input):
"Mutator: some_attribute"
self.__some_attribute = input
self.__has_changes = True
some_attribute = property(get_some_attribute, set_some_attribute)
def get_has_changes(self):
"Accessor: has_changes"
return self.__has_changes
has_changes = property(get_has_changes)
This particular example allows an easy tracking if a class contains changes. Without the need of calling myclass.get_some_attribute() or myclass.set_some_attribute(foo). You can simply do myclass.some_attribute = foo and the has_changes attribut will change accordingly.
If your class has many attributes, writing custom accessors and mutators can be tedious. So here’s a small Vim-mapping that get’s you started. Sure, you may still need to fine-tune some generated code, but the bulk is there.
<font color="#808bed">nmap</font> <font color="#c080d0"><</font><font color="#c080d0">F6</font><font color="#c080d0">></font> yyP<font color="#c080d0"><</font><font color="#c080d0">home</font><font color="#c080d0">></font>widef get_<font color="#c080d0"><</font><font color="#c080d0">end</font><font color="#c080d0">></font>(self):<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>yyP>>I"Accessor: <font color="#c080d0"><</font><font color="#c080d0">end</font><font color="#c080d0">></font>"<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">></font>yyP>>Ireturn self.__<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>o<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">></font>yyPIdef set_<font color="#c080d0"><</font><font color="#c080d0">end</font><font color="#c080d0">></font>(self, input):<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">></font>yyP>>I"Mutator: <font color="#c080d0"><</font><font color="#c080d0">end</font><font color="#c080d0">></font>"<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">></font>yyP>>Iself.__<font color="#c080d0"><</font><font color="#c080d0">end</font><font color="#c080d0">></font> = input<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>o<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">><</font><font color="#c080d0">down</font><font color="#c080d0">><</font><font color="#c080d0">home</font><font color="#c080d0">></font>wveyA = property(get_<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>pA, set_<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>pA)<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>o<font color="#c080d0"><</font><font color="#c080d0">esc</font><font color="#c080d0">></font>
Put this into your vimrc, or (like I do) into the ~/.vim/ftplugin/python.vim file so it get’s only loaded for python files. Then you only need to write the attribute name of the class, put your cursor on that line, be sure to be in normal mode (hit a few time <esc>)
and hit F6
If you want to change the shortcut, simply change the first parameter to this mapping line.
Posted in Python | No Comments »
September 17th, 2007 by exhuma.twn
This function uses the Haversine formula to calculate the distance which takes into account the spherical nature of the earth.
As the earth is not a perfect sphere, this function approximates this by using the average radius.
from math import sin, cos, radians, sqrt, atan2
def lldistance(a, b):
"""
Calculates the distance between two GPS points (decimal)
@param a: 2-tuple of point A
@param b: 2-tuple of point B
@return: distance in m
"""
r = 6367442.5 # average earth radius in m
dLat = radians(a[0]-b[0])
dLon = radians(a[1]-b[1])
x = sin(dLat/2) ** 2 + \
cos(radians(a[0])) * cos(radians(b[0])) *\
sin(dLon/2) ** 2
y = 2 * atan2(sqrt(x), sqrt(1-x))
d = r * y
return d
Posted in Python | No Comments »
September 10th, 2007 by exhuma.twn
Yet another thing that’s not automagic in Qt.
Here’s a python solution:
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
dw = app.desktop().width()
dh = app.desktop().height()
myapp = MainWindow()
myapp.setGeometry(
int((dw - (dw - (dw / 2)) * 1.5) / 2),
int((dh - (dh - (dh / 2)) * 1.5) / 2),
int((dw - (dw / 2)) * 1.5),
int((dh - (dh / 2)) * 1.5))
myapp.show()
sys.exit(app.exec_())
This will also resize the window, adapting to the desktop size.
Found and adapted from the Qt interest archive.
Posted in Python | No Comments »
September 10th, 2007 by exhuma.twn
Many things are very easy to accomplish in Qt. Qt’s designer is a great help with that. But the is no option to simply “enable” scrollbars for a frame. Also, the QScrollArea can nowhere be found in the designer. The solution is actually quite simple:
- Add the component you want to be scrollable (QFrame, QLabel, …) in the designer as usual
- In the application code:
- Create a new QScrollArea
- Set the parent of the widget you created in the designer to “None”
- Set the widget of the QScrollArea to the widget from step 1
- Insert the QScrollArea to you ui
And here’s some example code (boiled down to the essentials):
sa = QtGui.QScrollArea()
self.ui.myWidget.setParent(None)
sa.setWidget(self.ui.myWidget)
self.ui.vboxlayout1.insertWidget(0, sa)
You would probably write this somewhere in you application-constructor right after you call setupUi.
This will insert the scroll area at the top of vboxlayout1 (see the doc for insertWidget). You can of course also use addWidget.
It’s a shame that there’s no direct support for QScrollArea in designer. Maybe we will see it in some future release.
An example for C++ can be found in the Qt Forums.
Posted in Python | 1 Comment »
July 12th, 2007 by exhuma.twn
My preferred way to deploy python applications on Windows is to use py2exe.
py2exe is a Python Distutils extension which converts Python scripts into executable Windows programs, able to run without requiring a Python installation.
If you run in trouble with sip, read on…
Read the rest of this entry »
Posted in Python | 1 Comment »
July 2nd, 2007 by exhuma.twn
Writing Qt apps in Python with Qt3 worked. Somewhat. I never found the energy to get it all set up. Finally with Qt4 we have something that “just works”. At least on Ubuntu and Windows (haven’t tested anywhere else).
Yes, since version 4, trolltech finally decided to release Qt as well under the GPL. To most of you it’s all old news. But hey…. just felt like writing it down again
The problems with SIP are finally gone. So you just install python, Qt4 and pyqt4 and off you go.
Read the rest of this entry »
Posted in Python | No Comments »
June 27th, 2007 by exhuma.twn
Currently I am a bit busy, so I will just dump the post I found on usenet. Eventually I will clean it up…
On Wed, 22 Oct 2003 12:24:42 -0600
"Justin Johnson" <justinjohnson@fastmail.fm> wrote:
> The general consensus on the mailing list archives seems to be that
> I'd need to setup my code to not require twistd but just run
> standalone.
Uh. *Maybe* that's the consensus, but it's not actually correct. You can
have a tap or tac run as a NT service.
Lets say you have a script "server.py" that is runnable with "twistd
-y", you can do (and similar code will work for TAPs):
import sys, os
import win32serviceutil, win32service
class MyService(win32serviceutil.ServiceFramework):
"""NT Service."""
_svc_name_ = "MyService"
_svc_display_name_ = "MyService server"
def SvcDoRun(self):
import server
f = open(os.path.join(server.rootPath, "cyberhigh.log"), 'a')
from twisted.python.log import startLogging
from twisted.application.app import startApplication
from twisted.internet import reactor
startLogging(f)
startApplication(server.application, 0)
reactor.run()
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
from twisted.internet import reactor
reactor.stop()
if __name__ == '__main__':</pre>
win32serviceutil.HandleCommandLine(MyService)</pre>
--
Itamar Shtull-Trauring http://itamarst.org/
Available for Python & Twisted consulting
Posted in Python | No Comments »