Debian Codegraph @

wxgeometrie/geolib/formules.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 ##--------------------------------------#######
4 # Formule #
5 ##--------------------------------------#######
6 # WxGeometrie
7 # Dynamic geometry, graph plotter, and more for french mathematic teachers.
8 # Copyright (C) 2005-2013 Nicolas Pourcelot
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24 import re
25 from weakref import ref
26
27 from .routines import nice_display
28
29 from .. import param
30
31 class Formule(object):
32
33 # Le caractère d'erreur doit être accepté par le parser de matplotlib en mode normal *ET* en mode math.
34 _caractere_erreur = "<.>"
35
36 def _get_feuille(self):
37 return self.__feuille
38
39 def _set_feuille(self, value):
40 self.__feuille = value
41 liste = self._contenu[:]
42 for i in range(1, len(liste), 2):
43 self._contenu[i].feuille = value
44
45 feuille = property(_get_feuille, _set_feuille)
46
47 def __init__(self, parent, chaine = ""):
48 from .variables import Variable
49 if isinstance(chaine, Formule):
50 chaine = eval(repr(chaine))
51 ## print "Initialisation formule:", chaine, type(chaine)
52 self._parent = ref(parent)
53 # self._parent est une fonction qui renvoie parent s'il existe encore.
54 # Cela permet de ne pas le maintenir en vie artificiellement
55 # (pas de référence circulaire).
56 #~ self._cache_repr = chaine
57 #~ self._cache_str = "<?>".join()
58 if "{" not in chaine:
59 liste = ["", "{" + chaine + "}", ""]
60 else:
61 liste = re.split("([{][^}]+[}])", chaine)
62
63 for i in range(1, len(liste), 2):
64 cache = liste[i][1:-1] # "{A.x}" -> "A.x"
65 var = liste[i] = Variable(cache)
66 var._cache_formule = cache
67
68 var.enfants.add(parent if parent.etiquette is None
69 else parent.etiquette)
70 ## # on va maintenant redéfinir la méthode affiche de toutes les variables de la formule :
71 # au lieu d'être inactive, la méthode affiche va actualiser l'affichage de l'objet contenant la formule.
72 ## def affiche(self, actualiser = False, formule = self):
73 ## formule.parent.creer_figure()
74 ## var.affiche = new.instancemethod(affiche, var, var.__class__)
75 self._contenu = liste
76
77 # il faut faire un système de cache pour chaque variable :
78 # - si la variable est calculable, on renvoie la valeur de la variable (et on met à jour son cache)
79 # - sinon, on renvoie le cache s'il s'agit de repr, et <?> s'il s'agit de str.
80
81 self.feuille = self.parent.feuille
82
83
84 def variables(self):
85 return [elt for (i, elt) in enumerate(self._contenu) if i%2]
86
87 @property
88 def parent(self):
89 return self._parent()
90
91 def supprimer(self):
92 parent = self.parent
93 for var in self.variables():
94 var.enfants.remove(parent if parent.etiquette is None
95 else parent.etiquette)
96
97 def __repr__(self):
98 liste = self._contenu[:]
99 for i in range(1, len(liste), 2):
100 if liste[i].val is not None:
101 liste[i]._cache_formule = str(liste[i])
102 liste[i] = "{" + liste[i]._cache_formule + "}"
103 return repr("".join(liste))
104
105
106 def __str__(self):
107 liste = self._contenu[:]
108 for i in range(1, len(liste), 2):
109 if liste[i].val is None:
110 s = self._caractere_erreur
111 else:
112 s = nice_display(liste[i])
113 #~ if s == "None":
114 #~ s = "<?>"
115 liste[i] = s
116 return "".join(liste)
117