How can I create a dict that sets a flag if it's been modified

2006-01-12 Thread sandravandale
I can think of several messy ways of making a dict that sets a flag if
it's been altered, but I have a hunch that experienced python
programmers would probably have an easier (well maybe more Pythonic)
way of doing this.

It's important that I can read the contents of the dict without
flagging it as modified, but I want it to set the flag the moment I add
a new element or alter an existing one (the values in the dict are
mutable), this is what makes it difficult. Because the values are
mutable I don't think you can tell the difference between a read and a
write without making some sort of wrapper around them.

Still, I'd love to hear how you guys would do it.

Thanks,
-Sandra

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


How can I make a dictionary that marks itself when it's modified?

2006-01-12 Thread sandravandale
It's important that I can read the contents of the dict without
flagging it as modified, but I want it to set the flag the moment I add
a new element or alter an existing one (the values in the dict are
mutable), this is what makes it difficult. Because the values are
mutable I don't think you can tell the difference between a read and a
write without making some sort of wrapper around them.

Still, I'd love to hear how you guys would do it.

Thanks,
-Sandra

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


Re: how do real python programmers work?

2006-01-12 Thread sandravandale
I'm not an experienced python programmer, but I come from a C++
background as well. I like to code in Komodo ($29 for the personal
edition) and that lets me have multiple python files opened in tabs,
and multiple interpreters opened below, since the interpreter is
command based, it doesn't have to be very tall, and I find this works
great (the debugging windows also appear down at the bottom, which is
convienient. The debugger alone is worth the $29, within a week or two
of serious development and you've paid for the software in terms of
saved time.) Komodo runs on windows and linux.

This leaves my second screen free, where I keep all my web browsers,
with comp.lang.python, gmail, and all the documentation I need.

I also keep a macros.py file in a place where it's in the python path,
and keep it open in Komodo. Anything I find myself typing over and over
in the interpreters, I simply make into a function in the macros file
and then reload the macros module and call it. I work with mod_python a
lot, so I have a macro that reads in the last 10 lines or so of the
apache error log, removes extraneous information and prints them, huge
timesaver.

-Sandra

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


Re: How can I make a dictionary that marks itself when it's modified?

2006-01-12 Thread sandravandale
Thanks to everyone who posted comments or put some thought into this
problem.

I should have been more specific with what I want to do, from your
comments the general case of this problem, while I hate to say
impossible, is way more trouble than it's worth.

By modified I meant that the dictionary has been changed from its
original (constructed) state by either:
1) A new element was added
2) An existing element was changed

I want to wrap a dictionary of cookies in mod_python. Cookies are
represented as objects with no methods. In light of this and Michael's
excellent post, I came up with the following code.

class CookieCollection(dict):
   def __init__(self, d):
  for k, v in d:
 v.__setattr__ = self.__wrap_setattr(v.__setattr__)
 self[k] = v

  self.modified = False

   def __setitem__(self, key, value):
  super(dict, self).__setitem__(key, value)
  self.modified = True # we don't have to wrap this item, the dict
is modified, and can't ever be unmodified

   def __wrap_setattr(self, real_setattr):
  def setattr(attrname, val):
 self.modified = True
 real_setattr(attrname, val)
  return setattr

   def send(self, req):
  if self.modified:
 for cookie in req.cookies.values():
Cookie.add_cookie(req, cookie)

The purpose of which is to be able to store any cookies sent with the
request in the CookieCollection, and then automatically call send()
before the headers are finished being sent and it will only do
something if the cookies have been altered (otheriwse they don't need
to be sent again.) I haven't tested the code yet, but the general idea
should be correct at the very least I think.

Thanks again,
-Sandra

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


How can I test if an argument is a sequence or a scalar?

2006-01-10 Thread sandravandale
I want to be able to pass a sequence (tuple, or list) of objects to a
function, or only one.

It's easy enough to do:

isinstance(var, (tuple, list))

But I would also like to accept generators. How can I do this?

Anything else is assumed to be a single value (my fault if I pass a
dict or something else to the function) and I make it into a tuple with
a single value (var, ) in order to simplify the algorithm (wasn't there
a special name for a one item tuple? Its been a while since I've
programmed in Python.)

Is there an easy way to do that?

Thanks,
-Sandra

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


Re: How can I test if an argument is a sequence or a scalar?

2006-01-10 Thread sandravandale
Hi Jean-Paul,

Thank you for the advice, I appreciate it, my university training was
next to useless so I learn how to write good programs by the advice of
helpful people in online communities. I'm really thankfull when people
take the time to tell me when I'm doing something wrong and show me a
better way.

And I was still curious, so thank you for explaining how to distinguish
iterables from non-iterables.

Have a great day!

-Sandra

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


Re: How can I test if an argument is a sequence or a scalar?

2006-01-10 Thread sandravandale
Hi Jean-Paul,

Thank you for the advice, I appreciate it, my university training was
next to useless so I learn how to write good programs by the advice of
helpful people in online communities. I'm really thankfull when people
take the time to tell me when I'm doing something wrong and show me a
better way.

And I was still curious, so thank you for explaining how to distinguish
iterables from non-iterables.

Have a great day!

-Sandra

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