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

Presented here are two modules, "html_help" & "Zam". Both modules contain two classes.

"html_help" contains "html_table" and "html_month". A html_table produces tables with a readable format. A html_month is just a logical extention of the html_table.

"Zam" contains "array" and "matrix". An array is 1D list with fixed sized and mutable internals. A matrix is the same except for the fact that it is 2D.

Python, 233 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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
================================================================================
html_help.py
================================================================================
import time
import Zam

class html_table:

    def __init__(self, rows, columns, indent, style):
        self.__matrix = Zam.matrix(rows, columns, '')
        self.__indent = indent
        self.__style = style
        self.__table_option = ''
        self.__row_option = ''
        self.__column_option = ''

    def mutate(self, row, column, text):
        assert type(text) is str
        self.__matrix[row][column] = text
        return self

    def access(self, row, column):
        return self.__matrix[row][column]

    def table_option(self, string):
        assert type(string) is str
        self.__table_option = string
        return self

    def row_option(self, string):
        assert type(string) is str
        self.__row_option = string
        return self

    def column_option(self, string):
        assert type(string) is str
        self.__column_option = string
        return self

    def html(self):
        html = self.__style * self.__indent + '<table'
        if self.__table_option:
            html += ' ' + self.__table_option
        html += '>\n'
        for row in self.__matrix:
            html += self.__style * (self.__indent + 1) + '<tr'
            if self.__row_option:
                html += ' ' + self.__row_option
            html += '>\n'
            for item in row:
                html += self.__style * (self.__indent + 2) + '<td'
                if self.__column_option:
                    html += ' ' + self.__column_option
                html += '>\n'
                html += ''.join([self.__style * (self.__indent  + 3) + line + '\n' for line in item.splitlines()])
                html += self.__style * (self.__indent + 2) + '</td>\n'
            html += self.__style * (self.__indent + 1) + '</tr>\n'
        return html + self.__style * self.__indent + '</table>'

class html_month:

    def __init__(self, year, month, indent, style):
        self.matrix = matrix = self.__make_matrix(year, month)
        self.__table = html_table(len(matrix) + 1, 7, indent, style)
        for index, item in enumerate(('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')):
            self.__table.mutate(0, index, '<b>' + item + '</b>')
        for row in range(len(matrix)):
            for column in range(7):
                if matrix[row][column]:
                    self.__table.mutate(row + 1, column, '<b>' + str(matrix[row][column]).zfill(2) + '</b>\n<hr>\n')

    def __make_matrix(self, year, month):
        rows = [Zam.array(7, 0)]
        row = 0
        now = time.localtime(time.mktime(time.strptime(str(year).zfill(2) + ' ' + str(month).zfill(2) + ' 01', '%y %m %d')) + 14400)
        self.__first_day = (now.tm_wday + 1) % 7
        once = False
        while now.tm_mon == month:
            if once:
                if now.tm_wday == 6:
                    rows.append(Zam.array(7, 0))
                    row += 1
            else:
                once = True
            rows[row][(now.tm_wday + 1) % 7] = now.tm_mday
            self.__days_in_month = now.tm_mday
            now = time.localtime(time.mktime(now) + 86400)
        return rows

    def mutate(self, day, text):
        row, column = self.__get_pos(day)
        self.__table.mutate(row, column, self.__table.access(row, column)[:15] + text)
        return self

    def access(self, day):
        row, column = self.__get_pos(day)
        return self.__table.access(row, column)[15:]

    def __get_pos(self, day):
        assert 1 <= day <= self.__days_in_month
        pos = self.__first_day - 1 + day
        return pos / 7 + 1, pos % 7

    def table_option(self, string):
        self.__table.table_option(string)
        return self

    def row_option(self, string):
        self.__table.row_option(string)
        return self

    def column_option(self, string):
        self.__table.column_option(string)
        return self

    def html(self):
        return self.__table.html()
================================================================================
Zam.py
================================================================================
# Name & Description
# ==================

'''Support module for array and matrix use.

This module provides two classes that emulate one and two
dimentional lists with fixed sizes but mutable internals.'''

# Data & Imports
# ==============

__all__ = ['array', 'matrix']
__version__ = '1.1'

import sys

# Public Names
# ============

class array(object):

    '''array(length) -> new array
    array(length, value) -> initialized from value'''

    def __init__(self, length, value=None):
        '''x.__init__(...) initializes x'''
        self.__data = range(length)
        for index in range(length):
            self.__data[index] = value

    def __repr__(self):
        '''x.__repr__() <==> repr(x)'''
        return repr(self.__data)

    def __len__(self):
        '''x.__len__() <==> len(x)'''
        return len(self.__data)

    def __getitem__(self, key):
        '''x.__getitem__(y) <==> x[y]'''
        return self.__data[key]

    def __setitem__(self, key, value):
        '''x.__setitem__(i, y) <==> x[i]=y'''
        self.__data[key] = value

    def __delitem__(self, key):
        '''x.__delitem__(y) <==> del x[y]'''
        self.__data[key] = None

    def __iter__(self):
        '''x.__iter__() <==> iter(x)'''
        return iter(self.__data)

    def __contains__(self, value):
        '''x.__contains__(y) <==> y in x'''
        return value in self.__data

class matrix(object):

    '''matrix(rows, columns) -> new matrix
    matrix(rows, columns, value) -> initialized from value'''

    def __init__(self, rows, columns, value=None):
        '''x.__init__(...) initializes x'''
        self.__data = array(rows)
        for index in range(rows):
            self.__data[index] = array(columns, value)

    def __repr__(self):
        '''x.__repr__() <==> repr(x)'''
        return repr(self.__data)

    def __len__(self):
        '''x.__len__() <==> len(x)'''
        return len(self.__data)

    def __getitem__(self, key):
        '''x.__getitem__(y) <==> x[y]'''
        return self.__data[key]

    def __setitem__(self, key, value):
        '''x.__setitem__(i, y) <==> x[i]=y'''
        self.__data[key] = array(len(self.__data[key]), value)

    def __delitem__(self, key):
        '''x.__delitem__(y) <==> del x[y]'''
        self.__data[key] = array(len(self.__data[key]))

    def __iter__(self):
        '''x.__iter__() <==> iter(x)'''
        return iter(self.__data)

    def __contains__(self, value):
        '''x.__contains__(y) <==> y in x'''
        for item in self.__data:
            if value in item:
                return True
        return False

# Private Names
# =============

def main():
    print 'Content-Type: text/plain'
    print
    print file(sys.argv[0]).read()

# Execute Main
# ============

if __name__ == '__main__':
    main()

A demonstration of this code can be found at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/475112 (with a slightly longer html_table.__init__).

Note: the DST bug should be fixed now.

Created by Stephen Chappell on Sat, 11 Mar 2006 (PSF)
Python recipes (4591)
Stephen Chappell's recipes (233)

Required Modules

  • (none specified)

Other Information and Tasks