Solved

Code Interpretation Needed

Posted on 2015-02-24
6
87 Views
Last Modified: 2015-02-24
def _submit_callback(self):
       
 Looking at this snippet, what is the :

 self.months.index(self.end_month.get()) + 1,   doing?

Is there something there that will get say the ending month as count the number of days in that month?    


   # check that the input values are a real, legitimate date
        try:      
            start = date(int(self.start_year.get()),
                         self.months.index(self.start_month.get()) + 1,
                         int(self.start_day.get()))
           
            end = date(int(self.end_year.get()),
                         self.months.index(self.end_month.get()) + 1,
                         int(self.end_day.get()))
0
Comment
Question by:xoxomos
  • 3
  • 2
6 Comments
 

Author Comment

by:xoxomos
ID: 40629472
It's in some Python code i'm looking at.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 40629662
This is only a guess from the (very) limited amount of info that you've provided, but..


self.end_month       is probably a field of some sort that accepts the month, possibly as a string, ie. JAN or FEB, etc

self.end_month.get()       returns that month as a string, ie. "JAN"

self.months       is an array on all the month names as strings, ie. [ "JAN", "FEB", "MAR", "APR", .... ]

self.months.index()       looks up the array to find the index given a value passed in, so...

self.months.index(self.end_month.get())       if the end_month was "JAN", it will find "JAN" in the array which is at index = 0 and so return 0 (Note, array indexes are 0 based, ie. the first item in the array is index 0, the second item is index 1, etc)

self.months.index(self.end_month.get()) + 1       just adds 1 to the index retrieves. This is to cater for the above fact that indexes are 0 based. So if the end_month is "JAN", the index in the array will be 0 and then we add 1, so the result is 1 which is what the date() function would require, ie. the months of the year, "JAN" -> "DEC" have indexes in the 0 -> 11 so we add one to get the range 1 -> 12
0
 

Author Comment

by:xoxomos
ID: 40629666
Makes so much sense.  Mil gracias.
0
 

Author Closing Comment

by:xoxomos
ID: 40629668
Thanx
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40629681
You're welcome! :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question