python

How to strip " (quotes) from post data

I have a textbox. When the user enters the " symbol. I had to strip that symbol before storing into the database. Please help. Any help will be appreciated Django Code: postDict = request.POST.copy() profile = quser.get_profile() profile.i_like= postDict['value'] profile=profile.save() ...

App Engine regex issues directing URL to script and handlers

I am trying to break my app into separate scripts. Part of this effort meant breaking the api calls into it's own file. However the calls to the api (like http://example.com/api/game/new no longer work). My app.yaml contains this: - url: /api.* script: api.py which seems to be redirecting properly because this configuration works: ...

Are there any basic standards and practices for making human readable code?

More specifically making HTML, Java, and python more readable? Does anyone have suggestions for this programming student? ...

How to check wether a directory is a sub directory of another directory

Hello, I like to write a template system in Python, which allows to include files. e.g. This is a template You can safely include files with safe_include`othertemplate.rst` As you know, including files might be dangerous. For example, if I use the template system in a web application which allows users to create their own t...

Interesting Python Idiom for removing the only item in a single entry list

Stumbled across this today, thought it might be worthy of discussing. Python idiom for taking the single item from a list It sometimes happens in code that I have a list, let’s call it stuff, and I know for certain that this list contains exactly one item. And I want to get this item and put it in a variable, call it...

Can some explain this strange behavior of the hypergeometric distribution in scipy?

I am running Python 2.6.5 on Mac OS X 10.6.4 (this is not the native version, I installed it myself) with Scipy 0.8.0. If I do the following: >>> from scipy.stats import hypergeom >>> hypergeom.sf(5,10,2,5) I get an IndexError. Then I do: >>> hypergeom.sf(2,10,2,2) -4.44.... I suspect the negative value is due to bad floating point...

Ubuntu packages needed to compile Python 2.7

I've tried to compile Python 2.7 on Ubuntu 10.4, but got the following error message after running make: Python build finished, but the necessary bits to build these modules were not found: _bsddb bsddb185 sunaudiodev To find the necessary bits, look in setup.py in detect_modules() for the module's name. Wha...

in numpy what is the multi dimensional equivalent of take

I have this bit of code def build_tree_base(blocks, x, y, z): indicies = [ (x ,z ,y ), (x ,z+1,y ), (x ,z ,y+1), (x ,z+1,y+1), (x+1,z ,y ), (x+1,z+1,y ), (x+1,z ,y+1), (x+1,z+1,y+1), ] children = [blocks[i] for i in indicies] return Node(children=...

Python socket programming

How can I know if a node that is being accessed using TCP socket is alive or if the connection was interrupted and other errors? Thanks! ...

Proper way to create an abstraction layer in python

I have a project I'm working on (http://github.com/lusis/vogeler). One of the goals is to provide swappable persistance and messaging backends. I think I have a workable model in place but wanted to get input from the Python crowd about best practices. You can see the new implementation here: http://github.com/lusis/vogeler/blob/master/...

Checking to see if 3 points are on the same line

Hi, I want to know a piece of a code which can actually tell me if 3 points in a 2D space are on the same line or not. A pseudocode is also sufficient but Python is better. Thanks ...

How to mock chained function calls in python?

I'm using the mock library written by Michael Foord to help with my testing on a django application. I'd like to test that I'm setting up my query properly, but I don't think I need to actually hit the database, so I'm trying to mock out the query. I can mock out the first part of the query just fine, but I am not getting the results I...

In python, how to precise a format when converting int to string?

More precisely, I want my format to add leading zeros to have string with constant length. e.g. if the constant length is set to 4 1 would be converted into "0001" 12 would be converted into "0012" 165 would be converted into "0165" I have no constraint on the behaviour when the integer is greater than what can allow the given length (9...

Is there a free (or cheap) Matlab equivalent for statistics work?

Hi, I am looking for an easy and not expensive solution to work on a large amount of records coming from sensors and save in a MYSQL database. I would like to do statistics calculation on these records and other heavy calculation. this tool that I am looking for will be used by researchers or engineers who are expert in math and stati...

with statement-Python

Hi , I am trying to understand the with statement in python.Everywhere I look it talks of opening and closing a file, and is meant to replace the try , finally block. Could someone post some other examples too.I am just trying out flask and there are with statements galore in it.Definitely request someone to provide some clarity on it ...

Python: create fixed point decimal from two 32-bit ints (one for int portion, one for decimal)

I have a 64-bit timestamp unpacked from a file with binary data, where the top 32 bits are the number of seconds and the bottom 32 bits are the fraction of the second. I'm stuck with how to actually convert the bottom 32 bits into a fraction without looping through it bit-by-bit. Any suggestions? For reference, the number 4ca1f350 948...

Flex networking: How to read multiple AMF Objects

Hi there, I'm trying to write a very plain game client to get some practice with Actionscript 3 and the Flex Framework. I have some problems with following code: private function readResponse():void { var r:ByteArray = new ByteArray(); readBytes(r); while (r.bytesAvailable != 0) { try { var d:Object = r.readO...

how to set namespace prefixes in xml.etree

I wish to set the namespace prefix in xml.etree. I found register_namespace(prefix, url) on the Web but this threw "unknown attribute". I have also tried nsmap=NSMAP but this also fails. I'd be grateful for example syntax that shows how to add specified namespace prefixes ...

how to set priority in python

Hi, all, I have to write a python script to repeat the same functions of a perl script. Inside this perl script, there is a very important step setpriority(0,0,10) || fatalError("failed to lower priority\n"); exec(@shellCmdArray) || fatalError("failed to exec()\n"); how can I realize the same effect (lower the priority of the curre...

Python in my webpage?

If I want to write a webpage in Python, where do I begin? Do I save my file as "index.py"? Can I mix Python and HTML? what does that look like? EDIT: I want to learn how the Python process works on the web. I want to know if I can put Python into an existing webpage and having it render the Python code. I'm not asking about framewor...