python

Python. Transform str('+') to mathematical operation.

How transform str('+') to mathematical operation? For example: a = [0,1,2] # or a = ['0','1','2'] b = ['+','-','*'] c = int(a[0]+b[0]+a[1]) In other words, how transform str('-1*2') to int(), without for i in c: if i == '+': ... Thanks. ...

Pythonic equivalent of ./foo.py < bar.png

I've got a Python program that reads from sys.stdin, so I can call it with ./foo.py < bar.png. How do I test this code from within another Python module? That is, how do I set stdin to point to the contents of a file while running the test script? I don't want to do something like ./test.py < test.png. I don't think I can use fileinput, ...

Python indentation when adding looping statements to existing code

In Python, what do you do when you write 100 lines of code and forget to add a bunch of loop statements somewhere? I mean, if you add a while statement somewhere, you've to now indent all the lines below it. It's not like you can just put braces and be done with it. Go to every single line and add tabs/spaces. What if you were adding n...

What is the correct way to form MySQL queries in python?

I am new to python, I come here from the land of PHP. I constructed a SQL query like this in python based on my PHP knowledge and I get warnings and errors cursor_.execute("update posts set comment_count = comment_count + "+str(cursor_.rowcount)+" where ID = " + str(postid)) # rowcount here is int What is the right way to form queries...

How to keep all my django applications in specific folder

I have a Django project, let's say "project1". Typical folder structure for applications is: /project1/ /app1/ /app2/ ... __init__.py manage.py settings.py urls.py What should I do if I want to hold all of my applications in some separate folder, 'apps' for example? So th...

Is is possible to read a file from S3 in Google App Engine using boto?

I want to manipulate a pickled python object stored in S3 in Google App Engine's sandbox. I use the suggestion in boto's documentation: from boto.s3.connection import S3Connection from boto.s3.key import Key conn = S3Connection(config.key, config.secret_key) bucket = conn.get_bucket('bucketname') key = bucket.get_key("picture.jpg") fp...

Python, Ruby or Objective-C for newbies

I'm a Web designer who want to start learning a new language, I'm familiar with C#, Asp.net and C and decided to choose between Python Ruby Objective-C I need to learn this because of academic reason and I'll use this to implement some small project at university , I'm really free to choose and I have no idea about these. Here are 2...

Python replacement for PHP's header

How to send a raw http header in python just like header() in PHP ? ...

Copying a list of paths/files to a directory

I'm just doing an exercise where I have a list of files (given as absolute paths), which should be copied to a given directory if some sort of flag is set. This is my function to copy the files: def copy_to(paths, dst): if not os.path.exists(dst): os.makedirs(dst) for path in paths: shutil.copy(path, dst) However, the giv...

Prevent function overriding in python

Is there any way I can prevent a child class from overriding a method in the base class? My guess is that there is not, but I'm coming from the .net world and I'm trying to make my API as robust as possible, so any input is greatly appreciated. class Parent: def do_something(self): '''This is where some seriously important ...

Python if statement: False vs. 0.0

Is it possible to: for k,v in kwargs.items() if v == None or v == '' or v == 1.0 or v == False: del kwargs[k] without deleting the key if v == 0.0? (False seems to equal 0.0), and without deleting the keys who equal True. ...

how to use interactive icon for python application[PyQt] using py2exe

i have application called setup.py[PyQt] and i have compiled to setup.exe using py2exe suppose if i need to add a icon for the application how to do it. please help me i am newbie.. ...

In Python, how can I prohibit class inheritance?

Possible Duplicate: Final classes in Python 3.x- something Guido isn't telling me? I was watching a talk (How to design a good API and why it matters) in which it was said, literally, "design and document for inheritance, else prohibit it". The talk was using Java as an example, where there's the 'final' keyword for prohibitin...

storing sessions on server.. why?

Hi I have been using Flask for some time now and I am really enjoying the framework. One thing that I fail to understand is that in almost all other places they talk about storing the session on the server and the session id on the client, which would then identify the session.However after using flask , I dont feel the need to do so. Sa...

Dependency injection in (python) google-app-engine

I want to achieve maximum testability in my google-app-engine app which I'm writing in python. Basically what I'm doing is creating an all-purpose base handler which inherits the google.appengine.ext.webapp.RequestHandler. My base handler will expose common functionality in my app such as repository functions, a session object and the l...

Where is the best place to associate a 'form display text' with SQLAlchemy mapped property?

In django orm I can use the 'verbose_name' kwarg to set a label that will be displayed in model forms. Now I'm dynamically generating WTForms for each model in a SQLAlchemy mapped backend, but I'm not sure where to associate a display text to use in the auto generated fields for each form. For example, in django I could do this: class U...

Override reversed(...) in Python 2.5

I need a custom __reverse__ function for my class that I am deploying on App Engine, so it needs to work with Python 2.5. Is there a __future__ import or a workaround I could use? Subclassing list won't work, as I need my class to be a subclass of dict. EDIT: Using OrderedDict will not solve the problems, because the dict keys are not...

Calculating Pearson correlation and significance in Python

I am looking for a function that takes as input two lists, and returns the Pearson correlation, and the significance of the correlation. I am using Python. Thank you very much. Ariel ...

How is CPython's set() implemented?

I've seen people say that set objects in python have O(1) membership-checking. How are they implemented internally to allow this? What sort of data structure does it use? What other implications does that implementation have? Every answer here was really enlightening, but I can only accept one, so I'll go with the closest answer to my o...

Trouble with easy_install cheetah on WIndows Xp

I have installed PyQT from this URL: http://www.riverbankcomputing.co.uk/static/Downloads/PyQt4/PyQt-Py2.6-gpl-4.7.7-1.exe I have Python 2.6 installed. My OS is Windows XP SP3. I entered this into cmd.exe: easy_install cheetah This is the output: C:\Documents and Settings\All Users>easy_install cheetah Searching for cheetah Best ...