Skip to content

Appending to key does not work #30

Open
@AltayAkkus

Description

@AltayAkkus

Hello,
I've noticed that using .append() on a PickleShare Key does not represent the changes that were made.

Example

Command Line 1:

>>> db = PickleShareDB('C:\\Users\\NULL\\AppData\\Local\\Temp\\test')
>>> db['stack'] = []
>>> db['stack']
[]

Command Line 2:

>>> db = PickleShareDB('C:\\Users\\NULL\\AppData\\Local\\Temp\\test')
>>> db['stack']
[]

Command Line 1:

>>> db['stack'].append("Hello World!")
>>> db['stack']
['Hello World!']

Command Line 2:

>>> db['stack']
[]

In fact, I added my own debugging, and the .append() does not even trigger the __setitem__ function of the PickleShareDB class.

>>> from pickleshare import *
>>> db = PickleShareDB('test')
This is the debugged version.
>>> db['testvalue'] = 1234567
Set item called with key:testvalue and value:1234567
>>> db['stack'] = []
Set item called with key:stack and value:[]
>>> db['stack'].append("Hello")

Fix

Since the __setitem__ magic method only triggers when the value of a key is set, or updated, it does not trigger when you .append() something.
Also, there is no magic method that triggers when an .append() happens
So I suggest adding an .append() function:

    def append(self, key, value):
        currentValue = self[key]
        currentValue.append(value)
        updatedValue = currentValue
        self[key] = updatedValue

Test

Command Line 1:

>>> db = PickleShareDB('C:\\Users\\NULL\\AppData\\Local\\Temp\\test')
>>> db['stack'] = []
>>> db['stack']
[]

Command Line 2:

>>> db = PickleShareDB('C:\\Users\\NULL\\AppData\\Local\\Temp\\test')
>>> db['stack']
[]

Command Line 1:

>>> db.append('stack', 'Hello World!')
>>> db['stack']
['Hello World!']

Command Line 2:

>>> db['stack']
['Hello World!']

It would be nice if you could overwrite the default .append() function, like db['stack'].append(0) but since every db['key'] is not an instance of the class PickleShareDB but a list, I don't want to mess with it :D

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions