Posts: 6
Threads: 2
Joined: Feb 2019
Mar-06-2019, 03:12 AM
(This post was last modified: Mar-06-2019, 03:56 AM by Larz60+.)
Hi Everyone,
I need help on knowing if we can create a memoryview object for "List". I tried doing the basic steps, but this is the error I am getting:
>>> a = [1, 2, 3]
>>> type(a)
<class 'list'>
>>> memoryview(a) Error: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: memoryview: a bytes-like object is required, not 'list'
So, is there a way to create memoryview object for list datatype? Or it is not possible at all?
Thanks for help.
Posts: 12,023
Threads: 484
Joined: Sep 2016
>>> from array import array
>>> a = array('h', [1,2,3])
>>> mview = memoryview(a)
>>> mview.tolist()
[1, 2, 3]
Posts: 6
Threads: 2
Joined: Feb 2019
But I need to convert list to memoryview object, and not a memoryview object to list
Posts: 12,023
Threads: 484
Joined: Sep 2016
Look at the code closer.
I convert to memory view and then back again
mview is the memoryview.
Posts: 6
Threads: 2
Joined: Feb 2019
Mar-06-2019, 09:11 AM
(This post was last modified: Mar-06-2019, 09:11 AM by saumyagoel.)
Ok. Now I got your point. Thanks for this suggestion.
So, it basically means, that only array types support buffer protocol and not lists?
Posts: 1,950
Threads: 8
Joined: Jun 2018
Mar-06-2019, 09:13 AM
(This post was last modified: Mar-06-2019, 09:27 AM by perfringo.)
Good practice is to make sure you understand what memoryview is. Best place to start is Python official documentation ( class memoryview(obj))
Quote:Create a memoryview that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray.
So we need bytes or bytarray (or have our own class).
Maybe following will give general idea:
>>> m = memoryview(b'[1, 2, 3]')
>>> list(m)
[91, 49, 44, 32, 50, 44, 32, 51, 93]
>>> for el in m:
... print(chr(el))
...
[
1
,
2
,
3
]
>>> type(m)
memoryview
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 12,023
Threads: 484
Joined: Sep 2016
I'm pretty sure that is the case, although I haven't found anything yet that states this definitively.
I haven't found any examples of straight list conversion
Posts: 6
Threads: 2
Joined: Feb 2019
@ perfringo, thanks for this aspect. I am able to understand it in a better way.
Posts: 6
Threads: 2
Joined: Feb 2019
Would like to know one more thing, I wanted to try the same behavior for complex number list.
Eg -
>>> a = list([1+1j,2+2j])
But we can't represent the complex number to byte/bytearray, since these are not integers as per my knowledge.
Can you provide more inputs on this?
Posts: 1,950
Threads: 8
Joined: Jun 2018
First of all - why memoryview? From documentation:
Quote:memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying.
'withoug copying' means that main advantage of memoryview is with large binary data. Instead of copying one can have memoryview object.
One additional tidbit from documentation about memoryview and release:
Quote:Many objects take special actions when a view is held on them (for example, a bytearray would temporarily forbid resizing); therefore, calling release() is handy to remove these restrictions (and free any dangling resources) as soon as possible.
Regarding complex numbers - what do you want to accomplish? Bytes and bytearray objects contain single bytes (bytes is immutable and bytearray is a mutable sequence)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|