Overriding iadd for dictionary like objects

RunThePun ubershmekel at gmail.com
Mon Aug 31 08:26:59 CEST 2009


On Aug 30, 10:33 pm, a... at pythoncraft.com (Aahz) wrote:
> In article <e09276e8-8152-4002-8366-4c12705a8... at l35g2000vba.googlegroups.com>,
>
>
>
>
>
> RunThePun  <ubershme... 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 (a... at pythoncraft.com)           <*>        http://www.pythoncraft.com/
>
> "I support family values -- Addams family values" --www.nancybuttons.com

I'm guessing you meant __getitem__, which is what Jan Kaliszewski
suggested, but as you noted, would be a bit cumbersome in this case.



More information about the Python-list mailing list