On Sep 1, 3:00 am, a...@pythoncraft.com (Aahz) wrote:
> In article 
> <b11a8a0e-03ca-41c9-b0d0-c5180b6a2...@p15g2000vbl.googlegroups.com>,
>
>
>
>
>
> RunThePun  <ubershme...@gmail.com> wrote:
> >On Aug 30, 10:33=A0pm, a...@pythoncraft.com (Aahz) wrote:
> >> In article <e09276e8-8152-4002-8366-4c12705a8...@l35g2000vba.googlegroups=
> >.com>,
> >> RunThePun =A0<ubershme...@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 =3D3D FilesDict('spam')
> >>>>>> d['temp.txt'] =3D3D '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. =A0That brings a whole new set of problems, of course.
>
> >I'm guessing you meant __getitem__, which is what Jan Kaliszewski
> >suggested, but as you noted, would be a bit cumbersome in this case.
>
> Actually, what I meant was __setitem__.  The idea is that you create the
> proxy item when you add the data to the dict (wrapping the original
> data), and the proxy has an __iadd__ method, which would allow you to do
> the file append.
> --
> Aahz (a...@pythoncraft.com)           <*>        http://www.pythoncraft.com/
>
> "I support family values -- Addams family values" --www.nancybuttons.com

But you do mean that __getitem__ would return a wrapped object as
well, right? Otherwise I don't see how the iadd would be relevant
because:
       d['a'] += 3
is equivalent to:
       d.__setitem__('a', d.__getitem__('a').__iadd__(3))
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to