Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You will win success in whatever calling you adopt.


comp / comp.lang.python / Python's indexing conventions

SubjectAuthor
o Python's indexing conventionsStefan Ram

1
Subject: Python's indexing conventions
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Tue, 31 Dec 2024 21:00 UTC
Path: news.eternal-september.org!eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Python's indexing conventions
Date: 31 Dec 2024 21:00:26 GMT
Organization: Stefan Ram
Lines: 85
Expires: 1 Jan 2026 11:59:58 GMT
Message-ID: <Indexing-20241231214317@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de n21vmLFKTU1dMFbJoy7ESwFIhDxCX+lsBKprthfHUUfulU
Cancel-Lock: sha1:+rZLAHH6jNg0lcUb/hZc5Bn1oCQ= sha256:TAOMpHmY9Z2E3HuV44L8CH23nbqJ/FHmBqLUq7iGttA=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
|Raymond Boute <raymond.boute@pandora.be> wrote or quoted:
|>Moreover, using index -1 for the last item is a bad choice
|This feature is the bee's knees, man.

I was just looking for a list with a sort of "max-out" vibe
that turns all negative indices into zeros. My AI wingman
whipped it up for me.

In my code, it goes by "SourceList":

s = list( 'Example' )
t = SourceList( 'Example' )

print( repr( s[ -1: 2 ] ))
print( repr( t[ -1: 2 ] ))

spits out:

[]
['E', 'x']

'cause "t[ -1: 2 ]" is basically the same as "s[ 0: 2 ]".

Here's the whole shebang:

from collections.abc import MutableSequence

class SourceList(MutableSequence):
'''
Interpret all negative indices in subscriptions and slices to mean 0.
'''
def __init__(self, iterable=None):
self._list = list(iterable) if iterable is not None else []

def _normalize_index(self, index):
return max(0, index) if isinstance(index, int) else index

def _normalize_slice(self, key):
start = self._normalize_index(key.start) if key.start is not None else None
stop = self._normalize_index(key.stop) if key.stop is not None else None
return slice(start, stop, key.step)

def __getitem__(self, key):
if isinstance(key, slice):
key = self._normalize_slice(key)
else:
key = self._normalize_index(key)
return self._list[key]

def __setitem__(self, key, value):
if isinstance(key, slice):
key = self._normalize_slice(key)
else:
key = self._normalize_index(key)
self._list[key] = value

def __delitem__(self, key):
if isinstance(key, slice):
key = self._normalize_slice(key)
else:
key = self._normalize_index(key)
del self._list[key]

def __len__(self):
return len(self._list)

def insert(self, index, value):
self._list.insert(self._normalize_index(index), value)

def __str__(self):
return f"SourceList({self._list})"

def __repr__(self):
return self.__str__()

s = list( 'Example' )
t = SourceList( 'Example' )

print( repr( s[ 0: 2 ] ))
print( repr( t[ -1: 2 ] ))

.

1

rocksolid light 0.9.8
clearnet tor