'if' statement only accomplished only once in a loop

I am simulating a cache system by performing a cumulative sum of bytes and, when this sums up to 0.95 the size of the cache, forces the value to be 0.9 of the full size.

Final result should evolve as a saw tooth.

This is the code I implement to force this condition.

cache_size=1e11 #B 
high_mark=0.95
low_mark=0.50
ordered_datestamp['Cache']=0
for i in range(1,rows_number):
    ordered_datestamp['file_size_Cum'][i]=ordered_datestamp['file_size_Cum'][i-1]+ordered_datestamp['file_size'][i]
    ordered_datestamp['Cache'][i]=ordered_datestamp['file_size_Cum'][i]
    if(ordered_datestamp['Cache'][i]>high_mark*cache_size):
        ordered_datestamp['Cache'][i]=low_mark*ordered_datestamp['Cache'][i]

And this is the output.

Simulation output

How does the if statement is only accomplished once in the loop?


Asked by: Maria846 | Posted: 06-12-2021






Answer 1

Given the code, I don't see why the observed behavior is wrong. This is how you are capping the cumulative sum:

ordered_datestamp['Cache'][i]=low_mark*ordered_datestamp['Cache'][i]

You are scaling the value by low_mark factor and not actually capping it. So it is possible that it goes beyond the cache size. You probably intend to do:

ordered_datestamp['Cache'][i]=low_mark*cache_size

Also, I don't see where ordered_datestamp['file_size_Cum'][0] is initialized. You are initializing ordered_datestamp['Cache']=0 which is weird as ordered_datestamp['Cache'] seems to be a list.

Answered by: Caroline813 | Posted: 07-01-2022



Similar questions

python - How would a system tray application be accomplished on other platforms?

Windows has the "system tray" that houses the clock and alway-running services like MSN, Steam, etc. I'd like to develop a wxPython tray application but I'm wondering how well this will port to other platforms. What is the tray equivalent on each platform, and how much manual work would be required to support Windows, OSX and Linux (which shells in particular would be friendliest).


Python + ZMQ: Operation cannot be accomplished in current state

I am trying to get a python program to communicate with another python program via zeromq by using the request-reply pattern. The client program should send a request to the server program which replies. I have two servers such that when one server fails the other takes over. Communication works perfect when the first server works, however, when the first server fails and when I make a request to the second server,...


python - Pillow, centering of text not working, how is this accomplished?

I have tested the calculations and the math is correct (and takes into account the height and width of the font), but after Python creates the image and I put it into Photoshop, the vertical and horizontal centering of the text is not correct. Should I be doing something else with my code? from PIL import Image, ImageDraw, ImageFont # base = Image.open("Images/Phones/KK17018_Navy_KH10089.jpg").con...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top