python

Finding maximum of a list of lists by sum of elements in Python

What's the idiomatic way to do maximumBy (higher order function taking a comparison function for the test), on a list of lists, where the comparison we want to make is the sum of the list, in Python? Here's a Haskell implementation and example output: > maximumBy (compare `on` sum) [[1,2,3],[4,5,6],[1,3,5]] > [4,5,6] And implementati...

Likelihood of IOError during print vs. write

I recently encountered an IOError writing to a file on NFS. There wasn't a disk space or permission issue, so I assume this was just a network hiccup. The obvious solution is to wrap the write in a try-except, but I was curious whether the implementation of print and write in Python make either of the following more or less likely to rai...

Jythonc missing

Hi Guys, I just installed Jython 2.5.1. I want to convert my Python file into Java class file and it is instructed on the website to use the jythonc command-line tool but I can't find it. Does anyone know where I could find it? Basically what i was trying to accomplish is to get my Python code running client-side in a browser and the b...

how to create http headers from scratch

So, I made a simple socket server using python. And now I'm trying to structure a proper http response. However, I can't seem to find any sort of tutorial or spec that discusses how to format http responses. Could someone point me to the right place? ...

[Python/Tkinter] Grid within a frame?

Is it possible to place a grid of buttons in Tkinter inside another frame? I'm wanting to create a tic-tac-toe like game and want to use the grid feature to put gamesquares (that will be buttons). However, I'd like to have other stuff in the GUI other than just the game board so it's not ideal to just have everything in the one grid. ...

making urllib request in Python from the client side

Hi Guys, I've written a Python application that makes web requests using the urllib2 library after which it scrapes the data. I could deploy this as a web application which means all urllib2 requests go through my web-server. This leads to the danger of the server's IP being banned due to the high number of web requests for many users. ...

Python New-style Classes and the Super Function

This is not the result I expect to see: class A(dict): def __init__(self, *args, **kwargs): self['args'] = args self['kwargs'] = kwargs class B(A): def __init__(self, *args, **kwargs): super(B, self).__init__(args, kwargs) print 'Instance A:', A('monkey', banana=True) #Instance A: {'args': ('monkey',), ...

How do I use Python to prevent a file from being copied?

I have a file that I need to "protect" so that it cannot be copied! I am using Python on Windows XP. I think it may just be changing file permissions?? ...

How to print the sign + of a digit for positive numbers in Python.

Is there a better way to print the + sign of a digit on positive numbers? integer1 = 10 integer2 = 5 sign = '' total = integer1-integer2 if total > 0: sign = '+' print 'Total:'+sign+str(total) 0 should return 0 without +. ...

I am using Python on Windows. How do I delete my script after it is run?

I have written a Python script and compiled it into a MS Windows EXE file. I can modify the code, but how do I make it remove itself after running? ...

Webdriver with Python

I had written a scipt in Java with Webdriver and it worked fine and below is the code for the sample import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriverBackedSelenium; import org.openqa.selenium.firefox.Fire...

How can I handle dynamic calculated attributes in a model in Django?

In Django I calculate the breadcrumb (a list of fathers) for an geographical object. Since it is not going to change very often, I am thinking of pre calculating it once the object is saved or initialized. 1.) What would be better? Which solution would have a better performance? To calculate it at _init_ or to calculate it when the obje...

wxpython printing html

How it is possible wxHtmlEasyPrinting to force to work with utf-8 encoding? # -*- coding: utf-8 -*- import wx import wx.html as html import wx.lib.printout as printer class MyFrame(wx.Frame): def __init__(self, parent, title): wx.Frame.__init__(self, parent, -1, title, size=(450, 500)) wind = wx.SplitterWindow(se...

Tweepy + App Engine Example OAuth Help

Hi I am trying to follow the Tweepy App Engine OAuth Example app in my app but am running into trouble. Here is a link to the tweepy example code: http://github.com/joshthecoder/tweepy-examples Specifically look at: http://github.com/joshthecoder/tweepy-examples/blob/master/appengine/oauth_example/handlers.py Here is the relevant snip...

How to replace only part of the match with python re.sub

I need to match two cases by one reg expression and do replacement 'long.file.name.jpg' -> 'long.file.name_suff.jpg' 'long.file.name_a.jpg' -> 'long.file.name_suff.jpg' I'm trying to do the following re.sub('(\_a)?\.[^\.]*$' , '_suff.',"long.file.name.jpg") But this is cut the extension '.jpg' and I'm getting long.file.name_suff. ...

Python: How to write data in file in specific format?

i have an array called MAC1_Val: MAC1_Val array([ 1.00000000e+00, -1.00000000e+01, -2.06306600e+02, 2.22635749e+02, 1.00000000e+00, 1.00000000e+01, 1.00000000e+01, -2.06306600e+02, 2.22635749e+02, 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...

Process xml-like log file queue

Hi all, first of all: I'm not a programmer, never was, although had learn a lot during my professional carreer as a support consultant. Now my task is to process - and create some statistics about a constantly written and rapidly growing XML like log file. It's not valid XML, because it does not have a proper <root> element, e.g. the l...

Google App Engine (GAE) cron url: url?keyword=abc

Hi I would like to run cron job on GAE and using python In the cron.yaml, how do i insert the "url" field if consist of some "get" info The: description: whatever url: url?keyword=a schedule: every day 15:00 give me error when deploy ...

Is False == 0 and True == 1 in Python an implementation detail or guaranteed by the language?

Is it guaranteed that False == 0 and True == 1, in Python? For instance, is it in any way guaranteed that the following code will always produce the same results, whatever the version of Python (existing and in the foreseeable future)? 0 == False # True 1 == True # True ['zero', 'one'][False] # is 'zero' Any reference to the offi...

How to pdb Python code with input?

I'm debugging Python code with pdb. The code need input from stdin, like: python -m pdb foo.py < bar.in Then the pdb will accept the bar.in as commands. How to tell pdb that the input is for foo.py and not for pdb? ...