Skip to content

Commit 76d020f

Browse files
committed
Tweak tables
1 parent 9a454d0 commit 76d020f

File tree

2 files changed

+146
-45
lines changed

2 files changed

+146
-45
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ the author or aid those who want a quick refresher to the Python syntax.
1515
Thanks
1616
-------
1717

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
1919
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
2121
support my work and write reviews. Thanks!
2222

2323
.. figure:: img/book.jpg

python.rst

Lines changed: 144 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -123,30 +123,47 @@ reuse variable names for different object types (though you probably shouldn't):
123123
The ``#`` character denotes the start of a comment. There are no multi-line comments, though
124124
most editors with Python support can comment out a region.
125125

126+
The figure that follows illustrates how everything is an object in Python and variables just point to them.
127+
128+
126129
.. figure:: img/py/rebind.png
127130

128131
Illustration of reusing the same variable
129132

133+
.. raw:: latex
134+
135+
%\Needspace{5\baselineskip}
136+
\clearpage
137+
130138
Numbers
131139
-----------
132140

133141
Python includes three types of numeric literals:
134142
*integers*, *floats*, and *complex numbers*.
135143
Python 3.6 adds the ability to use underscores to
136-
improve readability (PEP 515)
137-
138-
================ ===========================
139-
Type Example
140-
================ ===========================
141-
Integer ``14``
142-
Integer (Hex) ``0xe``
143-
Integer (Octal) ``0o16``
144-
Integer (Binary) ``0b1110``
145-
Float ``14.0``
146-
Float ``1.4e1``
147-
Complex ``14+0j``
148-
Underscore ``1_000``
149-
================ ===========================
144+
improve readability (PEP 515).
145+
146+
.. raw:: latex
147+
148+
\Needspace{5\baselineskip}
149+
150+
.. longtable: format: {r l}
151+
152+
.. table:: Number types
153+
154+
155+
================ ===========================
156+
Type Example
157+
================ ===========================
158+
Integer ``14``
159+
Integer (Hex) ``0xe``
160+
Integer (Octal) ``0o16``
161+
Integer (Binary) ``0b1110``
162+
Float ``14.0``
163+
Float ``1.4e1``
164+
Complex ``14+0j``
165+
Underscore ``1_000``
166+
================ ===========================
150167

151168
There are many built-in functions for manipulating
152169
numbers ie. ``abs``, ``min``, ``max``, ``ceil``.
@@ -225,15 +242,24 @@ Strings
225242

226243
Python 3 strings hold unicode data. Python has a few ways to represent strings. There is also a bytes type (PEP 3137):
227244

228-
================ ===========================
229-
Type Example
230-
================ ===========================
231-
String ``"hello\tthere"``
232-
String ``'hello'``
233-
String ``'''He said, "hello"'''``
234-
Raw string ``r'hello\tthere'``
235-
Byte string ``b'hello'``
236-
================ ===========================
245+
.. raw:: latex
246+
247+
\Needspace{10\baselineskip}
248+
249+
250+
.. longtable: format: {r l}
251+
252+
.. table:: String types
253+
254+
================ ===========================
255+
Type Example
256+
================ ===========================
257+
String ``"hello\tthere"``
258+
String ``'hello'``
259+
String ``'''He said, "hello"'''``
260+
Raw string ``r'hello\tthere'``
261+
Byte string ``b'hello'``
262+
================ ===========================
237263

238264

239265
.. longtable: format: {p{.3\textwidth} l >{\raggedright\arraybackslash}p{.3\textwidth}}
@@ -326,7 +352,6 @@ Byte string ``b'hello'``
326352
======================================================= ===========================================================
327353

328354

329-
330355
Lists
331356
-----
332357

@@ -371,6 +396,10 @@ We can also do *slicing* operations on most sequences::
371396
['Ringo', 'George', 'John', 'Paul']
372397

373398
399+
.. raw:: latex
400+
401+
\Needspace{5\baselineskip}
402+
374403

375404
.. longtable: format: {p{.25\textwidth} l >{\raggedright\arraybackslash}p{.35\textwidth}}
376405
@@ -617,6 +646,8 @@ Sets are useful because they provide *set operations*, such as union
617646

618647
>>> empty = set()
619648

649+
650+
620651
.. longtable: format: {p{.25\textwidth} l >{\raggedright\arraybackslash}p{.35\textwidth}}
621652
622653
.. longtable: format: {>{\hangindent=1em\hangafter=1\raggedright\arraybackslash }p{.25\textwidth} l >{\hangindent=1em\hangafter=1\raggedright\arraybackslash }p{.35\textwidth}}
@@ -654,6 +685,11 @@ Sets are useful because they provide *set operations*, such as union
654685
``s ^ s2`` ``__xor__`` Set xor (see ``.symmetric_difference``)
655686
======================================= ========================= ============================================================
656687

688+
.. raw:: latex
689+
690+
%\Needspace{5\baselineskip}
691+
\clearpage
692+
657693

658694

659695
.. longtable: format: {p{.55\textwidth} p{.35\textwidth}}
@@ -981,19 +1017,24 @@ handle. There are various modes to open a file, depending on the content and
9811017
your needs. If you open the file in binary mode, you will get bytes out. In text
9821018
mode you will get strings back:
9831019

984-
================= ======================================================================
985-
Mode Meaning
986-
================= ======================================================================
987-
``'r'`` Read text file (default)
988-
``'w'`` Write text file (truncates if exists)
989-
``'x'`` Write text file, throw ``FileExistsError`` if exists.
990-
``'a'`` Append to text file (write to end)
991-
``'rb'`` Read binary file
992-
``'wb'`` Write binary (truncate)
993-
``'w+b'`` Open binary file for reading and writing
994-
``'xb'`` Write binary file, throw ``FileExistsError`` if exists.
995-
``'ab'`` Append to binary file (write to end)
996-
================= ======================================================================
1020+
.. longtable: format: {r l}
1021+
1022+
.. table:: File Modes
1023+
1024+
1025+
================= ======================================================================
1026+
Mode Meaning
1027+
================= ======================================================================
1028+
``'r'`` Read text file (default)
1029+
``'w'`` Write text file (truncates if exists)
1030+
``'x'`` Write text file, throw ``FileExistsError`` if exists.
1031+
``'a'`` Append to text file (write to end)
1032+
``'rb'`` Read binary file
1033+
``'wb'`` Write binary (truncate)
1034+
``'w+b'`` Open binary file for reading and writing
1035+
``'xb'`` Write binary file, throw ``FileExistsError`` if exists.
1036+
``'ab'`` Append to binary file (write to end)
1037+
================= ======================================================================
9971038

9981039
Writing Files
9991040
--------------
@@ -1629,8 +1670,7 @@ We can decorate a function with it like this::
16291670
... def add(x, y):
16301671
... return x + y
16311672

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::
16341674

16351675
>>> import functools
16361676
>>> def verbose(func):
@@ -1643,6 +1683,8 @@ Below, we use print functions to illustrate before/after behavior, otherwise thi
16431683
... return res
16441684
... return inner
16451685

1686+
Above, we use print functions to illustrate before/after behavior, otherwise this is very similar to identity decorator.
1687+
16461688
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
16471689
the decorator with the function passed into it::
16481690

@@ -1664,7 +1706,15 @@ Parameterized Decorators
16641706
Because we can use closures to create functions, we can use closures to create decorators as well.
16651707
This is very similar to our decorator above, but now we make a function that will
16661708
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+
::
16681718

16691719
>>> def verbose_level(level):
16701720
... def verbose(func):
@@ -1769,7 +1819,7 @@ Metaclasses with Classes
17691819

17701820
You can define a class decorator and use either ``__new__`` or
17711821
``__init__``. Typically most use ``__new__`` as it can alter
1772-
attributes like ``__slots__``
1822+
attributes like ``__slots__``.
17731823

17741824
::
17751825

@@ -1838,12 +1888,19 @@ The ``asyncio`` library (PEP 3153) provides asynchronous I/O in Python 3. We use
18381888
>>> co # Not running
18391889
<coroutine object greeting at 0x1087dcba0>
18401890

1841-
18421891
>>> loop = asyncio.get_event_loop()
18431892
>>> loop.run_until_complete(co)
18441893
Here they are!
18451894
>>> loop.close()
18461895

1896+
1897+
.. raw:: latex
1898+
1899+
1900+
\clearpage
1901+
1902+
1903+
18471904
To return an object, use an ``asyncio.Future``::
18481905

18491906
>>> async def compute(future):
@@ -2144,7 +2201,7 @@ Note that Python does not do type checking, you need to use something like mypy:
21442201
>>> add("foo", "bar")
21452202
'foobar'
21462203

2147-
You can also specify the types of variables::
2204+
You can also specify the types of variables with a comment::
21482205

21492206
>>> from typing import Dict
21502207
>>> ages = {} # type: Dict[str, int]
@@ -2237,3 +2294,47 @@ You can also rename imports using ``as``::
22372294
from packagename.module1 import fib as package_fib
22382295

22392296
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.
2338+
2339+
.. [#] https://docs.continuum.io/anaconda/
2340+

0 commit comments

Comments
 (0)