Proposal: Base variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Antoon Pardon

    #1

    Proposal: Base variables

    When you want to specify a location or moment, it sometimes
    is easier to do so in an absolute way and sometimes it
    is easier to do so relative to some kind of base. Examples
    of this are the Scheduler objects with the enter and enterabs
    methods and the seek method of files.

    However python has no unified approach for handling these
    cases. In the case of Schedular objects the choice was
    made to have two methods. In one method (enterabs) the
    "time" is interpreted as an absolute time. In the other
    method (enter) the "time" is interpreted relative to
    the moment of the call.

    With seeking in files the choice was made to use an extra
    parameter. The second parameter of the seek method determines
    relative to what the where parameter is to be interpreted.

    My idea is a third approach which (would it be accepted)
    should replace the other two so we would have one obvious
    way to do this in Python

    The idea is to to use symbolic bases when you want to
    specify a time or location relatice to something else.
    The standard bases would be among the following:

    Front, Rear, Position, Now

    So if you want to seek 90 bytes from the end, you use: Rear - 90
    If you want something to happen 900 second from now you use:
    Now + 900.

    So we would have the following calls:
    fl.seek(Rear - 90)
    shed.enter(Now + 90)

    The adavantages:
    One unified approach, that doesn't require extra methods or
    extra parameters. By using appropiate symbolic names the code
    is also more clear about what it does.

    The disadvantages:
    functions that use these variables as argument may be a
    bit more complicated to write. The Base variables can't
    be combined. Writing fl.seek((Front + Rear) / 2) wont work

    A sample implementation of these variables and a seek function
    using them is below, any comments?

    ---------------------------------------------------------------------

    def NewBase():

    class Base(object):
    def __init__(self, offset = 0):
    self.offset = offset
    def __add__(self, term):
    return Base(self.offse t + term)
    def __sub__(self, term):
    return Base(self.offse t - term)
    def __repr__(self):
    if self.offset < 0:
    return "< * - %s >" % abs(self.offset )
    else:
    return "< * + %s >" % self.offset

    return Base

    FrontType = NewBase()
    NowType = NewBase()
    RearType = NewBase()
    PosType = NewBase()

    Front = FrontType(0)
    Now = NowType(0)
    Rear = RearType(0)
    Pos = PosType(0)

    def seek(fl, where):
    if type(where) == RearType:
    return fl.seek(where.o ffset, 2)
    elif type(where) == PosType:
    return fl.seek(where.o ffset, 1)
    elif type(where) == FrontType:
    return fl.seek(where.o ffset, 0)
    return fl.seek(where, 0)

    fl = file('test')
    seek(fl, Rear - 90)
    fl.read()

    --
    Antoon Pardon

Working...