[Zope3-dev] Big import blocks

Guido van Rossum guido@python.org
Thu, 20 Mar 2003 11:26:11 -0500


Jim's always on a crusade against "dead chickens" (arbitrary things
you have to do before you can do what you want to do).  I'm beginning
to count the blocks of "from zope..... import ...." at the top of
every Python module a huge dead checken.

So I decided to count.  There are currently 4904 "from ... import ..."
lines in the entire Zope3 source tree (including tests and
zopeproducts).  Because many of these are of the form "from X import
A, B, C", these boil down to 6633 imported names.  Assuming all these
use absolute module names, Below is the list of the names that were
imported at least 10 times.  The list has 117 names out of 1727 total
names (I'll spare you the full list :-), but it accounts for 3130
of the imported names.  That means that 6.7% of the names account for
47% of the imports!

Assuming this data is roughly correct, we can cut the number of this
particular kind of dead chickens roughly in half by creating a module
with a short name (e.g. "zapi") that imports the 120 most popular
names.  Then instead of

  from zope.component import getService, getAdapter
  from zope.publisher.browser import BrowserView
  # etc

you'd write

  from zope.app import zapi

and then in the rest of your code you'd write

  x = zapi.getService(...)

rather than

  x = getService(...)

(And in your own code you can even write "from zope.app.zapi import *"
-- but expect that to be frowned upon in official Zope 3 code. :-)

 190 zope.interface.Interface
 166 unittest.makeSuite
 162 unittest.TestCase
 156 unittest.TestSuite
 154 unittest.main
  78 zope.component.getService
  64 zope.component.getAdapter
  60 zope.publisher.browser.BrowserView
  53 zope.app.tests.placelesssetup.PlacelessSetup
  52 persistence.Persistent
  50 zope.proxy.context.ContextWrapper
  44 zope.exceptions.NotFoundError
  44 zope.app.services.tests.placefulsetup.PlacefulSetup
  42 zope.testing.cleanup.CleanUp
  42 zope.proxy.introspection.removeAllProxies
  42 zope.interface.Attribute
  41 zope.component.getServiceManager
  40 zope.app.traversing.traverse
  38 zope.interface.verify.verifyObject
  38 transaction.get_transaction
  36 zope.component.exceptions.ComponentLookupError
  36 StringIO.StringIO
  33 zope.proxy.context.ContextMethod
  32 zope.publisher.browser.TestRequest
  31 zope.app.pagetemplate.viewpagetemplatefile.ViewPageTemplateFile
  30 zope.publisher.interfaces.browser.IBrowserPresentation
  30 zope.component.adapter.provideAdapter
  30 cStringIO.StringIO
  28 zope.app.interfaces.services.service.ISimpleService
  25 zope.component.getView
  24 zope.app.interfaces.annotation.IAnnotations
  23 zope.testing.cleanup.addCleanUp
  23 zope.component.service.serviceManager
  23 zodb.ztransaction.Transaction
  22 zope.schema.interfaces.ValidationError
  22 zope.configuration.action.Action
  22 zope.component.queryAdapter
  21 zope.app.interfaces.container.IContainer
  21 types.StringTypes
  20 zope.interface.verify.verifyClass
  20 zope.app.interfaces.services.configuration.Active
  20 zodb.storage.tests.base.zodb_unpickle
  19 zope.proxy.context.getWrapperContainer
  19 zope.app.security.registries.permissionregistry.permissionRegistry
  18 zope.configuration.xmlconfig.xmlconfig
  18 zope.configuration.xmlconfig.XMLConfig
  18 zope.configuration.exceptions.ConfigurationError
  18 zope.component.tests.placelesssetup.PlacelessSetup
  18 zope.app.services.servicenames.Adapters
  18 zope.app.security.settings.Allow
  18 zodb.storage.tests.minpo.MinPO
  18 zodb.db.DB
  17 zope.security.checker.NamesChecker
  17 zope.security.checker.CheckerPublic
  17 zope.component.queryView
  17 zope.app.traversing.getPath
  17 zope.app.services.service.ServiceManager
  17 zope.app.security.settings.Deny
  17 zodb.utils.u64
  16 zope.app.services.service.ServiceConfiguration
  15 zope.schema.Text
  15 zope.app.services.servicenames.Permissions
  15 zope.app.interfaces.event.ISubscriber
  15 zope.app.interfaces.annotation.IAttributeAnnotatable
  15 zope.app.event.publish
  15 zodb.timestamp.TimeStamp
  14 zope.publisher.interfaces.browser.IBrowserPublisher
  14 zope.app.services.servicenames.Authentication
  14 zope.app.interfaces.traversing.ITraverser
  14 zope.app.interfaces.services.configuration.Registered
  14 zope.app.interfaces.security.IPermissionService
  14 zope.app.attributeannotations.AttributeAnnotations
  14 zodb.btrees.OOBTree.OOBTree
  14 persistence.dict.PersistentDict
  13 zope.security.checker.defineChecker
  13 zope.i18n.interfaces.IUserPreferredCharsets
  13 zope.exceptions.Unauthorized
  13 zope.component.view.provideView
  13 zope.component.interfaces.IServiceService
  13 zope.app.interfaces.services.configuration.Unregistered
  13 zope.app.interfaces.event.IObjectModifiedEvent
  13 time.time
  12 zope.security.proxy.ProxyFactory
  12 zope.security.proxy.Proxy
  12 zope.interface.implements.implements
  12 zope.app.services.servicenames.HubIds
  12 zope.app.security.settings.Unset
  12 zope.app.interfaces.traversing.ITraversable
  12 zope.app.browser.form.tests.test_browserwidget.BrowserWidgetTest
  12 zodb.utils.p64
  12 zodb.storage.file.FileStorage
  12 types.TupleType
  12 datetime.datetime
  11 zope.publisher.http.HTTPCharsets
  11 zope.app.traversing.locationAsUnicode
  11 zope.app.security.registries.principalregistry.principalRegistry
  11 zope.app.interfaces.traversing.IPhysicallyLocatable
  11 zope.app.interfaces.security.IRoleService
  11 unittest.TextTestRunner
  11 types.StringType
  10 zope.schema.tests.test_field.FieldTestBase
  10 zope.schema.TextLine
  10 zope.publisher.http.IHTTPRequest
  10 zope.exceptions.Forbidden
  10 zope.exceptions.DuplicationError
  10 zope.configuration.interfaces.ISubdirectiveHandler
  10 zope.configuration.interfaces.INonEmptyDirective
  10 zope.app.services.servicenames.Interfaces
  10 zope.app.security.registries.roleregistry.roleRegistry
  10 zope.app.interfaces.security.IAuthenticationService
  10 zope.app.interfaces.event.IEvent
  10 zope.app.interfaces.dublincore.IZopeDublinCore
  10 zope.app.interfaces.dependable.IDependable
  10 zope.app.interfaces.container.IAdding
  10 zope.app.event.objectevent.ObjectModifiedEvent
  10 zope.app.container.zopecontainer.ZopeContainerAdapter
  10 zodb.storage.mapping.MappingStorage

--Guido van Rossum (home page: http://www.python.org/~guido/)