Re: threads/sockets quick question.

2005-09-19 Thread antred
This may be a really stupid question, but are you actually CALLING your
scan() function anywhere?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threads/sockets quick question.

2005-09-19 Thread antred
Ah shoot, never mind, I'm an idiot. =0

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threads/sockets quick question.

2005-09-19 Thread antred
maybe try this

while threading.activeCount() < MAX_THREADS:
# 


instead of 

while threading < MAX_THREADS:

-- 
http://mail.python.org/mailman/listinfo/python-list


Am I stupid or is 'assert' broken in Python 2.5??

2006-12-06 Thread antred
I've noticed something odd in Python 2.5, namely that the 2 argument
version of 'assert' is broken. Or at least it seems that way to me.

Run the following code in your Python interpreter:

myString = None

assert( myString, 'The string is either empty or set to the None type!'
)
assert( myString )



You'll notice that the first assert doesn't do anything, whereas the
second assert correctly recognizes that myString does not evaluate to
true. That doesn't seem right. Surely Python should have raised an
assertion error on the first assert statement, right??

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Am I stupid or is 'assert' broken in Python 2.5??

2006-12-06 Thread antred
Never mind, I'm a schmuck!!  =0

It should have been


assert myString, 'String empty or None!'


Sorry, ignore me. =\

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Am I stupid or is 'assert' broken in Python 2.5??

2006-12-06 Thread antred
Yeah, it hit me seconds after I had posted my message. =0 Why didn't I
think of it during the 30 minutes I spent banging my head against the
keyboard going nuts over this 'bug' ...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: passing class by reference does not work??

2007-04-11 Thread antred

> def b(item, a):
> a.val = a.val + 1
> return item + a.val




This is where the problem lies, specifically the line a.val = a.val +
1
What happens here is that the 1st a.val refers to a member of the
class instance a, called val ... which does not yet exist and is
therefore created as the result of taking the val member of the class
A and adding 1 to it. In other words, a.val is not the same variable
as A.val. Are you following? If not, change your b() method to this:

def b(item, a):
a.val = a.val + 1
assert a.val is A.val
return item + a.val

and see what happens.

-- 
http://mail.python.org/mailman/listinfo/python-list


Please have a look at this class

2007-01-25 Thread antred
Hello everyone,

While working on a program I encountered a situation where I'd
construct a largish data structure (a tree) from parsing a host of
files and would end up having to throw away parts of my newly built
tree if a file turned out to contain invalid data. My first thought was
'Well, you can always make a deep copy of your tree first, then add new
data to the copy and revert to the original if you need to.", but as
this tree can grow very big this is not exactly efficient.
So my second idea was to come up with a class that offers a very
limited degree of database-like behavior, meaning you can make changes
to the object and then decide whether you want to commit those changes
or roll them back to get back to the original. For example:



u = Unrollable()
u.someValue = 3.14
u.aString = 'Hi there'

# If we decide we want to keep those changes ...
u.commit()

# Or we could have reverted to the original. This would have restored
the state prior to the last call to commit() (or simply the state at
the beginning, if there hasn't been a call to commit yet).
#u.rollback()



The basic idea behind this is that each instance of the Unrollable
class keeps an internal dictionary (which, in lieu of a better name I'm
currently calling 'sand box') to which all changed attribute values are
saved (attribute changes are intercepted via __setattr__). Then, with a
call to commit(), all attributes are transferred to the instance's
__dict__ dictionary and hence become actual attributes per se.

Similarily, the rollback() function simply empties the contents of the
sand box without committing them to __dict__. The rollback() function
can work recursively, too, if passed some extra parameters. If so, it
walks either the sand box or the __dict__ (or both) and invokes the
rollback() function on any attribute members that are instances of the
Unrollable class or a derived class.

Finally, this works for 'private' attributes (i.e. names with two
leading underscores), too, as the __setattr__ implementation mangles
the name of the attribute if it detects a private name.

I'm posting this for 2 reasons. Firstly, I feel that I have finally
produced something that others _might_ find useful, too. Secondly,
since I'm still learning Python (yeah, even after 2 years), I would be
very grateful to hear people's criticisms. Are there things that could
be done more efficiently? Do you spot any grave errors? Does something
similar already exist that I should just have used instead? Right now
I'm rather pleased with my class, but if someone tells me there is
already something like this Python's library (and then it'll most
likely be more efficient anyway) then I'd of course rather use that.

Entire class definition + some test code attached to this post.

P.S. I __LOVE__ how something like this is just barely 70 lines of code
in Python!



class Unrollable( object ):
"""Provides a very simple commit/rollback system."""

def __setattr__( self, attributeName, attributeValue ):
"""Changes the specified attribute by setting it to the passed 
value.
The change is only made to the sandbox and is not committed."""

if attributeName.find( '__' ) == 0:
# Mangle name to make attribute private.
attributeName = '_' + self.__class__.__name__ + 
attributeName

try:
theDict = self.__dict__[ '_Unrollable__dSandBox' ]
except KeyError:
theDict = self.__dict__[ '_Unrollable__dSandBox' ] = {}


theDict[ attributeName ] = attributeValue


def __getattr__( self, attributeName ):
"""This method ensures an attribute can be accessed even when it
hasn't been committed yet (since it might not exist in the object
itself yet)."""

if attributeName.find( '__' ) == 0:
# Mangle name to make attribute private.
attributeName = '_' + self.__class__.__name__ + 
attributeName

try:
theDict = self.__dict__[ '_Unrollable__dSandBox' ]
except KeyError:
# Our sandbox doesn't exist yet, therefore the 
requested attribute
doesn't exist yet either.
raise AttributeError


try:
return theDict[ attributeName ]
except KeyError:
# No such attribute in our sandbox.
raise AttributeError

def commitChanges( self ):
"""Commits the contents of the sandbox to the actual object. 
Clears
the sandbox."""
while len( self.__dSandBox ) > 0:
key, value = self.__dSandBox.popitem()
self.__dict__[ key ] = value

def unroll( self, bRecurseSandBox = True, bRecurseDict = False ):
"""Ditches all changes

Re: Python email parser

2007-01-25 Thread antred
Hmm, I'm not sure whether I'd call 30 secs for 110 MB particularly
slow. I mean that *IS* a lot of data.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threads

2006-05-12 Thread antred
Aww shoot, I never knew that!! LOL, I implemented my own worker thread
class using a mutex protected job list and a pair of connected sockets
for interprocess communication when I could just have used the darn
Queue module instead. Gr  hehe.

-- 
http://mail.python.org/mailman/listinfo/python-list