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: README.rst
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -15,9 +15,9 @@ the author or aid those who want a quick refresher to the Python syntax.
15
15
Thanks
16
16
-------
17
17
18
-
If you enjoy this content, consider purchasing the physical version. It is a
18
+
If you enjoy this content, consider `purchasing the physical version<https://www.amazon.com/dp/1542883253/ref=as_li_ss_il?ie=UTF8&qid=1487086306&sr=8-7&keywords=python+3.6&linkCode=li2&tag=hairysuncom-20&linkId=5bed517e28e53633e149006968a55f67>`_. It is a
19
19
hand laid out version that fits in the pocket and has blank pages in the back
20
-
for note taking. It is available at Amazon. I'm indebted to those who
20
+
for note taking. It is available at `Amazon<https://www.amazon.com/dp/1542883253/ref=as_li_ss_il?ie=UTF8&qid=1487086306&sr=8-7&keywords=python+3.6&linkCode=li2&tag=hairysuncom-20&linkId=5bed517e28e53633e149006968a55f67>`_. I'm indebted to those who
@@ -1629,8 +1670,7 @@ We can decorate a function with it like this::
1629
1670
... def add(x, y):
1630
1671
... return x + y
1631
1672
1632
-
A more useful decorator can inject logic before and after calling the original function. To do this we create a function inside of the function and return that.
1633
-
Below, we use print functions to illustrate before/after behavior, otherwise this is very similar to identity decorator::
1673
+
A more useful decorator can inject logic before and after calling the original function. To do this we create a function inside of the function and return that::
1634
1674
1635
1675
>>> import functools
1636
1676
>>> def verbose(func):
@@ -1643,6 +1683,8 @@ Below, we use print functions to illustrate before/after behavior, otherwise thi
1643
1683
... return res
1644
1684
... return inner
1645
1685
1686
+
Above, we use print functions to illustrate before/after behavior, otherwise this is very similar to identity decorator.
1687
+
1646
1688
There is a special syntax for applying the decorator. We put ``@`` before the decorator name and place that on a line directly above the function we wish to decorate. Using the ``@verbose`` line before a function declaration is syntactic sugar for re-assigning the variable pointing to the function to the result of calling
1647
1689
the decorator with the function passed into it::
1648
1690
@@ -1664,7 +1706,15 @@ Parameterized Decorators
1664
1706
Because we can use closures to create functions, we can use closures to create decorators as well.
1665
1707
This is very similar to our decorator above, but now we make a function that will
1666
1708
return a decorator. Based on the inputs to that function, we can control (or parameterize)
1667
-
the behavior of the decorator::
1709
+
the behavior of the decorator:
1710
+
1711
+
.. raw:: latex
1712
+
1713
+
%\Needspace{5\baselineskip}
1714
+
\clearpage
1715
+
1716
+
1717
+
::
1668
1718
1669
1719
>>> def verbose_level(level):
1670
1720
... def verbose(func):
@@ -1769,7 +1819,7 @@ Metaclasses with Classes
1769
1819
1770
1820
You can define a class decorator and use either ``__new__`` or
1771
1821
``__init__``. Typically most use ``__new__`` as it can alter
1772
-
attributes like ``__slots__``
1822
+
attributes like ``__slots__``.
1773
1823
1774
1824
::
1775
1825
@@ -1838,12 +1888,19 @@ The ``asyncio`` library (PEP 3153) provides asynchronous I/O in Python 3. We use
1838
1888
>>> co # Not running
1839
1889
<coroutine object greeting at 0x1087dcba0>
1840
1890
1841
-
1842
1891
>>> loop = asyncio.get_event_loop()
1843
1892
>>> loop.run_until_complete(co)
1844
1893
Here they are!
1845
1894
>>> loop.close()
1846
1895
1896
+
1897
+
.. raw:: latex
1898
+
1899
+
1900
+
\clearpage
1901
+
1902
+
1903
+
1847
1904
To return an object, use an ``asyncio.Future``::
1848
1905
1849
1906
>>> async def compute(future):
@@ -2144,7 +2201,7 @@ Note that Python does not do type checking, you need to use something like mypy:
2144
2201
>>> add("foo", "bar")
2145
2202
'foobar'
2146
2203
2147
-
You can also specify the types of variables::
2204
+
You can also specify the types of variables with a comment::
2148
2205
2149
2206
>>> from typing import Dict
2150
2207
>>> ages = {} # type: Dict[str, int]
@@ -2237,3 +2294,47 @@ You can also rename imports using ``as``::
2237
2294
from packagename.module1 import fib as package_fib
2238
2295
2239
2296
package_fib()
2297
+
2298
+
Environments
2299
+
================
2300
+
2301
+
Python 3 includes the ``venv`` module for creating a sandbox for your project or a *virtual environment*.
2302
+
2303
+
To create an environment on Unix systems, run::
2304
+
2305
+
$ python3 -m venv /path/to/env
2306
+
2307
+
On Windows, run::
2308
+
2309
+
c:\>c:\Python36\python -m venv c:\path\to\env
2310
+
2311
+
To enter or *activate* the environment on Unix, run::
2312
+
2313
+
$ source /path/to/env/bin/activate
2314
+
2315
+
On Windows, run::
2316
+
2317
+
c:\>c:\path\to\env\Scripts\activate.bat
2318
+
2319
+
Your prompt should have the name of the active virtual environment in parentheses.
2320
+
To *deactivate* an environment on both platforms, just run the following::
2321
+
2322
+
(env) $ deactivate
2323
+
2324
+
Installing Packages
2325
+
-------------------
2326
+
2327
+
You should now have a ``pip`` executable, that will install a package from PyPI [#]_ into your virtual environment::
2328
+
2329
+
(env) $ pip install django
2330
+
2331
+
.. [#] https://pypi.python.org/pypi
2332
+
2333
+
To uninstall a package run::
2334
+
2335
+
(env) $ pip uninstall django
2336
+
2337
+
If you are having issues installing a package, you might want to look into alternative Python distributions such as Anaconda [#]_ that have prepackaged many harder to install packages.
0 commit comments