I am writing a minimal replacement for mod_python's publisher.py
The basic premise is that it is loading modules based on a URL scheme:
/foo/bar/a/b/c/d
Whereby /foo/ might be a directory and 'bar' is a method ExposedBar in a publishable class in /foo/index.py. Likewise /foo might map to /foo.py and bar is a method in the exposed cla...
I'm currently reading a great book called 'Programming Collective Intelligence' by Toby Segaran (which i highly recommend)
The code examples are all written in Python, and as I have already learnt one new language this year (graduating from VB.net to C#) i'm not keen to jump on another learning curve.
This leaves my with the issue of t...
When you call dict.values() the order of the returned items is dependent on the has value of the keys. This seems to be very consistent in all versions of cPython, however the python manual for dict simply states that the ordering is "arbitrary".
I remember reading somewhere that there is actually a PEP which specifically states the exp...
I'm trying to generate an XML document with namespaces, currently with Python's xml.dom.minidom:
import xml.dom.minidom
doc = xml.dom.minidom.Document()
el = doc.createElementNS('http://example.net/ns', 'el')
doc.appendChild(el)
print(doc.toprettyxml())
The namespace is saved (doc.childNodes[0].namespaceURI is 'http://example.net/ns')...
I have a problem which requires a reversable 1:1 mapping of keys to values.
That means sometimes I want to find the value given a key, but at other times I want to find the key given the value. Both keys and values are guaranteed unique.
x = D[y]
y == D.inverse[x]
The obvious solution is to simply invert the dictionary every time I...
I have a model that has a field named "state":
class Foo(models.Model):
...
state = models.IntegerField(choices = STATES)
...
For every state, possible choices are a certain subset of all STATES. For example:
if foo.state == STATES.OPEN: #if foo is open, possible states are CLOSED, CANCELED
...
if foo.state == STA...
Say I have the following loop:
i = 0
l = [0, 1, 2, 3]
while i < len(l):
if something_happens:
l.append(something)
i += 1
Will the len(i) condition being evaluated in the while loop be updated when something is appended to l?
...
When I have a given django model class like this:
class BaseClass(models.Model):
some_field = models.CharField(max_length = 80)
...
and some subclasses of it, for example
class SomeClass(BaseClass):
other_field = models.CharField(max_length = 80)
Then I know I can get the derived object by calling
base = BaseClass...
import pty
import os
import sys
import time
pid, fd = os.forkpty()
if pid == 0:
# Slave
os.execlp("su","su","MYUSERNAME","-c","id")
# Master
print os.read(fd, 1000)
os.write(fd,"MYPASSWORD\n")
time.sleep(1)
print os.read(fd, 1000)
os.waitpid(pid,0)
print "Why have I not seen any output from id?"
...
I need to write a series of matrices out to a plain text file from python. All my matricies are in float format so the simple
file.write() and file.writelines()
do not work. Is there a conversion method I can employ that doesn't have me looping through all the lists (matrix = list of lists in my case) converting the individual values...
Hi Everyone,
I've wanted to get into Python development for awhile and most of my programming experience has been in .NET and no mobile development. I recently thought of a useful app to make for my windows mobile phone and thought this could be a great first Python project.
I did a little research online and found PyCe which I thin...
I am unable to use nose (nosetests) in a virtualenv project - it can't seem to find the packages installed in the virtualenv environment.
The odd thing is that i can set
test_suite = 'nose.collector'
in setup.py and run the tests just fine as
python setup.py test
but when running nosetests straight, there are all sorts of import e...
I would like to create a shell which will control a separate process that I created with the multiprocessing module. Possible? How?
EDIT:
I have already achieved a way to send commands to the secondary process: I created a code.InteractiveConsole in that process, and attached it to an input queue and an output queue, so I can command t...
class Package:
def __init__(self):
self.files = []
# ...
def __del__(self):
for file in self.files:
os.unlink(file)
__del__(self) above fails with an AttributeError exception. I understand Python doesn't guarantee the existence of "global variables" (member data in this context?) when __del__(...
I'm working on a Python program that makes heavy use of eggs (Plone). That means there are 198 directories full of Python code I might want to search through while debugging. Is there a good way to search only the .py files in only those directories, avoiding unrelated code and large binary files?
...
When I have an object foo, I can get it's class object via
str(foo.__class__)
What I would need however is only the name of the class ("Foo" for example), the above would give me something along the lines of
"<class 'my.package.Foo'>"
I know I can get it quite easily with a regexp, but I would like to know if there's a more "clean...
Hello!
I need to test if a variable is a module or not. How to do this in the cleanest way?
I need this for initializing some dispatcher function and I want that the function can accept either dict or module as an argument.
...
I'd like to be able to do the following:
num_intervals = (cur_date - previous_date) / interval_length
or
print (datetime.now() - (datetime.now() - timedelta(days=5)))
/ timedelta(hours=12)
# won't run, would like it to print '10'
but the division operation is unsupported on timedeltas. Is there a way that I can implement div...
In an answer (by S.Lott) to a question about Python's try...else statement:
Actually, even on an if-statement, the
else: can be abused in truly terrible
ways creating bugs that are very hard
to find. [...]
Think twice about else:. It is
generally a problem. Avoid it except
in an if-statement and even then
consider do...
I did google. I may not have searched right. I read on another Stack Overflow question/comment that Python was just like Ruby, as it relates to "everything's and object," and everything in Python was an object, just like Ruby.
Is this true? Is everything an object in python like ruby?
Thank you.
EDIT:
How are the two different in ...