python

use/run python's 2to3 as or like a unittest

I have used the 2to3 utility to convert code from the command line. What I would like to do is run it basically as a unittest. Even if it tests the file rather than parts(funtions, methods...) as would be normal for a unittest. It does not need to be a unittest and I don't what to automatically convert the files I just want to monitor t...

Remove certain keys from a dictionary in python

I'm trying to construct a dictionary that contains a series of sets: {Field1:{Value1, Value2, Value3}, Field2{Value4}} The trouble is, I then wish to delete any fields from the dictionary that only have one value in the set. I have been writing code like this: for field in FieldSet: if len(FieldSet[field]) == 1: del(FieldSet[fie...

Write data to an xml file.

My aim is to write an XML file with few tags whose values are in the regional language. I'm using Python to do this and using IDLE (Pythong GUI) for programming. While I try to write the words in an xmls file it gives the following error: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in r...

How to find the file system type in python

I'm looking for a way in python to find out which type of file system is being used for a given path. I'm wanting to do this in a cross platform way. On linux I could just grab the output of df -T but that won't work on OSX or windows. ...

mongodb: insert if not exists

Hello, Every day, I receive a stock of documents (an update). What I want to do is inserting each of them if it does not exists. I also want to keep track of the first time I inserted them, and the last time I saw them in an update. I don't want to have duplicate documents. I don't want to remove a document which has previously bee...

Python: Is there a way to split a string of numbers into every 3rd number?

For example, if I have a string a=123456789876567543 could i have a list like... 123 456 789 876 567 543 ...

Detect ajax vs regular browser post in Python Pylons

How can I detect whether a form was submitted via an AJAX post or just a browser submit in Pylons? For example: if 'name' in request.POST: #Do something Would be true if 'name' was submitted via ajax or just a regular post. How can I differentiate? Thank you. ...

Generic function that accept a table and column name and returns all the primary key values that matches a filter value given

i have functions that look like this that is littered through out the code def get_M_status(S): M_id = marital.select(marital.c.marital_status_description == S).execute().fetchone() if M_id == None: print "Warning: No Marital id found for %s Marital status to Single" % S M_id = marital.select(marital.c.marital_...

python networkx

hi, i am trying to use networkx with python, when i run this program, it get this error, is there anything missing? #!/usr/bin/env python import networkx as nx import matplotlib import matplotlib.pyplot import matplotlib.pyplot as plt G=nx.Graph() G.add_node(1) G.add_nodes_from([2,3,4,5,6,7,8,9,10]) #nx.draw_graphviz(G) #nx_write_dot...

Find last match with python regular expression

I wanto to match the last occurence of a simple pattern in a string, e.g. list = re.findall(r"\w+ AAAA \w+", "foo bar AAAA foo2 AAAA bar2) print "last match: ", list[len(list)-1] however, if the string is very long, a huge list of matches is generated. Is there a more direct way to match the second occurence of "AAAA" or should I use ...

how can I convert a dictionary to a string of keyword arguments?

we can convert the dictionary to kw using **kw but if I want kw as str(kw) not str(dict), as I want a string with keyword arguments for code_generator, if I pass obj.method(name='name', test='test', relation = [('id','=',1)]) I want a function to return the string like "name='name', test='test', relation = [('id','=',1)]" ...

python-like Java IO library?

Java is not my main programming language so I might be asking the obvious. But is there a simple file-handling library in Java, like in python? For example I just want to say: File f = Open('file.txt', 'w') for(String line:f){ //do something with the line from file } Thanks! UPDATE: Well, the stackoverflow auto-accepted a wei...

Putting a simple if-then statement on one line

I'm just getting into Python and I really like the terseness of the syntax. However; is there an easier way of writing an if-then statement so it fits on one line? For example; say I have the simple test: if count == N: count = 0 else: count = N + 1 is there a simpler way of writing this? I mean, in Objective-C I would write ...

Django Python Macports

I installed Django via Macports. I wasted a lot of time on making it work. It still does not work. I would like to COMPLETELY uninstall Django (Macports) and install with the easy install (DJANGO). I would like to keep Macports and not uninstall it, because I read it SHOULD be useful. How can I achieve this? Thank you for your atten...

Overwrite method at runtime in python

I have method that run many times. I dont want to nest ifs inside but rather want to overwrite method and then run it. I know that i can overwrite class method by simple assigment, but overwriten method doesn't see private members: class X: def __init__(self, a): self.a = a self.__b = a def m(self): prin...

Incorrect XML produced by SUDS

Hi, I am trying to talk to a SOAP web service using SUDS and Python. After lots of messing around learning Python (yes I am new to this) and working out how to use SUDS I have come across a problem. The signature of the web method I am calling, according to suds, is (FWTCaseCreate){ ClassificationEventCode = None Priority = None Title...

How to make if-elif-else statement in python more space-saving?

I have a lot of if-elif-else statements in my code if message == '0' or message == '3' or message == '5' or message == '7': ... elif message == '1' or message == '2' or message == '4' or message == '6' or message == '8': ... else: ... Is it possible to format this in a more space-saving way? I tried it this way: if messag...

How to get these values with BeautifulSoup?

Hello everybody, I have this html table: <table> <tr> <td class="datax">a</td> <td class="datax">b</td> <td class="datax">c</td> <td class="datax">d</td> </tr> <tr> <td class="datax">e</td> <td class="datax">f</td> <td class="datax">g</td> <td class="datax">h</...

PyQt4: Why does Python crash on close when using QTreeWidgetItem?

I'm using Python 3.1.1 and PyQt4 (not sure how to get that version number?). Python is crashing whenever I exit my application. I've seen this before as a garbage collection issue, but this time I'm not sure how to correct the problem. This code crashes: import sys from PyQt4 import QtGui class MyWindow(QtGui.QMainWindow): def __...

Chart for deciphering terms in different programming languages

This has been bugging me every since I started to use Python - in PHP you have this ability to use a string as a key in an array. PHP calls these associative arrays. Python calls these dictionaries. Does anyone know of a premade chart that will let me see what the different terminology is in different languages. For example: PHP       ...