python

Django: Return 'None' from OneToOneField if related object doesn't exist?

I've got a Django class like this: class Breakfast(m.Model): # egg = m.OneToOneField(Egg) ... class Egg(m.Model): breakfast = m.OneToOneField(Breakfast, related_name="egg") Is it possible to have breakfast.egg == None if there is no Egg related to the Breakfast? Edit: Forgot to mention: I'd rather not change the related_...

Python Bed module

Hi, I need to create a Bed module with these functions: readBed(file)
–
read 
a 
BED 
format 
file 
and
 constructs 
a 
list 
of 
gene 
model 
objects 
from 
the
 data
 it 
contains.

 writeBed(models=models,fname=file)
 – 
writes 
the 
given 
list
 of
 gene 
model 
objects
 and 
 writes
 them 
to 
a
 file 
named 
fname. For the read...

Python string to integer value

I'd like to know how to convert strings in Python to their corresponding integer values, like so: >>>print WhateverFunctionDoesThis('\x41\x42') >>>16706 I've searched around but haven't been able to find an easy way to do this. Thank you. ...

Python Mechanize keeps giving me 'response_seek_wrapper' when I try to use .open

I'm not sure what's going on, as the script used to work (before I messed around with my python on my system...) But when I try something along the lines of import mechanize browser = mechanize.Browser() browser.open("http://google.com") I get something like <response_seek_wrapper at 0x10123fd88 whose wrapped object = <closeable_res...

Why should Python be used?

I am looking for reasons why Python should be used and learned. Python and Perl both are interpreted language, so which is better? Which applications are using Python. ...

Reading files in GAE using python

Hello all! I created a simple python project that serves up a couple of pages. I'm using the 'webapp' framework and django. What I'm trying to do is use one template file, and load 'content files' that contain the actual page text. When I try to read the content files using os.open, I get the following error: pageContent = os.open(pag...

pyinotify asyncnotifier thread question

I'm confused about how asyncnotifier works. What exactly is threaded in the notifier? Is the just the watcher threaded? Or does each of the callbacks to the handler functions run on its own thread? The documentation says essentially nothing about the specifics of the class. ...

How to pass variable arguments from bash script to python script

Hi All... I've been trying to solve this issue for sometime now with no luck. The crust of the situation is that I'm using a bash script to send parameters to a a python script: Example: foo.sh calls bar.py....the call looks like: bar.py $var1 $var2 ... $varn The python script then prints all the arguments using the sys.argv array. T...

How to use Query.order() on string properties containing non-english characters?

How to use Query.order() on string properties containing non-english characters so entities where fetched in correct order? Query.order is oddly putting any non-english characters on the end of the list, like this: Dolnośląskie Kujawsko-Pomorskie Lubelskie Lubuskie Mazowieckie Małopolskie <- incorrect order Opolskie Podkarpackie Podlas...

Stop when get input in Python

How can I run a loop in Python and code it to stop when the user press a button (not ctr+c)? ...

python circular imports once again (aka what's wrong with this design)

Let's consider python (3.x) scripts: main.py: from test.team import team from test.user import user if __name__ == '__main__': u = user() t = team() u.setTeam(t) t.setLeader(u) test/user.py: from test.team import team class user: def setTeam(self, t): if issubclass(t, team.__class__): self.t...

Nested Dictionaries in Python, with implicit creation of non-existing intermediate containers?

I want to create a polymorphic structure that can be created on the fly with minimum typing effort and be very readable. For example: a.b = 1 a.c.d = 2 a.c.e = 3 a.f.g.a.b.c.d = cucu a.aaa = bau I do not want to create an intermediate container such as: a.c = subobject() a.c.d = 2 a.c.e = 3 My question is similar to this one: http...

What PEP 8 guidelines do you ignore, and which ones do you stick to?

The title says most of it really :) Over the years, the more Python I write, the more I find myself agreeing with most of the guidelines, some though I consistently and intentionally break for my own reasons. I'd be curious to know what in PEP 8 (or other PEPs too maybe) people religiously stick to and why, and what people find inconven...

python codes runs using idle but fails on command line?

Hi all, I am learning python, i have written a script per an example in the book I am reading where it imports the urllib library. This works fine when i run it from IDLE but if i go to the folder when the file is and run "python test.py" i get an error where it tells me that Traceback (most recent call last): File "test.py", li...

How to determine bottlenecks in code, besides visual inspection?

When visual inspection fails, how do you determine where the slowest points in your code are? Where are the bottlenecks that are ruining your runtime? ...

Can't load pywin32 library win32gui

I'm trying to use the win32gui module included with pywin32 but I can't get it working. I have downloaded it, built it and everything seem to be located under site-packages, I've found win32gui.pyd at site-packages/win32/win32gui.pyd but when I try to import it I get: import pyHook, win32gui ImportError: DLL load failed: The specified ...

How to speed up string construction from characters in a doubly-nested list?

A common speedup for string concatenations is changing something like s = "" for x in list: s += some_function(x) to slist = [some_function(elt) for elt in somelist] s = "".join(slist) However, how could this apply if your 'for' was doubly nested? For example... s = "" for x in list: for y in x: s += some_function...

Python: Convert this list into dictionary

Hi, I've got a problem , and do not know how to code in python. I've got a list[10, 10, 10, 20, 20, 20, 30] I want it be in a dictionary like this {"10": 1, "20": 3, "30" : 1} How could I achieve this? ...

Python- Bouncing Balls? How to pass seperately as one parameter?

Hi there, I have a question and I was wondering if anyone could help... How do I pass ball_boundary_2 and ball_boundary_3 separately as one parameter to ball_collisions function? Here is the code below: ## Import PyGame system import sys, pygame ## Initialise PyGame system pygame.init() ## Function to handle ball colissions ###...

What is the PHP equivalent to Python's Try: ... Except:...

I am a strong Python programmer, but not quite there when it comes to PHP. I need to try something, and if that doesn't work out, do something else. This is what it would look like in Python: try: print "stuf" except: print "something else" What would this be in PHP? ...