473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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.offset + term)
def __sub__(self, term):
return Base(self.offset - 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.offset, 2)
elif type(where) == PosType:
return fl.seek(where.offset, 1)
elif type(where) == FrontType:
return fl.seek(where.offset, 0)
return fl.seek(where, 0)

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

--
Antoon Pardon

May 10 '06 #1
0 817

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Mike Meyer | last post by:
PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $ Last-Modified: $Date: 2003/09/22 04:51:50 $ Author: Mike Meyer <mwm@mired.org> Status: Draft Type: Staqndards...
15
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
8
by: codymanix | last post by:
Often you don't wish that clientcode does not depend on a certain implementation of your methods: class MyClass { public IList DoIt(){return new ArrayList();} } clientcode could now cast...
28
by: C# Learner | last post by:
Note ---- Please use a fixed-width font to view this, such as Courier New. Problem
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more...
8
by: Paddy | last post by:
Proposal: Named RE variables ====================== The problem I have is that I am writing a 'good-enough' verilog tag extractor as a long regular expression (with the 'x' flag for...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.