Posts: 1,583
Threads: 3
Joined: Mar 2020
As mentioned, you append() bytes. Since each element from the bytearray is a byte, that should work fine.
>>> b = bytearray()
>>> for x in bytearray(b'abc'):
... b.append(x)
...
>>> b
bytearray(b'abc')
Posts: 6,823
Threads: 20
Joined: Feb 2020
You don't do this:
Quote:>>> b=bytearray()
>>> for x in bytearray(b'abc'):
... b.extend(x)
You do this:
>>> b.extend(b'abc')
If you want to append x, and you don't know what x is, you could do some duck typing.
junk = (5, (1, 2, 3), b"123")
my_byte_array = bytearray(b"This is my bytearray: ")
for thing in junk:
try:
my_byte_array.extend(thing)
except TypeError:
my_byte_array.append(thing)
print(my_byte_array) Or if you wanted to be a little more specific:
junk = (5, (1, 2, 3), b"123")
my_byte_array = bytearray(b"This is my bytearray: ")
for thing in junk:
try:
iter(thing)
except TypeError:
my_byte_array.append(thing)
else:
my_byte_array.extend(thing)
print(my_byte_array) Output: bytearray(b'This is my bytearray: \x05\x01\x02\x03123')
Posts: 4,654
Threads: 1,497
Joined: Sep 2016
Mar-14-2023, 10:19 PM
(This post was last modified: Mar-14-2023, 10:20 PM by Skaperen.)
each element from a bytes is a byte, also. it wants ints. so a bytearray or bytes of len 1 is not good enough:
Output: lt1a/forums/3 /home/forums 5> py
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> b = bytearray()
>>> for x in b'abc':
... print(repr(x))
... b.append(x)
...
97
98
99
>>> b
bytearray(b'abc')
>>> b.append(100)
>>> b.append(b'e')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bytes' object cannot be interpreted as an integer
>>> b.append(bytearray(b'e'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bytearray' object cannot be interpreted as an integer
>>>
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 4,654
Threads: 1,497
Joined: Sep 2016
apparently a byte (no ' s') is (equivalent to) an int for bytearray.append() to work with:
Output: lt1a/forums/3 /home/forums 6> py
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> b = bytearray()
>>> b.extend(b'abc')
>>> b
bytearray(b'abc')
>>>
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 6,823
Threads: 20
Joined: Feb 2020
Mar-15-2023, 01:18 AM
(This post was last modified: Mar-15-2023, 01:18 AM by deanhystad.)
I do not understand any of your last 3 posts. Do you have a question?
Posts: 4,654
Threads: 1,497
Joined: Sep 2016
(Mar-15-2023, 01:18 AM)deanhystad Wrote: I do not understand any of your last 3 posts. Do you have a question?
i did post a question (post #8). reading answers changes that question, now. how to i extend a bytearray with another bytearray? originally i wanted to use .append() thinking a bytearray was always "one thing" regardless of length and as such did not check for .extend() . now i know i must treat any of the string types as the sequences they are and work it all together that way.
and i realized i could append one (and only one) byte code to a bytearray with .append() by giving it an int.
Output: lt1a/forums/2 /home/forums 4> py
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a=bytearray()
>>> a.append(97)
>>> a.append(98)
>>> a.append(99)
>>> a.append(100)
>>> a
bytearray(b'abcd')
>>>
i was also exploring other alternatives (slicing) to achieve the original goal. i found bytes can be extended onto bytearrays as ints. understanding that a bytearray is a sequence of ints will be somewhat confusing since a list of ints is a different thing, yet basically the same but, with a different API.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 6,823
Threads: 20
Joined: Feb 2020
Mar-16-2023, 02:11 AM
(This post was last modified: Mar-16-2023, 02:13 AM by deanhystad.)
a.extend(b) works fine if a is a bytearray and b is bytes, a bytearray, or a list of ints.
Posts: 6,823
Threads: 20
Joined: Feb 2020
Mar-16-2023, 03:06 PM
(This post was last modified: Mar-16-2023, 03:06 PM by deanhystad.)
You cannot append int objects to a byte array. You can only append bytes. You can specify a byte using an int value in the range 0 to 255, which makes it look like you can append int objects, but Python only uses the int object to create a byte object.
x = bytearray()
for y in range(100000):
x.append(y) Error: Traceback (most recent call last):
File "...test.py", line 3, in <module>
x.append(y)
ValueError: byte must be in range(0, 256)
Notice the error is from byte, not bytearray.
Posts: 4,803
Threads: 77
Joined: Jan 2018
(Mar-14-2023, 10:19 PM)Skaperen Wrote: each element from a bytes is a byte, also. it wants ints. Python documentation Wrote:While bytes literals and representations are based on ASCII text, bytes objects actually behave like immutable sequences of integers, with each value in the sequence restricted such that 0 <= x < 256 (attempts to violate this restriction will trigger ValueError).
Posts: 4,654
Threads: 1,497
Joined: Sep 2016
Mar-18-2023, 11:24 PM
(This post was last modified: Mar-18-2023, 11:24 PM by Skaperen.)
can it be said, when an int is used to be the value of a byte that the int is " coerced" from an int into a byte? can it be thought of that there is a byte type but it is not available directly (limited to internal use) and one must use an int in the context that works with int, and so on?
in the unnamed language i created 3 decades ago, that Python reminds me of, there was a "byte" type in which literals for it could be written like ints (123, 0x7b, etc.). yeah, it did create some ambiguity issues i never got around to resolve (supply chain issues for round tuits).
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
|