Python Help

Wardawg1001

Senior member
Sep 4, 2008
653
1
81
This is not homework.

I am fairly new at python, and I'm working with a script that has a function defined in it. Here is the snippet of code thats giving me an issue:

if not ((iDD > 0 and iDD < 32 and iMM == 1 )
or (iYYYY &#37; 4 == 0 and iYYYY % 400 != 0 and iDD > 0 and iDD < 30 and iMM == 2 )
or (iDD > 0 and iDD < 29 and iMM == 2 )
or (iDD > 0 and iDD < 32 and iMM == 3 )
or (iDD > 0 and iDD < 31 and iMM == 4 )
or (iDD > 0 and iDD < 32 and iMM == 5 )
or (iDD > 0 and iDD < 31 and iMM == 6 )
or (iDD > 0 and iDD < 32 and iMM == 7 )
or (iDD > 0 and iDD < 32 and iMM == 8 )
or (iDD > 0 and iDD < 31 and iMM == 9 )
or (iDD > 0 and iDD < 32 and iMM == 10 )
or (iDD > 0 and iDD < 31 and iMM == 11 )
or (iDD > 0 and iDD < 32 and iMM == 12 )):
sys.stderr.write("invalid day\n")
return -1

Basically its parsing a date, and checking the values in iDD, iMM, and iYYYY to make sure that they form a valid date of a year. I thought that it might have been an earlier piece of code that wasn't storing the variables correctly, but I've confirmed that the variables form valid dates, its just this piece of code isn't working right. The two dates it checks currently store the values:

iDD = 31, iMM = 12, iYYYY = 2002
iDD = 02, iMM = 01, iYYYY = 2001

For the life of me I can't figure out why these values are causing the "invalid day" error to generate every time. Any insight is appreciated!

EDIT: The forum wont recognize white space. Note that the 'if not' and 'or' statements are all indented to the same column, while the 'sys.stderr' and 'return' lines are both indented by one more 'tab.'
 

Wardawg1001

Senior member
Sep 4, 2008
653
1
81
Sigh, I figured it out already, of course right after I ask for help, and of course its a simple fix. Needed to cast all of the values as integers inside the 'if not' statement.
 

Doublejr

Senior member
Jul 25, 2004
205
0
0
There are code tags that help formatting :)

You can also optimize it a little by eliminating some of the repeated comparisons. I don't code in python so this may not be proper syntax.

Code:
if not ((iDD > 0 and iDD < 32 and iMM == 1 )
or (iYYYY % 4 == 0 and iYYYY % 400 != 0 and iDD > 0 and iDD < 30 and iMM == 2 )
or (iDD > 0 and iDD < 29 and iMM == 2 )
or (iDD > 0 and iDD < 32 and (iMM == 3 or iMM == 5 or iMM == 7 or iMM == 8 or iMM == 10 or iMM == 11)
or (iDD > 0 and iDD < 31 and (iMM == 4 or iMM == 6 or iMM == 9 or iMM == 11 )

I might have missed some months in my implementation, but you get the idea. It could be optimized further I am sure. Good luck with your project.
 

bobross419

Golden Member
Oct 25, 2007
1,981
1
0
I'm no Python programmer, but couldn't you use a built in date function of some kind?

I found this one really fast, not sure if it will work for your needs:
http://docs.python.org/library/datetime.html#date-objects
class datetime.date(year, month, day)All arguments are required. Arguments may be ints or longs, in the following ranges:

  • MINYEAR <= year <= MAXYEAR
  • 1 <= month <= 12
  • 1 <= day <= number of days in the given month and year
If an argument outside those ranges is given, ValueError is raised.
Just attempt to create a new instance of datetime.date with the arguments for month/day/year. You'd have to handle the ValueError, but you won't have to worry about some ugly if statement.

Too much time reading thedailywtf helps me get into the mindset of looking for tools for repetitive tasks ;)
 

Wardawg1001

Senior member
Sep 4, 2008
653
1
81
Thanks for the responses guys.

I have a more general question this time around.

The script I've written is reading in a large text document (about 120,000 rows, average of 100-150 characters per row), and I've added a timer to it so I could judge how quickly it is running. Problem is I'm getting pretty wildly varying times, on 10 runs I've gotten anywhere from 90 seconds to 130 seconds. I'm curious how this is happening because its doing the exact same thing, reading the same file, creating the same output, etc. Nothing is changing between runs but its taking significantly longer sometimes, and I'm not sure why. Any insights?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,835
4,815
75
My guess: your disk is doing other things, and interrupting the smooth linear reading. Try it on a different disk, or SSD, if you have one.