randomContent.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 » randomContent.py
"""Generates a random forum content"""

import sys, string, re
sys.path.append("../..")
import RandomText
import gadflyStorage, random, forumClasses
import time

now=time.time()
msgNb=1000  # number of messages
authNb=30  # number of authors

t=open(r"c:\cygwin\home\e_vrenigenn_diwezhan.txt").read()
t=re.sub("\[.*\]","",t)

c=RandomText.RandomText(t,5)

parents=[-1]
authors=[]
db=gadflyStorage.Storage("messages","n")

for i in range(authNb):
  authors.append(c.sentence(8,1,string.whitespace+string.punctuation).strip())

nb=0
titles={}
msgs={}
step=0

msgs[step]=[]
# first step
parent=-1
nbchildren=50
for i in range(nbchildren):
  author=random.choice(authors)
  title=c.sentence(20,mode=1,startAfter=string.whitespace+string.punctuation).strip()
  content=c.sentence(random.randrange(300),mode=2)
  msg=forumClasses.Message(author,title,content,parent)
  msg.date=time.localtime(now-random.randrange(1000000))
  db.write(msg)
  msg.thread=msg.__id__
  titles[msg.__id__]=title
  msgs[step].append(msg)

db.set_key("__id__")
for msg in db.find():
  msg.thread=msg.__id__

  db[msg.__id__]=msg

db.commit()

def genChildren():
  global nb,step
  msgs[step+1]=[]
  for msg in msgs[step]:
    parent=msg.__id__
    thread=msg.thread
    t=time.mktime(msg.date)
    nbchildren=random.randrange(2/(step+1),6)
    for i in range(nbchildren):
      author=random.choice(authors)
      title="Re: "+titles[parent]
      content=c.sentence(random.randrange(300),mode=2)
      msg=forumClasses.Message(author,title,content,parent,thread)
      msg.date=time.localtime(t+random.randrange(int(now-t)))
      db.write(msg)

      msgs[step+1].append(msg)
      titles[msg.__id__]=title
      nb+=1

while nb<msgNb:
  genChildren()
  print "step %s nb %s" %(step,nb)
  step+=1
  
db.commit()

def countChildren(msg):
  global nb
  try:
    ch=db[msg.__id__]
    if type(ch)==type([]):
      #nb+=len(ch)
      for c in ch:
        nb+=1
        countChildren(c)
    else:
      nb+=1
      countChildren(ch)
  except gadflyStorage.StorageError:
    return 0
  return nb

db.set_key("parent")
heads=db[-1]
print heads

db.set_key("thread")
for msg in heads:
  msg.numChildren=len(db.find(where="thread=%s" %msg.__id__))
  print msg.title,msg.numChildren
  db.update(msg,where="__id__=%s" %msg.__id__)

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