python

python target string key count invalid syntax

Hi Why am i getting an "invalid syntax" when i run below code. Python 2.7 from string import * def countSubStringMatch(target,key): counter=0 fsi=0 #fsi=find string index while fsi<len(target): fsi=dna.find(key,fsi) if fsi!=-1: counter+=1 else: counter=0 fsi=...

Check if all values of iterable are zero

Is there a good, succinct/built-in way to see if all the values in an iterable are zeros? Right now I am using all() with a little list comprehension, but (to me) it seems like there should be a more expressive method. I'd view this as somewhat equivalent to a memcmp() in C. values = (0, 0, 0, 0, 0) # Test if all items in values tuple...

When should I submit my Django form's results?

The contents of my form must be submitted to another application server for validation and execution (specifically, I call a RESTful web service with the posted values in the form). The service will either return a 200 SUCCESS or a 400/409 error with a body that describes the exact field errors. When should I do this submission? Should ...

Python variable resolving

Given the following code: a = 0 def foo(): # global a a += 1 foo() When run, Python complains: UnboundLocalError: local variable 'a' referenced before assignment However, when it's a dictionary... a = {} def foo(): a['bar'] = 0 foo() The thing runs just fine... Anyone know why we can reference a in the 2nd chunk of code, b...

Python - why is it not reading my variables??

I'm a python newbie and I don't understand why it won't read my IP and ADDR variables in the function dns.zone.query(IP, ADDR)??? import dns.query import dns.zone import sys IP = sys.stdin.readline() ADDR = sys.stdin.readline() z = dns.zone.from_xfr(dns.query.xfr(IP , ADDR)) names = z.nodes.keys() names.sort() for n in names: pri...

String Division in Python

Hello, I have a list of strings that all follow a format of parts of the name divided by underscores. Here is the format: string="somethingX_somethingY_one_two" What I want to know how to do it extract "one_two" from each string in the list and rebuild the list so that each entry only has "somethingX_somethingY". I know that in C, ther...

Pythonic way to compare two lists and print the unmatched items?

I have two Python lists of dictionaries, entries9 and entries10. I want to compare the items and write joint items to a new list called joint_items. I also want to save the unmatched items to two new lists, unmatched_items_9 and unmatched_items_10. This is my code. Getting the joint_items and unmatched_items_9 (in the outer list) is qui...

Which key-value store has decent twisted API (nonblocking) ?

I need a reliable K-V storage to be run in network. Main requirements: Network connectivity has nonblocking twisted API be reliable, production ready. No data loss write performance is more important than read performance support for distributed operation and failover would be great (So I just specify list of nodes) java/ruby/erlang AP...

Finding all sentences from list of keywords to dict

I have list of possible words to make anagram of the given words. Each string of list is key to dictionary and has value of one or more words. Which is the best (fastest, pythonic) way to make all possible sentences in the order of the keys from the words in each list of the corresponding keys in the dictionary. Lists have variable numbe...

Pydev can't find matplotlib modules

I just installed matplotlib on my windows 7 Python 2.6.5 machine with the win32 installer . I've tried some examples from the matplotlib site to test the installation, under Idle everthing works fine but Pydev 1.9 (Eclipse 3.6) cant find any of the sub modules. e.g import matplotlib doesn't cause any errors but from matplotlib.path ...

GitPython get commit for a file

I'm looking to use gitpython to get data on a tree.. to list when the file was commit and the log given.. as far as I have gotten is from git import * repo = get_repo("/path/to/git/repo") for item in repo.tree().items(): print item[1] That just lists things like <git.Tree "ac1dcd90a3e9e0c0359626f222b99c1df1f11175"> <git.Blob "764...

redirecting standard output to print messages in gui instead of terminal

#!/usr/bin/python import wx import os import sys class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(480, 400)) self.panel = MyPanel(self, -1) self.Centre() self.Show(True) setstd() print 'test' """ syncall()...

Problem with 2D interpolation in SciPy, non-rectangular grid

Hi all, I've been trying to use scipy.interpolate.bisplrep() and scipy.interpolate.interp2d() to find interpolants for data on my (218x135) 2D spherical-polar grid. To these I pass 2D arrays, X and Y, of the Cartesian positions of my grid nodes. I keep getting errors like the following (for linear interp. with interp2d): "Warning: ...

PHP returning content-length of 0 to python's urllib

My code fetches CSV data from a PHP page using httplib. When I open the page in Firefox or Chrome, the data displays just fine. However, when I try to fetch it with my python code, I get a header with content-length: 0 and no data. This page is the only one that does this - in another page in the same directory, the python httplib fetchi...

Python + MySQLDB Batch Insert/Update command for two of the same databases.

Hi all, I'm working with two databases, a local version and the version on the server. The server is the most up to date version and instead of recopying all values on all tables from the server to my local version, I would like to enter each table and only insert/update the values that have changed, from server, and copy those value...

Django extra() and None values

Hi, I have a query in extra() that sometimes returns no rows. In that case, I need that variable assigned to be None for sorting purposes. Is there a way to do this? ...

Why do some Django ORM queries end abruptly with the message "Killed"?

Sometimes, when fetching data from the database either through the python shell or through a python script, the python process dies, and one single word is printed to the terminal: Killed That's literally all it says. It only happens with certain scripts, but it always happens for those scripts. It consistently happens with this one sin...

Can I add "Smartypants" to restructuredText?

I use restructuredText, and I like what smartypants does for Markdown. Is there a way to enable the same thing for restructuredText? ...

difference between default and optional arguments

okay code: #!/usr/bin/python import wx import sys class XPinst(wx.App): def __init__(self, redirect=False, filename=None): wx.App.__init__(self, redirect, filename) def OnInit(self): frame = wx.Frame(None, -1, title='Redirect Test', size=(620,450), style=wx.STAY_ON_TOP|wx.DEFAULT_FRAME_STYLE) panel = wx...

Determine local connectivity in python

How can I tell if my client system has a network connection using python? I can assume the client connected with DHCP. I can't use lists of known reliable sites to ping to test the connection, as it needs to work in isolated networks as well as open ones. I thought about fetching the local ip (should work, so long as it doesn't only ret...