Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8401373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:47:23+00:00 2026-06-09T21:47:23+00:00

I am writing an application for time_slot in python please look at this code

  • 0

I am writing an application for time_slot in python

please look at this code

from datetime import datetime ,timedelta
appointments = [(datetime(2012, 5, 22, 10), datetime(2012, 5, 22, 10, 30)),

                    (datetime(2012, 5, 22, 12), datetime(2012, 5, 22, 13)),

                    (datetime(2012, 5, 22, 15, 30), datetime(2012, 5, 22, 17, 10))]

hours = (datetime(2012, 5, 22, 9), datetime(2012, 5, 24, 18))
duration = timedelta(minutes = 120)
def get_slots(hours, appointments,duration):

    slots = sorted([(hours[0], hours[0])] + appointments + [(hours[1], hours[1])])

    for start, end in ((slots[i][1], slots[i+1][0]) for i in range(len(slots)-1)):

        assert start <= end, "Cannot attend all appointments"

        while start + duration <= end:
            dt_obj = start
            date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")


            today18 = start.replace(hour=17, minute=59, second=59, microsecond=0)

            if start <  today18:
               print "{:%d:%H:%M} - {:%d:%H:%M}".format(start, start + duration)

               start += duration
            else:
               start += timedelta(hours = 15)
if __name__ == "__main__":
    get_slots(hours, appointments,duration)

The above code is working fine for me when i am running it like python time_slots.py

But when i am using above code in djanog view like

from datetime importdatetime ,timedelta 
    duration = timedelta(minutes = 60)
    appointments = [(datetime(2012, 5, 22, 10), datetime(2012, 5, 22, 10, 30)),

                (datetime(2012, 5, 22, 12), datetime(2012, 5, 22, 13)),

                (datetime(2012, 5, 22, 15, 30), datetime(2012, 5, 22, 17, 10))]


   # hours = (datetime.datetime(2012, 5, 24, 18), datetime.datetime(2012, 5, 22, 9))
    hours = (datetime(2012, 5, 22, 9), datetime(2012, 5, 24, 18))
    slots = sorted([(hours[0], hours[0])] + appointments + [(hours[1], hours[1])])
    for start, end in ((slots[i][1], slots[i+1][0]) for i in range(len(slots)-1)):

        assert start <= end, "Cannot attend all appointments"

        while start + duration <= end:

            today18 = start.replace(hour=17, minute=59, second=59, microsecond=0)

            if start <  today18:

               print "{:%H:%M} - {:%H:%M}".format(start, start + duration)

            else:
               start += timedelta(hours = 15)

and when i am calling this view it’s returning an infinite loop in console

Please help me what i am doing wrong here i want the available time slots as i am getting in time_slots.py

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:47:25+00:00Added an answer on June 9, 2026 at 9:47 pm

    In the Django view, you have forgotten to add duration to start in the if start < today18 clause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing small application in python which read records from Oracle and insert
I am writing application in Linux using C, pthreads and sockets. This will be
I'm writing an application in Python with Postgresql 8.3 which runs on several machines
I'm writing an application that needs to read a list of strings from a
I am writing this application in Java, where I have a JTree on the
Problem Description I am writing application for Android and use native code, And test
I am writing an application that is supposed to fetch the source code of
I'm writing an application which takes data input from a series of arbitrary RSS
I am writing an application in Qt using python that will process a txt
I am writing an application to allow users to schedule one-time long-running tasks from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.