python

Python: asynchronous tcp socketserver

I'm looking http://docs.python.org/library/socketserver.html to try and handle asynchronous requests with the socketserver in python. At the very bottom there is an example, but it doesn't make sense. It says you use port 0 which assigns an arbitrary unused port. But how do you know what port to use for the client if they are not in the ...

raw escape in python except last char

Why in python I can't use: r"c:\" ...

Conditional counting in Python

Hello there, not sure this was asked before, but I couldn't find an obvious answer. I'm trying to count the number of elements in a list that are equal to a certain value. The problem is that these elements are not of a built-in type. So if I have class A: def __init__(self, a, b): self.a = a self.b = b stuff = [] ...

Python os.getcwd paths

I'm using os.listdir() to get all the files from a directory and dump them out to a txt file. I'm going to use the txt file to import into access to generate hyperlinks. The problem I'm having is getting the correct path. So when the script is ran it uses whatever directory you are in. Here is an example. Right now it half works, it crea...

Is there an algorithm to find unique combinations of 2 lists? 5 lists?

I have N Lists I'd like to find unique combinations of. I've written it out on my whiteboard and it all seems to have a pattern, I just haven't found it yet. I feel I can express a brute-force method and that will certainly be something I pursue. Is there an alternative? Would a different data structure (binary tree?) make a job like thi...

Python type long vs C 'long long'

I would like to represent a value as a 64bit signed long, such that values larger than (2**63)-1 are represented as negative, however Python long has infinite precision. Is there a 'quick' way for me to achieve this? ...

How to execute Javascript from Python on Windows?

Hello, how can I execute Javascript from Python on Windows? I want to get python-spidermonkey functionality. Just like this: >>> class Foo: ... def hello(self): ... print "Hello, Javascript world!" >>> cx.bind_class(Foo, bind_constructor=True) >>> cx.eval_script("var f = new Foo(); f.hello();") Hello, Javascript world! I can't ...

How to omit using python coverage lib ?

I would like to omit some module that are in some particular directory : eggs and bin coverage -r -i --omit=/usr/lib/,/usr/share/,eggs,bin Name Stmts Exec Cover ----------------------------------------------------------------------------------------- bin/test ...

Width and Precision using *

I'm learning Python from a book and came across this example: >>> '%f, %.2f, %.*f % (1/3.0, 1/3.0, 4, 1/3.0) # Result: '0.333333, 0.33, 0.3333' Don't quite understand what's happening here, especially the '4' in between. ...

How to compute laplacian of a field?

I'm trying to compute the laplacian of a 2d field A using scipy.ndimage.convolve. stencil = numpy.array([[0, 1, 0],[1, -4, 1], [0, 1, 0]]) scipy.ndimage.convolve(A, stencil, mode='wrap') This doesn't seem to give me the right answer though. Any ideas where I'm going wrong, or are there better ways of computing the laplacian in numpy? ...

Favorite 3rd-party Python Libraries?

The more I've learned about Python, The more I've heard about several external libraries that are available, and I was wondering what are some of your favorite python libraries that are availible. A few of my favorites I've discovered: Beautiful Soup - for scraping web sites PyGame - for game development MySQLdb - for interacting with...

How to avoid console window with .pyw file containing os.system call

If save my code files as .pyw, no console window appears--which is what I want--but if the code includes a call to os.system, I still get a pesky console window. I assume it's caused by the call to os.system. Is there a way to execute other files from within my .pyw script without raising the console window at all? Thanks. ...

Troubles with python list and file saving

Hi guys, I really don't know why my code is not saving for me the readings from the adc and gps receiver to the file I already open it in the first line in the code. it save only one record from both adc and gps receiver. this is my code: import MDM f = open("cord+adc.txt", 'w') def getADC(): res = MDM.send('AT#ADC?\r', 0) res =...

What are the differences in variable scoping between Python and Scheme?

Refering to Variable Scoping. I'm trying to figure out what are the differences between those 2. For example, Anonymous functions in a scheme function has access to the variables local to that function. Does python have this? Thanks! ...

To have two Pg queries in one Python method

Thank you for Denis who solves the first bug! How can you have two Postgres queries in one Python method? Example where the 2nd query is not run def comp_func(pgmasi): pgmasi.query("""CREATE TABLE courses ( course_id SERIAL PRIMARY KEY)""") pgmasi.query("""CREATE TABLE files ( # not executed for some u...

Python nested classes scope

Im trying to understand scope in nested classes in python. Here is my example code : class OuterClass: outer_var = 1 class InnerClass: inner_var = outer_var The creation of class does not complete and I get the error : <type 'exceptions.NameError'>: name 'outer_var' is not defined trying inner_var = Outerclass...

Using Jython From Eclipse Plugin

I am having a tough time getting jython to work properly when run from an Eclipse plugin. I have a simple object factory that loads a python module conforming to a Java Interface. All of this works fine in standalone mode. However, when I package this as an eclipse plugin, I get a different error based on a few variables: Given that ...

Remove a tag using BeautifulSoup but keep its contents

Currently I have code that does something like this: soup = BeautifulSoup(value) for tag in soup.findAll(True): if tag.name not in VALID_TAGS: tag.extract() soup.renderContents() Except I don't want to throw away the contents inside the invalid tag. How do I get rid of the tag but keep the contents inside ...

Python: Binding method

In following example I am trying to bind a method object via types.MethodType(...). It does not seem to work. Any suggestions? Thanks in advance. import types class Base: def payload(self, *args): print "Base:payload" class Drvd(Base): def iter(self, func): derived_func = types.MethodType(func, self, Drvd) # b...

Python, removing the \'s before getting them processed

Hello, I'm making a program that asks for a path, and Windows' paths contain backslashes, which can be interpreted as an escape sequence by python if the letter right next is the wrong one. I tried string.replace() but it doesn't work as these backslashes get transformed into escape sequences before having the replace function executed. ...