python

Which dynamic language can easily use libraries from other languages?

Tell why you think Python, Perl, Ruby, etc is easiest for plugging in modules from other languages with minimal thought. To clarify, an example: I want to write business logic in Python, but use functionality that conveniently exists as a Perl module. In other words, which language "just works" with the most modules? ...

How can I access the "through" object of a Django ManyToManyField?

I have the following models in my Django app. How can I from the Team model find all the User objects who have accepted as True in the Membership model? I know I need to use Team.objects.filter(), but I'm not sure how to check the value of the accepted field. from django.contrib.auth.models import User class Team(models.Model): memb...

Which implementation of OrderedDict should be used in python2.6?

As some of you may know in python2.7/3.2 we'll get OrderedDict with PEP372 however one of the reason the PEP existed was because everyone did their own implementation and they were all sightly incompatible. So which one of the 8 current implementations link text is backwards compatible with the 2.7 odict from python 2.7 in a way we can...

Looping an executable to get the result from Python script

In my python script, I need to call within a for loop an executable, and waiting for that executable to write the result on the "output.xml". How do I manage to use wait() & how do I know when one of my executable is finished generating the result to get the result? How do I close that process and open a new one to call again the execut...

Is str.replace(..).replace(..) ad nauseam a standard idiom in Python?

For instance, say I wanted a function to escape a string for use in HTML (as in Django's escape filter): def escape(string): """ Returns the given string with ampersands, quotes and angle brackets encoded. """ return string.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').repla...

How do I override a parent class's functions in python?

I have a private method def __pickSide(self): in a parent class that I would like to override in the child class. However, the child class still calls the inherited def __pickSide(self):. How can I override the function? The child class's function name is exactly the same as the parent's function name. ...

PyLab - changing text color and background fill color of text box

Hey all, I'm using PyLab to make some graphs in Python. I want to make a text box that is colored magenta with black text, but cannot get the text to be black. text(x, y, 'Summary', backgroundcolor = 'm', color = 'k') This gives me a magenta background and then text that is almost just as pink. Any ideas what I'm doing wrong? Many th...

Prevent python from printing newline

I have this code in Python inputted = input("Enter in something: ") print("Input is {0}, including the return".format(inputted)) that outputs Enter in something: something Input is something , including the return I am not sure what is happening; if I use variables that don't depend on user input, I do not get the newline after for...

How to optimize my PageRank calculation?

In the book Programming Collective Intelligence I found the following function to compute the PageRank: def calculatepagerank(self,iterations=20): # clear out the current PageRank tables self.con.execute("drop table if exists pagerank") self.con.execute("create table pagerank(urlid primary key,score)") self.con.execute("...

How to display a QGraphicsScene?

I've got the following code and I'm not sure how to add the QGraphicsScene to my layout.. class MainForm(QDialog): def __init__(self, parent=None): super(MainForm, self).__init__(parent) self.scene = QGraphicsScene(self) self.scene.setSceneRect(0, 0, 500, 500) self.view = QGraphicsView() self....

Is it possible to plot implicit equations using Matplotlib?

Hello all - many thanks again to people who have kindly offered help (especially to Mark for his outstanding response to my previous question). I would like to plot implicit equations (of the form f(x,y)=g(x,y) eg. x^y=y^x) in Matplotlib. Is this possible? All the best, Geddes ...

what are the advantages of C# over Python

I like Python mostly for the great portability and the ease of coding, but I was wondering, what are some of the advantages that C# has over Python? The reason I ask is that one of my friends runs a private server for an online game (UO), and he offered to make me a dev if I wanted, but the software for the server is all written in C#. ...

From web programming to embedded systems (Windows CE), best learning path?

(tl;dr: What's the best learning path to go from PHP/Python web development to application development for Windows CE?) I'm a self-taught programmer, and I tend to learn by starting from the goal and researching backwards until I get to a point I know. However, I now have a project involving barcode scanners on a pocket-pc, and I'm at a...

What's the Ruby equivalent of Python's output[:-1]?

In Python, if I want to get the first n characters of a string minus the last character, I do: output = 'stackoverflow' print output[:-1] What's the Ruby equivalent? ...

Modify Django settings variables in a middleware

I set a variable MAX_REQUEST = 100 in settings.py I write a middleware which may lower this value for request origining from a proxy ip address by the following code: settings.MAX_REQUEST = 10 However, looks like the above modification affects all legitimate users. Is it normal? ...

The problem with installing PIL using virtualenv or buildout.

When I install PIL using easy_install or buildout it installs in such way, that I must do 'import Image', not 'from PIL import Image'. However, if I do "apt-get install python-imaging" or use "pip -E test_pil install PIL", all work fine. Here are examples of how I trying to install PIL using virtualenv: # virtualenv --no-site-packages...

Learning Python and trying to get first two letters and last two letters of a string.

Here's my code: # B. both_ends # Given a string s, return a string made of the first 2 # and the last 2 chars of the original string, # so 'spring' yields 'spng'. However, if the string length # is less than 2, return instead the empty string. def both_ends(s): if len(s) <= 2: return "" else: return s[0] + s[1] + s[len(s)-2]...

pdb show different variable values than print statements

Hi, everyone. I am debugging a python module with homemade c extensions. The output seems correct when I print it with 'p' in pdb. But if I use a normal print statement or pickle it, the output is wrong. What could be causing pdb to show different values than normal execution? I can even step to the print statement in debug mode, an...

Calling gawk from Python

I am trying to call gawk (the GNU implementation of AWK) from Python in this manner. import os import string import codecs ligand_file=open( "2WTKA_ab.txt", "r" ) #Open the receptor.txt file ligand_lines=ligand_file.readlines() # Read all the lines into the array ligand_lines=map( string.strip, ligand_lines ) ligand_file.close() for ...

Pythonic way of adding "ly" to end of string if it ends in "ing"?

This is my first effort on solving the exercise. I gotta say, I'm kind of liking Python. :D # D. verbing # Given a string, if its length is at least 3, # add 'ing' to its end. # Unless it already ends in 'ing', in which case # add 'ly' instead. # If the string length is less than 3, leave it unchanged. # Return the resulting string. def...