下面的代码显示了每小时的预约次数和“没有预约”#39;如果没有预约。
我想连续分组'没有预约'根据时间插槽
例如,当前代码的输出是 -
# plotting all the file data and saving the plots
import os
import csv
import matplotlib.pyplot as plt
def graphWriterIRIandRut():
m = 0
List1 = []
List2 = []
List3 = []
List4 = []
fileList = []
for file in os.listdir(os.getcwd()):
fileList.append(file)
while m < len(fileList):
for col in csv.DictReader(open(fileList[m],'rU')):
List1.append(col['x1'])
List2.append(col['y1'])
List3.append(col['x2'])
List4.append(col['y2'])
plt.subplot(2, 1, 1)
plt.grid(True)
# colors = np.random.rand(2)
plt.plot(List1,List2,c=colors)
plt.tick_params(axis='both', which='major', labelsize=8)
plt.subplot(2, 1, 2)
plt.grid(True)
# colors = np.random.rand(2)
plt.plot(List1,List3,c=colors)
plt.tick_params(axis='both', which='major', labelsize=8)
m = m + 1
continue
plt.show()
plt.gcf().clear()
plt.close('all')
我希望输出为
10:00 AM to 11:00 AM -> No Appointment
11:00 AM to 12:00 PM -> No Appointment
12:00 PM to 01:00 PM -> No Appointment
代码如下: -
10:00 AM to 01:00 PM -> No Appointment
答案 0 :(得分:1)
首先,可以准备阵列:
@time_group = ['6:00:00','7:00:00', ...]
@stacked = @time_group.each_with_object([]) do |start, memo|
range = Time.parse(start)..Time.parse(start)+3600
booked = Appointment.where(start_time: range)
if memo.empty? || !booked.empty? || !memo.last.last.empty?
memo << [range, booked] # no sequenced empty slots
else
memo << [memo.pop.begin..range.end, booked] # merge ranges
end
end.to_h # { ranges => booked }
现在让我们输出它们:
<% @stacked.each do |range, booked| %>
.....
<% end %>
答案 1 :(得分:0)
试试这个
<% @time_group = ['6:00:00','7:00:00','8:00:00','9:00:00','10:00:00','11:00:00','12:00:00','13:00:00','14:00:00','15:00:00','16:00:00','17:00:00','18:00:00','19:00:00','20:00:00','21:00:00','22:00:00','23:00:00'] %>
#Loop through each hour in @time_group
<% @time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% @booked = Appointment.where(start_time: t1..t2) %>
#It will holds the No Appointment Timings into it
no_appointment_array = []
#if appointment is available show appointment else show no appointment
<% if @booked.size > 0 %>
#Calling the Function
display_no_appointment(no_appointment_array) if not no_appointment_array.empty?
#Show time interval e.g 1:00 PM to 2:00 PM
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
<% @booked.each do |app| %>
<%= app.start_time.strftime('%I:%M') %>
<% end %>
<% else %>
no_appointment_array << [Time.parse(t).strftime('%I:%M %p'), (Time.parse(t) + 3600).strftime('%I:%M %p')]
<% end %>
<% end %>
def display_no_appointment(no_appointment_array)
puts "#{no_appointment_array.first[0]} to #{no_appointment_array.last[1]} -> No Appointment"
#It Clears all Previous No Appointment
no_appointment_array.clear
end
答案 2 :(得分:0)
首先,我建议你使用'@ booked.present?'而不是'if @ booked.size&gt; 0'
你尝试过这样的事吗? 我没有测试过这段代码,这对于一个可能的解决方案几乎是一个想法。 希望它对你有所帮助。
<% no_appointment_starts_at = nil %>
<% no_appointment_ends_at = nil %>
<% @time_group.each_with_index do |t,index| %>
<% t1 = Time.parse(t) %>
<% t2 = Time.parse(t) + 3600 %>
<% @booked = Appointment.where(start_time: t1..t2 %>
#Show time interval e.g 1:00 PM to 2:00 PM
#if appointment is available show appointment else show no appointment
<% if @booked.present? %>
<% @booked.each do |app| %>
<% unless no_appointment_ends_at.blank? %>
<%= Time.parse(no_appointment_starts_at).strftime('%I:%M %p') %> to <%= (Time.parse(no_appointment_ends_at) + 3600).strftime('%I:%M %p') %>
<% no_appointment_ends_at = nil%>
<% end %>
<%= Time.parse(t).strftime('%I:%M %p') %> to <%= (Time.parse(t) + 3600).strftime('%I:%M %p') %>
<%#= app.start_time.strftime('%I:%M') %>
<% no_appointment_starts_at = t2 %>
<% end %>
<% else %>
<% last_appointment_ends_at = t2 %>
<% end %>
<% end %>