python

Python subclass with C++ baseclass

I have some C++ I have exposed to Python through SWIG. In there is a base class with a single pure virtual function. In Python, I import my module and define a class that uses the abstract class as base. import mymodule class Foo(mymodule.mybase): ... In that module is also a manager class, I want to add my new defined class to t...

Why does Python allow comparison of a callable and a number?

I used python to write an assignment last week, here is a code snippet def departTime(): ''' Calculate the time to depart a packet. ''' if(random.random < 0.8): t = random.expovariate(1.0 / 2.5) else: t = random.expovariate(1.0 / 10.5) return t Can you see the problem? I compare random.random w...

How to get image details from firefox webdriver?

Hello, I've got an image on a page rendered by Firefox via Webdriver, I can get its object (wd.find_element_by_xpath("id('main')/form/p[5]/img")), but how can I get its body either base64-encoded or just a location on my hard drive? PS: please don't suggest getting the src and fetching it with an external tool. I want the image I alread...

Data visualization in python - after connecting to a database

Can you help me to connect to my postgresql database with python? I need to create graphic interface with python which will visualize shapefile data from my database (i have about 50 polygons in shapefile format in that database). Can you help me with creating such application? I am begginer in python. ...

When should I drop support for python2.4 on my public python library?

I maintain an open source python project. Right now it supports python 2.4, 2.5, 2.6. I am looking for to add support for python 3. I guess it will be easier if I drop 2.4 support. I know it is possible to support all but it is very annoying if I have to install 4 or 5 python versions on my machine and run the tests on all of them. Alth...

SQL syntax error using Python and psycopg

How can you fix this SQL-code? My Python code: import os, pg, sys, re, psycopg2 conn = psycopg2.connect("dbname=tk user=masi password=123") cur = conn.cursor() cur.execute("""INSERT INTO courses ('course_nro') VALUES ( `:1` )""", ['hen']) I get: Traceback (most recent call last): ...

How to access Firefox cache from webdriver?

Hello, I'm able to access pages like about:cache-entry?client=HTTP&sb=1&key=(some URL) directly in Firefox, but when it renders the page, it certainly gets the data from some storage. How can I access the latter from Python Firefox Webdriver? ...

Populating values in module namespace

I have a python module. I want to populate some values to it at runtime, how do I do it. Eg. I have a list, ['A', 'B', 'C'] I am creating there classes with these names, and want them to available as if I created them normally for el in ['A', 'B', 'C']: type(el, (object,), {}) ...

Boolean fields in MySQL Django Models?

At Django, a boolean field in MySQL is stored as a TINYINT. When I retrieve it, I get 0 or 1. Shouldn't I get False or True? Is there a way to achieve this behaviour? ...

Is PyGTK or PyQT preferred for making GTK-native Python apps?

I'm a web developer looking to get my feet wet with coding up a little desktop app for Ubuntu in Python. I've scoured the web looking for the pros and cons of PyGTK vs. PyQT and can't really find any good comparisons. What do you guys think? Do they both produce native-looking widgets on a GNOME system? Is one easier to use than the oth...

To insert to Pg by Psycopg

How can you fix the SQL-statement in Python? The db connection works. However, cur.execute returns none which is false. My code import os, pg, sys, re, psycopg2 try: conn = psycopg2.connect("dbname='tk' host='localhost' port='5432' user='naa' password='123'") except: print "unable to connect to db" cur = conn.cursor() print cur...

Problem with logic in Django template

Supose this portion of a Django template. regs is a list of Reg objects. Reg.editable is a BooleanField. I want to render a radio button per element in the list. If r.editable is False, the radio button must be disabled: {% for r in regs %} <input type="radio" value="{{ forloop.counter }}" {% if forloop.first %}checked="checked"{% endif...

There is no spawnl function in python 2.6?

I just noticed that my old codes written in python 2.5 does not work now. I am in python 2.6 btw. >>> os.spawnl(os.P_NOWAIT,"setup.exe") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "c:\python26\lib\os.py", line 612, in spawnl return spawnv(mode, file, args) OSError: [Errno 22] Invalid argument >>...

Converting SQL commands to Python's ORM

How would you convert the following codes to Python's ORM such as by SQLalchemy? #1 Putting data to Pg import os, pg, sys, re, psycopg2 #conn = psycopg2.connect("dbname='tkk' host='localhost' port='5432' user='noa' password='123'") conn = psycopg2.connect("dbname=tk user=naa pass...

Swapping 1 with 0 and 0 with 1 in a pythonic way?

In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1. How do you do it in a Pythonic way? if val == 1: val = 0 elif val == 0: val = 1 it's too long! I did: swap = {0: 1, 1:0} So I can use it: swap[val] Other ideas? ...

Small "embeddable" database that can also be synced over the network?

I am looking for a small database that can be "embedded" into my Python application without running a separate server, as one can do with SQLite or Metakit. I don't need an SQL database, in fact storing free-form data like Python dictionaries or JSON is preferable. The other requirement is that to be able to run an instance of the data...

How do I use Python to convert a string to a number if it has commas in it as thousands separators?

I have a string that represents a number which uses commas to separate thousands. How can I convert this to a number in python? >>> int("1,000,000") Generates a ValueError. I could replace the commas with empty strings before I try to convert it, but that feels wrong somehow. Is there a better way? ...

what does abstract mean in this context?

I'm completley new to python, so I'm having a hard time getting started with some code we got as a framework for a homework assignment. I'll stress that I don't want help with doing the actual homework, only some help in understanding some python concepts. here is the code snippet: class TilePuzzleProblem(search.Problem): """ This cla...

python metaclasses vs class decorators

what are the main differences ? is there something i can do with one method but not with the other one ? ...

Django: request.META['REMOTE_ADDR'] is always '127.0.0.1'

I have an application running with debug=True on a remote host somewhere. Now somehow every time I access REMOTE_ADDR it returns 127.0.0.1 no matter where the request is from. I'm not sure where to start and why this is happening. ...