Lecture Notes 10#

What is the _ variable?#

In an interactive context _ refers to the value of the previous expression, e.g.

>>> 8 * 8
64
>>> _
64
>>> _ * 8
512
>>> _
512
>>> _ * 2
1024

something that can be used to gradually build up a value as in a pocket calculator. This has been inherited in the notebook as well - value of previous cell. E.g.

'hello'
'hello'
_ + ' world'
'hello world'

Note that the print function does not have a return value although things may look the same on the screen, it does not modify the _ variable

print('hello')
hello
_ + ' world'
'hello world world'

What is a list comprehension?#

We generate a new list from the members of a nother list (or sequence) with a compact notation, like turning a for loop inside out

[i**2 for i in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[word.upper() for word in ['hello', 'world']]
['HELLO', 'WORLD']

Cell magic#

Functions specific for interactive Python and Jupyter with leading percent (%) signs

  • Line commands with leading single %

  • Cell commands with leading double %%

E.g. to write the content of a cell to a file story.txt

%%file story.txt
Mr and Mrs Dursley, of number four,
Privet Drive, were proud to say that
they were perfectly normal, thank
you very much.
Overwriting story.txt

Stock example continued#

Here we use the csv module to write the collected data to a single file total_results.csv. To start with we organize conceived lines in the resulting file as a list of dictionary with common keys. The keys will become the headers in the file and the values the data lines in the files

"""
Goal:
collect the data in the files into a table



         Total shares      Final value
amazon:  62.2256792377505  6350.13
apple:   63.04476565302408 10691.76
starbucks: 41.58404967698385 4763.87
"""

import csv
import sys
from rich import print
from pathlib import Path


data = []
for result_file in sys.argv[1:]:
    result_file = Path(result_file)
    with open(result_file) as f:
        line1 = f.readline()
        line2 = f.readline()

    total_shares = float(line1.split()[-1])
    final_value = float(line2.split()[-1])

    data.append(
        {
            # 'Company': result_file.replace('.results', ''),
            # 'Company': result_file.split('.')[0],
            'Company': result_file.stem.title(),
            'Total shares': total_shares,
            'Final Value': final_value,
        }
    )
print(data)

with open('total_results.csv', mode='w') as f:
    writer = csv.DictWriter(f, data[0].keys())
    writer.writeheader()
    writer.writerows(data)

In the final rewrite we do a so called refactoring. To rewrite the structure without changing the behaviour We have separate functions for collecting data and writing data and a main function which controls every thing

"""
Goal:
collect the data in the files into a table



         Total shares      Final value
amazon:  62.2256792377505  6350.13
apple:   63.04476565302408 10691.76
starbucks: 41.58404967698385 4763.87
"""

import csv
import sys
from rich import print
from pathlib import Path



def get_data():
    data = []
    for result_file in sys.argv[1:]:
        result_file = Path(result_file)
        with open(result_file) as f:
            line1 = f.readline()
            line2 = f.readline()

        total_shares = float(line1.split()[-1])
        final_value = float(line2.split()[-1])

        data.append(
            {
                # 'Company': result_file.replace('.results', ''),
                # 'Company': result_file.split('.')[0],
                'Company': result_file.stem.title(),
                'Total shares': total_shares,
                'Final Value': final_value,
            }
        )
    return data


def save_data(data):
    with open('total_results.csv', mode='w') as f:
        writer = csv.DictWriter(f, data[0].keys())
        writer.writeheader()
        writer.writerows(data)


def main():
    data = get_data()
    print(data)
    save_data(data)


if __name__ == "__main__":
    main()

Running the code with the downloaded examples

$ python *.results
[
    {'Company': 'Amazon', 'Total shares': 62.2256792377505, 'Final Value': 6350.13},
    {'Company': 'Apple', 'Total shares': 63.04476565302408, 'Final Value': 10691.76},
    {'Company': 'Meta', 'Total shares': 21.859148044183414, 'Final Value': 5229.58},
    {'Company': 'Starbucks', 'Total shares': 41.58404967698385, 'Final Value': 4763.87},
    {'Company': 'Tesla', 'Total shares': 116.52728787723488, 'Final Value': 18680.49}
]