python

Python: Memory usage and optimization when modifying lists

The problem My concern is the following: I am storing a relativity large dataset in a classical python list and in order to process the data I must iterate over the list several times, perform some operations on the elements, and often pop an item out of the list. It seems that deleting one item out of a Python list costs O(N) since Py...

Checkers board structure

I am implementing a checkers game board with python. Here is how I generate the board structure as an [8][8] array: _matrix = [] for i in xrange(8): _matrix.append( [' '] * 8 ) for row in xrange(0, 8): for col in xrange(0, 8): if _darkQuad(row, col) == True: _matrix[row][col] = '#' ...

Python "Every Other Element" Idiom

Hey guys, I feel like I spend a lot of time writing code in Python, but not enough time creating Pythonic code. Recently I ran into a funny little problem that I thought might have an easy, idiomatic solution. Paraphrasing the original, I needed to collect every sequential pair in a list. For example, given the list [1,2,3,4,5,6], I ...

Converting BMP to Grey Scale in Image Module Python

Hi I was wondering how I can convert BMPs to a grey scale using Image Module of Python? Thanks ...

How to check wether a path represented by a QString with german umlauts exists?

Hey, i get a QString which represents a directory from a QLineEdit. Now i want to check wether a certain file exists in this directory. But if i try this with os.path.exists and os.path.join and get in trouble when german umlauts occur in the directory path: #the direcory coming from the user input in the QLineEdit #i take this QString...

Converting IPv4 or IPv6 address to a long for comparisons

In order to check if an IPv4 or IPv6 address is within a certain range, I've got code that takes an IPv4 address, turns that into a long, then does that same conversion on the upper/lower bound of the subnet, then checks to see if the long is between those values. I'd like to be able to do the same thing for IPv6, but saw nothing in the...

Generate an image / thumbnail of a webpage using X/Gui-less linux

There exists numerous solutions on generating a thumbnail or an image preview of a webpage. Some of these solutions are webs-based like websnapshots, windows libraries such as PHP's imagegrabscreen (only works on windows), and KDE's wkhtml. Many more do exist. However, I'm looking for a GUI-less solution. Something I can create an API a...

using a "temporary files" folder in python

I recently wrote a script which queries PyPI and downloads a package; however, the package gets downloaded to a user defined folder. I`d like to modify the script in such a way that my downloaded files go into a temporary folder, if the folder is not specified. The temporary-files folder in *nix machines is "/tmp" ; would there be any ...

SQLAlchemy: a better way for update with declarative?

I am a SQLAlchemy noob. Let's say I have an user table in declarative mode: class User(Base): __tablename__ = 'user' id = Column(u'id', Integer(), primary_key=True) name = Column(u'name', String(50)) When I know user's id without object loaded into session, I update such user like this: ex = update(User.__table__).where(...

Can Python directory names be keywords? E.g. 'import'?

Am I allowed to have a directory named 'import' containing Python code? Or will the import command fail to parse it as a result? Is there any way around that? ...

How do I get the path of the current executed file in python?

This may seam a newbie question but it is not. It looks that common approaches are not always working: Currently I know only two options but none of them looks to work an all cases. sys.argv[0] This means using path = os.path.abspath(os.path.dirname(sys.argv[0])) but this does not work if you are running from another python script fro...

Count the number of files in a directory using python

I need to count the number of files in a directory using Python. I guess the easiest way is len(glob.glob('*')), but I also count the directory as file. Is there any way to count only the files in a directory? ...

Regex pattern problem in python

I need to extract parts of a string using regex in Python. I'm good with basic regex but I'm terrible at lookarounds. I've shown two sample records below. The last big is always a currency field e.g. in the first one it is 4,76. In the second one it is 2,00. The second has an account number that is the pattern of \d{6}-\d{6}. Anything a...

What does binding mean exactly?

I always see people mention that "Python binding" and "C Sharp binding" etc. when I am actually using their C++ libraries. What does binding mean? If the library is written in C, and does Python binding means that they use SWIG kind of tool to mock a Python interface? Newbie in this field, and any suggestion will be welcomed. ...

Is it possible to do a wx.TextCtrl with no border?

Hello, I want to do a wx.TextCtrl with no border usign wxpython :P How can I do it? ...

What is the fastest way to send 100,000 HTTP requests in Python?

Hello, I am opening a file which has 100,000 url's. I need to send an http request to each url and print the status code. I am using Python 2.6, and so far looked at the many confusing ways Python implements threading/concurrency. I have even looked at the python concurrence library, but cannot figure out how to write this program corr...

Python integer incrementing with ++

Possible Duplicate: Python: Behaviour of increment and decrement operators I've always laughed to myself when I looked back at my VB6 days, "What modern language doesn't allow incrementing with double plus signs?": number++ To my surprise I can't find anything about this in the Python docs. Must I really subject myself to n...

can someone please show me how to convert this pseudocode to python using nested loops

N=5 For(rate=0.05, rate <= 0.15, rate = rate +0.05) For(principle=1000, principle <= 1500, principle=principle + 1000) simple = principle * (1 + rate * N) #Where N is the number of years compound = principle * (1 + rate) ^ N print simple + " " + compound End For End For ...

How to get a Device Specific UID using Python in ASE on Android?

I am working on am Android Scripting Environment (ASE) script in Python to replicate an iPhone app. I need a device UID of some sort. My thoughts where a salted MD5 hash of the MAC address or device phone number, but I can't figure out how to access either of those using the Python APIs within ASE. What can I do to create a UID in Pyth...

sqlalchemy, select all row

Hi! I try to get all row from table. in controler i try to meta.Session.query(User).all() But the result is [, ] In this table i heve 2 rows. I get this model for the table: import hashlib import sqlalchemy as sa from sqlalchemy import orm from allsun.model import meta t_user = sa.Table("users",meta.metadata,autoload=True) ...