Welcome, guest | Sign In | My Account | Store | Cart

Represents the so-called "perpetual clock" ( http://www.evilmadscientist.com/article.php/perpetualcalendar ) with numbers instead of pennies. Thinking about making a simple window with heads/tails images.

Python, 73 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'''Program made by riddle to represent the so-called "perpetual calendar"
( http://www.evilmadscientist.com/article.php/perpetualcalendar )
BTW: if you don't know what year it is, you're just fucked.
'''
from datetime import date

daysofweek = ("Sunday","Monday","Tuesday",
              "Wednesday","Thursday","Friday","Saturday")

months = ("January","February","March","April","May","June","July","August",
          "September","October","November","December")

today = (date.today().isoweekday()+1,
         date.today().isoformat()[5:7],
         date.today().isoformat()[8:])

class Matrix(object):
    '''Matrix type is initialized with two args: # of columns and # of rows'''
    def __init__(self, cols, rows):
        self.cols = cols; self.rows = rows; self.matrix = []
        for i in range(rows):
            ea_row = []
            for j in range(cols): ea_row.append(0)
            self.matrix.append(ea_row)

    def setitem(self, col, row, x):
        self.matrix[col-1][row-1] = x
    
    def getitem(self, col, row):
        return self.matrix[col-1][row-1]
    
    def __repr__(self):
        outStr = ""
        for i in range(self.rows): outStr += '%s\n' % (self.matrix[i])
        return outStr

      
def d2b(n):
    '''Returns the base 2 value of non-negative int n (base 10) as a list'''
    binStr= ''
    if n < 0: raise ValueError("number must be non-negative")
    if n == 0: return '0'
    while n > 0: binStr = str(n%2)+binStr; n = n>>1
    return list(binStr)

    
def binarycalendar(dweek=today[0], mnth=today[1], dmonth=today[2]):
    '''Returns, as a Matrix, a binary representations of
   the day of the week, the month, and the day of month.
   
   Keyword arguments:
   dweek -- day of the week; defaults to today
   mnth -- month; defaults to the current month
   dmonth -- day of the month; defaults to today
   
   '''
    bcal = (d2b(dweek),d2b(int(mnth)),d2b(int(dmonth)))
    bcmatrix= Matrix(3,5)
    for x in range(3):
        if len(bcal[x])<5:
            for y in range(5-len(bcal[x])): bcal[x].insert(0,'0')
        for y in range(5): bcmatrix.setitem(y+1,x+1,bcal[x][y])
    bcmatrix.setitem(1,1,' ')
    bcmatrix.setitem(2,1,' ')
    bcmatrix.setitem(1,2,' ')
    return bcmatrix
   

if __name__ == '__main__':
    print(str(binarycalendar().__repr__()).replace('[','')
          .replace('\'','').replace(',','').replace(']',''))
    print("%s, %s %s" %
          (daysofweek[int(today[0]-1)], months[int(today[1])-1], today[2]))

1 comment

sundanceriddle (author) 10 years, 7 months ago  # | flag

perpetual calendar* :O