Skip to content

Commit 0938838

Browse files
committed
Merge pull request #3 from jefrailey/print_function
Print statement to function - Session 7 and 8
2 parents b3e6aaf + f1fe1ba commit 0938838

File tree

2 files changed

+45
-45
lines changed

2 files changed

+45
-45
lines changed

source/session07.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ Watch This Video:
251251
http://pyvideo.org/video/879/the-art-of-subclassing
252252

253253

254-
|
255-
|
256-
|
254+
|
255+
|
256+
|
257257
258258
( I pointed you to it last week, but Seriously, well worth the time. )
259259

@@ -695,7 +695,7 @@ argument
695695
In [41]: class Classy(object):
696696
....: x = 2
697697
....: def a_class_method(cls, y):
698-
....: print "in a class method: ", cls
698+
....: print(u"in a class method: ", cls)
699699
....: return y ** cls.x
700700
....: a_class_method = classmethod(a_class_method)
701701
....:
@@ -718,7 +718,7 @@ more declarative style of programming:
718718
x = 2
719719
@classmethod
720720
def a_class_method(cls, y):
721-
print "in a class method: ", cls
721+
print(u"in a class method: ", cls)
722722
return y ** cls.x
723723
724724
.. nextslide:: Why?
@@ -733,7 +733,7 @@ more declarative style of programming:
733733
Consider this:
734734

735735
.. code-block:: ipython
736-
736+
737737
In [44]: class SubClassy(Classy):
738738
....: x = 3
739739
....:

source/session08.rst

+39-39
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Decorators
5151
5252
def substitute(a_function):
5353
def new_function(*args, **kwargs):
54-
return "I'm not that other function"
54+
return u"I'm not that other function"
5555
return new_function
5656
5757
A Definition
@@ -93,9 +93,9 @@ one:
9393
.. code-block:: python
9494
9595
def add(a, b):
96-
print "Function 'add' called with args: %r" % locals()
96+
print(u"Function 'add' called with args: %r" % locals())
9797
result = a + b
98-
print "\tResult --> %r" % result
98+
print(u"\tResult --> %r" % result)
9999
return result
100100
101101
.. nextslide::
@@ -109,13 +109,13 @@ Now imagine we defined the following, more generic *decorator*:
109109
110110
def logged_func(func):
111111
def logged(*args, **kwargs):
112-
print "Function %r called" % func.__name__
112+
print(u"Function %r called" % func.__name__)
113113
if args:
114-
print "\twith args: %r" % args
114+
print(u"\twith args: %r" % args)
115115
if kwargs:
116-
print "\twith kwargs: %r" % kwargs
116+
print(u"\twith kwargs: %r" % kwargs)
117117
result = func(*args, **kwargs)
118-
print "\t Result --> %r" % result
118+
print(u"\t Result --> %r" % result)
119119
return result
120120
return logged
121121
@@ -291,7 +291,7 @@ Let's define another decorator that will time how long a given call takes:
291291
start = time.time()
292292
result = func(*args, **kwargs)
293293
elapsed = time.time() - start
294-
print "time expired: %s" % elapsed
294+
print(u"time expired: %s" % elapsed)
295295
return result
296296
return timed
297297
@@ -380,7 +380,7 @@ Remember this from last week?
380380
def delx(self):
381381
del self._x
382382
x = property(getx, setx, delx,
383-
"I'm the 'x' property.")
383+
u"I'm the 'x' property.")
384384
385385
.. nextslide:: The Decorator version
386386

@@ -430,14 +430,14 @@ The Iterator Protocol
430430
An iterator must have the following methods:
431431

432432
.. code-block:: python
433-
433+
434434
an_iterator.__iter__()
435435
436436
Returns the iterator object itself. This is required to allow both containers
437437
and iterators to be used with the ``for`` and ``in`` statements.
438438

439439
.. code-block:: python
440-
440+
441441
an_iterator.next()
442442
443443
Returns the next item from the container. If there are no further items,
@@ -502,10 +502,10 @@ The ``iter()`` function:
502502
In [20]: iter([2,3,4])
503503
Out[20]: <listiterator at 0x101e01350>
504504
505-
In [21]: iter("a string")
505+
In [21]: iter(u"a string")
506506
Out[21]: <iterator at 0x101e01090>
507507
508-
In [22]: iter( ('a', 'tuple') )
508+
In [22]: iter( (u'a', u'tuple') )
509509
Out[22]: <tupleiterator at 0x101e01710>
510510
511511
for an arbitrary object, ``iter()`` calls the ``__iter__`` method. But it knows about some object (``str``, for instance) that don't have a ``__iter__`` method.
@@ -568,14 +568,14 @@ In the ``Examples/Session08`` dir, you will find: ``iterator_1.py``
568568
it = IterateMe_2(2, 20, 2)
569569
for i in it:
570570
if i > 10: break
571-
print i
571+
print(i)
572572
573573
And then pick up again:
574574

575575
.. code-block:: python
576576
577577
for i in it:
578-
print i
578+
print(i)
579579
580580
* Does ``xrange()`` behave the same?
581581

@@ -689,7 +689,7 @@ yet another way to make a generator:
689689
>>> (x * 2 for x in [1, 2, 3])
690690
<generator object <genexpr> at 0x10911bf50>
691691
>>> for n in (x * 2 for x in [1, 2, 3]):
692-
... print n
692+
... print(n)
693693
... 2 4 6
694694
695695
@@ -761,7 +761,7 @@ Context Managers
761761

762762
.. code-block:: python
763763
764-
file_handle = open('filename.txt', 'r')
764+
file_handle = open(u'filename.txt', u'r')
765765
file_content = file_handle.read()
766766
file_handle.close()
767767
# do some stuff with the contents
@@ -781,7 +781,7 @@ You can write more robust code for handling your resources:
781781
.. code-block:: python
782782
783783
try:
784-
file_handle = open('filename.txt', 'r')
784+
file_handle = open(u'filename.txt', u'r')
785785
file_content = file_handle.read()
786786
finally:
787787
file_handle.close()
@@ -811,7 +811,7 @@ lines of defensive code have been replaced with this simple form:
811811

812812
.. code-block:: python
813813
814-
with open('filename', 'r') as file_handle:
814+
with open(u'filename', u'r') as file_handle:
815815
file_content = file_handle.read()
816816
# do something with file_content
817817
@@ -888,13 +888,13 @@ Consider this code:
888888
http://pymotw.com/2/contextlib/#module-contextlib
889889
"""
890890
def __init__(self, handle_error):
891-
print '__init__(%s)' % handle_error
891+
print(u'__init__(%s)' % handle_erro)r
892892
self.handle_error = handle_error
893893
def __enter__(self):
894-
print '__enter__()'
894+
print(u'__enter__()')
895895
return self
896896
def __exit__(self, exc_type, exc_val, exc_tb):
897-
print '__exit__(%s, %s, %s)' % (exc_type, exc_val, exc_tb)
897+
print(u'__exit__(%s, %s, %s)' % (exc_type, exc_val, exc_tb))
898898
return self.handle_error
899899
900900
.. nextslide::
@@ -905,8 +905,8 @@ the order in which things happen:
905905
.. code-block:: ipython
906906
907907
In [46]: with Context(True) as foo:
908-
....: print 'This is in the context'
909-
....: raise RuntimeError('this is the error message')
908+
....: print(u'This is in the context')
909+
....: raise RuntimeError(u'this is the error message')
910910
__init__(True)
911911
__enter__()
912912
This is in the context
@@ -924,8 +924,8 @@ What if we try with ``False``?
924924
.. code-block:: ipython
925925
926926
In [47]: with Context(False) as foo:
927-
....: print 'This is in the context'
928-
....: raise RuntimeError('this is the error message')
927+
....: print(u'This is in the context')
928+
....: raise RuntimeError(u'this is the error message')
929929
__init__(False)
930930
__enter__()
931931
This is in the context
@@ -934,8 +934,8 @@ What if we try with ``False``?
934934
RuntimeError Traceback (most recent call last)
935935
<ipython-input-47-de2c0c873dfc> in <module>()
936936
1 with Context(False) as foo:
937-
2 print 'This is in the context'
938-
----> 3 raise RuntimeError('this is the error message')
937+
2 print(u'This is in the context')
938+
----> 3 raise RuntimeError(u'this is the error message')
939939
4
940940
RuntimeError: this is the error message
941941
@@ -951,16 +951,16 @@ Consider this code:
951951
952952
@contextmanager
953953
def context(boolean):
954-
print "__init__ code here"
954+
print(u"__init__ code here")
955955
try:
956-
print "__enter__ code goes here"
956+
print(u"__enter__ code goes here")
957957
yield object()
958958
except Exception as e:
959-
print "errors handled here"
959+
print(u"errors handled here")
960960
if not boolean:
961961
raise
962962
finally:
963-
print "__exit__ cleanup goes here"
963+
print(u"__exit__ cleanup goes here")
964964
965965
.. nextslide::
966966

@@ -971,8 +971,8 @@ And using it has similar results. We can handle errors:
971971
.. code-block:: ipython
972972
973973
In [50]: with context(True):
974-
....: print "in the context"
975-
....: raise RuntimeError("error raised")
974+
....: print(u"in the context")
975+
....: raise RuntimeError(u"error raised")
976976
__init__ code here
977977
__enter__ code goes here
978978
in the context
@@ -986,8 +986,8 @@ Or, we can allow them to propagate:
986986
.. code-block:: ipython
987987
988988
In [51]: with context(False):
989-
....: print "in the context"
990-
....: raise RuntimeError("error raised")
989+
....: print(u"in the context")
990+
....: raise RuntimeError(u"error raised")
991991
__init__ code here
992992
__enter__ code goes here
993993
in the context
@@ -997,8 +997,8 @@ Or, we can allow them to propagate:
997997
RuntimeError Traceback (most recent call last)
998998
<ipython-input-51-641528ffa695> in <module>()
999999
1 with context(False):
1000-
2 print "in the context"
1001-
----> 3 raise RuntimeError("error raised")
1000+
2 print(u"in the context")
1001+
----> 3 raise RuntimeError(u"error raised")
10021002
4
10031003
RuntimeError: error raised
10041004
@@ -1063,7 +1063,7 @@ HTML 'p' tag:
10631063
...: return string
10641064
...:
10651065
1066-
In [5]: return_a_string("this is a string")
1066+
In [5]: return_a_string(u"this is a string")
10671067
Out[5]: '<p> this is a string </p>'
10681068
10691069
Note that this is a **very** simple version of the very useful decorators

0 commit comments

Comments
 (0)