#while loop in python

89 messages · Page 1 of 1 (latest)

gleaming parcelBOT
#

@solid dune

Blocked Web TLDs

To maintain a wholesome and friendly environment we block all links to websites using certain TLDs. Your message has been deleted.

Detected TLD(s)
xxx

solid dune
#

print function wont execute inside while loop
heres my implementation

import socket
from socket import AF_INET, SOCK_STREAM
import struct

with socket.socket(AF_INET, SOCK_STREAM) as cl_sock:
    cl_sock.connect((HOST, PORT))

    # getting the name of file
    name = cl_sock.recv(1024).decode()

    print(f"name received filename: {name}")

    # getting the size of file
    packed_size = cl_sock.recv(8)
    total_buffer_size = struct.unpack("Q", packed_size)[0]

    print(f"size received totalsize: {total_buffer_size}")

    # bcz dynamic allocation sux:(
    bytes_received = bytearray(total_buffer_size)

    print(f"getting akshuall file now pls work")
    # getting the akshual file
    while (x := total_buffer_size) > (y := len(bytes_received)):
        print("working...?")
        chunk_size = min(4096, total_buffer_size - bytes_received)
        chunk = cl_sock.recv(chunk_size)
        bytes_received.extend(chunk)
        # walrus bcz me lazy + no lsp :/
        print(f"almost there {x}/{y}")

    # saving file :)
    with open(name, "wb") as file:
        file.write(bytes_received)

print("script executed successfully")
#

code works just fine tho

spice jewel
#

You're looping while total_buffer_size is greater than len(bytes_received) but when you create bytes_received you tell it to fill it with total_buffer_size zeros so its length is always equal to total_buffer_size and so total_buffer_size is never greater than len(bytes_received)

#

And that was a run on sentence...

#
bytes_received = bytearray()
while total_buffer_size > len(bytes_received):
    print("working...?")
    chunk_size = min(4096, total_buffer_size - bytes_received)
    chunk = cl_sock.recv(chunk_size)
    bytes_received.extend(chunk)
    print(f"almost there {total_buffer_size}/{len(bytes_recieved)}")
#

You need an empty bytes_recieved for your code to work

solid dune
#

appreciate it

spice jewel
#

Also no need for assignment expressions in your while condition

solid dune
#

yeah i was just coding without anything...

#

never done that

#

like without even a type checker or lsp

solid dune
#

while total_buffer_size > len(bytes_received):
print("working...?")
chunk_size = min(4096, total_buffer_size - len(bytes_received))
chunk = cl_sock.recv(chunk_size)
bytes_received.extend(chunk)
print(f"almost there {total_buffer_size}/{len(bytes_recieved)}")

#

_ _
ik it isnt much but i was coding without a code editor......

#

also i thought not doing dynamic allocation would be an option for me since i already know the file size :/

spice jewel
#

The first and second time it's the initial value, the third time its a new value. So if you assign it to y it'll be wrong in the third use

#

So if you assign it anywhere, assign it in the third use

solid dune
#

ooo yesss it gets evaluated from there

spice jewel
#
bytes_received = bytearray()
y = 0
while total_buffer_size > y:
    print("working...?")
    chunk_size = min(4096, total_buffer_size - y)
    chunk = cl_sock.recv(chunk_size)
    bytes_received.extend(chunk)
    y = len(bytes_recieved)
    print(f"almost there {total_buffer_size}/{y}")
#

Might actually be faster to just add chunk_size to y

#
bytes_received = bytearray()
y = 0
while total_buffer_size > y:
    print("working...?")
    chunk_size = min(4096, total_buffer_size - y)
    chunk = cl_sock.recv(chunk_size)
    bytes_received.extend(chunk)
    y += chunk_size
    print(f"almost there {total_buffer_size}/{y}")
solid dune
#

interesting... dont call it y plz

solid dune
# spice jewel Might actually be faster to just add chunk_size to y
    rb_list = bytearray(total_buffer_size)

    print(f"getting akshuall file now pls work")
    # getting the akshual file
    start = 0
    while chunk := cl_sock.recv(4096):
        rb_list[start : start + len(chunk)] = chunk
        start += len(chunk)
        print(f"bytes received: {total_buffer_size}/{start}")
#

btw my whole concept of array is ruined now

#

its not an array

solid dune
#

tested it with an image and it worked

#

valid phyton 💀


  4 barray = bytearray(9)
  3 print(len(barray))
  2 barray[0:3] = 2, 2, 2, 8, 90
  1
 12  print(len(barray))
~
#

i thought arrays were data structures of fixed len

solid dune
solid dune
solid dune
#

bro how is this more efficient

    y = 0
    while total_buffer_size > y:
        chunk = cl_sock.recv(4096)
        rb_list.extend(chunk)
        y += len(chunk)
        print(f"bytes received: {total_buffer_size}/{y}")

this took:
total time taken: 0.008171840978320688
....
while my implementation took:
total time taken: 0.009359975985717028

#

i mean ive only tested it on an image will try sending the os file through it

spice jewel
#

More efficient than what specifically?

spice jewel
#

That's doing a copy

rb_list[start : start + len(chunk)] = chunk

So yeah, slow

#

Extend drops the existing object into the bytearray while using a slice forces the data to be copied over to fill the existing memory that belongs to the bytearray

coarse stratus
#

been too long since I've done python this all just looks like squiggles to me ngl

spice jewel
coarse stratus
#

bro I speak polish, I can understand English perfectly well

#

but that....naw

#

that ain't English, tf is an rb_list

#

why you gotta abbreviate everything and love the underscores

#

and when I saw "y"as a variable... bro ima cry

#

I'm going to my corner, this is just too much for me

solid dune
coarse stratus
#

that's betterrrrrr

#

should've just written that

solid dune
coarse stratus
#

oh yeah a list in python is just a dynamic array right?

solid dune
#

wbrb_seq?

coarse stratus
#

nah

#

what is that

#

rib_ sequence

solid dune
coarse stratus
#

idk I hate static arrays, they made me wanna kill someone

solid dune
coarse stratus
#

close

#

bytes, bits, ribs

solid dune
#

lol

coarse stratus
#

also tf you analysing data for? you making a bomb or smthn?

solid dune
coarse stratus
#

I mean you can always work around static arrays, just make a new one and release the old one and whatever

solid dune
coarse stratus
#

oh

#

you just downloading shit

#

or uploading

solid dune
#

ye

#

both

#

since its not the full code

coarse stratus
#

aha

solid dune
coarse stratus
#

what are you even transfering

solid dune
#

eh i tested it on an image

coarse stratus
#

aha so uhhhh I assume you just did it cause why not

solid dune
coarse stratus
#

or you tryna move your hentai stash without anyone looking, I get it been there tok

#

God if my gf saw me saying that I'd be dead, she's literally right next to me god save me

solid dune
coarse stratus
#

you got your damn IP in the open like that??

solid dune
coarse stratus
#

wait why tf you.got your ip out like that, yknow having it out won't change anything lol

solid dune
coarse stratus
#

like oh wow some is gonna ddos meeee but wait I can just calllllll my isp and they will c**kblock yoy

#

I mean most people don't know that you can abuse the ntp servers to send terabytes of data per second, thanks a proper fucking ddos

solid dune
#

#💬general

coarse stratus
#

nah nah wait uhhh yeah I got no excuse

#

ok see you there