2929 unpack_pnp_id ,
3030)
3131from .compile import compile_file
32+ from .tools import chunk
3233from .tools .checksum import xor_bytes
3334from .usbconnection import USBConnection
3435
@@ -295,17 +296,13 @@ async def run(self, py_path, wait=True, print_output=True):
295296 length = len (mpy ).to_bytes (4 , byteorder = "little" )
296297 await self .send_message (length )
297298
298- # Divide script in chunks of bytes
299- n = 100
300- chunks = [mpy [i : i + n ] for i in range (0 , len (mpy ), n )]
301-
302299 # Send the data chunk by chunk
303300 with logging_redirect_tqdm (), tqdm (
304301 total = len (mpy ), unit = "B" , unit_scale = True
305302 ) as pbar :
306- for chunk in chunks :
307- await self .send_message (chunk )
308- pbar .update (len (chunk ))
303+ for c in chunk ( mpy , 100 ) :
304+ await self .send_message (c )
305+ pbar .update (len (c ))
309306
310307 # Optionally wait for the program to finish
311308 if wait :
@@ -424,11 +421,6 @@ async def run(self, py_path, wait=False):
424421 with open (py_path , "rb" ) as demo :
425422 program = demo .read ()
426423
427- chunk_size = 512
428- chunks = [
429- program [i : i + chunk_size ] for i in range (0 , len (program ), chunk_size )
430- ]
431-
432424 while response is None or "transferid" not in response :
433425 response = await self .send_command_and_get_response (
434426 "start_write_program" ,
@@ -450,15 +442,15 @@ async def run(self, py_path, wait=False):
450442 with logging_redirect_tqdm (), tqdm (
451443 total = len (program ), unit = "B" , unit_scale = True
452444 ) as pbar :
453- for chunk in chunks :
445+ for c in chunk ( program , 512 ) :
454446 response = await self .send_command_and_get_response (
455447 "write_package" ,
456448 {
457- "data" : base64 .b64encode (chunk ).decode ("ascii" ),
449+ "data" : base64 .b64encode (c ).decode ("ascii" ),
458450 "transferid" : transferid ,
459451 },
460452 )
461- pbar .update (len (chunk ))
453+ pbar .update (len (c ))
462454
463455 await asyncio .sleep (0.5 )
464456 response = await self .send_command_and_get_response (
@@ -798,17 +790,13 @@ async def run(self, py_path, wait=True, print_output=True):
798790 length = len (mpy ).to_bytes (4 , byteorder = "little" )
799791 await self .send_block (length )
800792
801- # Divide script in chunks of bytes
802- n = 100
803- chunks = [mpy [i : i + n ] for i in range (0 , len (mpy ), n )]
804-
805793 # Send the data chunk by chunk
806794 with logging_redirect_tqdm (), tqdm (
807795 total = len (mpy ), unit = "B" , unit_scale = True
808796 ) as pbar :
809- for chunk in chunks :
810- await self .send_block (chunk )
811- pbar .update (len (chunk ))
797+ for c in chunk ( mpy , 100 ) :
798+ await self .send_block (c )
799+ pbar .update (len (c ))
812800 finally :
813801 self .loading = False
814802
0 commit comments