Zanurkuj w Pythonie/Skrypty i strumienie - wszystko razem: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Robwolfe (dyskusja | edycje)
Nie podano opisu zmian
Robwolfe (dyskusja | edycje)
Nie podano opisu zmian
Linia 1:
{{WEdycji}}
 
{{Podświetl|py}}
== Wszystko razem==
 
Przemierzyliśmy kawał drogi. Zatrzymajmy się na chwilę i zobaczmy jak te wszystkie elementy do siebie pasują.
You've covered a lot of ground. Let's step back and see how all the pieces fit together.
Zaczniemy od skryptu, który pobiera argumenty z linii poleceń używając modułu <tt>getopt</tt>.
To start with, this is a script that takes its arguments on the command line, using the getopt module.
 
def main(argv):
Linia 16 ⟶ 14:
...
Tworzymy nową instancję klasy <tt>KantGenerator</tt> i przekazujemy jej plik z gramatyką oraz źródło, które może być, ale nie musi, podane w linii poleceń.
You create a new instance of the KantGenerator class, and pass it the grammar file and source that may or may
not have been specified on the command line.
 
k = KantGenerator(grammar, source)
 
Instancja klasy <tt>KantGenerator</tt> automatycznie wczytuje gramatykę, która jest plikiem XML. Wykorzystujemy naszą funkcję <tt>openAnything</tt> do otwarcia pliku (który może być ulokowany lokalnie lub na zdalnym serwerze), następnie używamy wbudowanego zestawu funkcji parsujących <tt>minidom</tt> do sparsowania XML-a do postaci drzewa obiektów Pythona.
The KantGenerator instance automatically loads the grammar, which is an XML file. You use your custom
openAnything function to open the file (which could be stored in a local file or a remote web server), then use the
built−in minidom parsing functions to parse the XML into a tree of Python objects.
 
def _load(self, source):
Linia 30 ⟶ 25:
sock.close()
 
Ach i po drodze wykorzystujemy naszą wiedzę o strukturze dokumentu XML do utworzenia małego bufora referencji, którymi są po prostu elementy dokumentu XML.
Oh, and along the way, you take advantage of your knowledge of the structure of the XML document to set up a little
cache of references, which are just elements in the XML document.
 
def loadGrammar(self, grammar):
Linia 37 ⟶ 31:
self.refs[ref.attributes["id"].value] = ref
 
Jeśli został podany jakiś materiał źródłowy w linii poleceń, używamy go. W przeciwnym razie na podstawie gramatyki wyszukujemy referencję na najwyższym poziomie (tą do której nie mają odnośników żadne inne elementy) i używamy jej jako punktu startowego.
If you specified some source material on the command line, you use that; otherwise you rip through the grammar
looking for the "top−level" reference (that isn't referenced by anything else) and use that as a starting point.
 
def getDefaultSource(self):
Linia 48 ⟶ 41:
return '<xref id="%s"/>' % random.choice(standaloneXrefs)
 
Teraz przedzieramy się przez materiał źródłowy. Ten materiał to także XML i parsujemy go węzeł po węźle. Aby podzielić nieco kod i uczynić go łatwiejszym w utrzymaniu, używamy oddzielnych funkcji obsługi (handlers) dla każdego typu węzła.
Now you rip through the source material. The source material is also XML, and you parse it one node at a time. To
keep the code separated and more maintainable, you use separate handlers for each node type.
 
def parse_Element(self, node):
Linia 55 ⟶ 47:
handlerMethod(node)
 
Przelatujemy przez gramatykę, parsując wszystkie elementy potomne każdego elementu <tt>p</tt>,
You bounce through the grammar, parsing all the children of each p element,
 
def do_p(self, node):
Linia 62 ⟶ 54:
for child in node.childNodes: self.parse(child)
 
zastępując elementy <tt>choice</tt> losowym elementem potomnym,
replacing choice elements with a random child,
 
def do_choice(self, node):
self.parse(self.randomChildElement(node))
 
i zastępując elementy <tt>xref</tt> losowym elementem potomnym odpowiedniego elementu <tt>ref</tt>, który wcześniej został zachowany w buforze.
and replacing xref elements with a random child of the corresponding ref element, which you previously cached.
 
def do_xref(self, node):
Linia 73 ⟶ 65:
self.parse(self.randomChildElement(self.refs[id]))
 
W końcu parsujemy wszystko do zwykłego tekstu,
Eventually, you parse your way down to plain text,
 
def parse_Text(self, node):
Linia 80 ⟶ 72:
self.pieces.append(text)
 
który wypisujemy.
which you print out.
 
def main(argv):