Permalink
Browse files

add function to raw a classroom

  • Loading branch information...
sdpython committed Oct 17, 2016
1 parent eca324f commit 63c2620bc68b0006ac97c2c1e29817e2636af25c
@@ -0,0 +1,82 @@
#-*- coding: utf-8 -*-
"""
@brief test log(time=4s)
"""
import sys
import os
import unittest
try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src
try:
import pyquickhelper as skip_
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..",
"..",
"pyquickhelper",
"src")))
if path not in sys.path:
sys.path.append(path)
import pyquickhelper as skip_
from pyquickhelper.loghelper import fLOG
from pyquickhelper.pycode import get_temp_folder, fix_tkinter_issues_virtualenv
from src.code_beatrix.algorithm.data import load_prenoms_w
from src.code_beatrix.algorithm.classroom import random_positions, plot_positions
class TestClassRoom(unittest.TestCase):
def test_names(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
names = load_prenoms_w()
self.assertEqual(len(names), 50)
def test_random_position(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
positions = random_positions(24)
self.assertEqual(len(positions), 24)
temp = get_temp_folder(__file__, "temp_random_position")
fix_tkinter_issues_virtualenv()
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 8))
plot_positions(positions, ax=ax)
assert ax is not None
img = os.path.join(temp, "img.png")
fig.savefig(img)
assert os.path.exists(img)
if __name__ == "__main__":
fig.show()
plt.close('all')
fLOG("end")
if __name__ == "__main__":
unittest.main()
View
@@ -34,7 +34,8 @@
packages = find_packages('src', exclude='src')
package_dir = {k: "src/" + k.replace(".", "/") for k in packages}
package_data = {project_var_name + ".scratch.example_echiquier": ["*.sb2"],
package_data = {project_var_name + ".algorithm.data": ["*.txt"],
project_var_name + ".scratch.example_echiquier": ["*.sb2"],
project_var_name + ".scratch.example_tri": ["*.sb2"],
project_var_name + ".scratch.example_pyramide": ["*.sb2"],
project_var_name + ".scratch.example_chute": ["*.sb2"],
@@ -0,0 +1,75 @@
"""
@file
@brief Position in a classroom
"""
import random
from .data import load_prenoms_w
def plot_positions(positions, ax=None, **options):
"""
draw positions and first names into a graph
@param positions list of 3-uple (name, x, y)
@param ax axis
@param options options for matplotlib
@return ax
First position: 0
"""
from matplotlib.patches import Rectangle
if ax is None:
import matplotlib.pyplot as plt
_, ax = plt.subplots(
nrows=1, ncols=1, figsize=options.get('figsize', (5, 5)))
maxx = -1
maxy = -1
for name, x, y in positions:
r = Rectangle((x - .45, y - 0.45), 0.9, 0.9,
fill=(0, 0, 255), alpha=0.5)
ax.add_patch(r)
ax.text(x * 1.0, y * 1.0, name,
verticalalignment='center', horizontalalignment='center',
fontsize=options.get('fontsize', 15),
color=options.get('color_text', (0, 0, 0)))
maxx = max(x, maxx)
maxy = max(y, maxy)
ax.set_xlim([-1, maxx + 1])
ax.set_ylim([-1, maxy + 1])
return ax
def random_positions(nb, names=None):
"""
draws random position for some person in a classroom
@param nb number of persons
@param names names (None for default)
@return list of 3-uple(name, x, y)
"""
if names is None:
names = load_prenoms_w()
names = names[:nb]
if nb > len(names):
raise ValueError("nb={} > len(names)={}".format(nb, len(names)))
names = names.copy()
random.shuffle(names)
nbs = int(nb ** 0.5)
if nbs != nb**0.5:
nbs += 1
positions = []
ci = 0
cj = 0
for name in names:
positions.append((name, ci, cj))
cj += 1
if cj >= nbs:
ci += 1
cj = 0
return positions
@@ -0,0 +1,18 @@
"""
@file
@brief shortcut to data
"""
import os
def load_prenoms_w():
"""
returns a list of first names,
taken from `Nominis <http://nominis.cef.fr/contenus/prenom/alphabetique.html>`_.
"""
folder = os.path.abspath(os.path.dirname(__file__))
name = os.path.join(folder, "prenoms_w.txt")
with open(name, "r", encoding="utf-8") as f:
names = f.readlines()
names = [_.strip(" \n\r\t") for _ in names]
return [_ for _ in names if _]
@@ -0,0 +1,50 @@
Waast
Wafaa
Walburge
Waldemar
Walder
Waldy
Walfrid
Walfroy
Wallerand
Walter
Walther
Wanda
Wandrille
Wandy
Warren
Waudru
Weltaz
Wenceslas
Wendy
Werburge
Werner
Wilbert
Wilbrord
Wilfrid
Wilfried
Wilhelm
Wilhelmine
Willard
Willehad
Willem
William
Willibald
Willibrord
Willy
Wilmart
Wilson
Winebald
Winifred
Winnie
Winnoc
Winoc
Winston
Wivine
Wladimir
Wolfgang
Wolfram
Wulfran
Wulfstan
Wunibald
Wynnebald

0 comments on commit 63c2620

Please sign in to comment.