python

Python to C# Code Explanation

My ultimate aim is to convert the below code in python to C#, but I'd like to do it my self by learning the python syntax. I understand that the code is recursive. The code produces polynomials of degree n with k variables. More specifically the list of exponents for each variable. def multichoose(n,k): if k < 0 or n < 0: return "...

Python - Rpy2 - Can't import a bunch of packages

I've just started using rpy2 with Python. I've installed it and am able to do basic things, like call R's plot function from inside Python. For everything I've done, I've used import calls like: import rpy2 import rpy2.robjects From robjects I can do most things I want to do. However, if I want to use things like ggplot2, I am unab...

Is it possible to narrow down the generated query in SQLAlchemy if it was created via query_property?

I have another quick question about SQLAlchemy. If I have added a query_property [1] field in my SQLAlchemy Table class, is it possible to narrow down the SELECTed fields? Here is what I mean. Suppose my class Comments has this: class Comments: query = Session.query_property() ... Then if I do the following: >>> print Ses...

Python, len, and size of ints

So, cPython (2.4) has some interesting behaviour when the length of something gets near to 1<<32 (the size of an int). r = xrange(1<<30) assert len(r) == 1<<30 is fine, but: r = xrange(1<<32) assert len(r) == 1<<32 ValueError: xrange object size cannot be reported`__len__() should return 0 <= outcome Alex's wowrange has this beha...

Pylons: free module-level variables?

Not even sure if module-level is correct here, but... I have a Pylons project and within the model component I have a global variable, doc, in __init__.py that I want to use from different Query objects. (doc is a Document handle on an XML file that I am using as a fake DB.) My question is, when does __init__.py's scope end? Currently I...

Code translation, from Python to Ruby

Hi, I would like to convert a few Python lines on Ruby, from this excellent article signed by Thomas Guest at: http://wordaligned.org/articles/drawing-chess-positions (note: I'm a really bigger Python noob) Here is a copy of the original Python version: def expand_blanks(fen): '''Expand the digits in an FEN string into spaces ...

Python: xml.dom.minidom empty nodeValue nonempty toxml() value

I have a line that gets the nodeValue of a Node: parent.getElementsByTagName("Url")[0].nodeValue that returns nothing: <br/> When I do: parent.getElementsByTagName("Url")[0].toxml() it returns: < Url>www.something.com< /Url> I am not sure what is going on here. Another data point: when I do nodeName instead of nodeValue it re...

Clojure Jython interop

Hi, I was wondering if anyone has tried somehow calling Jython functions from within Clojure, and how you went about doing this if so. I have not used Jython, but I would imagine the Jython interpreter can be invoked in the same way as any other java code, and Python programs can be run within it. However I wonder if it would be possi...

gtk.Builder() and multiple glade files breaks

Hi I have a glade gui, and I want to insert another object using a glade file as well. When I do it as bellow (this is essentially what I am doing) the whole app hangs and the self.show() and maxes out the CPU at 100%. If I replace the first line of one's init() with self.builder = gtk.Builder() then the app runs, I can set widgets,...

IP address of client in Python SimpleXMLRPCServer ?

I have a SimpleXMLRPCServer server (Python). How can I get the IP address of the client in the request handler? This information appears in the log. However, I am not sure how to access this information from within the request handler. ...

Trying to run a Python program but all that opens is a DOS Window

I just completed a program that should (hopefully) play a GUI Tetris program. I've cleaned up all the syntax errors, but now when I double-click, or go through the Command Prompt to run the program a DOS window pops up for less than a second and disappears without ever running the program. What's going on? Heres a link to the code at...

Dynamically assign special methods to objects but not classes in Python

I would like to do the following: class A(object): pass a = A() a.__int__ = lambda self: 3 i = int(a) Unfortunately, this throws: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: int() argument must be a string or a number, not 'A' This only seems to work if I assign the "special" method to the ...

Mysql + SQLAlchemy + Pylons Issue

I'm trying to setup my models in Pylons using a MySQL backend and I keep getting the following: Traceback (most recent call last): File "/usr/local/bin/paster", line 8, in <module> load_entry_point('PasteScript==1.7.3', 'console_scripts', 'paster')() File "/usr/local/lib/python2.6/dist-packages/PasteScript-1.7.3-py2.6.egg/pa...

How can I set up Celery to call a custom initialization function before running my tasks?

I have a Django project and I'm trying to use Celery to submit tasks for background processing ( http://ask.github.com/celery/introduction.html ). Celery integrates well with Django and I've been able to submit my custom tasks and get back results. The only problem is that I can't find a sane way of performing custom initialization in t...

Classes, methods, and polymorphism in Python

I made a module prototype with the aim of building complex timer schedules in python. The class prototypes emulate Timer objects, each with their waiting times, Repeat objects that group Timer and other Repeat objects, and a Schedule class, just for holding a whole construction or Timers and Repeat instances. The construction can be as c...

copying functions located in instances

Here's some (simplified) code for what I'm trying to do: class a: pass class b: def printSelf(self): print self instOfA = a() instOfB = b() instOfA.printSelf = instOfB.printSelf instOfA.printSelf() <__main__.b instance at 0x0295D238> When I call instOfA.printSelf(), it prints self as being instOfB. But I want sel...

pyinstaller exe's not dying after sys.exit()

I have a cherrypy app compiled with pyinstaller. One function does the following: cherrypy.engine.stop() sys.exit() the cherrypy engine stops without problem, but the process doesn't actually die and I can't figure out why. ...

django error in my code ,what does i do

from django.core.management import setup_environ from register2 import settings setup_environ(settings) from django import forms from django.contrib.auth.forms import AuthenticationForm from django.utils.translation import ugettext_lazy as _ class AuthenticationRememberMeForm ( AuthenticationForm ): """ Subcl...

splitting a list of arbitrary size into only roughly N-equal parts

hi all, what is the best way to divide a list into roughly equal parts? for example, if I have a list with 54 elements and i want to split it into 3 roughly equal parts? I'd like the parts to be as even as possible, hopefully assigning the elements that do not fit in a way that gives the most equal parts. to give a concrete case, if th...

put stockprices into groups when they are within 0.5% of each other

Hi: Thanks for the answers, I have not used StackOverflow before so I was suprised by the number of answers and the speed of them - its fantastic. I have not been through the answers properly yet, but thought I should add some information to the problem specification. See the image below. I can't post an image in this because i don't ...