Skip to content

__setitem__ key type check#5

Closed
JakubBlaha wants to merge 7 commits intoCode0x58:masterfrom
JakubBlaha:master
Closed

__setitem__ key type check#5
JakubBlaha wants to merge 7 commits intoCode0x58:masterfrom
JakubBlaha:master

Conversation

@JakubBlaha
Copy link
Copy Markdown
Contributor

Doing something like

JsonStore('test.json')[1] = 2

resulted in the error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\jakub\AppData\Local\Programs\Python\Python37-32\lib\site-packages\jsonstore.py", line 154, in __setitem__
    path, _, key = name.rpartition(".")
AttributeError: 'int' object has no attribute 'rpartition'

which didn't really tell what the problem is. Therefore I have added a check for the name parameter in the __setitem__ method, which will now raise a TypeError if something other then a str is used as a key.

@JakubBlaha
Copy link
Copy Markdown
Contributor Author

Also sorry about all of the commits. I am quite new to working with git and don't really know how to clean it up...

@Code0x58
Copy link
Copy Markdown
Owner

Thanks for reporting and working on this @JakubBlaha! I think the PR would disallow things like store["list", 1:], so I had a go at it in last night in #6 and tried to clean up some of the code and exceptions. Would you mind reviewing it to see if it helps in the case you encountered?

Re. using git, you can do things like git commit --amend to overwrite your last commit to include your new changes, or with you get to the end of a chain of commits, you might find something like git rebase -i helpful. The docs here look like they're good on the topic.

@JakubBlaha
Copy link
Copy Markdown
Contributor Author

Thanks a lot for rewriting so much of the code! Now I am even more exceited to use this amazing library.

The only thing that this library is missing now is a support to directly append to lists stored in a json file. I would be really excited to work on this feture. I am unsure about few things and probably would need a bit of advice. Should I open a new issue?

@JakubBlaha JakubBlaha closed this May 16, 2020
@Code0x58
Copy link
Copy Markdown
Owner

Code0x58 commented May 16, 2020

:D That PR may now allow you to do the direct appending in a sneaky way with slices:

store.list = ["foo"]
# as long as the slice start is >= the list length this will work
store["list", 9999:] = ["bar"]
assert store.list == ["foo", "bar"]

... I just had a play, and it looks like you just do:

store.list = ["foo"]
# as long as the slice start is >= the list length this will work
store["list"] += ["bar"]
assert store.list == ["foo", "bar"]

Adding to the README and a test to make sure all behaves as expected sounds good to me, maybe something like this:

store.list = []
extension = [{"key": "value"}]

# make sure += happens
store["list"] += extension
store.list += extension
assert len(store.list) == 2

# make sure a deepcopy occurred
assert store.list[0] is not extension[0]

@JakubBlaha
Copy link
Copy Markdown
Contributor Author

JakubBlaha commented May 16, 2020

This is interesting. I thought this would not work since you are using deepcopying.

return deepcopy(obj)

Edit: I am talking about this: store["list"] += extension

@Code0x58
Copy link
Copy Markdown
Owner

Code0x58 commented May 16, 2020

Given this magic method behaviour [src]:

If a specific method is not defined, the augmented assignment falls back to the normal methods. For instance, if x is an instance of a class with an __iadd__() method, x += y is equivalent to x = x.__iadd__(y) . Otherwise, x.__add__(y) and y.__radd__(x) are considered, as with the evaluation of x + y.

I think store["list"] += extension gets evaluated as store["list"] = store["list"] + extension, which my do more copies than absolutely needed, but should get the job done.

As long as you use things like store[key1, key2, ...] += extension the deepcopying shouldn't get in your way. It would stop things like store.dict["list"] += extension or store.list.extend(extension) from behaving as you might hope though.

Tests may be best for making sure though

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

Successfully merging this pull request may close these issues.

2 participants