SENCE 2022 Examples¶
Open file with default program¶
import os
os.startfile('output/SchPark01.csv') # At least on Windows
Dataclasses¶
In [1]:
Copied!
from dataclasses import dataclass, astuple
@dataclass
class Point:
x: float = 0
y: float = 0
z: float = 0
def distance_square(self, other):
return (other.x - self.x)**2 +\
(other.y - self.y)**2 +\
(other.z - self.z)**2
def distance(self, other):
return self.distance_square(other)**0.5
from dataclasses import dataclass, astuple
@dataclass
class Point:
x: float = 0
y: float = 0
z: float = 0
def distance_square(self, other):
return (other.x - self.x)**2 +\
(other.y - self.y)**2 +\
(other.z - self.z)**2
def distance(self, other):
return self.distance_square(other)**0.5
In [2]:
Copied!
some_point = Point(1, 2)
another_point = Point(4, 6)
some_point = Point(1, 2)
another_point = Point(4, 6)
In [3]:
Copied!
some_point
some_point
Out[3]:
Point(x=1, y=2, z=0)
In [4]:
Copied!
another_point
another_point
Out[4]:
Point(x=4, y=6, z=0)
In [5]:
Copied!
some_point.x
some_point.x
Out[5]:
1
In [6]:
Copied!
some_point == another_point
some_point == another_point
Out[6]:
False
In [7]:
Copied!
some_point == Point(1, 2)
some_point == Point(1, 2)
Out[7]:
True
In [8]:
Copied!
some_point.distance(another_point)
some_point.distance(another_point)
Out[8]:
5.0
In [9]:
Copied!
some_point.x = 3
some_point.x = 3
In [10]:
Copied!
some_point.distance(another_point)
some_point.distance(another_point)
Out[10]:
4.123105625617661
In [11]:
Copied!
astuple(some_point)
astuple(some_point)
Out[11]:
(3, 2, 0)
Tests¶
In [12]:
Copied!
import unittest
from pathlib import Path
SCRIPT_DIR = Path('.')
OUTPUT_DIR = Path('output')
class TestCSVSolutions(unittest.TestCase):
def test_output_folder(self):
self.assertTrue(OUTPUT_DIR.exists(), 'Please create "%s" folder' % OUTPUT_DIR)
def test_scripts_is_written(self):
py_script = '01_workshop_example.py'
self.assertTrue((SCRIPT_DIR / py_script).exists(), '"%s" should exist.' % py_script)
with open(py_script) as f:
content = f.readlines()
self.assertFalse(all(line.startswith('#') for line in content),
'"%s" should have more than just comments. Please write some code.' % py_script)
def test_csv_output_file(self):
csv_path = OUTPUT_DIR / 'SchPark01.csv'
self.assertTrue(csv_path.exists(), 'Please generate "%s" file' % csv_path)
with open(csv_path) as out:
content = out.readlines()
self.assertEqual(8760 * 4, len(content),
"CSV should have 15-minute values for a complete year")
self.assertTrue(
"2011/01/01;00:00;0.0;-0.6" in content[0], "First line of %s should be for 1st of January" % csv_path)
self.assertTrue(
"2011/12/31;23:45;0.0;8.1" in content[-1], "Last line of %s should be for 31st of December" % csv_path)
t_sum, g_sum = 0, 0
i = 0
for line in content:
cells = line.replace(' ', '').split(';')
if all(cells):
t_sum += float(cells[3])
g_sum += float(cells[2])
i += 1
t_average = t_sum / i
g_average = g_sum / i
self.assertAlmostEqual(0.97, i / 8760 / 4, msg="Most of lines should have values", places=2)
self.assertAlmostEqual(12.8, t_average, places=2)
self.assertAlmostEqual(137, g_average, places=0)
unittest.main(argv=[''], verbosity=2, exit=False);
import unittest
from pathlib import Path
SCRIPT_DIR = Path('.')
OUTPUT_DIR = Path('output')
class TestCSVSolutions(unittest.TestCase):
def test_output_folder(self):
self.assertTrue(OUTPUT_DIR.exists(), 'Please create "%s" folder' % OUTPUT_DIR)
def test_scripts_is_written(self):
py_script = '01_workshop_example.py'
self.assertTrue((SCRIPT_DIR / py_script).exists(), '"%s" should exist.' % py_script)
with open(py_script) as f:
content = f.readlines()
self.assertFalse(all(line.startswith('#') for line in content),
'"%s" should have more than just comments. Please write some code.' % py_script)
def test_csv_output_file(self):
csv_path = OUTPUT_DIR / 'SchPark01.csv'
self.assertTrue(csv_path.exists(), 'Please generate "%s" file' % csv_path)
with open(csv_path) as out:
content = out.readlines()
self.assertEqual(8760 * 4, len(content),
"CSV should have 15-minute values for a complete year")
self.assertTrue(
"2011/01/01;00:00;0.0;-0.6" in content[0], "First line of %s should be for 1st of January" % csv_path)
self.assertTrue(
"2011/12/31;23:45;0.0;8.1" in content[-1], "Last line of %s should be for 31st of December" % csv_path)
t_sum, g_sum = 0, 0
i = 0
for line in content:
cells = line.replace(' ', '').split(';')
if all(cells):
t_sum += float(cells[3])
g_sum += float(cells[2])
i += 1
t_average = t_sum / i
g_average = g_sum / i
self.assertAlmostEqual(0.97, i / 8760 / 4, msg="Most of lines should have values", places=2)
self.assertAlmostEqual(12.8, t_average, places=2)
self.assertAlmostEqual(137, g_average, places=0)
unittest.main(argv=[''], verbosity=2, exit=False);
test_csv_output_file (__main__.TestCSVSolutions) ... ok test_output_folder (__main__.TestCSVSolutions) ... ok test_scripts_is_written (__main__.TestCSVSolutions) ... ok ---------------------------------------------------------------------- Ran 3 tests in 0.147s OK
Some links¶
- PyCharm. Excellent Python IDE : https://www.jetbrains.com/pycharm/download/