python

How Can I Make This Python Code More Usable And Readable?

Beginner in python, but been programming for about 5 years now. I suspect I have a lot to learn about doing things the object oriented way, but I know the basics. I planned on programming a calculator that shows it's work for the challenge and knowledge i'll gain from it. I just started and this is what i've got, and it just looks really...

Django auto_now and auto_now_add

For Django 1.1. I have this in my models.py: class User(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) When updating a row I get : [Sun Nov 15 02:18:12 2009] [error] /home/ptarjan/projects/twitter-meme/django/db/backends/mysql/base.py:84: Warning: Column 'crea...

What's wrong with this program?

def short_distance(origins,(x,y),gap): for (i,j) in origins.spilt(“ ”): h=[] h.append(float(math.sqrt ((i-x)*(i-x)+(j-y)*(j-y)))) for n in h: if not gap < n: print 0 if gap < n : print n ...

web2py - enforce encoding

how to enforce encoding of a generated csv report to make it "utf8" Thanks in advance ...

SQLAlchemy 0.5.5 - defining table schema

I used sqlautocode to generate a model.py for an existing MySQL database and here's a table example: fusion_articles = Table('fusion_articles', metadata, Column(u'article_id', Integer(), primary_key=True, nullable=False), Column(u'article_cat', Integer(), primary_key=False, nullable=False), Column(u'article_...

File open error by using codec utf-8 in python

I execute following code on windows xp and python 2.6.4 But it show IOError. How to open file whose name has utf-8 codec. >>> open( unicode('한글.txt', 'euc-kr').encode('utf-8') ) Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> open( unicode('한글.txt', 'euc-kr').encode('utf-8') ) IOError: [Errno 22] inva...

Django: debugging entries just disapearing

So, I'm pulling my hair out here, and maybe someone has an insight. I have a cronjob that loops over all my Link objects, does some stuff, might change properties on the object and does a save(). That's it. Every so often (around once an hour), one of my rows just disappears. Poof. Nothing in the logs. So, I'm trying to add debugging...

How should I create a web interface for my application?

First, a little background: I have written a little application in python with SQLAlchemy, which is roughly an improved RSS reader: it selects links that should interest the user, and shows them to him. I already have a very simple command-line interface, and I envision a variety of interfaces: web, instant-messaging, desktop... For now...

The Matlab equivalent of Python's "None"

Is there a keyword in Matlab that is roughly equivalent to None in python? I am trying to use it to mark an optional argument to a function. I am translating the following Python code def f(x,y=None): if y == None: return g(x) else: return h(x,y) into Matlab function rtrn = f(x,y) if y == []: rtrn = g(x);...

Safely executing user-submitted python code on the server

I am looking into starting a project which involves executing python code that the user enters via a HTML form. I know this can be potentially lethal (exec), but I have seen it done successfully in at least one instance. I sent an email off to the developers of the Python Challenge and I was told they are using a solution they came up ...

QGraphicsScene and positioning

How to put QGraphicsScene's (0,0) to top-left corner of QGraphicsView? ...

Dict has key from list

How can I determine if any of the list elements are a key to a dict? The straight forward way is, for i in myList: if i in myDict: return True return False but is there a faster / more concise way? ...

change 2 byte in a string

Hi All, I'd like to change 2 byte in a string like this: "ABCDEFGHIJKLMNOPQRSTUVWXYZ" Let's imagine I want to replace 'RS' by 11, I know how to do it with one byte like [:], but for 2 or more in the middle of the string ? Thanks ! ...

How does the Python's range function work?

Hi,all,I am not really understand for loop in the following,can anyone give some tips?thanks in advance for i in range(5): print i then it gives 0,1,2,3,4 is that python assign 0,1,2,3,4 to i at the same time? however if i wrote: for i in range(5): a=i+1 then I call a, it gives 5 only, but if i add ''print a'' it gives ...

httplib2, how to set more than one cookie?

As you are probably aware, more often than not, an HTTP server will send more than just a session_id cookie; however, httplib2 handles cookies with a dictionary, like this: response, content = http.request(url, 'GET', headers=headers) headers = {'Cookie': response['set-cookie']} url = 'http://www.example.com/home' response, content...

Pylons and Memcached

Anyone happen to use this combination in their web application? I'm having a bit of trouble finding some sort of tutorial or guideline for configuring this. Also seeing as how I started using Pylons recently I'm not familiar so please keep the advice very newbie friendly ( I haven't even used modules like Beaker all that much ). I'm usi...

help!for function

hi all,I have post the similar question before,but this time the problem is different,I got stuck with the following code..can anyone help with it?thanks in advance I have fixed mu code as suggested,thanks from numpy import * #vs,fs,rs are all m*n matrixs,got initial values in,i.e vs[0],fs[0],rs[0] are known #want use this foor loop to...

Python: how to make two lists from a dictionary

I have a dictionary. {1 : [1.2, 2.3, 4.9, 2.0], 2 : [4.1, 5.1, 6.3], 3 : [4.9, 6.8, 9.5, 1.1, 7.1]} I want to pass each key:value pair to an instance of matplotlib.pyplot as two lists: x values and y values. Each key is an x value associated with each item in its value. So I want two lists for each key: [1,1,1,1] [1.2,2.3,4.9,2.0...

Count the number of occurences of letters in a Python string

So I got a DNA sequence. ACCAGAGCGGCACAGCAGCGACATCAGCACTAGCACTAGCATCAGCATCAGCATCAGC CTACATCATCACAGCAGCATCAGCATCGACATCAGCATCAGCATCAGCATCGACGACT ACACCCCCCCCGGTGTGTGTGGGGGGTTAAAAATGATGAGTGATGAGTGAGTTGTGTG CTACATCATCACAGCAGCATCAGCATCGACATCAGCATCAGCATCAGCATCGACGACT TTCTATCATCATTCGGCGGGGGGATATATTATAGCGCGCGATTATTGCGCAGTCTACG TCATCGACTACGATCAGC...

How do I import the render_to_response method from Django 1.1 inside of Google App Engine?

I'm a Python newbie, so I'm sure this is easy. Here's the code in my main.py: import os os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from google.appengine.dist import use_library use_library('django', '1.1') # Use django form library. from django import forms from django.shortcuts import render_to_response The last line breaks...