python

python regular expression to validate types of strings

I want to do the following with python: Validate if a UTF8 string is an integer. Validate if a UTF8 string is a float. Validate if a UTF8 string is of length(1-255). Validate if a UTF8 string is a valid date. I'm totally new to python and I believe this should be done with regular expression, except maybe for the last one. Your help ...

Why am I leaking memory with this python loop?

I am writing a custom file system crawler, which gets passed millions of globs to process through sys.stdin. I'm finding that when running the script, its memory usage increases massively over time and the whole thing crawls practically to a halt. I've written a minimal case below which shows the problem. Am I doing something wrong, or h...

Python database access using single file/already build file

Hi everyone! I have to save some data into MySQL from Python. I have tried MySQLdb, but it needs to be built. Build fails on my Mac; moreover, I need to have one file to copy to server. I don't have access to install anything. Can you recommend me any solution, please! Even where I can find MySQLdb build for specific platforms. Thank...

Decoding tcp packets using python

Hello I am trying to decode data received over a tcp connection. The packets are small, no more than 100 bytes. However when there is a lot of them I receive some of the the packets joined together. Is there a way to prevent this. I am using python I have tried to separate the packets, my source is below. The packets start with STX byt...

EventHandler, event, delegate based programming in Python any example would appreciate?

Basically I'm a C# developer, I know the way C# does, EventHandler, delegate, even... but whats the best way to implement it on Python. ...

How to receive uploaded file from the inlined form in django?

I'm trying to create a "cool" image uploading interface in django admin panel. The problem is that I'm using inline interface for uploading image instances, but I'm using jQuery Ajax form to upload images, so there were another form I need to create for uploading. And now django view function can't receive request.FILES from this form, b...

Python Loop Early

How do I loop early in python? for a in b: if criteria in list1: print "oh no" #Force loop i.e. force next iteration without going on someList.append(a) Also, in java you can break out of a loop, is there an equivalent in Python? ...

draw hebrew text to image (python,PIL)

Possible Duplicate: Drawing Hebrew text to and image using Image module (python) this is the code: import Image import ImageDraw import ImageFont import _imaging a=u'1אריאל' #u'..' converts to unicode font = ImageFont.truetype('c:/test/Miriam.ttf',24) #standard hebrew font, this line throws the error im=Image.new('RGB',(200,...

How to get the most represented object from an array

Hi, I have an array with some objects, and there are several objects that are alike. E.g: fruit = [apple, orange, apple, banana, banana, orange, apple, apple] What is the most efficient way to get the most represented object from this array? In this case it would be "apple" but how would you go out and calculate that in an efficient wa...

Python thinks a 3000-line text file is one line long?

I have a very long text file that I'm trying to process using Python. However, the following code: for line in open('textbase.txt', 'r'): print 'hello world' produces only the following output: hello world It's as though Python thinks the file is only one line long, though it is many thousands of lines long, when viewed in a t...

How do I detect a lost client connection in a server that inherits from pb.Root?

For example, I have a client that connects to the server with the following: class MyClientFactory(pb.PBClientFactory, ReconnectingClientFactory): def __init__(self): pb.PBClientFactory.__init__(self) self.ipaddress = None def clientConnectionMade(self, broker): log.msg('Started to connect.') pb....

Even numbers in Python

Hello, Does anyone know if Python has an in-built function to work to print out even values. Like range() for example. Thanks ...

python : best way to test is a variable is a list or tuple

In python, what's the best way to test if a variable contains a list or a tuple? (ie. a collection) Is isinstance as evil as suggested here? http://www.canonical.org/~kragen/isinstance/ ...

How to pass data to another function from a class (in HTMLParser)?

I'm beginning to learn python. My python version is 3.1 I've never learnt OOP before, so I'm confused by the HTMLParser. from html.parser import HTMLParser class parser(HTMLParser): def handle_data(self, data): print(data) p = parser() page = """<html><h1>title</h1><p>I'm a paragraph!</p></html>""" p.feed(page) I'll get this: ...

Suggested GA operators for a TSP problem?

I'm building a genetic algorithm to tackle the traveling salesman problem. Unfortunately, I hit peaks that can sustain for over a thousand generations before mutating out of them and getting better results. What crossover and mutation operators generally do well in this case? ...

Writing Python packages with multiple C modules

Hello, I am writing a Python C extension which uses the pygame C API. So far so good. Now I wonder how I organize my source code so that I can have multiple submodules in the package. All the tutorials out there focus on one .c file extensions. I tried looking at some projects setup.py files but they blew my mind with complexity, and I ...

Python / Regex: exclude everything except one thing

Suppose I have these strings: a = "hello" b = "-hello" c = "-" d = "hell-o" e = " - " How do I match only the -(String C)? I've tried a if "-" in something but obviously that isn't correct. Could someone please advise? Let's say we put these strings into a list, looped through and all I wanted to extract was C. How would I do th...

Pylons and Flex 3

Hi all, Has anyone used Python/Pylons as the server backend for a Flex 3 application? Does anyone have any thoughts on how well this would work? I read Bruce Eckel's article about tying Flex 3 to Twisted, and I've done Twisted programming, but for just a web service I think Pylons is simpler to use. Thanks in advance, Doug ...

Images caching in browser - app-engine-patch aplication

Hi, I have a little problem with caching the images in the browser for my app-engine aplication I`m sending last-modified, expires and cache-control headers but image is loaded from the server every time. Here is the header part of the code: response['Content-Type'] = 'image/jpg' response['Last-Modified'] = current_time.strftime('%a, ...

installing _imagingft

It seem that some of my problems with working with fonts, are due to the luck of _imagingft module. how can I install it? what are the steps ...