python

Help writing the output to another file:

Hi here is the program I have: with open('C://avy.txt', "rtU") as f: columns = f.readline().strip().split(" ") numRows = 0 sums = [0] * len(columns) for line in f: # Skip empty lines if not line.strip(): continue values = line.split(" ") for i in xrange(len(values)): ...

SQLAlchemy Select statement - SQL Syntax Error

I have created a table (MySQL 5.1) from sqlalchemy import * def get(): db = create_engine('mysql://user:password@localhost/database') db.echo = True metadata = MetaData(db) feeds = Table('feeds', metadata, Column('id', Integer, primary_key=True), Column('title', String(100)), Column...

Python: How to find list intersection?

a = [1,2,3,4,5] b = [1,3,5,6] c = a and b print c actual output: [1,3,5,6] expected output: [1,3,5] How can we achieve a boolean AND operation (list intersection) on two lists? Any help will be highly appreciated. ...

How can I get better error information with try/catch in Python

Consider this try/catch block I use for checking error message stored in e. Try/Catch to get the e queryString = "SELECT * FROM benchmark WHERE NOC = 2" try: res = db.query(queryString) except SQLiteError, e: # `e` has the error info print `e` The e object here contains nothing more than the above string. When python re...

Is there a functional programming idiom for filtering a list into trues and falses?

Say you have some list L and you want to split it into two lists based on some boolean function P. That is, you want one list of all the elements l where P(l) is true and another list where P(l) is false. I can implement this in Python like so: def multifilter(pred,seq): trues,falses = [],[] for x in seq: if pred(x): ...

Replacing some part of string with Python

I have a SQL string, for example SELECT * FROM benchmark WHERE xversion = 1.0 And actually, xversion is aliased variable, and self.alias has all the alias info something like {'CompilationParameters_Family': 'chip_name', 'xversion': 'CompilationParameters_XilinxVersion', 'opt_param': .... 'chip_name': 'CompilationParameters_...

is there built-in custom numeric formating in python?

in C#, I can do (#.####), which prints up to 4 significant digits after the decimal point. 1.2 -> 1.2 1.234 -> 1.234 1.23456789 -> 1.2345 Afaik, in python, there is only the c-style %.4f which will always print to 4 decimal points padded with 0s at the end if needed. I don't want those 0s. Any suggestions for what is the cleanest way t...

In textmate, how do I reverse indent a block of selected code?

I have a block of code selected, I want to un-indent this selected code. On a pc, I would do a shift-tab and it would un-indent. ...

How to test the login func in flask?

I write this according to flaskr sample, I can login with browser,but test fails. Thanks for your help! @app.route('/login', methods=['GET', 'POST']) def login(): error = None if request.method == 'POST': username = request.form['username'] password = request.form['password'] if lib.authenticate_u...

Using webpy's web.template.render() with a relative path when deployed on Apache

Using webpy, what's the proper way to reference the templates directory for web.template.render() so that it works on both the webpy development web server and on Apache? The following code works using the development server but not when running on my Apache server. import web urls = ( '/', 'index', ) class index: def GET(self)...

pretty printer with Python?

I have a list of labels, and data as follows. ['id', 'Version', 'chip_name', 'xversion', 'device', 'opt_param', 'place_effort'] [1, 1.0, u'virtex2', u'xilinx11.5', u'xc5vlx50', u'Speed', u'High'] I need to print them into console. And for this, I'm iterating over the list, and print out each element with a tab ('\t'). But, unfortuna...

Python: intersection of lists/sets

def boolean_search_and(self, text): results = [] and_tokens = self.tokenize(text) tokencount = len(and_tokens) term1 = and_tokens[0] print ' term 1:', term1 term2 = and_tokens[1] print ' term 2:', term2 #for term in and_tokens: if term1 in self._inverted_index.keys(): resultlist1 = self._i...

Help with starting web development.

I am currently learning django, i want to be a freelancer. From what i have gathered that there is not much work in python/django on the internet and i have to get my hands on php. I want advice on what should i do? Please help me get started. Thanks in advance. ...

Why is the Python script unreliable when run from rc.local on first boot?

The script below works great when logged in as root and run from the command line, but when run at first boot using /etc/rc.local in Ubuntu 10.04, it fails about 25% of the time- the system root, mysql root and some mysql user passwords are set correctly, but one will fail with console log reporting standard mysql login error: "ERROR...

Python multiprocessing handling sessions

I have a script receiveing data from a socket, each data contains a sessionid that a have to keep track of, foreach incomming message, i'm opening a new process with the multiprocessing module, i having trouble to figure out a way to keep track of the new incoming messages having the same sessionid. For example: 100100|Hello -- (open ...

prefer windows or unix line ending for code?

I writing code that should compiled and run on both Windows and unix like Linux. I know about difference between line endings, but question is which to prefer for my code? Does it matter? I want it to be consistent - say all my code uses LF only, or is it better CRLF only? Are there critaria for comparing? If it matters mostly I care fo...

How to pass current object reference(self) to the Timer class of timeit module?

Hello everyone, I am trying to pass current object reference i.e. self to the Timer class of timeit module but somehow i am not getting it how to do it. I tried to find it in timeit documentation but i could not find it. I have attached my code with this question. from timeit import Timer import time import math class PollingDemo(T...

Get the subdictionary based on range

Hi, I have a dictionary which has coordinate pairs as a keys: i.e.: d = {(15,21): "value1", (7,45): "value2", (500,321): "value3",...} Now I need to return a sub dictionary of the elements where the keys are within a certain range: for example: the range of (6:16, 20:46) should return the following dictionary: d = {(15,21): "Value1",...

Geting cursor position in Python

Is it possible to get the overall cursor position in Windows using the standard Python libraries? ...

Python, Django, how to use getattr (or other method) to call object that has multiple attributes?

After trying to get this to work for a while and searching around I am truly stumped so am posting here... I want to make some functions in classes that I am writing for django as generic as possible so I want to use getattr to call functions such as the one below in a generic manner: the way I do it that works (non-generic manner): fr...