You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/topics/address-tools.txt
+29-27Lines changed: 29 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -14,8 +14,7 @@ getting the "address" of a class, and possibly shortening it to an equivalent
14
14
but shorter string. But after I implemented that, I realized that this could be
15
15
generalized into a pair of functions, :func:`address_tools.describe` and
16
16
:func:`address_tools.resolve`, that can replace the built-in :func:`repr` and
17
-
:func:`eval` functions. So I will explain about those first, and after that I
18
-
will explain about the "class address" and shortening business.
17
+
:func:`eval` functions.
19
18
20
19
So, Python has two built-in functions called :func:`repr` and :func:`eval`. You
21
20
can say that they are opposites of each other: :func:`repr` "describes" a
@@ -99,33 +98,36 @@ We get a pointy-bracketed un-:func:`eval`able string. How about a function?
99
98
100
99
Same thing. We get a string that we can't put back in :func:`eval`. Is this really necessary? Why not return ``'decimal.Decimal'`` or ``'re.match'`` so we could :func:`eval` those later and get the original objects?
101
100
102
-
It *is* sometimes helpful that the :func:`repr` string ``"<class 'decimal.Decimal'>"`` informs us that this is a class; but sometimes you want a string that you can turn back into an object. Although... :func:`eval` might not be able to find it, because :mod:`decimal` might not be imported.
101
+
It *is* sometimes helpful that the :func:`repr` string ``"<class
102
+
'decimal.Decimal'>"`` informs us that this is a class; but sometimes you want a
103
+
string that you can turn back into an object. Although... :func:`eval` might
104
+
not be able to find it, because :mod:`decimal` might not be imported.
105
+
103
106
Enter :mod:`address_tools`:
104
-
<h2>:func:`address_tools.describe` and :func:`address_tools.resolve`</h2>
107
+
108
+
109
+
:func:`address_tools.describe` and :func:`address_tools.resolve`
Let's play with :func:`address_tools.describe` and :func:`address_tools.resolve`:
106
-
<pre>>>> from garlicsim.general_misc import address_tools
107
-
>>> import decimal
108
-
>>> address_tools.describe(decimal.Decimal)
109
-
'decimal.Decimal'
110
-
</pre>
111
-
That's a nice description string! We can put that back into <code>resolve</code> and get the original class:
112
-
<pre>>>> address_tools.resolve(address_tools.describe(decimal.Decimal)) is decimal.Decimal
113
-
True</pre>
114
-
We can use <code>resolve</code> to get this function, without <code>re</code> being imported, and it will import <span>re</span> by itself:
115
-
<pre>>>> address_tools.resolve('re.match')
116
-
<function match at 0x00B5E6B0></pre>
117
-
This shtick also works on classes, functions, methods, modules, and possibly other kinds of objects.
118
-
<h2>Address shortening</h2>
119
-
One of the original purposes of :mod:`address_tools` was address-shortening. Sometimes I have a class inside a module inside a package inside my root package. For example, the most important class in <code>garlicsim</code> is <code>garlicsim.asynchronous_crunching.project.Project</code>. But that's a really long string. And because it's such an important class, it's also available at the friendlier address <code>garlicsim.Project</code>.
120
-
Using :func:`address_tools.describe`, we can get the shorter address of the class:
121
-
<pre>>>> from garlicsim.general_misc import address_tools
122
-
>>> import garlicsim
123
-
>>> address_tools.describe(garlicsim.Project) # By default we get the full address:
I recommend using this feature in any <a href="http://docs.python.org/reference/datamodel.html#object.__repr__"><code>__repr__</code></a> methods that you write for your classes, to make the resulting string shorter. <a href="https://github.com/cool-RR/GarlicSim/blob/master/garlicsim/garlicsim/asynchronous_crunching/project.py#L542">Here's how I do it.</a>
113
+
114
+
>>> from python_toolbox import address_tools
115
+
>>> import decimal
116
+
>>> address_tools.describe(decimal.Decimal)
117
+
'decimal.Decimal'
118
+
119
+
That's a nice description string! We can put that back into :func:`resolve <address_tools.resolve>` and get the original class:
120
+
121
+
>>> address_tools.resolve(address_tools.describe(decimal.Decimal)) is decimal.Decimal
122
+
True
123
+
124
+
We can use :func:`resolve <address_tools.resolve>` to get this function, without :mod:`re` being imported, and it will import :mod:`re` by itself:
125
+
126
+
>>> address_tools.resolve('re.match')
127
+
<function match at 0x00B5E6B0>
128
+
129
+
This shtick also works on classes, functions, methods, modules, and possibly
0 commit comments