python

Dictonaries and Lambda inside a class?

How can i do something like this: class Foo(): do_stuff = { "A" : lambda x: self.do_A(x), "B" : lambda x: self.do_B(x) } def __init__(self): print "hi" def run(self): muh = ['A', 'B', 'A'] for each in muh: self.do_stuff[each](each) def do_A(self, moo): print "A" def do_B(self, boo): print "B" if(__name__ == '__main_...

In Python, how can I efficiently manage references between script files?

I have a fair number of Python scripts that contain reusable code that are used and referenced by other Python scripts. However, these scripts tend to be scattered across different directories and I find it to be somewhat tedious to have to include (most often multiple) calls to sys.path.append on my top-level scripts. I just want to pro...

Print in terminal with colors using python ?

I want to print in the terminal with colors ? how can I do that in python ? Another questions what is the best character that when it is printed it look like a box [brick] ? I want to print colored blocks, it is part of game :) ...

Connect to Exchange mailbox with Python

I need to connect to an Exchange mailbox in a Python script, without using any profile setup on the local machine (including using Outlook). If I use win32com to create a MAPI.Session I could logon (with the Logon() method) with an existing profile, but I want to just provide a username & password. Is this possible? If so, could someo...

Is it correct to inherit from built-in classes?

Hello, I want to parse an Apache access.log file with a python program in a certain way, and though I am completely new to object-oriented programming, I want to start doing it now. I am going to create a class ApacheAccessLog, and the only thing I can imagine now, it will be doing is 'readline' method. Is it conventionally correct to i...

Can I use named groups in a Perl regex to get the results in a hash?

Is it possible to perform a named-group match in Perl's regex syntax as with Python's? I always bind the $n values to proper names after matching, so I'd find it more convenient to do it in the regex itself if it's possible. Python does it like so: >>> import re >>> regex = re.compile(r'(?P<count>\d+)') >>> match = regex.match('42') >>...

receiving data over a python socket

I'm making a program that retrieves decently large amounts of data through a python socket and then immediately disconnects when the information is finished sending. But I'm not sure how to do this All the examples on the web are of tcp clients where they have while 1: data = sock.recv(1024) But this creates a look to infinite loo...

Google AppEngine App Version

Is it possible to fetch the current application version programmatically for use in urls with far future expires header? For example: <link rel="stylesheet" href="app.js?v=1.23" /> Should be automatically updated to: <link rel="stylesheet" href="app.js?v=1.24" /> In order to do so I need to get the version. ...

Why does Excel macro work in Excel but not when called from Python?

I have an Excel macro that deletes a sheet, copies another sheet and renames it to the same name of the deleted sheet. This works fine when run from Excel, but when I run it by calling the macro from Python I get the following error message: ' Run-time error '1004': Cannot rename a sheet to the same name as another sheet, a referenced o...

py2exe setup.py with icons

how do i make icons for my exe file when compiling my python program thanks in advance ...

How do I use my icons when compiling my python program with py2exe?

I don't know what commands to enter into the setup.py file when compiling a python program to use my icons. Can anyone help me? Thanks in advance. ...

How can I improve this number2words script

import sys words = { 1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five', 6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', 10 : 'ten', 11 : 'eleven', 12 : 'twelve', 13 : 'thirteen', 14 : 'fourteen', 15 : 'fifteen', 16 : 'sixteen', 17 : 'seventeen', 18 : 'eight...

What's the simplest way to access mssql with python or ironpython?

I've got mssql 2005 running on my personal computer with a database I'd like to run some python scripts on. I'm looking for a way to do some really simple access on the data. I'd like to run some select statements, process the data and maybe have python save a text file with the results. Unfortunately, even though I know a bit about pyt...

Search in a list by predicate

Hello, I would want to do something like: >>> lst = [1, 2, 3, 4, 5] >>> lst.find(lambda x: x % 2 == 0) 2 >>> lst.findall(lambda x: x % 2 == 0) [2, 4] Is there anything even nearing such behavior in Python's standard libraries ? P.S. I know it's very easy to roll-your-own here, but I'm looking for a more standartized way ...

Using python to build web applications

This is a follow-up to two questions I asked a week or so back. The upshot of those was that I was building a prototype of an AI-based application for the web, and I wondered what language(s) to use. The conclusion seemed to be that I should go for something like python and then convert any critical bits into something faster like Java o...

Django Forms - How to Not Validate?

Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True) And I have a default value for one field but not the other. So I set it up like this: default_data = {'first_name','greg'} form1=ContactForm(default_data) However now when I go ...

Is it possible for a running python program to overwrite itself?

Is it possible for a python script to open its own source file and overwrite it? The idea was to have a very simple and very dirty way for a python script to download an update of itself so that the next time it is run it would be an updated version. ...

Search directory in SVN for files with specific file extension and copy to another folder?

I would like my python script to search through a directory in SVN, locate the files ending with a particular extension (eg. *.exe), and copy these files to a directory that has been created in my C drive. How can I do this? I'm new to Python so a detailed response and/or point in the right direction would be very much appreciated. Fol...

Python exception backtrace tells me where line ends, where does it begin?

When A Python exception is thrown by code that spans multiple lines, e.g.: myfoos = [foo("bar", "baz", "quux", i) for i in range(10)] Python will report the line number of the last line, and will show the code fragment from that line: Traceback (most recent call last): File "test....

How do I split a huge text file in python

I have a huge text file (~1GB) and sadly the text editor I use won't read such a large file. However, if I can just split it into two or three parts I'll be fine, so, as an exercise I wanted to write a program in python to do it. What I think I want the program to do is to find the size of a file, divide that number into parts, and f...