Zanurkuj w Pythonie/Pakiety: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Piotr (dyskusja | edycje)
mNie podano opisu zmian
Piotr (dyskusja | edycje)
tłumaczenie
Linia 1:
{{WEdycji}}
{{Python/Do tłumaczenia}}
 
{{Podświetl|py}}
== Pakiety ==
 
W rzeczywistość przetwarzanie dokumentu XML jest bardzo proste, wystarczy jedna linia kodu. Jednakże, zanim dojdziemy do tej linii kodu, będziemy musieli krótko omówić, czym są pakiety.
Actually parsing an XML document is very simple: one line of code. However, before you get to that line of code, you
need to take a short detour to talk about packages.
 
'''ExamplePrzykład 9.5. LoadingŁadowanie andokumentu XML document (a sneak peek)'''
 
>>> from xml.dom import minidom #(1)
>>> xmldoc = minidom.parse('~/diveintopython/common/py/kgp/binary.xml')
 
# Tę składnię nie widzieliśmy wcześniej. This is a syntax you haven't seen before. ItWygląda looksto almost likeniemal, thejak <tt>from module import</tt>, youktóry knowznamy andi lovekochamy, butale thez "." giveswygląda itna awaycoś aswyższego somethingi aboveinnego andniż beyond a simpleproste <tt>import</tt>. InTak fact,na prawdę <tt>xml</tt> isjest whatczymś, isco knownjest asznane apod nazwą ''pakiet'' (ang. ''package''), <tt>dom</tt> isjest azagnieżdżonym nested packagepakietem withinwewnątrz <tt>xml</tt>-a, anda <tt>minidom</tt> isjest amodułem moduleznajdującym się withinwewnątrz <tt>xml.dom</tt>.
 
Brzmi to skomplikowanie, ale tak naprawdę nie jest. Jeśli zobaczymy na konkretną implementacją, może nam to pomóc. Pakiet to niewiele więcej niż katalog z modułami, a zagnieżdżone pakiety to podkatalogami. The modules within a package (or a nested package) are still just .py files, like always, except that they're in a subdirectory instead of the main lib/ directory of your Python installation.
That sounds complicated, but it's really not. Looking at the actual implementation may help. Packages are little more
than directories of modules; nested packages are subdirectories. The modules within a package (or a nested package)
are still just .py files, like always, except that they're in a subdirectory instead of the main lib/ directory of your
Python installation.
 
'''ExamplePrzykład 9.6. FilePlikowa layoutstruktura of a packagepakietu'''
 
<pre>
Python21/ root Python installation (home of the executable)
+−−lib/ library directory (home of the standard library modules)
|
+−− xml/ xml package (really just a directory with other stuff in it)
|
+−−sax/ xml.sax package (again, just a directory)
|
+−−dom/ xml.dom package (contains minidom.py)
|
+−−parsers/ xml.parsers package (used internally)
</pre>
 
<span>Python21/ root Python installation (home of the executable)
So when you say from xml.dom import minidom, Python figures out that that means "look in the xml
|
directory for a dom directory, and look in that for the minidom module, and import it as minidom". But Python is
+−−lib/ library directory (home of the standard library modules)
even smarter than that; not only can you import entire modules contained within a package, you can selectively import
|
specific classes or functions from a module contained within a package. You can also import the package itself as a
+−− xml/ xml package (really just a directory with other stuff in it)
module. The syntax is all the same; Python figures out what you mean based on the file layout of the package, and
|
automatically does the right thing.
+−−sax/ xml.sax package (again, just a directory)
|
+−−dom/ xml.dom package (contains minidom.py)
|
+−−parsers/ xml.parsers package (used internally)</span>
 
Dlatego kiedy powiesz <tt>from xml.dom import minidom</tt>, Python zrozumie to jako "znajdź w katalogu <tt>xml</tt> katalog <tt>dom</tt>, a następnie szukaj tutaj modułu <tt>minidom</tt> i zaimportuj go jako <tt>minidom</tt>". Lecz Python jest także mądrzejszy; nie tylko możesz zaimportować cały moduł zawarty wewnątrz pakietu, ale także możesz wybiórczo zaimportować wybrane klasy czy funkcje z modułu znajdującego się wewnątrz pakietu. Możesz także zaimportować sam pakiet jako moduł. Składnia będzie taka sama; Python wywnioskuje, co masz na myśli na podstawie struktury plików pakietu i automatycznie wykona poprawną czynność.
'''Example 9.7. Packages are modules, too'''
 
'''Przykład 9.7. Pakiety także są modułami'''
&gt;&gt;&gt; from xml.dom import minidom #(1)
&gt;&gt;&gt; minidom
&lt;module 'xml.dom.minidom' from 'C:\Python21\lib\xml\dom\minidom.pyc'&gt;
&gt;&gt;&gt; minidom.Element
&lt;class xml.dom.minidom.Element at 01095744&gt;
&gt;&gt;&gt; from xml.dom.minidom import Element #(2)
&gt;&gt;&gt; Element
&lt;class xml.dom.minidom.Element at 01095744&gt;
&gt;&gt;&gt; minidom.Element
&lt;class xml.dom.minidom.Element at 01095744&gt;
&gt;&gt;&gt; from xml import dom #(3)
&gt;&gt;&gt; dom
&lt;module 'xml.dom' from 'C:\Python21\lib\xml\dom\__init__.pyc'&gt;
&gt;&gt;&gt; import xml #(4)
&gt;&gt;&gt; xml
&lt;module 'xml' from 'C:\Python21\lib\xml\__init__.pyc'&gt;
 
&gt;&gt;&gt;>>> from xml.dom import minidom #(1)
#Here you're importing a module (minidom) from a nested package (xml.dom). The result is that minidom is imported into your namespace, and in order to reference classes within the minidom module (like Element), you need to preface them with the module name.
&gt;&gt;&gt;>>> minidom
#Here you are importing a class (Element) from a module (minidom) from a nested package (xml.dom). The result is that Element is imported directly into your namespace. Note that this does not interfere with the previous import; the Element class can now be referenced in two ways (but it's all still the same class).
&lt;<module 'xml.dom.minidom' from 'C:\Python21\lib\xml\dom\minidom.pyc'&gt;>
#Here you are importing the dom package (a nested package of xml) as a module in and of itself. Any level of a package can be treated as a module, as you'll see in a moment. It can even have its own attributes and methods, just the modules you've seen before.
&gt;&gt;&gt;>>> minidom.Element
#Here you are importing the root level xml package as a module.
&lt;<class xml.dom.minidom.Element at 01095744&gt;>
&gt;&gt;&gt;>>> from xml.dom.minidom import Element #(2)
&gt;&gt;&gt;>>> Element
&lt;<class xml.dom.minidom.Element at 01095744&gt;>
&gt;&gt;&gt;>>> minidom.Element
&lt;<class xml.dom.minidom.Element at 01095744&gt;>
&gt;&gt;&gt;>>> from xml import dom #(3)
>>> dom
&lt;<module 'xml.dom' from 'C:\Python21\lib\xml\dom\__init__.pyc'&gt;>
&gt;&gt;&gt;>>> import xml #(4)
>>> xml
&lt;<module 'xml' from 'C:\Python21\lib\xml\__init__.pyc'&gt;>
 
# Here you're importing a module (minidom) from a nested package (xml.dom). The result is that minidom is imported into your namespace, and in order to reference classes within the minidom module (like Element), you need to preface them with the module name.
# Here you are importing a class (Element) from a module (minidom) from a nested package (xml.dom). The result is that Element is imported directly into your namespace. Note that this does not interfere with the previous import; the Element class can now be referenced in two ways (but it's all still the same class).
# Here you are importing the dom package (a nested package of xml) as a module in and of itself. Any level of a package can be treated as a module, as you'll see in a moment. It can even have its own attributes and methods, just the modules you've seen before.
# Here you are importing the root level xml package as a module.
 
So how can a package (which is just a directory on disk) be imported and treated as a module (which is always a file