Comparing dateutil.relativedelta

2024/10/12 6:25:11

I'm trying to do a '>' comparison between two relativedeltas:

if(relativedelta(current_date, last_activity_date) > relativedelta(minutes=15)):

Here is the output from the debugger window in Eclipse:

debug window

One of the relativedeltas is only 15 minutes-- far smaller than the other one. Why does this comparison return false and not true as expected? What would be a better solution?

Answer

dateutil.relativedelta doesn't implement __cmp__ sensibly, so instances can't be compared. There's an open bug on the issue; the argument that it doesn't make sense to say whether 29 days or 1 month is greater, and that therefore the whole thing falls back on python's default comparisons seems a bit flimsy to me, but that's just an opinion.

Depending on what you're actually doing, using datetime.timedelta may be a better solution.

https://en.xdnf.cn/q/69683.html

Related Q&A

Python. Argparser. Removing not-needed arguments

I am parsing some command-line arguments, and most of them need to be passed to a method, but not all.parser = argparse.ArgumentParser() parser.add_argument("-d", "--dir", help = &q…

Where can I find some hello world-simple Beautiful Soup examples?

Id like to do a very simple replacement using Beautiful Soup. Lets say I want to visit all A tags in a page and append "?foo" to their href. Can someone post or link to an example of how to …

Difference between isnumeric and isdecimal in Python

What is the difference between isnumeric and isdecimal functions for strings ( https://www.tutorialspoint.com/python3/python_strings.htm )? They seem to give identical results: >>> "1234…

Estimating zip size/creation time

I need to create ZIP archives on demand, using either Python zipfile module or unix command line utilities. Resources to be zipped are often > 1GB and not necessarily compression-friendly.How do I e…

How to rename the index of a Dask Dataframe

How would I go about renaming the index on a dask dataframe? I tried it like sodf.index.name = foobut rechecking df.index.name shows it still being whatever it was previously.

Determine if a line segment intersects a polygon

If I have a vector (a line consisting of 2 points) on a 2D plane how can I determine if it has passed through a polygon? I know I can take each line which makes up the polygon and see if any intersect…

How to send a value from Arduino to Python and then use that value

I am in the process of building a robot that is remote controlled using Python to send control messages via the Internet through a simple GUI.I have gotten part of my code working pretty well, the GUI …

recaptcha wasnt solving by anticaptcha plugin in selenium python

Ive recently started using selenium for a project Ive been working on for a while that involves automation. One of the roadblocks in the plan was the ReCaptcha system, so I decided to use anti-captcha …

Python - check for class existance

Is there a way to check if a class has been defined/exists? I have different options on a menu, and some of them require inherited variables, so if you try to select that function before you have set …

getting a matplotlib colorbar tick outside data limits for use with boundaries keyword

I am trying to use a colorbar to label discrete, coded values plotted using imshow. I can achieve the colorbar that I want using the boundaries and values keywords, which makes the maximum value of the…