Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 20th, 2006, 4:05 AM   #1
Digit
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 Digit is on a distinguished road
School Project (Need Help)

Well, I don't really know jack squat in programming except for web programming, and a custom programming language (wich unluckly can't create the type of program I require). Here's my little problem, we had to draw our project out of a hat so I got this one. I can go into further detail about it if you want but for now I'll skip that.
Anyway, what I need to do is construct a program that will generate an HTML document with something like the following:
<h3>Test results for [Student Name]<br>
[year]/[month]/[day]</h3><br>
<table border=1>
 <tr>
  <th>Date</th>
  <th>Chapter</th>
  <th>Score</th>
  <th>Time spent</th>
  <th>Activity</th>
 </tr>
//-----^Above part at the top of the HTML Page only.
//--------------------------------------------------
<tr>
  <td>[year]/[month]/[day]</td>
  <td>[Subject] Level [1 or 2] Unit [1-12] Lesson [1-11]</td>
  <td>[random whole number between 80-100]%</td>
  <td>[random between 20sec and 3:40min]0:00:00 (in that format)</td>
  <td>[A1-E4](not random)</td>
 </tr>
//-----There will be 10 of these for each lesson (so 10 with the Lesson set
//- as 1, 10 as Lesson 2 etc.), each with the same Date, Subject, and Unit
//- (unit only changes every 11 lessons) But different %, time
//- [in the 0:00:00(h:min:sec) format], also the last thing ([A1-E4]) goes 
//- as follows:
// A1
// A2
// A3
// A4
// B1
// B2
// C1
// C2
// E2
// E4
//----- As you can see there are 10 of them, so each horizontal column will
//- be a different one, for example the first will be A1 and the very last E4
Now, the way I want to construct the program is so that it will allow me to input the Student Name, Date, Subject and Level manually, but it will automatically start at Unit and Lesson 1 and every 11 lessons increase the Unit number by one.

Like I stated, I have next to no programming knowledge so if you think its an easy enough task please feel free to help. I'm asking for help since, I know its possible, the problem is just that I don't have the knowledge(or rather programs) to create it. If you could at least point me in the right direction I'd be highly appreciative. I need to have this project completed by May 5th (when I go back to school), I attempted doing this without having to program anything but unluckly it took me over three hours to fill in one Lesson, and let me just say using randomizer.org for the % is really annoying lol. My friend who also got this project ditched on me and went on holiday with his family, bastard... So please, any help is appreciated.
Digit is offline   Reply With Quote
Old Apr 20th, 2006, 7:34 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I would recommend PHP or similar, though you can do it with VB. I don't suppose between here and May is enough time to delve into templates. Usually assignments are given far enough in advance to complete. Most people aren't expected to learn a language and produce an assignment in a couple of weeks. Someone probably needs their ass kicked. I doubt it's the teacher, though anything's possible.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 20th, 2006, 8:17 AM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
PHP does seem a natural choice here, but I notice that you attempted to do it manually, which implies that all you need is to have a program that can generate a set of HTML files given a fixed set of inputs.

Take the following template. Let's call it hello.tmpl:
Hello %(name)s, your score is %(random_number)d
Now the following Python code:
import random

variables = dict(
    name = "Fred",
    random_number = random.randint(80, 100)
)

file = open("hello.tmpl", "r")
text = file.read() % variables
file.close()

file = open("output.txt", "w")
file.write(text)
file.close()
When run, the Python code will create a new file called output.txt that will look something like this:
Hello Fred, your score is 87
Note that the score will be a random number between 80 and 100. Is that the sort of thing you were after, or have I gotten the wrong idea?
Arevos is offline   Reply With Quote
Old Apr 20th, 2006, 2:58 PM   #4
Digit
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 Digit is on a distinguished road
Quote:
Originally Posted by DaWei
I would recommend PHP or similar, though you can do it with VB. I don't suppose between here and May is enough time to delve into templates. Usually assignments are given far enough in advance to complete. Most people aren't expected to learn a language and produce an assignment in a couple of weeks. Someone probably needs their ass kicked. I doubt it's the teacher, though anything's possible.
Okay, thanks for the advice.
Yeah, well thats the time they gave me to do it in.
Unlucky I can't argue.
Well, I think whoever thought that that assignment was reasonable should get their ass kicked.
Doing it manually would take me well over a month since I have to do four different students and doing half a unit takes me give or take three hours.


Quote:
Originally Posted by Arevos
PHP does seem a natural choice here, but I notice that you attempted to do it manually, which implies that all you need is to have a program that can generate a set of HTML files given a fixed set of inputs.

Take the following template. Let's call it hello.tmpl:
Hello %(name)s, your score is %(random_number)d
Now the following Python code:
import random

variables = dict(
    name = "Fred",
    random_number = random.randint(80, 100)
)

file = open("hello.tmpl", "r")
text = file.read() % variables
file.close()

file = open("output.txt", "w")
file.write(text)
file.close()
When run, the Python code will create a new file called output.txt that will look something like this:
Hello Fred, your score is 87
Note that the score will be a random number between 80 and 100. Is that the sort of thing you were after, or have I gotten the wrong idea?
You have it head on.
Thats exactly what I need, I'll see if I can't upload the "example page" its how my teacher wants it to look.
It should probably clear up any confusion I might have caused, I don't know if I might have written it so that you're like "what?".

Edit:
Okay, I added an attachment with the example file my teacher gave me.
The only difference is that I had to save it as a txt so I could add it as an attachment.
If you want to see how it should look just rename it to .html
Attached Files
File Type: txt examplework.txt (15.0 KB, 21 views)

Last edited by Digit; Apr 20th, 2006 at 3:18 PM.
Digit is offline   Reply With Quote
Old Apr 20th, 2006, 4:06 PM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
I'll give you a quick lowdown on how to build a templating system in Python. I'm a little leery about using Python, as that's a language I advocate a lot, and perhaps there's a language that can do this better. Still, I can't think of any currently, and Python's pretty easy to get to grips with for simple things like this.

First, download python-2.4.3.msi and install on your machine.

Create a text file called "makehtml.py". The ".py" extension will tell Windows this is a Python program. When you click on it Windows will try to run it, but since we haven't written any code yet, it won't do anything.

I'll keep this short and very much to the point, so I'll illustrate Python templating with a detailed example. Open makehtml.py in your favourite text editor (notepad will do in a pinch) and write the following code:

import random

# This is a comment. Python ignores all lines that start with a #

# These are variables
# This is a variable that contains some text, or a string
name = "Fred"

# This is a variable that contains a number
age = 20

# This is a variable that contains a list of strings
subjects = ["English", "Maths", "Science"]

# This is a variable containing a the name of the file we want to save to:
filename = "testoutput.html"

# This is a long string of text. It will be the top of the page
top = """
<html>
   <head>
      <title>Templating Test</title>
   </head>
   <body>
      <h1>Templating Test</h1>

      <h2>Details</h2>
      <p>
         Name: %(name)s<br/>
         Age: %(age)d
      </p>

      <h2>Test scores</h2>
      <ul>
""" % vars()        # "% vars()" puts the name and age variables into our text

# This will be the repeated middle of the page
repeated_middle = """
   <li>Subject %(subject_name)s, %(random_score)s
"""

# To make the whole middle, we can use a loop
middle = ""
for subject_name in subjects:
    random_score = random.randint(80, 100)
    middle += repeated_middle % vars()

# This is the end of the page
end = """
      </ul>
   </body>
</html>
"""

# We now put them all together:
text = top + middle + end

# And save it to a file
file = open(filename, "w")
file.write(text)
file.close()
Save, and click on the makehtml.py file. It should run and create a file called "testoutput.html".

The above code is an example that you can adapt to fit your needs. If you need any help working with it, just put up a post on the forums. We won't do your work for you, but we'll help out and give examples
Arevos is offline   Reply With Quote
Old Apr 22nd, 2006, 3:17 AM   #6
Digit
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 Digit is on a distinguished road
Quote:
Originally Posted by Arevos
I'll give you a quick lowdown on how to build a templating system in Python. I'm a little leery about using Python, as that's a language I advocate a lot, and perhaps there's a language that can do this better. Still, I can't think of any currently, and Python's pretty easy to get to grips with for simple things like this.

First, download python-2.4.3.msi and install on your machine.

Create a text file called "makehtml.py". The ".py" extension will tell Windows this is a Python program. When you click on it Windows will try to run it, but since we haven't written any code yet, it won't do anything.

I'll keep this short and very much to the point, so I'll illustrate Python templating with a detailed example. Open makehtml.py in your favourite text editor (notepad will do in a pinch) and write the following code:

import random

# This is a comment. Python ignores all lines that start with a #

# These are variables
# This is a variable that contains some text, or a string
name = "Fred"

# This is a variable that contains a number
age = 20

# This is a variable that contains a list of strings
subjects = ["English", "Maths", "Science"]

# This is a variable containing a the name of the file we want to save to:
filename = "testoutput.html"

# This is a long string of text. It will be the top of the page
top = """
<html>
   <head>
      <title>Templating Test</title>
   </head>
   <body>
      <h1>Templating Test</h1>

      <h2>Details</h2>
      <p>
         Name: %(name)s<br/>
         Age: %(age)d
      </p>

      <h2>Test scores</h2>
      <ul>
""" % vars()        # "% vars()" puts the name and age variables into our text

# This will be the repeated middle of the page
repeated_middle = """
   <li>Subject %(subject_name)s, %(random_score)s
"""

# To make the whole middle, we can use a loop
middle = ""
for subject_name in subjects:
    random_score = random.randint(80, 100)
    middle += repeated_middle % vars()

# This is the end of the page
end = """
      </ul>
   </body>
</html>
"""

# We now put them all together:
text = top + middle + end

# And save it to a file
file = open(filename, "w")
file.write(text)
file.close()
Save, and click on the makehtml.py file. It should run and create a file called "testoutput.html".

The above code is an example that you can adapt to fit your needs. If you need any help working with it, just put up a post on the forums. We won't do your work for you, but we'll help out and give examples
Thanks a ton bro, I'll see what I can do with what you gave me here.
Oh yeah, sorry about my irregular response times.
I'm so damn busy trying to finish all my junk up before the week after next.
All the seniors have to have everything finished by then so we're all scrambling like hell haha.
Digit is offline   Reply With Quote
Old Apr 22nd, 2006, 4:38 AM   #7
Digit
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 Digit is on a distinguished road
I'm terribly sorry for the double post but I am unable to edit my earlier post for some reason...
The button just isn't there anymore...
Anyway, I tried doing as much as I could and ended up with this.
import random
#----------------------------------------------------------------------
#-----------------------------Student Name-----------------------------
#----------------------------------------------------------------------
name = "MyStudent"
#----------------------------------------------------------------------
#-----------------------------Subject Level----------------------------
#----------------------------------------------------------------------
level = 2
#----------------------------------------------------------------------
#-----------------------------Subject Unit-----------------------------
#----------------------------------------------------------------------
unit = 9
#----------------------------------------------------------------------
#-----------------------------Subject List-----------------------------
#----------------------------------------------------------------------
subjects = ["English", "Maths", "Science"]
#----------------------------------------------------------------------
#-----------------------------Output Name------------------------------
#----------------------------------------------------------------------
filename = "test.html"
#----------------------------------------------------------------------
#-------------------------------Page Top-------------------------------
#----------------------------------------------------------------------
top = """
<html>
   <head>
<title>Test results for %(name)s</title>
</head>
<body>
<h3>Test results for %(name)s<br>
2006/02/28</h3><br>
<table border=1>
 <tr>
  <th>Date</th>
  <th>Chapter</th>
  <th>Score</th>
  <th>Time spent</th>
  <th>Activity</th>
 </tr>

    <ul>
""" % vars()        
#----------------------------------------------------------------------
#------------------------------Repeat Mid------------------------------
#----------------------------------------------------------------------
repeated_middle = """
<tr>
  <td>2006/01/04</td>
  <td>%(subject_name)s Level %(level)s Unit %(unit)s Lesson 1</td>
  <td>%(random_score)s</td>
  <td>0:0%(random_min)s:%(random_sec)s</td> 
  <td>A1</td>
 </tr>
"""
#----------------------------------------------------------------------
#------------------------------Mid Loop--------------------------------
#----------------------------------------------------------------------
middle = ""
for subject_name in subjects:
    random_score = random.randint(80, 100)
    random_min = random.randint(1, 2)
    random_sec = random.randint(10, 59)
    middle += repeated_middle % vars()
#----------------------------------------------------------------------
#------------------------------Page End--------------------------------
#----------------------------------------------------------------------
end = """
      </ul>
    </table>
   </body>
</html>
"""
#----------------------------------------------------------------------
#------------------------------Combine---------------------------------
#----------------------------------------------------------------------
text = top + middle + end
#----------------------------------------------------------------------
#----------------------------Save to File------------------------------
#----------------------------------------------------------------------
file = open(filename, "w")
file.write(text)
file.close()
It seems like it will be quite possible since it outputs it very nicely.
However, I got stuck with a few things and looked up some tutorials and tried to find code snippets but had no luck.
Google just keeps spitting out crap.
I want to try and make it so that the:
Quote:
<tr>
<td>2006/01/04</td>
<td>%(subject_name)s Level %(level)s Unit %(unit)s Lesson 1</td>
<td>%(random_score)s</td>
<td>0:0%(random_min)s:%(random_sec)s</td>
<td>A1</td>
</tr>
Section is repeated eleven times instead of just having it make it once, but each time its repeated it should change the "Lesson" variable.
For example instead of it being Lesson 1 in the next line it should be Lesson 2.
So basically just count upwards from 1-11.
I also want it to do that with the "A1" thing as I stated in an earlier post (A1-E4). [I made the areas I was talking about red so that you wouldn't have to look for it]
Also some other minor problems is that I had to make the minimum seconds 10 since it doesn't add a 0 before 1-9, I tried experimenting with it but didn't get the desired result.
I'm still trying to find a date recall function but its not a majour concern since I could just change those manually, it shouldn't take that much time.
The score output it great thanks a ton for that code, I just can't seem to get it to place a % sign afterwards (it makes sense though since its a function sign), in the programming code I used to program with long ago I remember there was a way of adding a sign even though it was a function/variable sign.
Is there maybe a way to do this in python?
But that too like the date isn't a big deal, I could also just go in and add that manually if push comes to shove.
Digit is offline   Reply With Quote
Old Apr 22nd, 2006, 6:19 AM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Sometimes you have to double-post on these forums, as editing is only available for a certain time. In fact, if you have new content, creating a new post is recommended, so don't worry about it

A percent sign is represented by %%:
score = 98
"Score: %(score)d%%" % vars()"    # becomes: "Score: 98%"
Padding a number with zeros is also relatively easy:
"%(score)03d" % vars()    # pads with zeros to be at least 3 digits long
"%(score)3d" %vars()      # pads with blank spaces
Using "d" for numbers instead of "s" gives you a few more options. If you use "s", Python thinks that the number is just another piece of text. If you use "d", then Python knows you explicitly want a number, and so you get some options to do with padding it.

With regards to your A1-E4 problem, the solution is slightly more complex. It would help if you had a quick explanation of "for" loops.

A for loop repeats a section of code for each element in a list. It uses indentation to work out which code is inside the loop, and which code is outside. For instance:
for item in long_list:
    # this line of code is inside the loop, as it's indented
    # this line is too. Not that it's got to be indented by the same amount
# this line is not indented, so it's outside the loop
You can embed for loops inside other for loops:
for item in long_list:
    # this line of code is in the first (outer) loop
    for item in another list:
        # this line of code is in the second (inner) loop
        # this line is in the second loop too
    # this line is back in the first loop
# this line is outside both loops
I'll now show you why having for loops inside other for loops is so important, especially for your purposes:
# A list of letters
letters = ["A", "B", "C", "D"]

# The range(x, y) function creates a list of numbers that are greater than or
# equal to x, and less than y. So range(1, 6) creates the list:
# [1, 2, 3, 4, 5]
numbers = range(1, 6)

middle_template = """<p>%(number)d - %(letter)s</p>
"""

middle = ""
for number in numbers:
    for letter in letters:
        middle += middle_template % vars()
The "middle" variable now holds the following text:
<p>1 - A</p>
<p>1 - B</p>
<p>1 - C</p>
<p>1 - D</p>
<p>2 - A</p>
<p>2 - B</p>
<p>2 - C</p>
<p>2 - D</p>
<p>3 - A</p>
<p>3 - B</p>
<p>3 - C</p>
<p>3 - D</p>
<p>4 - A</p>
<p>4 - B</p>
<p>4 - C</p>
<p>4 - D</p>
<p>5 - A</p>
<p>5 - B</p>
<p>5 - C</p>
<p>5 - D</p>
Hopefully that gives you a clue on how to solve your problem.

As for a date recall function, what exactly do you mean?

Last edited by Arevos; Apr 22nd, 2006 at 6:40 AM.
Arevos is offline   Reply With Quote
Old Apr 23rd, 2006, 10:23 PM   #9
Digit
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 Digit is on a distinguished road
Thanks for the help, I'll start messing around with it later tonight because I have some other work to do first but if it works like I've put together in my mind this should help me accomplish what I want.
Oh, as for the recall date function, I found a tutorial for scripting that on askjeeves so its okay.
Thanks a ton, I'd be screwed without you haha.
Luckly if I keep the pace I'm going with my work now I'll have the whole of next week to work on this so I should also be able to post more.
Digit is offline   Reply With Quote
Old Apr 28th, 2006, 6:20 AM   #10
Digit
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 Digit is on a distinguished road
Okay cool.
I completed the project.
Thank you Arevos.
I really appreciate it a lot.
I'll pretty much be tied down untill graduation, but after that I should hopefully be able to stop in a lot more.
Unless my brother is able to convince me to join the French Foreign Legion that is haha.
Anyway, thanks again.
Digit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC