Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148429
  • 博文数量: 35
  • 博客积分: 2386
  • 博客等级: 大尉
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-05 06:11
文章分类

全部博文(35)

文章存档

2011年(1)

2010年(2)

2009年(32)

分类:

2009-12-10 23:17:37

Version 0.00

# -*- coding: utf-8 -*-


# counter_demo.py V0.00


# TODOS:
# 1) Combine format with the counter information
# 2) Define global counter for consistent recording
# 3) Make use of session mechanism to prevent refreshing pages


from django.http import HttpResponse

# use self-defined counter
from counter import Counter


def counter_demo(request):
    record = Counter()
    record.open()
    record.incre()
    record.update()
    
    output = '''
        
            网站访问计数器Demo    
            您是第 %s 位访问者
        
    '
'' % record.get()
    
    record.close()

    return HttpResponse(output)    


# counter.py V0.00


# 1) create a record and store it somewhere (in file or database)
# 2) increment the record if there are new visitors
# 3) update the backup-stored recored


# TODOS:
# 1) Improve for concurrent environment by using whatever locking mechanism
# 2) Testing for file existed is consuming, so should refine the code
#    to make use of the test result to reduce file open/close operation


LOG_FILE = 'counter.log'


class Counter(object):

    def __init__(self, logfile = LOG_FILE):
        self.log = logfile

        # ensure the log file exists before being accessed
        try:
            open(self.log, 'r').close()
        except:
            open(self.log, 'w').close()
            
    def open(self):
        'Get the recorded counter'
        logfile = open(self.log, 'r')
        record = logfile.read().strip()
        if record:
            self.record = int(record)
        else:
            self.record = 0
        logfile.close()        

    def incre(self):
        self.record += 1

    def update(self):
        'store the backup counter'
        logfile = open(self.log, 'w')
        logfile.write(str(self.record))
        logfile.close()

    def get(self):
        'give the numeric counter'
        return self.record

    def close(self):
        self.update()
        self.log = ''
        self.record = -1


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Version 0.01

# -*- coding: utf-8 -*-


# counter_demo.py V0.01

# TODOs:
# 1) Combine format with the counter information
# 2) Define global counter for consistent recording

# DOs:
# 1) request.session needs support form database, so settings.py
#    should be modified to support database



from django.http import HttpResponse

# use self-defined counter
from counter import Counter


def counter_demo(request):
    record = Counter()
    record.open()
    record.incre()
    record.update()
    
    if not request.session.get('counter', None):
        count = record.get()
        request.session['counter'] = count

    record.close()

    count = request.session.get('counter', None)
    
    output = '''
        
            网站访问计数器Demo    
            您是第 %s 位访问者
        
    '
'' % count

    return HttpResponse(output)    


--- counter_demo.bak    2009-12-10 21:51:31.000000000 +0800
+++ counter_demo.py    2009-12-18 19:00:54.000000000 +0800
@@ -12,13 +12,19 @@
     record.incre()
     record.update()
     
+    if not request.session.get('counter', None):
+        count = record.get()
+        request.session['counter'] = count
+
+    record.close()
+
+    count = request.session.get('counter', None)
+    
     output = '''
         
             网站访问计数器Demo    
             您是第 %s 位访问者
         
-    '
'' % record.get()
-    
-    record.close()
+    ''




阅读(927) | 评论(0) | 转发(0) |
0

上一篇:轨 迹

下一篇:表单处理

给主人留下些什么吧!~~