Let's you have a science experiment called Weather
and it records general i开发者_运维百科nformation about the weather, one of which is recording the humidity
every minute. After say 10 hours, you'll have 600 database values stored.
Weather.objects.filter().exclude(time__lt = commence).exclude(time__gt = cease)
This would create a filter for the Weather
table, between the commence
and cease
times.
Let's say the time criteria is equal to -4 hours from present, therefore it would return the last 4 hours worth of data which is equal to 300 values. How would I ask for an interval of 10? Meaning, from these 300 values, how would I receive the 10th one of each, so 10, 20, 30, 40 ... 280, 290, 300... which would mean 30 values are being returned from the filter.
EDIT:
Would it be possible to ask for an interval of say 10 seconds? As opposed to each individual 10th value..
SQL doesn't have a way to ask for every tenth, so ORMs won't typically give you a way to express that need. You could do that work in Python:
samples = list(Weather.objects.filter().exclude(time__lt=commence).exclude(time__gt=cease))
for s10 in samples[::10]:
# do something with every tenth...
This will pull all the samples from the database in one query, then only use 10% of them. You could also pull only the ones you need, one to a query:
sample_count = 600 # Somehow determine how many samples are in your time window.
samples = Weather.objects.filter().exclude(time__lt=commence).exclude(time__gt=cease)
for s_num in range(0, sample_count, 10):
s = samples[s_num]
# Now you have one sample in s.
To elaborate: range
takes three arguments, start, stop, and step. For example:
>>> range(0, 100, 10)
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
In the second example, the range expression is used to create the index values of the samples we want. Then samples[s_num]
evaluates the QuerySet to get just that one value from the database.
精彩评论