Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appending to key does not work #30

Open
AltayAkkus opened this issue Jul 15, 2022 · 0 comments
Open

Appending to key does not work #30

AltayAkkus opened this issue Jul 15, 2022 · 0 comments

Comments

@AltayAkkus
Copy link

AltayAkkus commented Jul 15, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant