[Checkins] SVN: zope3docs/ Merge notes from optimization document to the Python style guide as they
Christian Theune
ct at gocept.com
Thu Feb 19 12:28:41 EST 2009
Log message for revision 96788:
Merge notes from optimization document to the Python style guide as they
weren't really notes about optimization but style.
Changed:
_U zope3docs/
U zope3docs/source/codingstyle/index.rst
D zope3docs/source/codingstyle/optimizations.rst
U zope3docs/source/codingstyle/python-style.rst
-=-
Property changes on: zope3docs
___________________________________________________________________
Modified: bzr:revision-info
- timestamp: 2009-02-19 16:08:24.144000053 +0100
committer: Christian Theune <ct at gocept.com>
properties:
branch-nick: zope3docs.bzr
+ timestamp: 2009-02-19 16:22:01.474999905 +0100
committer: Christian Theune <ct at gocept.com>
properties:
branch-nick: zope3docs.bzr
Modified: bzr:file-ids
- source/codingstyle/python-style.rst 96696 at 62d5b8a3-27da-0310-9561-8e5933582275:zope3docs:source%2Fcodingstyle%2Fpython-style.rst
+ source/codingstyle/index.rst 96696 at 62d5b8a3-27da-0310-9561-8e5933582275:zope3docs:source%2Fcodingstyle%2Findex.rst
source/codingstyle/python-style.rst 96696 at 62d5b8a3-27da-0310-9561-8e5933582275:zope3docs:source%2Fcodingstyle%2Fpython-style.rst
Modified: bzr:revision-id:v3-single-zope3docs
- 16 ct at gocept.com-20090219095131-k60befy2nrfx9q99
17 ct at gocept.com-20090219101837-g1g0cg0op0o8a104
18 ct at gocept.com-20090219104745-nh3531xbm8hsd2v9
19 ct at gocept.com-20090219105006-0mre3cx94v8awk9m
20 ct at gocept.com-20090219105021-qywg1ei5q6a21223
21 ct at gocept.com-20090219105034-je5udw7hy1r7xel5
22 ct at gocept.com-20090219133402-h5djqf2t1nfow9k3
23 ct at gocept.com-20090219133410-1vdvhnc62v0ix14u
24 ct at gocept.com-20090219134127-4xq75osx1qizp1lh
25 ct at gocept.com-20090219134620-3kaefpqounb7me7f
26 ct at gocept.com-20090219145818-u7qndkg2cjev8a2q
27 ct at gocept.com-20090219150824-7xz32ylkrdfil1dj
+ 16 ct at gocept.com-20090219095131-k60befy2nrfx9q99
17 ct at gocept.com-20090219101837-g1g0cg0op0o8a104
18 ct at gocept.com-20090219104745-nh3531xbm8hsd2v9
19 ct at gocept.com-20090219105006-0mre3cx94v8awk9m
20 ct at gocept.com-20090219105021-qywg1ei5q6a21223
21 ct at gocept.com-20090219105034-je5udw7hy1r7xel5
22 ct at gocept.com-20090219133402-h5djqf2t1nfow9k3
23 ct at gocept.com-20090219133410-1vdvhnc62v0ix14u
24 ct at gocept.com-20090219134127-4xq75osx1qizp1lh
25 ct at gocept.com-20090219134620-3kaefpqounb7me7f
26 ct at gocept.com-20090219145818-u7qndkg2cjev8a2q
27 ct at gocept.com-20090219150824-7xz32ylkrdfil1dj
28 ct at gocept.com-20090219152201-yjolyqchvlv1tr6e
Modified: zope3docs/source/codingstyle/index.rst
===================================================================
--- zope3docs/source/codingstyle/index.rst 2009-02-19 17:28:37 UTC (rev 96787)
+++ zope3docs/source/codingstyle/index.rst 2009-02-19 17:28:41 UTC (rev 96788)
@@ -15,7 +15,6 @@
python-style
zcml-style
todocomments
- optimizations
checkin-guidelines
writingtests
Deleted: zope3docs/source/codingstyle/optimizations.rst
===================================================================
--- zope3docs/source/codingstyle/optimizations.rst 2009-02-19 17:28:37 UTC (rev 96787)
+++ zope3docs/source/codingstyle/optimizations.rst 2009-02-19 17:28:41 UTC (rev 96788)
@@ -1,49 +0,0 @@
-Optimizations
-=============
-
-Although code optimization is not the primary goal in the current development
-cycle of Zope3, here are some rules that should make further optimizations and the possible use of unicode less painful.
-
-string module
-
- - Avoid to use the 'string' module. Instead use string methods. They are
- always much faster and share the same API with unicode strings !
-
- - Avoid slicing for checking if a string has a special prefix or suffix::
-
- NO: if foo[:3]=='bar'...
- YES: if foo.startswith('bar'):
-
- NO: if foo[-5:]=='.html'...
- YES: if foo.endswith('.html'):
-
- Using startwith()/endswith() is faster, cleaner and less error-prone.
-
-usage of type()
-
- - constructs likes 'if type(obj) is type("")' should be replaced
- using isinstance()::
-
- NO: if type(obj) is type(""):
- YES: if isinstance(obj, str):...
-
- When checking if a string is a string, keep in mind that
- it might be a unicode string too! The types module has
- the StringTypes type defined for that purpose. So a check
- for string or unicode string would look like that::
-
- from types import StringTypes
- if isinstance(obj, StringTypes):...
-
-
-
-
-<hr solid id=comments_below>
-
-
-tim_one (May 24, 2002 10:14 am; Comment #1) --
- Just noting that Python (probably 2.3) will introduce a common base class for str and unicode, so that isinstance will work without need of types.!StringTypes.
-
-stevea (Jul 8, 2002 1:55 pm; Comment #2) --
- Here or perhaps elsewhere, write something about not using '[]' as a marker object to detect default values when passed to queryNNNN functions. Instead, use an object(), as these don't get wrapped in security proxies.
-
Modified: zope3docs/source/codingstyle/python-style.rst
===================================================================
--- zope3docs/source/codingstyle/python-style.rst 2009-02-19 17:28:37 UTC (rev 96787)
+++ zope3docs/source/codingstyle/python-style.rst 2009-02-19 17:28:41 UTC (rev 96788)
@@ -182,7 +182,61 @@
except ValueError:
...
+String handling
+---------------
+Use ``startswith`` and ``endswith`` because it is faster, cleaner and less
+error-prone than comparing sliced strings::
+
+ # Yes:
+ if foo.startswith('bar'):
+ ...
+ if foo.endswith('.html'):
+ ...
+
+ # No:
+ if foo[:3]=='bar':
+ ...
+ if foo[-5:]=='.html':
+ ...
+
+.. note::
+ TODO: Is this rule already PEP 8?
+
+When checking if a string is a string, keep in mind that it might be a
+unicode string too! The ``basestring`` type matches both ``str`` and
+``unicode`` objects::
+
+ if isinstance(obj, basestring):
+ ...
+
+.. note::
+ TODO Does PEP 8 talk about this already?
+
+Type checks
+-----------
+
+Constructs like ``if type(obj) is type('')`` should be replaced using
+``isinstance()``::
+
+ # Yes:
+ if isinstance(obj, int):
+ ...
+
+ # No:
+ if type(obj) is type(1):
+ ...
+ if type(obj) is int:
+
+
+Marker objects
+--------------
+
+Use instances of ``object`` if you need to construct marker objects (for
+example when detecting default values). Compare them using ``is`` as
+recommended by PEP 8.
+
+
Interfaces
----------
More information about the Checkins
mailing list