[Zope3-checkins] SVN: Zope3/trunk/src/zope/interface/ Added russian
translation for human.txt (work in progress)
Dmitry Vasiliev
dima at hlabs.spb.ru
Fri Apr 14 10:25:28 EDT 2006
Log message for revision 66946:
Added russian translation for human.txt (work in progress)
Changed:
A Zope3/trunk/src/zope/interface/human.ru.txt
U Zope3/trunk/src/zope/interface/tests/test_adapter.py
-=-
Added: Zope3/trunk/src/zope/interface/human.ru.txt
===================================================================
--- Zope3/trunk/src/zope/interface/human.ru.txt 2006-04-14 14:23:22 UTC (rev 66945)
+++ Zope3/trunk/src/zope/interface/human.ru.txt 2006-04-14 14:25:26 UTC (rev 66946)
@@ -0,0 +1,155 @@
+===============================
+ÐÑполÑзование ÑееÑÑÑа адапÑеÑов
+===============================
+
+ÐаннÑй докÑÐ¼ÐµÐ½Ñ ÑодеÑÐ¶Ð¸Ñ Ð½ÐµÐ±Ð¾Ð»ÑÑÑÑ Ð´ÐµÐ¼Ð¾Ð½ÑÑÑаÑÐ¸Ñ Ð¿Ð°ÐºÐµÑа ``zope.interface`` и
+его ÑееÑÑÑа адапÑеÑов. ÐокÑÐ¼ÐµÐ½Ñ ÑаÑÑÑиÑÑвалÑÑ ÐºÐ°Ðº конкÑеÑнÑй, но более Ñзкий
+пÑÐ¸Ð¼ÐµÑ Ñого как иÑполÑзоваÑÑ Ð¸Ð½ÑеÑÑейÑÑ Ð¸ адапÑеÑÑ Ð²Ð½Ðµ Zope 3.
+
+СнаÑала нам необÑ
одимо импоÑÑиÑоваÑÑ Ð¿Ð°ÐºÐµÑ Ð´Ð»Ñ ÑабоÑÑ Ñ Ð¸Ð½ÑеÑÑейÑами.
+
+ >>> import zope.interface
+
+ТепеÑÑ Ð¼Ñ ÑазÑабоÑаем инÑеÑÑÐµÐ¹Ñ Ð´Ð»Ñ Ð½Ð°Ñего обÑекÑа - пÑоÑÑого Ñайла. ÐаÑ
+Ñайл бÑÐ´ÐµÑ ÑодеÑжаÑÑ Ð²Ñего один аÑÑибÑÑ - body, в коÑоÑом ÑакÑиÑеÑки бÑдеÑ
+ÑоÑ
Ñанено ÑодеÑжимое Ñайла.
+
+ >>> class IFile(zope.interface.Interface):
+ ...
+ ... body = zope.interface.Attribute(u'СодеÑжимое Ñайла.')
+ ...
+
+ÐÐ»Ñ ÑÑаÑиÑÑики нам ÑаÑÑо необÑ
одимо знаÑÑ ÑÐ°Ð·Ð¼ÐµÑ Ñайла. Ðо бÑло Ð±Ñ Ð½ÐµÑколÑко
+ÑопоÑно ÑелизовÑваÑÑ Ð¾Ð¿Ñеделение ÑазмеÑа пÑÑмо Ð´Ð»Ñ Ð¾Ð±ÑекÑа Ñайла, Ñ.к. ÑазмеÑ
+болÑÑе оÑноÑиÑÑÑ Ðº меÑа-даннÑм. Таким обÑазом Ð¼Ñ Ñоздаем еÑе один инÑеÑÑейÑ
+Ð´Ð»Ñ Ð¿ÑедÑÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ ÑазмеÑа какого-либо обÑекÑа.
+
+ >>> class ISize(zope.interface.Interface):
+ ...
+ ... def getSize():
+ ... 'Return the size of an object.'
+ ...
+
+ТепеÑÑ Ð¼Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ ÑоздаÑÑ ÐºÐ»Ð°ÑÑ ÑеализÑÑÑий Ð½Ð°Ñ Ñайл. ÐеобÑ
одимо ÑÑо Ð±Ñ Ð½Ð°Ñ
+обÑÐµÐºÑ Ñ
Ñанил инÑоÑмаÑÐ¸Ñ Ð¾ Ñом, ÑÑо он ÑеализÑÐµÑ Ð¸Ð½ÑеÑÑÐµÐ¹Ñ `IFile`. ÐÑ Ñакже
+Ñоздаем аÑÑибÑÑ Ñ ÑодеÑжимÑм Ñайла по ÑмолÑÐ°Ð½Ð¸Ñ (Ð´Ð»Ñ ÑпÑоÑÐµÐ½Ð¸Ñ Ð½Ð°Ñего пÑимеÑа).
+
+ >>> class File(object):
+ ...
+ ... zope.interface.implements(IFile)
+ ... body = 'foo bar'
+ ...
+
+ÐалÑÑе Ð¼Ñ Ñоздаем адапÑеÑ, коÑоÑÑй бÑÐ´ÐµÑ Ð¿ÑедоÑÑавлÑÑÑ Ð¸Ð½ÑеÑÑÐµÐ¹Ñ `ISize`
+полÑÑÐ°Ñ Ð»Ñбой обÑÐµÐºÑ Ð¿ÑедоÑÑавлÑÑÑий инÑеÑÑÐµÐ¹Ñ `IFile`. Ðо ÑоглаÑÐµÐ½Ð¸Ñ Ð¼Ñ
+иÑполÑзÑем аÑÑибÑÑ `__used_for__` Ð´Ð»Ñ ÑÐºÐ°Ð·Ð°Ð½Ð¸Ñ Ð¸Ð½ÑеÑÑейÑа коÑоÑÑй Ð¼Ñ Ð¾Ð¶Ð¸Ð´Ð°ÐµÐ¼
+пÑедоÑÑавлÑÐµÑ Ð°Ð´Ð°Ð¿ÑиÑÑемÑй обÑекÑ, `IFile` в наÑем ÑлÑÑае. Ðа Ñамом деле ÑÑоÑ
+аÑÑибÑÑ Ð¸ÑполÑзÑеÑÑÑ ÑолÑко Ð´Ð»Ñ Ð´Ð¾ÐºÑменÑиÑованиÑ. Ð ÑлÑÑае еÑли адапÑеÑ
+иÑполÑзÑеÑÑÑ Ð´Ð»Ñ Ð½ÐµÑколÑкиÑ
инÑеÑÑейÑов можно ÑказаÑÑ Ð¸Ñ
вÑе в виде коÑÑежа.
+
+ÐпÑÑÑ Ð¶Ðµ по ÑоглаÑÐµÐ½Ð¸Ñ ÐºÐ¾Ð½ÑÑÑÑкÑÐ¾Ñ Ð°Ð´Ð°Ð¿ÑеÑа полÑÑÐ°ÐµÑ Ð¾Ð´Ð¸Ð½ аÑгÑÐ¼ÐµÐ½Ñ - context
+(конÑекÑÑ). РнаÑем ÑлÑÑае конÑекÑÑ - ÑÑо ÑкземплÑÑ `IFile` (обÑекÑ,
+пÑедоÑÑавлÑÑÑий `IFile`) коÑоÑÑй иÑполÑзÑеÑÑÑ Ð´Ð»Ñ Ð¿Ð¾Ð»ÑÑÐµÐ½Ð¸Ñ Ð¸Ð· него ÑазмеÑа.
+Так же по ÑоглаÑÐµÐ½Ð¸Ñ ÐºÐ¾Ð½ÑекÑÑ ÑоÑ
ÑанÑеÑÑÑ Ð° адапÑеÑе в аÑÑибÑÑе Ñ Ð¸Ð¼ÐµÐ½ÐµÐ¼
+`context`. Twisted комÑÑниÑи ÑÑÑлаеÑÑÑ Ð½Ð° конÑекÑÑ ÐºÐ°Ðº на обÑÐµÐºÑ `original`.
+Таким обÑазом можно Ñакже даÑÑ Ð°ÑгÑменÑÑ Ð»Ñбое подÑ
одÑÑее имÑ, напÑÐ¸Ð¼ÐµÑ `file`.
+
+ >>> class FileSize(object):
+ ...
+ ... zope.interface.implements(ISize)
+ ... __used_for__ = IFile
+ ...
+ ... def __init__(self, context):
+ ... self.context = context
+ ...
+ ... def getSize(self):
+ ... return len(self.context.body)
+ ...
+
+ТепеÑÑ ÐºÐ¾Ð³Ð´Ð° Ð¼Ñ Ð½Ð°Ð¿Ð¸Ñали Ð½Ð°Ñ Ð°Ð´Ð°Ð¿ÑÐµÑ Ð¼Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ Ð·Ð°ÑегиÑÑÑиÑоваÑÑ ÐµÐ³Ð¾ в
+ÑееÑÑÑе адапÑеÑов, ÑÑо Ð±Ñ ÐµÐ³Ð¾ можно бÑло запÑоÑиÑÑ ÐºÐ¾Ð³Ð´Ð° он понадобиÑÑÑ.
+ÐдеÑÑ Ð½ÐµÑ ÐºÐ°ÐºÐ¾Ð³Ð¾-либо глобалÑного ÑееÑÑÑа адапÑеÑов, Ñаким обÑазом Ð¼Ñ Ð´Ð¾Ð»Ð¶Ð½Ñ
+ÑамоÑÑоÑÑелÑно ÑоздаÑÑ Ð´Ð»Ñ Ð½Ð°Ñего пÑимеÑа ÑееÑÑÑ.
+
+ >>> from zope.interface.adapter import AdapterRegistry
+ >>> registry = AdapterRegistry()
+
+РееÑÑÑ ÑодеÑÐ¶Ð¸Ñ Ð¾ÑобÑажение Ñого, ÑÑо адапÑÐµÑ ÑеализÑÐµÑ Ð½Ð° оÑнове дÑÑгого
+инÑеÑÑейÑа коÑоÑÑй пÑедоÑÑавлÑÐµÑ Ð¾Ð±ÑекÑ. ÐоÑÑÐ¾Ð¼Ñ Ð´Ð°Ð»ÑÑе Ð¼Ñ ÑегиÑÑÑиÑÑем адапÑеÑ
+коÑоÑÑй адапÑиÑÑÐµÑ Ð¸Ð½ÑеÑÑÐµÐ¹Ñ `IFile` к инÑеÑÑейÑÑ `ISize`. ÐеÑвÑй аÑгÑÐ¼ÐµÐ½Ñ Ðº
+меÑÐ¾Ð´Ñ `register()` ÑееÑÑÑа - ÑÑо ÑпиÑок адапÑиÑÑемÑÑ
инÑеÑÑейÑов. РнаÑем
+ÑлÑÑае Ð¼Ñ Ð¸Ð¼ÐµÐµÐ¼ ÑолÑко один адапÑиÑÑемÑй инÑеÑÑÐµÐ¹Ñ - `IFile`. СпиÑок
+инÑеÑÑейÑов Ð¸Ð¼ÐµÐµÑ ÑмÑÑл Ð´Ð»Ñ Ð¸ÑполÑÐ·Ð¾Ð²Ð°Ð½Ð¸Ñ ÐºÐ¾Ð½ÑепÑии мÑлÑÑи-адапÑеÑов, коÑоÑÑе
+ÑÑебÑÑÑ Ð½ÐµÑколÑкиÑ
оÑигиналÑнÑÑ
обÑекÑов Ð´Ð»Ñ Ð°Ð´Ð°Ð¿ÑаÑии к Ð½Ð¾Ð²Ð¾Ð¼Ñ Ð¸Ð½ÑеÑÑейÑÑ. Ð
+ÑÑой ÑиÑÑаÑии конÑÑÑÑкÑÐ¾Ñ Ð°Ð´Ð°Ð¿ÑеÑа бÑÐ´ÐµÑ ÑÑебоваÑÑ Ð½Ð¾Ð²Ñй аÑгÑÐ¼ÐµÐ½Ñ Ð´Ð»Ñ ÐºÐ°Ð¶Ð´Ð¾Ð³Ð¾
+оÑигиналÑного инÑеÑÑейÑа.
+
+ÐÑоÑой аÑгÑÐ¼ÐµÐ½Ñ Ð¼ÐµÑода `register()` ÑÑо инÑеÑÑÐµÐ¹Ñ ÐºÐ¾ÑоÑÑй пÑедоÑÑавлÑеÑ
+адапÑеÑ, в наÑем ÑлÑÑае `ISize`. ТÑеÑий аÑгÑÐ¼ÐµÐ½Ñ - Ð¸Ð¼Ñ Ð°Ð´Ð°Ð¿ÑеÑа. СейÑÐ°Ñ Ð½Ð°Ð¼ не
+важно Ð¸Ð¼Ñ Ð°Ð´Ð°Ð¿ÑеÑа и Ð¼Ñ Ð¿ÐµÑедаем его как пÑÑÑÑÑ ÑÑÑокÑ. ÐбÑÑно имена полезнÑ
+еÑли иÑполÑзÑÑÑÑÑ Ð°Ð´Ð°Ð¿ÑеÑÑ Ð´Ð»Ñ Ð¾Ð´Ð¸Ð½Ð°ÐºÐ¾Ð²Ð¾Ð³Ð¾ набоÑа инÑеÑÑейÑов, но в ÑазлиÑнÑÑ
+ÑиÑÑаÑиÑÑ
. ÐоÑледний аÑгÑÐ¼ÐµÐ½Ñ - ÑÑо клаÑÑ Ð°Ð´Ð°Ð¿ÑеÑа.
+
+ >>> registry.register([IFile], ISize, '', FileSize)
+
+ТепеÑÑ Ð¼Ñ Ð¼Ð¾Ð¶ÐµÐ¼ иÑполÑзоваÑÑ ÑееÑÑÑ Ð´Ð»Ñ Ð·Ð°Ð¿ÑоÑа адапÑеÑа.
+
+ >>> registry.lookup1(IFile, ISize, '')
+ <class '__main__.FileSize'>
+
+ÐопÑобÑем более пÑакÑиÑнÑй пÑимеÑ. Создадим ÑкземплÑÑ `File` и Ñоздадим
+адапÑÐµÑ Ð¸ÑполÑзÑÑÑий запÑÐ¾Ñ ÑееÑÑÑа.
+
+Let's get a little bit more practical. Let's create a `File` instance and
+create the adapter using a registry lookup. Then we see whether the adapter
+returns the correct size by calling `getSize()`.
+
+ >>> file = File()
+ >>> size = registry.lookup1(IFile, ISize, '')(file)
+ >>> size.getSize()
+ 7
+
+However, this is not very practical, since I have to manually pass in the
+arguments to the lookup method. There is some syntactic candy that will allow
+us to get an adapter instance by simply calling `ISize(file)`. To make use of
+this functionality, we need to add our registry to the adapter_hooks list,
+which is a member of the adapters module. This list stores a collection of
+callables that are automatically invoked when IFoo(obj) is called; their
+purpose is to locate adapters that implement an interface for a certain
+context instance.
+
+You are required to implement your own adapter hook; this example covers one
+of the simplest hooks that use the registry, but you could implement one that
+used an adapter cache or persistent adapters, for instance. The helper hook is
+required to expect as first argument the desired output interface (for us
+`ISize`) and as the second argument the context of the adapter (here
+`file`). The function returns an adapter, i.e. a `FileSize` instance.
+
+ >>> def hook(provided, object):
+ ... adapter = registry.lookup1(zope.interface.providedBy(object),
+ ... provided, '')
+ ... return adapter(object)
+ ...
+
+We now just add the hook to an `adapter_hooks` list.
+
+ >>> from zope.interface.interface import adapter_hooks
+ >>> adapter_hooks.append(hook)
+
+Once the hook is registered, you can use the desired syntax.
+
+ >>> size = ISize(file)
+ >>> size.getSize()
+ 7
+
+Now we have to cleanup after ourselves, so that others after us have a clean
+`adapter_hooks` list.
+
+ >>> adapter_hooks.remove(hook)
+
+That's it. I have intentionally left out a discussion of named adapters and
+multi-adapters, since this text is intended as a practical and simple
+introduction to Zope 3 interfaces and adapters. You might want to read the
+`adapter.txt` in the `zope.interface` package for a more formal, referencial
+and complete treatment of the package. Warning: People have reported that
+`adapter.txt` makes their brain feel soft!
Modified: Zope3/trunk/src/zope/interface/tests/test_adapter.py
===================================================================
--- Zope3/trunk/src/zope/interface/tests/test_adapter.py 2006-04-14 14:23:22 UTC (rev 66945)
+++ Zope3/trunk/src/zope/interface/tests/test_adapter.py 2006-04-14 14:25:26 UTC (rev 66946)
@@ -20,6 +20,7 @@
from zope.interface.adapter import AdapterRegistry
import zope.interface
+
class IF0(zope.interface.Interface):
pass
class IF1(IF0):
@@ -180,7 +181,7 @@
>>> class IX(zope.interface.Interface):
... pass
-
+
>>> registry.register([None, IR0], IB0, 'bob', 1)
>>> registry.register([IF1, IX], IB0, 'bob', 2)
>>> registry.lookup([IF1, IR1], IB0, 'bob')
@@ -228,7 +229,7 @@
>>> zope.interface.interface.adapter_hooks.append(_hook)
Now we create some interfaces and an implementation:
-
+
>>> class IX(zope.interface.Interface):
... pass
@@ -244,11 +245,11 @@
... self.original=original
and register an adapter:
-
+
>>> globalRegistry.register((IX,), IY, '', Y)
at first, we still expect the adapter lookup from `X` to `IY` to fail:
-
+
>>> IY(X()) #doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
Traceback (most recent call last):
...
@@ -318,16 +319,14 @@
'X'
>>> registry.lookup((I2, ), IR0, 'y')
'Y'
+"""
-
-"""
-
def test_suite():
from zope.testing import doctest, doctestunit
return unittest.TestSuite((
doctestunit.DocFileSuite('../adapter.txt', '../human.txt',
- 'foodforthought.txt',
+ '../human.ru.txt', 'foodforthought.txt',
globs={'__name__': '__main__'}),
doctest.DocTestSuite(),
))
More information about the Zope3-Checkins
mailing list