python

Can I skip an indeterminate amount of steps for an enclosing loop? (Python)

This is perhaps a result of bad design, but here it goes. I wasn't quite sure how to explain this problem. So I have code that iterates over a list of words. (This list does not change.) The code then parses and combines certain words together, depending on a set of criteria, storing them in a new list. The master loop, which is taking ...

PyGTK Timers and Maximized Windows

I have a timer in a PyGTK app, and it works fine --- until I maximize the window; then the timer stops firing. Has anyone experienced this? What can be done about it? Here's a code sample: import gtk, gobject files = ["C:/images/test1.jpg", "C:/images/test2.jpg", "C:/images/test3.jpg"] win = gtk.Window() win.set_title("Slideshow") ...

month name to month number and vice versa in python

I am trying to create a function that can convert a month number to an abbreviated month name or an abbreviated month name to a month number. I thought this might be a common question but I could not find it online. I was thinking about the calendar module. I see that to convert from month number to abbreviated month name you can just ...

python: appends only '0'

big_set=[] for i in results_histogram_total: big_set.append(100*(i/sum_total)) big_set returns [0,0,0,0,0,0,0,0........,0] this wrong because i checked i and it is >0 what am i doing wrong? ...

In-place dictionary inversion in Python

I need to invert a dictionary of lists, I don't know how to explain it in English exactly, so here is some code that does what I want. It just takes too much memory. def invert(oldDict): invertedDict = {} for key,valuelist in oldDict.iteritems(): for value in valuelist: try: entry = invertedDi...

Python: Unicode and ElementTree.parse

Hi, I'm trying to move to Python 2.7 and since Unicode is a Big Deal there, I'd try dealing with them with XML files and texts and parse them using the xml.etree.cElementTree library. But I ran across this error: >>> import xml.etree.cElementTree as ET >>> from io import StringIO >>> source = """\ ... <?xml version="1.0" encoding="UTF-...

Eclipse: Debug script that expects command line parameters

I have a python script I am trying to debug in eclipse. I can execute it, breakpoint all that jazz, but this specific script requires a handful of command line parameters. Is it possible to setup my dev environment in eclipse to put these parameters in? Right now my program is just generating the line to execute, like: script.py -aword...

Deleting lines in python after i write them

Ok here is my existing code: ////////////// = [] for line in datafile: splitline = line.split() for item in splitline: if not item.endswith("JAX"): if item.startswith("STF") or item.startswith("BRACKER"): //////////.append( item ) for line in ////////// print ///////////// ///////////...

Django Template Headings

Is there a way in Django templates to show a heading for a field (name of the field) only if the field has a value. For instance if one of the fields was called Year Established it might look something like this. Year Established: 1985 But if the field was empty then it wouldn't show Year Established like this. Year Estabished: I k...

Execute python script inside a python script

I have a scenario where i want to dynamically generate a python script - inside my main python script - store it as a string and then when need be, execute this dynamically generated script from my main script. Is this possible, if so how? thanks ...

py. 'decimal' mod.: why flags on context rather than numbers

Here is an example to explain what I'm on about: c = Decimal(10) / Decimal(3) c = Decimal(10) / Decimal(2) If I do this, then print c, the inexact and rounded flags are raised. Even though the result is accurate. Shouldn't flags therefore be attributes of numbers rather than the context? This problem is especially apparent when I prog...

Using mod_rewrite to hide the .py extension of a Python script accessed on a web browser

I want to hide the .py extension of a Python script loaded in a web browser and still have the script run. For example: typing url.dev/basics/pythonscript in the address bar fires pythonscript.py and shows the results in the browser window. The URL url.dev/basics/pythonscript fetches the static file /pythonscript.py The browser...

traceback.print_exc() python question

I am using the following line of code in IDLE to print out my traceback in an eception: traceback.print_exc() For some reason I get the red text error message, but then it is followed by a blue text of "None". Not sure what that None is about, any ideas? ...

python: appending values to dictionary

i have a dictionary to which i want to append to each drug a list of numbers. like this: append(0), append(1234), append(123)....... def make_drug_dictionary(data): drug_dictionary={'MORPHINE':[], 'OXYCODONE':[], 'OXYMORPHONE':[], 'METHADONE':[], 'BUPRENORPHINE':[], 'HYDROMORPHONE':[], ...

Vector algebra in functional

How to implement vector sum, using functional programming in python. This code work for n <100, but not for n > 1000. from itertools import * #n=10000 # do not try!!! n=100 twin=((i,i**2,i**3) for i in xrange(1,n+1)) def sum(x=0,y=0): return x+y def dubsum(x,y): return (reduce(sum,i) for i in izip(x,y) ) print [ i for i in r...

No module named 'XXX' error

I have a program 'a.py' which start with: import XXX The import works perfectly and a.py runs fine. Then I wrote a program 'b.py' which calls 'a.py' to run continuously. It looks like this: import os def main(): return os.system("a.py") c=main() while(c): c=main() The I got the error says that 'Import error: no module named ...

Python method overload based on argument count?

If I call QApplication's init without arguments i get TypeError: arguments did not match any overloaded call: QApplication(list-of-str): not enough arguments QApplication(list-of-str, bool): not enough arguments QApplication(list-of-str, QApplication.Type): not enough arguments QApplication(Display, int visual=0, int colormap=0...

Real world typo statistics?

Where can I find some real world typo statistics? I'm trying to match people's input text to internal objects, and people tend to make spelling mistakes. There are 2 kinds of mistakes: typos - "Helllo" instead of "Hello" / "Satudray" instead of "Saturday" etc. Spelling - "Shikago" instead of "Chicago" I use Damerau-Levenshte...

Python Delete a File?

Hi, I'm trying to delete a certain file within the directory that I'm running my python program in. def erase_custom_file(): directory=os.listdir(os.getcwd()) for somefile in directory: if somefile=="file.csv": os.remove(???) I'm not sure what my next step should be. I know that os.remove takes in a pa...

incorporating a sys.argv into a mySQL query in python

I'm writing a program that first queries with mySQL and then sorts that data. I want to be able to have a user type "python program_name.py mySQL_query" and have the program insert "mySQL_query" into the query at the beginning of the program. The issue I'm running into is the sys.argv command converts the input into a string, which mySQL...