python

How to plot empirical cdf in matplotlib in Python?

How can I plot the empirical CDF of an array of numbers in matplotlib in Python? I'm looking for the cdf analog of pylab's "hist" function. One thing I can think of is: from scipy.stats import cumfreq a = array([...]) # my array of numbers num_bins = 20 b = cumfreq(a, num_bins) plt.plot(b) Is that correct though? Is there an easie...

How do I change out the underlying object inside a method call?

I thought all function and method arguments in Python were passed by reference, leading me to believe that the following code would work: class Insect: def status(self): print "i am a %s" % self class Caterpillar(Insect): def grow_up(self): self = Butterfly() # replace myself with a new object ...

to extract specific columns from a csv file and copy it to another using python

i have a csv file which is actually a matrix of 0's and 1's i need exclude those columns that have 0's and pick only those that have 1's and copy them to another csv file... using python.... i tried this: reader=csv.DictReader(open("test1.csv","r"),[]) for data in reader: if data==1: print data please ...

data.dat python

say i have the following data in a .dat file: *A-1-2-3-4*B-8-2-4*C-4-2-5-1-5 how can i print the these data like this?: A : 1 2 3 4 B : 8 2 4 C : 4 2 5 1 5 randomly print any one number for each letter. A, B and C can be any word. and the amount of the numbers can be different. i know that it has some thing to do with the * and the...

How do I make a method available as a web service in Python?

Thank you for your answer ...

What is the most pythonic way to have a generator expression executed?

More and more features of Python move to be "lazy executable", like generator expressions and other kind of iterators. Sometimes, however, I see myself wanting to roll a one liner "for" loop, just to perform some action. What would be the most pythonic thing to get the loop actually executed? For example: a = open("numbers.txt", "w") ...

Java equivalent of Python's struct.pack?

Is there any function equivalent to Python's struct.pack in Java that allows me to pack and unpack values like this? pump_on = struct.pack("IIHHI", 0, 0, 21, 96, 512) ...

How to get pynotify to display line breaks and HTML?

How can I make pynotify display line breaks and HTML in the notifications? Here is what I got: >>> import pynotify >>> n = pynotify.Notification ("This is a test.\n\nAnd this too!", "","notification-message-im") >>> n.show() Contrary to what is expected, there is no line-break between the two sentences....

Determining number of sites on a website in python

Dear Pythonistas, I have the following link: http://www.europarl.europa.eu/sides/getDoc.do?type=REPORT&mode=XML&reference=A7-2010-0001&language=EN the reference part of the url has the following information: A7 == The parliament (current is the seventh parliament, the former is A6 and so forth) 2010 == year 0001 == docu...

How do I get list of all Python types (programmatically)?

A discussion in a recent question here (what-is-the-most-pythonic-way-to-have-a-generator-expression-executed) - that min/max() need objects to be comparable - made me wonder how do i find which Python types support particular method (__cmp__, or maybe __lt__ - specifics not important). Pivotal to that seems to be ability to get list of...

Parsing LIME IPTV scripts to check for CSS syntax errors (Python, cssutils)

I'm trying to make a program where users can input a file and have the program check for CSS syntax errors. I have a few questions regarding this. I've found that cssutils is a very useful parser but, 1) How can I add in CSS properties that are not defined in the cssutils rules? 2) How can I extract out the CSS portion of the LIME scri...

urllib2 returns a different page the bowser does?

I'm trying to scrape a page (my router's admin page) but the device seems to be serving a different page to urllib2 than to my browser. has anyone found this before? How can I get around it? this the code I'm using: >>> from BeautifulSoup import BeautifulSoup >>> import urllib2 >>> page = urllib2.urlopen("http://192.168.1.254/index.cgi...

How to call C functions from python?

I have a scenario where GUI (Developed in VB) is sending commands to the target system application developed in C language (Xilinx). User on a PC sends the commands to target using GUI. But now I need to remove the GUI and want to send commands (call C functions in target system application) using Python. I found some useful info from...

Google application engine Datastore - any alternatives to aggregate functions and group by?

Hi All, As is mentioned in the doc for google app engine, it does not support group by and other aggregation functions. Is there any alternatives to implement the same functionality? I am working on a project where I need it on urgent basis, being a large database its not efficient to iterate the result set and then perform the logic. ...

Can python3.1 scripts be freezed in mac os x using cxfreeze?

I m new to this and i need to freeze python3.1 scripts so that it can be run in other machines which does'nt have python3.1. CXFREEZE is the one which supports python 3.1 as far as i know. But i could not find any thread saying that freeze is successful for python3.x. So can anybody tell me will it be done with cxfreeze or i have to ch...

Pythonic way to check if two dictionaries have the identical set of keys?

For example, let's say I have to dictionaries: d_1 = {'peter': 1, 'adam': 2, 'david': 3} and d_2 = {'peter': 14, 'adam': 44, 'david': 33, 'alan': 21} What's the cleverest way to check whether the two dictionaries contain the same set of keys? In the example above it should return False because d_2 contains the 'alan' key, which d_1...

Python: Why does os.getcwd() sometimes crash with OSError?

I have this program that at one point accesses os.getcwd(), but some times, depending on where the program has gone before getting to that line, it crashes with the message "OSError: [Errno 2] No such file or directory". I cannot figure out what i can do wrong then calling os.getcwd(). There's no arguments, and there should always be a ...

Many-to-many relationships in Google AppEngine - efficient?

Hi, I'm using Google Appengine to store a list of favorites, linking a Facebook UserID to one or more IDs from Bing. I need function calls returning the number of users who have favorited an item, and the number of times an item has been favorited (and by whom). My question is, should I resolve this relationship into two tables for eff...

python file read

def file_open(filename): fo=open(filename,'r') #fo.seek(5) fo.read(3) fo.close() file_open("file_ro.py") I expect above program to return first 3 bytes from file . But it returns nothing. When I ran these in interactive python command prompt - I get expected output! ...

How to join two generators in Python?

Dear all, I want to change the following code for directory, dirs, files in os.walk(directory_1): do_something() for directory, dirs, files in os.walk(directory_2): do_something() to this code: for directory, dirs, files in os.walk(directory_1) + os.walk(directory_2): do_something() I get the error: unsupported op...