Adding the analysis command subclass

We'll extend our application by adding the analysis feature. As we're using the Command design pattern, we can add yet another subclass for analysis.

Here's our analysis feature, also designed as a subclass of the Command class:

class Analyze_Command(Command):

def run(self) -> None:
with Path(self.config["outputfile"]).open() as target:
rdr = csv.reader(target)
outcomes = (float(row[10]) for row in rdr)
first = next(outcomes)
sum_0, sum_1 = 1, first
value_min = value_max = first
for value in outcomes:
sum_0 += 1 # value**0
sum_1 += value # value**1
value_min = min(value_min, value)
value_max = max(value_max, value)
mean = sum_1 / sum_0
print(
f"{self.config['outputfile']} "
f"Mean = {mean:.1f} "
f"House Edge = {1 - mean / 50:.1%} "
f"Range = {value_min:.1f} {value_max:.1f}"
)

This class inherits the general features of the Command class. In this case, there's only one small feature, which is to save the configuration information. The work performed by the run() method is not too statistically meaningful—true, but the point is to show you a second command that uses the configuration namespace to do work related to our simulation. We used the outputfile configuration parameter to name the file that is read to perform some statistical analysis.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset