Skip to content

Example code single integer ValueError #29

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

Closed
FoamyGuy opened this issue Dec 17, 2021 · 5 comments
Closed

Example code single integer ValueError #29

FoamyGuy opened this issue Dec 17, 2021 · 5 comments

Comments

@FoamyGuy
Copy link
Contributor

In the simpletest example there is commented out code that writes multiple bytes to the FRAM device at once:

# values = list(range(100)) # or bytearray or tuple
# fram[0] = values
# print(fram[0:99])

But if you uncomment and attempt to run this code it raises this error:

Traceback (most recent call last):
  File "code.py", line 31, in <module>
  File "adafruit_fram.py", line 173, in __setitem__
ValueError: Data must be a single integer for single addresses

I think the line writing the data needs to include a slice of the proper length like this:

fram[0:99] = values

I tested that locally and it seems to work, though I have very little experience with FRAM so I can't be certain it's correct.

@tekktrik
Copy link
Member

tekktrik commented Dec 18, 2021

That's new from the introduction of slices I added, the intention was to be more in line with Issue #26 and make use of slice notation. You're correct, now that it's implemented, ideally the example should be fram[0:100] = values and that should work (which you said, but I think it needs to be [0:100] which is accounting for Issue #30. The default behavior could now be to just take values[0] and make that the value of fram[0] in this context.

@tekktrik
Copy link
Member

So I could add that functionality and/or update the example, happy to do whatever makes most sense!

@FoamyGuy
Copy link
Contributor Author

My understanding of #26 is that normal python behavior for:

fram[0] = [2,3,4,5]

would store the list [2,3,4,5] at index 0 in fram. If fram were a regular list this would be possible since any type of thing can be stored in the indexes.

But because fram is not actually a regluar list but instead a list-like API to the hardware storage, essentially the same as a bytearray it's not possible to lists in the individual indexes. It's only possible to store ints in each of the indexes in frame.
so that same code:

fram[0] = [2,3,4,5]

Should probably raise some error indicating that you are not allowed to store a list object in that index, only a int.

I think we do need to update the example either way since we don't want it to raise an exception.

I do think it's also good to go ahead update the library as well to raise the error about not being able to store list objects instead of ints like the example shown in #26

@tekktrik
Copy link
Member

Sounds good! I'll submit a PR that:

  1. Changes the warning to say that only ints can be stored in an index. I think that was the intention meant to be communicated with this error text, but your phrasing makes much more sense!
  2. Updates the example accordingly by removing the problematic line, or stores a list of ints correctly like fram[0:3] = [1, 2, 3]

@FoamyGuy
Copy link
Contributor Author

closed by #32

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

2 participants