1

i currently have a result set with the following data:

((0L, datetime.date(2018, 1, 29)), (0L, datetime.date(2018, 1, 30)), (0L, 
datetime.date(2018, 1, 31)), (0L, datetime.date(2018, 2, 1)))

I am trying to check if the current date has the status 0 or 1.

Currently i do this:

   if checkdate():
        if result_set[0] == 1:

The checkdate function checks if the currentdate is inside the list somewhere. But there i'm stuck trying to figure out if that date has the status 1 or 0. The status is the first item on the list (0l and 1L) mysql somehow adds a L after the 0 and 1.

Currently my checkdate function looks like this:

return any(d[1] == cd for d in result_set)

Would love some help!


Edit: Now, checkdate looks like this:

return next(((s,d) for (s,d) in dates if d == date), None)

entry = checkdate([x[1] for x in result_set], cd)
            if entry is not None:
7
  • Can you define what do you mean by status 0 or 1 ? Commented Feb 1, 2018 at 10:36
  • Yes my bad, the first result in the lists (0L and 1L) is supposed to be the status. in my database this is either 0 or 1. MySQL adds a L for some reason. Commented Feb 1, 2018 at 10:37
  • Change function checkdate to return the index of the current date in the list, or -1 if it is not in the list. Then you can simply do something like index = checkdate(); if index >= 0 and result_set[index][0] == 1: .... Commented Feb 1, 2018 at 10:38
  • @goodvibration That sounds good. How would i do this? My current checkdate function is the following: return any(d[1] == cd for d in result_set) Commented Feb 1, 2018 at 10:39
  • 1
    You probably should redesign the data, and use a dictionary that maps date objects to ints. Commented Feb 1, 2018 at 10:39

2 Answers 2

1

You should change your checkdate function to return the actual (status, date) pair instead of just whether such a pair exists. For this, you can translate your any expression almost 1:1 into a next statement, using None as the default in case no such pair exists.

For example, like this (I also changed the function to take parameters instead of using global variables, but that's not essential for this to work).

import datetime
result_set = ((0, datetime.date(2018, 1, 29)), (1, datetime.date(2018, 1, 30)),   
              (0, datetime.date(2018, 1, 31)), (0, datetime.date(2018, 2, 1)))
cd = datetime.date(2018, 1, 30)

def checkdate(dates, date):
    return next(((s, d) for (s, d) in dates if d == date), None)

entry = checkdate(result_set, cd)
if entry is not None and entry[0] == 1:
    print("found")

Or like this, if you prefer using the global result_set and cd variables:

def checkdate():
    return next(((s, d) for (s, d) in result_set if d == cd), None)

entry = checkdate()
if entry is not None and entry[0] == 1:
    print("found")
Sign up to request clarification or add additional context in comments.

2 Comments

Trying this i get the following error: datetime.date is not iterable
@WesleyvanStraalen I just saw your edit. You should pass the list of (status, date) pairs to checkdate, not just the dates as you seem to do.
0

Change function checkdate to return the index of the current date in the list, or -1 if it is not there.

Then you can simply do something like:

index = checkdate()
if index >= 0 and result_set[index][0] == 1:
    ...

4 Comments

Why not return the tuple itself or None?
Seems nice, how would i make the function return the index?
@hjpotter92: That's also an option (and probably not the only one).
@WesleyvanStraalen: How about iterating the list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.