Hallo zusammen,

wie gestern besprochen, sende ich Euch ein Python Skript für die Bestimmung des 
ersten Donnerstags im Monat. Das Skript kann auch für jeden anderen Wochentag 
verwendet werden, vorausgesetzt, es wird nach dem Vorkommen des Wochentags in 
der ersten Woche des betrachteten Monats gesucht.

Das Script ist dokumentiert. Ggfs. gibt es eine einfachere Lösung. Dann bitte 
entsprechend über die Liste informieren. Das Skript kann auf der Konsole 
aufgerufen als auch als Modul importiert werden.

Fragen beantworte ich gerne.

Viele Grüße

Georg
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
-------------------------------------------------------------------------------
Program: find_days_of_1st_week.py

Find the first day in the first week by giving the name of the day, e. g. Thursday.

Parameters:

   month - Month (integer)
   year - Year (interger)
   name of the day - Name of the day searched for in the first week of a month (string)

Operation:

   Prints the date of the day searched for

Calling example:

   python find_day_of_1st_week.py 1 2011 Thursday

-------------------------------------------------------------------------------

The module cotains to functions which deliver the first day given by name, e. g. 
Thursday for the first week of a month.
It can be run from the command line or imported as a module into
other programs. It shall be used in a DocuWiki calendar widget.

This python program makes use of the possibility to terminate each command
with a ";" if the is no other termination symbol on the line. This makes it
possible to ship the code over the internet and not loosing the program
structure which is essential in Python program.

Documentation is done with NaturalDocs (see http://www.naturaldocs.org).

-------------------------------------------------------------------------------

(C)opyright by TroLUG (Georg Maubach) 2010
All rights reserved.

$License$
This program is licensed under GPLv2 or higher.

$Version$
Version 1.0 (2010-12-30)

-------------------------------------------------------------------------------

"""

def find_first_day(year, month, name_of_day):
   """
   Function: find_first_day

   Find the first day with a given name, e. g. Thursday in
   the first week of a month

   Parameters:

      year - Year (integer)
      month - Month (integer)
      name_of_day: Name of the day searched for, e. g. Thursday (string)

   Returns:

      Number of the first day with the name searched for, e. g. Thursday
      as an integer

   """
   import calendar

   calendar.setfirstweekday(calendar.MONDAY)

   # Define a list of days as integers as a basis for the search for day
   days = [d +1 for d in xrange(7)];

   # Define a dictionary to be able to translate a name of a day into
   # a corresponding integer that can be put into the method "calendar.weekday"
   dayMap = {"Monday": 0,
             "Tuesday": 1,
             "Wednesday": 2,
             "Thursday": 3,
             "Friday": 4,
             "Saturday": 5,
             "Sunday": 6};

   # Search for the first day with a given name, e. g. Thursday
   # in the first week of a given month and year
   for day in days:
      if (calendar.weekday(year, month, day) == dayMap[name_of_day]):
        return(day); 

def print_all_first_days(year, name_of_day):
   """
   Function: print_all_first_days

   Print all first days given by name of the first week of all months in a year.

   Parameters:

      year - Year (integer)
      name_of_day - Name of the day searched for, e. g. Thursday (string)

   Returns:

      Prints a list of days given by their name of the first week of each month

   """

   # Define a list of months as integers for input into the "calendar.weekday"
   months = [m +1 for m in xrange(12)];

   for month in months:
      day = find_first_day(year, month, name_of_day);
      print name_of_day + ": " + str(day) + "." + str(month) + "." + str(year);

if (__name__ == "__main__"):
   import sys
   import calendar

   year = int(sys.argv[2]);
   month = int(sys.argv[1]);
   name_of_day = sys.argv[3];

   # Example 1
   day = find_first_day(year, month, name_of_day);
   print "\n\nWochentag beginnend mit 0 = Montag: " + str(calendar.weekday(year, month, day));
   print "In diesem Fall ist " + name_of_day + " der 3. Tag in der Woche, da die Zaehlung bei '0' beginnt.\n";

   # Example 2
   print "Gesamte Liste der Tage in der ersten Woche des Monats";
   print_all_first_days(year, name_of_day);

# EOF

_______________________________________________
Trolug_trolug.de mailing list
trolug@trolug.de
https://ml01.ispgateway.de/mailman/listinfo/trolug_trolug.de

Antwort per Email an