[Tutor] Assistance request when using python 'calendar'

dn PyTutor at DancesWithMice.info
Wed Aug 5 11:37:54 EDT 2020


On 05/08/2020 09:39, o1bigtenor wrote:
>>> I regularly work in planning through multiple years at once.
>>> This means that I like to have a lot of stuff available in a calendar
>>> function.
>>> Python seems to be locked when I need to display more than 1 year at a
>>> time.
>>>
>>> I don't see a way to display something like 3 years worth of calendar
>>> starting at a point 23 months from now.
>>> (I see how to display 1 year at a time but not multiple years.)
>>
>>          Bottom up: Multiple (Jan-Dec) years is done by looping once for each
>> year and combining the returned data yourself.
>>
>>          For arbitrary start/end months you will have to loop asking for each
>> month, and you will have to combine that data into whatever structure makes
>> it suitable for your output criteria.
>>
> Very interesting, a program from the mid 1980's called 'cal' is far
> more flexible
> and productive.
> 
> Over the weekend (a long weekend here) I actually had to use 3 different
> iterations of the calendar. It is not uncommon in this task to need to work for
> over much more than 3 years. As the present iteration only works from today's
> date as soon as the time frame stumbles forward the calendar must needs
> be extended.
...

>>          This appears to be a feature request -- in that you are dissatisfied by
>> the calendar module as it exists. You haven't shown any code attempting to
>> develop your solution, nor any exploration of outside sources.
..

> I have been looking for about 4 years for software usable over a period of at
> least 2 years. Had found 'cal' but was frustrated that I could only get a
> display of 3 months in width no matter what terminal screen column count
> was available because the 80 x 25 terminal is hard wired into 'cal'. So I was
> looking at how to misdirect cal into believing that it could write to a 220+
> column wide terminal. That would have given me the flexibility in use and
> most of the features I needed. In retrospect maybe I had complained a few
> too many times about hard limits in programs carrying over some almost 40
> years and so it was quite to my surprise when my friend sent me a few pages
> of source code and said - - - - try it, tell me what doesn't work. So the last
> version I received was about the 6th (IIRC). This was something he worked
> on sporadically likely as much for amusement as for the pleasure of doing it.
> We traded ideas and concepts in a very very large variety of areas and with
> spring approaching we both got busy with other ideas and projects. He was
> not able to complete the program.
...

>> If you need to produce output in a format other than one of the available
>> defaults, you can use calendar to calculate the dates and organize the
>> values into week and month ranges, then iterate over the result yourself.
>> The weekheader(), monthcalendar(), and yeardays2calendar() methods of
>> Calendar are especially useful for that sort of work.
>> """
> 
> The second of the above examples has some ideas which need to be
> extended to get closer to what I need.
> My research has shown that week numbers is something that is actually
> quite a foreign concept in many cultures as this just isn't the way things
> are done. Except - - - - - if I give you a date and ask you for the date
...

> There are LOTS of calendars like that out there. They mostly all have one
> huge limitation - - - - they work within one (1) year - - - so when you need
> 2.5 to 3 - - - - better luck next time - - - - and yes I've looked. Installed I
> don't know how many business/project management programs and its
> one of the common joys - - - - as soon as you want to look at a calendar
> over more than one years time - - - highly unlikely.
>>
>>          I would also point out that the calendar module is in the Python
>> library directory, and is a Python source code file (not some compiled
>> C-language extension), so you can readily study it to work out potential
>> subclassing. In particular, you may wish to study the .formatyear() methods
>> of the text and HTML classes.
> 
> I have spent some time looking for 'official' Python information on sub-classes
> and their development. Found nothing to date but have only spent some 45
> minutes so maybe with more time some to be found - - - maybe.
> 
>>
>>          Even if you manage to create a subclass which can take a start
>> year/month and an end year/month, take note that you have no ability to
>> annotate this data; that is, you can not mark specific dates as having some
>> event happen on that date. The best you could do is generate an HTML output
>> and later edit the cells of the HTML tables to contain your annotations. If
>> you intend to create a full "business planning" application you'll have to
>> extend calendar to work with something that can hold the annotations
>> between sessions (save/load logic), probably using raw datetime objects for
>> each day, and modifying/subclassing calendar so that it can use the
>> datetime object embedded in your data to determine where to place it on the
>> output calendar.
>>
> Much work to be done and it is ever so fascinating to see how prevalent
> business thinking is confined into small time frames. But then business is
> primarily worried about profitability per quarter - - - - - well that
> is what the
> senior staff earn their bonuses on - - - - but here - - -  I really do need to
> work across at least 3 and preferably even 5 years but that's considered
> to be a corner case - - - - .


This was discussed at some length on the Python-List.

The PSL library produces calendars for a 12-month horizon or by the 
month. Instead of thinking of 40-months as three-years plus four-months. 
Perhaps think of it as 40 separate months that will be joined into a 
single, cohesive, unit? (as previously suggested)

Now, the limitation is the width of the presentation-screen (which 
appears to be the specification, so please say if that is not so).

The discussion of dates and their presentation is faulty. There is an 
ISO standard (8601) for dates (https://www.w3.org/QA/Tips/iso-date) - 
although its presentation may not suit some, eg Arab and Jewish 
definitions of "weeks" and US resistance to "international" or "world". 
Specifically, the calendar library offers week-numbers (see previously 
offered web.ref).

Aside: I was amused by @wlfraed's comment about HTML calendars. Some 
time last century I was introducing a municipality's first Internet and 
Intranet sites (as they termed the project - it was last century!) and I 
wanted an Events Calendar. The young HTML 'programmer' I had been given 
claimed it couldn't be done. So, I spent the next weekend teaching 
myself HTML and proving him wrong - and a new career-direction was born! 
Oh, and yes, each date could be a link, to a list of events 'on' that 
day. The question is rarely, can it possibly be done - but rather: how 
much effort will it take and do I have the problem-solving ability to 
achieve the required/desired result?

So, with sympathies for your friend, and kudos for the idea of migrating 
to Python, what have you achieved? Right now the conversation seems to 
be repeating itself (from Python to Tutor-list).

Two of us have suggested taking the output from calendar, no matter how 
traditional, and post-processing that/bending it to your will. What did 
you try, and how far did you get?

(as above) How about showing us (the core of) some code which produces 
'something' - together with a critique of the short-comings. Then we may 
be able to assist you to make progress...

Remember, the way to solve a 'big problem' is to break it down into 
smaller problems (which are more easily solved)!

Suggestions/recommendations to include in a future response:
1) clear summary-list of functional requirements/limitations
2) how to deal with the concept of 'width' (further to above comments 
about the limits of 80-col screens and your-previous of wider 
screen-resolution) and relate that back to (mono) font-width or 
string-lengths, ie how do you relate a calendar ~four years long to 
physical display limitations?
3) sample code to collect the calendar's first and last year-month, or 
year-week, extent
4) sample code to calculate the weekNR of the first year-month (if 
that's the input spec)
5) any other steps you have achieved to-date (perhaps similar to the 
lines of interactive-console prototyping illustrated elsewhere)
-- 
Regards =dn


More information about the Tutor mailing list