473,372 Members | 879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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

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

Jan 12 '06 #1
11 2036
Ideally, I would have made a wrapper to add/delete/modify/read from
the dictionay.

But other than this, one way i can think straight off is to "pickle"
the dict, and to see if the picked object is same as current object.

cheers,
amit.

On 12 Jan 2006 01:15:38 -0800, sa***********@yahoo.com
<sa***********@yahoo.com> wrote:
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

--
----
Endless the world's turn, endless the sun's spinning
Endless the quest;
I turn again, back to my own beginning,
And here, find rest.
Jan 12 '06 #2
sa***********@yahoo.com writes:
Still, I'd love to hear how you guys would do it.


Make a subclass of dict, or an object containing a dictionary, that
has a special __setattr__ method that traps updates and sets that
modification flag. There are contorted ways the caller can avoid
triggering the flag, but Python is not Java and it in general makes no
attempt to protect its objects against hostile code inside the
application.

Your suggestion of using pickle is shaky in my opinion, since pickle's
docs don't guarantee that pickling the same dictionary twice will
return the same pickle both times. If it happens to work that way,
it's just an implementation accident.
Jan 12 '06 #3
sa***********@yahoo.com said unto the world upon 12/01/06 03:15 AM:
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


Hi Sandra,

here's one attempt. (I'm no expert, so wait for better :-)
class ModFlagDict(dict): def __init__(self, *args, **kwargs):
super(ModFlagDict, self).__init__(*args, **kwargs)
self.modified = False
def __setitem__(self, key, value):
self.modified = True
super(ModFlagDict, self).__setitem__(key, value)

md = ModFlagDict(a=4, b=5)
md {'a': 4, 'b': 5} md.modified False md[3]=5
md {'a': 4, 3: 5, 'b': 5} md.modified True
It's broken in at least one way:
newmd = ModFlagDict(3=4, 1=5) SyntaxError: keyword can't be an expression


So, as it stands, no integers, floats, tuples, etc can be keys on
initialization. I think that can be be worked around by catching the
exceptions and setting the desired key-value pairs that way. But, it
is almost 4am, and I also suspect there is a much better way I am not
thinking of :-)

Best,

Brian vdB
Jan 12 '06 #4
Brian van den Broek said unto the world upon 12/01/06 03:42 AM:
sa***********@yahoo.com said unto the world upon 12/01/06 03:15 AM:
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.
<snip>
here's one attempt. (I'm no expert, so wait for better :-)
>>> class ModFlagDict(dict): def __init__(self, *args, **kwargs):
super(ModFlagDict, self).__init__(*args, **kwargs)
self.modified = False
def __setitem__(self, key, value):
self.modified = True
super(ModFlagDict, self).__setitem__(key, value)
<snip>
It's broken in at least one way:
>>> newmd = ModFlagDict(3=4, 1=5) SyntaxError: keyword can't be an expression >>>
So, as it stands, no integers, floats, tuples, etc can be keys on
initialization. I think that can be be worked around by catching the
exceptions and setting the desired key-value pairs that way. But, it
is almost 4am, and I also suspect there is a much better way I am not
thinking of :-)


Sorry for the self-reply, but I just realized my original code isn't
quite so bad as:

# class ModFlagDict as before
mdict = ModFlagDict({42:"This will work", (7, 6):"Python comes through, again!"}) mdict {42: 'This will work', (7, 6): 'Python comes through, again!'} mdict.modified False mdict[42]=":-)"
mdict {42: ':-)', (7, 6): 'Python comes through, again!'} mdict.modified True


I'll wager someone will point out a better way still, though.

Best,

Brian vdB
Jan 12 '06 #5
Should the dict flag when the dict itself has been updated? Or also
when any of the items in the dict has been updated?

Say you have a dict consisting of lists...

The dict should be flagged as modified when an item is added; or when
an item is replaced (you call dict.__setitem__ with a key that already
exists).
This is clear, and easy to achieve with a simple wrapper or subclass.

But should the dict also be flagged as 'modified' when I append an item
to one of the lists that is in the dict?
l = d['a']
l.append('1')

Does that code mean that the dict should be flagged as 'modified' in
your use-case? or not?

If yes, then the only feasible way might be to pickle the dict. And if
repeated pickling of the same dict is not guaranteed to give the same
results, then perhaps pickling d.getitems() would give the right
results since, AFAIK, dict.getitems() is at least guaranteed to
maintain the same order given that A) The dict is not changed and B)
You're using the same Python version.

Right?

Jan 12 '06 #6
sa***********@yahoo.com wrote:
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.

Detecting when a dictionary is updated is easy, just subclass dict and
override the __setitem__ method.

Detecting when an object contained within the dictionary is mutated is
effectively impossible.

If you are willing to compromise you can do something similar to the
solution used in the ZODB (persistent object database): it marks an object
as dirty when you rebind an attribute, but it doesn't attempt to catch
mutation of contained objects. If you want a contained dictionary or list
to be handled automatically by the persistence machinery you can use a
PersistentDict or PersistentList object, otherwise you can use an ordinary
dict/list and manually flag the container as dirty when you mutate it.

This leads to code such as (assuming self is the container):

....
self.somelist.append(something)
self.somelist = self.somelist

which isn't wonderful but mostly works. Of course:

self.somelist += [something]

also has the desired effect.
Jan 12 '06 #7
On Thu, 12 Jan 2006 03:42:21 -0600,
Brian van den Broek <br***@cc.umanitoba.ca> wrote:
It's broken in at least one way:
newmd = ModFlagDict(3=4, 1=5) SyntaxError: keyword can't be an expression So, as it stands, no integers, floats, tuples, etc can be keys on
initialization ...


That has nothing to do with your code:
dict(1=4) SyntaxError: keyword can't be an expression int(4=5)

SyntaxError: keyword can't be an expression

The names of keyword arguments have look like Python identifiers; 1 and
4 are *not* valid Python identifiers.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jan 12 '06 #8
Brian van den Broek <br***@cc.umanitoba.ca> writes:
It's broken in at least one way:
>>> newmd = ModFlagDict(3=4, 1=5)

SyntaxError: keyword can't be an expression


newmd = ModFlagDict(**{3:4, 1:5})
Jan 12 '06 #9
Paul Rubin wrote:
sa***********@yahoo.com writes:
Still, I'd love to hear how you guys would do it.

Make a subclass of dict, or an object containing a dictionary, that
has a special __setattr__ method that traps updates and sets that


/__setattr__/__setitem__/ ?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Jan 12 '06 #10
Steve Holden <st***@holdenweb.com> writes:
Make a subclass of dict, or an object containing a dictionary, that
has a special __setattr__ method that traps updates and sets that


/__setattr__/__setitem__/ ?


Yes, thinkographical error. Thanks.
Jan 12 '06 #11
>> here's one attempt. (I'm no expert, so wait for better :-)
>>> class ModFlagDict(dict):

def __init__(self, *args, **kwargs):
super(ModFlagDict, self).__init__(*args, **kwargs)
self.modified = False
def __setitem__(self, key, value):
self.modified = True
super(ModFlagDict, self).__setitem__(key, value)


You also need to catch __delitem__.

You may need to catch pop, update, clear and setdefault, depending on
whether or not they call the __setitem__/__delitem__. Personally, I'd
catch them all and make sure the flag got set appropriately, because
whether or not they use those methods is an implementation detail that
may change in the future.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jan 12 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: bearophileHUGS | last post by:
I'm frequently using Py2.4 sets, I find them quite useful, and I like them, even if they seem a little slower than dicts. Sets also need the same memory of dicts (can they be made to use less...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
11
by: mwt | last post by:
Hi. I'm reworking a little app I wrote, in order to separate the data from the UI. As a start, I wanted to create a iron-clad data recepticle that will hold all the important values, and stand up...
3
by: Gregory Piñero | last post by:
Hey guys, I don't understand why this isn't working for me. I'd like to be able to do this. Is there another short alternative to get this intersection? >>> set().intersection() Traceback...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
3
by: weston | last post by:
I'm making a foray into trying to create custom vertical scrollbars and sliders, and thought I had a basic idea how to do it, but seem to be having some trouble with the implementation. My...
1
by: bearophileHUGS | last post by:
The PEP 3100: http://www.python.org/dev/peps/pep-3100/ says: Return iterators instead of lists where appropriate for atomic type methods (e.g. dict.keys(), dict.values(), dict.items(), etc.);...
6
by: kaens | last post by:
Hey everyone, this may be a stupid question, but I noticed the following and as I'm pretty new to using xml and python, I was wondering if I could get an explanation. Let's say I write a simple...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.