Source

weeklib / weeklib.py

#!/usr/bin/env python
#-*- coding: UTF-8 -*-


import datetime


class WeekDb(object):

    def __init__(self, year):

        if isinstance(year, basestring):
            year = int(year)
        self.__week_db = self.__init_db(year)


    def __init_db(self, year):

        if isinstance(year, basestring):
            year = int(year)

        start_day = datetime.date(year, 1, 1)
        stop_day = datetime.date(year, 12, 31)

        week_db = {}
        for x in range(0, (stop_day - start_day).days):
            day = start_day + datetime.timedelta(days=x)
            week_num = day.isocalendar()[1]
            if week_num not in week_db:
                week_db[week_num] = []
            week_db[week_num].append(day)

        return week_db


    def __getitem__(self, x):

        if isinstance(x, basestring):
            x = int(x)

        return self.__week_db[x]


def getweeknum(day=None):

    if not day:
        day = datetime.date.today()

    return day.isocalendar()[1]


if __name__ == '__main__':

    import sys

    argv = sys.argv[1:]
    args = len(argv)
    if args >= 1:
        week_num = argv[0]
        if args >= 2:
            year = sys.argv[1]
        else:
            year = datetime.date.today().year
        db = WeekDb(year)
        print db[week_num]
    else:
        print 'usage: python %s WEEK_NUM [YEAR]'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.