generateForum.py :  » Web-Frameworks » Karrigell » Karrigell-3.1 » common » demo » sqlite » forum » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Karrigell 
Karrigell » Karrigell 3.1 » common » demo » sqlite » forum » generateForum.py
import os
import random
import datetime
import string

from PyDbLite import SQLite

def word(m):
    res = ''
    ln = random.randint(1,m)
    for i in range(ln):
        res += random.choice(string.letters)
    return res

def sentence(n,m):
    ln = random.randint(1,n)
    res = []
    for i in range(ln):
        res.append(word(m))
    return ' '.join(res)

test_dir = os.path.join(os.getcwd(),'test')
if not os.path.exists(test_dir):
    os.mkdir(test_dir)
    
db_path = os.path.join(test_dir,"forum_test.sqlite")
db = SQLite.Database(db_path)

authors = 'pierre','jean','simon','philippe','claire','muriele'

table = SQLite.Table("forum",db)
table.create(('recid','INTEGER PRIMARY KEY AUTOINCREMENT'),
    ('parent','INTEGER'),
    ('thread','INTEGER'),
    ('author','TEXT'),
    ('title','TEXT'),
    ('content','TEXT'),
    ('date','BLOB'),
    ('lastDate','BLOB'),
    ('numChildren','INTEGER'),
    mode="override")
table.is_datetime('date')
table.is_datetime('lastDate')

thread_table = SQLite.Table("threads",db)
thread_table.create(('recid','INTEGER PRIMARY KEY AUTOINCREMENT'),
    ('author','TEXT'),
    ('title','TEXT'),
    ('date','BLOB'),
    ('lastDate','BLOB'),
    ('numChildren','INTEGER'),
    mode="override")
thread_table.is_datetime('date')
thread_table.is_datetime('lastDate')

nbthreads = 200
for i in range(nbthreads):
    # generate thread
    author = random.choice(authors)
    title = sentence(10,10)
    content = sentence(100,10)
    thread_date = datetime.datetime(random.randint(2006,2008),random.randint(1,12),
        random.randint(1,28),random.randint(0,23),random.randint(0,59),
        random.randint(0,59))
    # save in threads table
    thread_id = thread_table.insert(
        author=author,
        title=title,
        date=thread_date,
        lastDate=thread_date,
        numChildren=0)
    # save in forum table
    rec_id=table.insert(parent=-1,
        thread=thread_id,
        author=author,
        title=title,
        content=content,
        date=thread_date,
        lastDate=thread_date,
        numChildren=0)

    # generate comments
    nbanswers = random.randint(0,10)
    oldest = datetime.datetime(2000,1,1,0,0,0)
    for i in range(nbanswers):
        author = random.choice(authors)
        content = sentence(50,10)
        tdelta = datetime.datetime(2009,1,1,0,0,0) - thread_date
        c_date = thread_date + datetime.timedelta(random.randint(1,tdelta.days))
        c_date = datetime.datetime(c_date.year,c_date.month,c_date.day,
          random.randint(0,23),random.randint(0,59),random.randint(0,59))
        if c_date > oldest:
            oldest = c_date
        #print "inserting child of thread %s" %thread_id
        new_id = table.insert(parent=rec_id,
            thread=thread_id,
            author=author,
            title=title,
            content=content,
            date=c_date,
            lastDate=c_date,
            numChildren=0)
        #print "new id",new_id
        #print table[new_id]
        #raw_input()
    # increment number of children
    rec = thread_table[thread_id]
    thread_table.update(rec,lastDate=oldest,numChildren=nbanswers)
table.commit()

# test

table.cursor.execute("SELECT * FROM threads")
rec = table.cursor.fetchone()
print rec
thread = rec[0]
print "thread",thread
table.cursor.execute("SELECT * FROM forum WHERE thread=%s" %thread)
for rec in table.cursor.fetchall():
    print rec

thread_table.cursor.execute("SELECT numChildren FROM threads WHERE recid=%s" %thread)
num_children = thread_table.cursor.fetchone()[0]
print "children",num_children,num_children.__class__

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.