Overriding iadd for dictionary like objects

Aahz aahz at pythoncraft.com
Sun Aug 30 15:33:29 EDT 2009


In article <e09276e8-8152-4002-8366-4c12705a8e6a at l35g2000vba.googlegroups.com>,
RunThePun  <ubershmekel at gmail.com> wrote:
>
>I made a DictMixin where the keys are filenames and the values are the
>file contents. It was very simple and easy to do thanks to DictMixin.
>
>For example this code writes "abc" in a file named "temp.txt" and
>prints the contents of the file named "swallow", these files are
>looked up/created/deleted in the directory "spam":
>>>> d =3D FilesDict('spam')
>>>> d['temp.txt'] =3D 'abc'
>>>> print(d['swallow'])
>
>My problem arose when I wanted to append a string to a file which
>using open(..., 'ab') would have been miles more efficient because I
>wouldn't have to read the entire file (__getitem__) and then write the
>entire file back (__setitem__). The files are expected to be as big as
>600 KB which will be appended 30 bytes at a time about 3 times a
>second. Performance-wise the system would probably work without open
>(..., 'ab') but it would be a real thrashing so the current solution
>uses a method "AddTo" as Robert suggested, sacrificing the neat
>getitem/setitem syntax.

You can do mostly what you want, I think, by having __setitem__()
convert string values into FileProxy() objects that have an appropriate
__iadd__() method.  That brings a whole new set of problems, of course.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"I support family values -- Addams family values" --www.nancybuttons.com



More information about the Python-list mailing list