With pyjamas HEAD edbefad4c3729dcd9d05f3be8e06df8aa9dfd916:
I have an app that fetches data from a server via json-rpc. The data
is cached in the browser as a sorted list. Of course, I can't afford
to hold the whole database in my browser, so I delete items from the
list if it grows beyond a certain size. The general algorithm is:
if len(cache) > MAX:
if adding_to_beg_of_cache:
# delete from opposite (right side) of list
del cache[MAX:]
elif adding_to_end_of_cache:
# delete from opposite (left side) of list
del cache[:-MAX]
The first del works, the one in the ELIF does not. If I change the
second del to:
del cache[:len(cache)-MAX]
then it works.
Attached is a demo.
Is this a real issue or am I neglecting something?
Cheers,
Daniel
import pyjd
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.HorizontalPanel import HorizontalPanel
from pyjamas.ui.Button import Button
from pyjamas.ui.HTML import HTML
from pyjamas.ui.Label import Label
from pyjamas import Window
ITEMS = 50
MAX = 20
PREAMBLE = '''
This demonstrates how pyjamas does not handle negative indices in del
slices. <p>With a cache of %d items, but with a MAX of only %d, after
deleting from the right I will have items [0,...,19], but when I
delete from the left I <em>should</em> have items [30,...,49], but
nothing happens: the slice is not deleted.
<hr>
''' % (ITEMS, MAX)
class deltest:
def __init__(self):
self.do_layout()
self.reset()
def do_layout(self):
rp = RootPanel()
rp.add(HTML(PREAMBLE))
self.label = label = Label()
rp.add(label)
hp = HorizontalPanel(Spacing=50)
self.b1 = b1 =Button('Click to remove from the left', self.del_left)
self.b2 = b2 = Button('Click to remove from the right', self.del_right)
hp.add(b1)
hp.add(b2)
rp.add(hp)
rp.add(Button('Reset', self.reset))
def reset(self, w=None):
self.cache = range(ITEMS)
self.b1.setEnabled(True)
self.b2.setEnabled(True)
self.update_label()
def update_label(self):
self.label.setText('Length: %d values: %s' %
(len(self.cache),str(self.cache)))
def del_left(self, w):
# should leave cache with [30,...,49]
del self.cache[:-MAX]
# This works
#del self.cache[:len(self.cache)-MAX]
self.b1.setEnabled(False)
self.b2.setEnabled(False)
self.update_label()
def del_right(self, w):
# should leave cache with [0,...,19]
del self.cache[MAX:]
self.b1.setEnabled(False)
self.b2.setEnabled(False)
self.update_label()
def onModuleLoad():
deltest()
if __name__ == '__main__':
pyjd.setup("public/deltest.html")
onModuleLoad()
pyjd.run()