Data generation with defined size (byte)

How can I generate a dictionary that contains random values with different sizes: 100 bytes, 200 bytes, 1 kbytes, 1 Gbytes, etc., ?

I have already tried with getsizeof, but I didn't get what I was expecting.

output=[]
var=[]
max=input("bytes size \n")

while ((sys.getsizeof(var))- max)<=0:

    sensor = {
        'dt': time.time(),
        'data': random.randint(10,10),
        'unit': 'Celsius'
    }

    var.append(sensor)

    if ((sys.getsizeof(var))- max)<=0:
        output.append(sensor)

    #print(sys.getsizeof(output))

data=str(output)
jdata=json.dumps(data)
print (jdata)

Answers


use this

import os
import sys
fout = open('output_file', 'wb')
fout.write(os.urandom(1024)) # replace 1024 with size_kb if not unreasonably large
fout.close()
fin = open('output_file', 'rb')
content = fin.read()
print sys.getsizeof(content)-21
# and from here do anything with content

EDIT

After reading again your question and analyse your code, the code work fine. You wil never get an output with exact size because your size of sensor objects is not multiple of 1.

The previous code create only a dummy data with a specific size


Need Your Help

Mailer does net send mails in engine

ruby-on-rails email ruby-on-rails-4 rails-engines

I'm coding a newsletter engine in rails 4 for a simple eCommerce system.

Xceed property grid - [RefreshProperties(RefreshProperties.All)] attribute not working

c# wpf xaml propertygrid xceed

I have a class which has properties, based on selecting one item in the combobox property, other properties will be shown or hidden. I am using [RefreshProperties(RefreshProperties.All)] for the co...