Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99542

Looking for ideas to improve library API

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Lalancette <clalancette@gmail.com>
Newsgroups comp.lang.python
Subject Looking for ideas to improve library API
Date Wed, 25 Nov 2015 15:52:25 -0500
Lines 42
Message-ID <mailman.121.1448527308.20593.python-list@python.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de Vq/ZP19RMyts+1LvC9QHXQGPLZ+GqSErkLdZ7dE0gLPA==
Return-Path <clalancette@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'context': 0.05; 'memory.': 0.05; 'modified': 0.05; 'f.close()': 0.07; 'method,': 0.07; 'api': 0.09; "'w')": 0.09; 'descriptor': 0.09; 'open()': 0.09; 'subject:library': 0.09; 'url:github': 0.09; "'r')": 0.16; '(),': 0.16; 'appreciated!': 0.16; 'complains': 0.16; 'descriptor.': 0.16; 'file.\xc2\xa0': 0.16; 'files)': 0.16; 'fp.close()': 0.16; 'missed.': 0.16; 'received:209.85.223.173': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:ideas': 0.16; 'library,': 0.18; 'passes': 0.18; 'typical': 0.18; 'input': 0.18; 'library': 0.20; 'fine,': 0.22; 'ok.': 0.22; 'parse': 0.22; 'file.': 0.22; 'leave': 0.23; 'this:': 0.23; 'thanks,': 0.24; 'import': 0.24; "i've": 0.25; 'chris': 0.26; 'message-id:@mail.gmail.com': 0.27; 'disk': 0.27; 'developing': 0.28; 'i/o': 0.29; 'objects': 0.29; "i'm": 0.30; 'operations': 0.31; 'another': 0.32; 'especially': 0.32; 'point': 0.33; 'problem': 0.33; 'open': 0.33; 'file': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'something': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'to:addr :python-list': 0.36; 'really': 0.37; 'being': 0.37; 'done.': 0.37; 'thought': 0.37; 'received:209': 0.38; 'skip:o 20': 0.38; 'means': 0.39; 'along': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'hello,': 0.40; 'close': 0.61; 'skip:u 10': 0.61; 'entire': 0.61; 'subject:improve': 0.84; '\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=XWLvX4XZ6MSKLrMVWH65FJ63z09thy4awae/U1ImgkU=; b=ILRPy9Y8C++dMC2T+5CWgzGmi6qmCV9Uf8NsbYX/1ZuNPnqFGkr3eYZn24bd3k9dAm h/7m6bplEnvh/rNPvKOoJ3cExhSxyFffxVUkmrBlCxVfa8do6IKFrpDm7WVQzh6Z13UD tOQC6+T32ExVtorhyj3qEF+YUUa6JJGFLh8A0yJRhU1nGqRzC9vERabMZtxv6jpNVJ8O wiKhzFy2vmZFDhpINaVpJdj6aP8XIR2pefsX1ObFugdmv9zNVNpnL/93pzyqNmJvj4TR 9+G9G4pMOtVU8dNiliwbDqLOonA1NxK2kPPLdD0lxhqZ4FLTfSBDVBdwQct+g+Nr/z9i BHMw==
X-Received by 10.107.44.210 with SMTP id s201mr45904935ios.62.1448484745174; Wed, 25 Nov 2015 12:52:25 -0800 (PST)
X-Mailman-Approved-At Thu, 26 Nov 2015 03:41:47 -0500
X-Content-Filtered-By Mailman/MimeDel 2.1.20+
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:99542

Show key headers only | View raw


Hello,
     I'm currently developing a library called pyiso (
https://github.com/clalancette/pyiso), used for manipulating ISO disk
images.  I'm pretty far along with it, but there is one part of the API
that I really don't like.
    Typical usage of the library is something like:

import pyiso

p = pyiso.PyIso() // create the object
f = open('/path/to/original.iso', 'r')
p.open(f)  // parse all of the metadata from the input ISO
fp = open('/path/to/file/to/add/to/iso', 'r')
p.add_fp(fp)  // add a new file to the ISO
out = open('/path/to/modified.iso', 'w')
p.write(out)  // write out the modified ISO to another file
out.close()
fp.close()
f.close()

This currently works OK.  The problem ends up being the file descriptor
lifetimes.  I want the user to be able to do multiple operations to the
ISO, and I also don't want to read the entire ISO (and new files) into
memory.  That means that internal to the library, I take a reference to the
file object that the user passes in during open() and add_fp().  This is
fine, unless the user decides to close the file object before calling the
write method, at which point the write complains of I/O to a closed file.
This is especially problematic when it comes to using context managers,
since the user needs to leave the context open until they call write().
      I've thought of a couple ways to deal with this:

1.  Make a copy of the file object internal to the library, using os.dup()
to copy the file descriptor.  This is kind of nasty, especially since I
want to support other kinds of file objects (think StringIO).
2.  Just document the fact that the user needs to leave the file objects
open until they are done.  This is simple, but not super user-friendly.

I'm looking for any ideas of how to do this better, or something I missed.
Any input is appreciated!

Thanks,
Chris Lalancette

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Looking for ideas to improve library API Chris Lalancette <clalancette@gmail.com> - 2015-11-25 15:52 -0500

csiph-web