Datafile¶
The datafile
directive works with Activecode to emulate reading data from a file.
Synopsis¶
The general format of the datafile
directive is:
.. datafile:: filename
:options:
+ --- Content area ---+
|
| zero or more lines of data
|
+ --------------------+
The id of the directive serves as the filename. The filename can be referenced in an activecode code block as if it was a physical file on the filesystem.
Required Arguments¶
The required argument is the ‘filename’ (actually a unique_id that simulates a physical file). It is the filename you must inform users of so that they can perform file reading operations in activecode windows.
This must be unique within the document as it does become the id of the element. It does not have the same global uniqueness constraint that unique id’s require.
Optional Arguments¶
- cols
Integer
. Set the width of the textarea. If not set, then Runestone will size the area to the contents, or provide a default.- edit
Boolean
. Toggle whether adatafile
is editable. If not set, then false.Editable text areas can also be resized by the user.
- hide
Boolean
. Toggle whether adatafile
is visible. If not set, then true.Why hide this element?
If you have an exceptionally long file that you want to use in an example where it is not important that the student see all the data.
In an example when you want students to solve a problem dependent on file reading operations in which they should not be able to determine the answer by looking at the file.
Hidden datafiles can still be referenced in code.
- rows
Integer
. Set the height of the textarea. If not set, then Runestone will size the area to the contents, or provide a default.
Languages supported¶
Python only at this time.
Sphinx configuration options¶
No directive specific configuration options exist.
Internationalization¶
tbd
Known limitations and bugs¶
An empty datafile
with no default content cannot determine a default size for itself.
If a blank data file is specified (no default content),
then the rows and cols parameters must be set.
Examples¶
A datafile
with no optional parameters set.
.. datafile:: ex0_text
:rows: 5
:cols: 60
Note that a this empty datafile is not editable!
A datafile
with some default content
.. datafile:: ex1_text
here is the first line in the data file
also, this is the second line in the data file
and this is the third line
here is the first line in the data file also, this is the second line in the data file and this is the third line
A datafile
with some default content
.. datafile:: ex2_text
:edit:
here is the first line in the data file
also, this is the second line in the data file
and this is the third line
here is the first line in the data file also, this is the second line in the data file and this is the third line
A datafile
with some default content
.. datafile:: ex3_text
:edit:
:rows: 5
:cols: 40
here is the first line in the data file
also, this is the second line in the data file
and this is the third line
here is the first line in the data file also, this is the second line in the data file and this is the third line
The following example shows a complete use of the datafile
directive
that defines a non-trivial input file and use in a small python program.
.. datafile:: qbdata_text
:rows: 40
:cols: 70
Colt McCoy QB CLE 135 222 1576 6 9 60.8% 74.5
Josh Freeman QB TB 291 474 3451 25 6 61.4% 95.9
Michael Vick QB PHI 233 372 3018 21 6 62.6% 100.2
Matt Schaub QB HOU 365 574 4370 24 12 63.6% 92.0
Philip Rivers QB SD 357 541 4710 30 13 66.0% 101.8
Matt Hasselbeck QB SEA 266 444 3001 12 17 59.9% 73.2
Jimmy Clausen QB CAR 157 299 1558 3 9 52.5% 58.4
Joe Flacco QB BAL 306 489 3622 25 10 62.6% 93.6
Kyle Orton QB DEN 293 498 3653 20 9 58.8% 87.5
Jason Campbell QB OAK 194 329 2387 13 8 59.0% 84.5
Peyton Manning QB IND 450 679 4700 33 17 66.3% 91.9
Drew Brees QB NO 448 658 4620 33 22 68.1% 90.9
Matt Ryan QB ATL 357 571 3705 28 9 62.5% 91.0
Matt Cassel QB KC 262 450 3116 27 7 58.2% 93.0
Mark Sanchez QB NYJ 278 507 3291 17 13 54.8% 75.3
Brett Favre QB MIN 217 358 2509 11 19 60.6% 69.9
David Garrard QB JAC 236 366 2734 23 15 64.5% 90.8
Eli Manning QB NYG 339 539 4002 31 25 62.9% 85.3
Carson Palmer QB CIN 362 586 3970 26 20 61.8% 82.4
Alex Smith QB SF 204 342 2370 14 10 59.6% 82.1
Chad Henne QB MIA 301 490 3301 15 19 61.4% 75.4
Tony Romo QB DAL 148 213 1605 11 7 69.5% 94.9
Jay Cutler QB CHI 261 432 3274 23 16 60.4% 86.3
Jon Kitna QB DAL 209 318 2365 16 12 65.7% 88.9
Tom Brady QB NE 324 492 3900 36 4 65.9% 111.0
Ben Roethlisberger QB PIT 240 389 3200 17 5 61.7% 97.0
Kerry Collins QB TEN 160 278 1823 14 8 57.6% 82.2
Derek Anderson QB ARI 169 327 2065 7 10 51.7% 65.9
Ryan Fitzpatrick QB BUF 255 441 3000 23 15 57.8% 81.8
Donovan McNabb QB WAS 275 472 3377 14 15 58.3% 77.1
Kevin Kolb QB PHI 115 189 1197 7 7 60.8% 76.1
Aaron Rodgers QB GB 312 475 3922 28 11 65.7% 101.2
Sam Bradford QB STL 354 590 3512 18 15 60.0% 76.5
Shaun Hill QB DET 257 416 2686 16 12 61.8% 81.3
Colt McCoy QB CLE 135 222 1576 6 9 60.8% 74.5 Josh Freeman QB TB 291 474 3451 25 6 61.4% 95.9 Michael Vick QB PHI 233 372 3018 21 6 62.6% 100.2 Matt Schaub QB HOU 365 574 4370 24 12 63.6% 92.0 Philip Rivers QB SD 357 541 4710 30 13 66.0% 101.8 Matt Hasselbeck QB SEA 266 444 3001 12 17 59.9% 73.2 Jimmy Clausen QB CAR 157 299 1558 3 9 52.5% 58.4 Joe Flacco QB BAL 306 489 3622 25 10 62.6% 93.6 Kyle Orton QB DEN 293 498 3653 20 9 58.8% 87.5 Jason Campbell QB OAK 194 329 2387 13 8 59.0% 84.5 Peyton Manning QB IND 450 679 4700 33 17 66.3% 91.9 Drew Brees QB NO 448 658 4620 33 22 68.1% 90.9 Matt Ryan QB ATL 357 571 3705 28 9 62.5% 91.0 Matt Cassel QB KC 262 450 3116 27 7 58.2% 93.0 Mark Sanchez QB NYJ 278 507 3291 17 13 54.8% 75.3 Brett Favre QB MIN 217 358 2509 11 19 60.6% 69.9 David Garrard QB JAC 236 366 2734 23 15 64.5% 90.8 Eli Manning QB NYG 339 539 4002 31 25 62.9% 85.3 Carson Palmer QB CIN 362 586 3970 26 20 61.8% 82.4 Alex Smith QB SF 204 342 2370 14 10 59.6% 82.1 Chad Henne QB MIA 301 490 3301 15 19 61.4% 75.4 Tony Romo QB DAL 148 213 1605 11 7 69.5% 94.9 Jay Cutler QB CHI 261 432 3274 23 16 60.4% 86.3 Jon Kitna QB DAL 209 318 2365 16 12 65.7% 88.9 Tom Brady QB NE 324 492 3900 36 4 65.9% 111.0 Ben Roethlisberger QB PIT 240 389 3200 17 5 61.7% 97.0 Kerry Collins QB TEN 160 278 1823 14 8 57.6% 82.2 Derek Anderson QB ARI 169 327 2065 7 10 51.7% 65.9 Ryan Fitzpatrick QB BUF 255 441 3000 23 15 57.8% 81.8 Donovan McNabb QB WAS 275 472 3377 14 15 58.3% 77.1 Kevin Kolb QB PHI 115 189 1197 7 7 60.8% 76.1 Aaron Rodgers QB GB 312 475 3922 28 11 65.7% 101.2 Sam Bradford QB STL 354 590 3512 18 15 60.0% 76.5 Shaun Hill QB DET 257 416 2686 16 12 61.8% 81.3
.. activecode:: df_ac_ex_qb
:nocodelens:
:datafile: qbdata_text
:enabledownload:
qbfile = open("qbdata_text", "r")
for aline in qbfile:
values = aline.split()
print('QB ', values[0], values[1], 'had a rating of ', values[10] )
qbfile.close()
Adding a datafile
directive doesn’t break non-python code,
but it doesn’t use it (yet) either.
.. datafile:: poem_text
:edit:
Beware the Jabberwock, my son!
The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
The frumious Bandersnatch!"
He took his vorpal sword in hand:
Long time the manxome foe he sought --
So rested he by the Tumtum tree,
And stood awhile in thought.
And, as in uffish thought he stood,
The Jabberwock, with eyes of flame,
Came whiffling through the tulgey wood,
And burbled as it came!
One, two! One, two! And through and through
The vorpal blade went snicker-snack!
He left it dead, and with its head
He went galumphing back.
And, has thou slain the Jabberwock?
Come to my arms, my beamish boy!
O frabjous day! Callooh! Callay!'
He chortled in his joy.
Beware the Jabberwock, my son! The jaws that bite, the claws that catch! Beware the Jubjub bird, and shun The frumious Bandersnatch!" He took his vorpal sword in hand: Long time the manxome foe he sought -- So rested he by the Tumtum tree, And stood awhile in thought. And, as in uffish thought he stood, The Jabberwock, with eyes of flame, Came whiffling through the tulgey wood, And burbled as it came! One, two! One, two! And through and through The vorpal blade went snicker-snack! He left it dead, and with its head He went galumphing back. And, has thou slain the Jabberwock? Come to my arms, my beamish boy! O frabjous day! Callooh! Callay!' He chortled in his joy.
.. activecode:: df_ac_ex_poem
:language: cpp
:compileargs: ['-Wall', '-Wextra', '-pedantic', '-std=c++11']
:datafile: poem_text
:enabledownload:
#include <fstream>
#include <iostream>
int main () {
std::ifstream is("poem_text");
char c;
while (is.get(c)) {
std::cout << c;
}
is.close();
return 0;
}