Skip to content

Commit 50fb01f

Browse files
committed
[FINALIZE] The blog post by correcting a grammar check. Pending uploding GIFs of demonstration.
1 parent 415b10e commit 50fb01f

File tree

1 file changed

+143
-151
lines changed

1 file changed

+143
-151
lines changed

_posts/2018-08-12-walkthrough-with-features-introduced-in-core-python-3-7.md

Lines changed: 143 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ tag:
1010

1111

1212
In this post, I will try to explain improvements done in Core Python version
13-
3.7. Below is the summary of features convered in this post.
13+
3.7. Below is the summary of features covered in this post.
1414

1515
* Breakpoints
1616

@@ -22,187 +22,179 @@ In this post, I will try to explain improvements done in Core Python version
2222

2323
* Namedtuples
2424

25-
* Hashbased Python object file
26-
25+
* Hash-based Python object file
2726

2827
* Breakpoint:
2928

30-
* breakpoint():
29+
* breakpoint():
3130

32-
Breakpoints are extream important for debugging. Since I started learning
33-
Python, I am using the same API of putting breakpoints. With this release,
34-
breakpoint() is a new build-in function for putting breakpoints in your
35-
code. This way is more handy than its previous way which is importing and
36-
calling a set_trace method from the pdb module like pdb.set_trace
31+
Breakpoints are extream important for debugging. Since I started learning
32+
Python, I am using the same API for putting breakpoints. With this release,
33+
breakpoint() is a new built-in function for putting breakpoints in your code.
34+
This way is handier than its previous way which is importing and calling a
35+
set_trace method from the pdb module like pdb.set_trace
3736

38-
Below is the example of using putting breakpoint in your program:
37+
Below is the example of putting a breakpoint in your program:
3938

40-
# Note: A Gif will be more batter example for this.
41-
```python
42-
>>> for i in range(5):
43-
... if i % 2 == 0:
44-
... breakpoint()
45-
... else:
46-
... print(i)
47-
...
48-
```
39+
# Note: A Gif will be more batter example for this.
4940

50-
Explaination:
41+
'''python
42+
>>> for i in range(5):
43+
... if i % 2 == 0:
44+
... breakpoint()
45+
... else:
46+
... print(i)
47+
...
48+
'''
5149

52-
The above code will take a pause by opening a Python Debugger (pdb) when the
53-
value of iterator variable i will be even. All the odd values are printed on
54-
screen.
50+
Explanation:
5551

56-
* PYTHONBREAKPOINT environment variable:
52+
The above code will take a pause by opening a Python Debugger (pdb) when the
53+
value of iterator variable i is even. All the odd values of it are printed on
54+
the screen.
5755

58-
You can disable all the breakpoints in your code by setting this environment
59-
variable to "1". Sometimes debugging can lead to a level where number of
60-
breakpoints are out of control. Process of finding, removing and re-ading a
61-
breakpoint becomes overhead. This environment variable can provide a great
62-
way of toggling them in one shot.
56+
* PYTHONBREAKPOINT environment variable:
6357

64-
# TODO: Add demonstration of gif for this point.
58+
You can disable all the breakpoints in your code by setting this environment
59+
variable to “1”. Sometimes debugging can lead to a level where a number of
60+
breakpoints are out of control. The process of finding, removing and reading
61+
breakpoints becomes an overhead. This environment variable can provide a great
62+
way of toggling them in one shot.
6563

66-
I advise to set "PYTHONBREAKPOINT=0" at production environment. This will
67-
prevent your code from long holding if you merge a code with breakpoint by
68-
mistake.
64+
# TODO: Add demonstration of GIF for this point.
6965

66+
I advise setting "PYTHONBREAKPOINT=0" in the production environment. This will
67+
prevent your code from long holding if you merge a code with breakpoint by
68+
mistake.
7069

7170
* Subprocess got new parameter capture_output:
7271

73-
You can pipe the output of Standard Output stream (stdout) and Standard Error
74-
stream (stderr) by enabling capture_output parameter of subprocess.run
75-
command.
76-
77-
Without this parameter, capturing the Standard streams was lengthy. Below is
78-
the example by using subprocess.PIPE parameter
79-
80-
```python
81-
subprocess.run(
82-
["ls", "-l", "/var"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
83-
)
84-
```
85-
86-
With capture_output parameter, this will become more concise like below
87-
88-
```python
89-
subprocess.run(["ls", "-l", "/var"], capture_output=True)
90-
```
91-
92-
Below is the detailed example of using captured_true parameter with
93-
subprocess.run command.
94-
95-
```python
96-
>>> import subprocess
97-
>>> ls = subprocess.run(["ls", "-l", "/var"], capture_output=True)
98-
>>> print(ls.stdout.decode())
99-
total 28
100-
drwxr-xr-x 11 root root 4096 Apr 24 15:14 cache
101-
drwxr-xr-x 4 root root 4096 Aug 12 15:28 db
102-
drwxr-xr-x 2 root root 4096 Jul 2 13:47 empty
103-
drwxr-xr-x 34 root root 4096 Jul 20 04:22 lib
104-
lrwxrwxrwx 1 root root 9 Oct 12 2017 lock -> /run/lock
105-
drwxr-xr-x 11 root root 4096 Aug 12 14:52 log
106-
lrwxrwxrwx 1 root root 15 Apr 23 14:08 mail -> /var/spool/mail
107-
lrwxrwxrwx 1 root root 4 Oct 12 2017 run -> /run
108-
drwxr-xr-x 7 root root 4096 Jun 15 09:22 spool
109-
drwxrwxrwt 3 root root 4096 Aug 10 15:15 tmp
110-
111-
>>> print(ls.stderr.decode())
112-
72+
You can pipe the output of Standard Output stream (stdout) and Standard Error
73+
stream (stderr) by enabling capture_output parameter of subprocess.run
74+
command.
75+
76+
Without this parameter, capturing the Standard streams was lengthy. Below is the example by using subprocess.PIPE parameter
77+
78+
'''python
79+
subprocess.run(
80+
["ls", "-l", "/var"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
81+
)
82+
'''
83+
84+
With capture_output parameter, this will become more concise like below
85+
86+
'''python
87+
subprocess.run(["ls", "-l", "/var"], capture_output=True)
88+
'''
89+
90+
Below is the detailed example of using captured_true parameter with
91+
subprocess.run command.
92+
93+
'''python
94+
>>> import subprocess
95+
>>> ls = subprocess.run(["ls", "-l", "/var"], capture_output=True)
96+
>>> print(ls.stdout.decode())
97+
total 28
98+
drwxr-xr-x 11 root root 4096 Apr 24 15:14 cache
99+
drwxr-xr-x 4 root root 4096 Aug 12 15:28 db
100+
drwxr-xr-x 2 root root 4096 Jul 2 13:47 empty
101+
drwxr-xr-x 34 root root 4096 Jul 20 04:22 lib
102+
lrwxrwxrwx 1 root root 9 Oct 12 2017 lock -> /run/lock
103+
drwxr-xr-x 11 root root 4096 Aug 12 14:52 log
104+
lrwxrwxrwx 1 root root 15 Apr 23 14:08 mail -> /var/spool/mail
105+
lrwxrwxrwx 1 root root 4 Oct 12 2017 run -> /run
106+
drwxr-xr-x 7 root root 4096 Jun 15 09:22 spool
107+
drwxrwxrwt 3 root root 4096 Aug 10 15:15 tmp
108+
109+
>>> print(ls.stderr.decode())
113110

114111
* Dataclasses module:
115112

116-
The new class level decorator "@dataclass" will reduce many lines of your
117-
code. Python is well known for developing features which allows to achieve
118-
more by writing less. For understanding this feature batter, you should be
119-
aware of type hints. I will try to write a tutorial on type hints in up coming
120-
days. Until there, I will suggest to grab any available tutorial from your
121-
Google search.
122-
123-
Below is the usual way to define a Class and creating an object from it. I
124-
will explain how below approach can be reduced by wrapping a @dataclass
125-
decorator to it.
126-
127-
```python
128-
>>> class Point:
129-
... def __init__(self, x, y):
130-
... self.x = x
131-
... self.y = y
132-
...
133-
>>> p = Point(2, 4)
134-
>>> p
135-
<__main__.Point object at 0x7f3c73688b00>
136-
```
137-
138-
Converting above example with @dataclass decorator, Those lines of putting
139-
odious construct code is gone! Additionally, the class level decorator
140-
@dataclass will automatically define a behaviour for dander methods
141-
__repr__(), __eq__() and __hash__() for us.
142-
143-
Below is the example code of wrapping Point class with @dataclass decorator
144-
demonstrated earlier.
145-
146-
```python
147-
>>> from dataclasses import dataclass
148-
>>> @dataclass
149-
... class Point:
150-
... x: float
151-
... y: float
152-
...
153-
>>> p = Point(2, 4)
154-
>>> p
155-
Point(x=2, y=4)
156-
```
157-
113+
The new class level decorator "@dataclass" will reduce many lines of your code.
114+
Python is well-known for developing features which allow achieving more by
115+
writing less. For understanding this feature batter, you should be aware of type
116+
hints. I will try to write a tutorial on type hints in upcoming days. Until
117+
there, I will suggest grabbing any available tutorial from Google.
118+
119+
Below is the usual way to define a Class and creating an object from it. I will
120+
explain how below approach can be reduced by wrapping a @dataclass decorator to
121+
it.
122+
123+
'''python
124+
&gt;&gt;&gt; class Point:
125+
... def __init__(self, x, y):
126+
... self.x = x
127+
... self.y = y
128+
...
129+
&gt;&gt;&gt; p = Point(2, 4)
130+
&gt;&gt;&gt; p
131+
<__main__.Point object at 0x7f3c73688b00>
132+
'''
133+
134+
Converting above example with @dataclass decorator, Those lines of putting
135+
obvious construct code will gone! Additionally, it will define a behaviour for
136+
dander methods __repr__(), __eq__() and __hash__() for us.
137+
138+
Below is the example code of wrapping Point class demonstrated earlier with
139+
@dataclass decorator.
140+
141+
'''python
142+
&gt;&gt;&gt; from dataclasses import dataclass
143+
&gt;&gt;&gt; @dataclass
144+
... class Point:
145+
... x: float
146+
... y: float
147+
...
148+
&gt;&gt;&gt; p = Point(2, 4)
149+
&gt;&gt;&gt; p
150+
Point(x=2, y=4)
151+
'''
158152

159153
* Putting underscores in defining Integers:
160154

161-
Writing large numbers as value invites a difficulty of reading it later. Many
162-
times I myself put finger on screen and count the places to get an idea of
163-
number. In this version, you can put _ for separating numbers for constructing
164-
more readable value. Below example will make more understanding to this.
165-
166-
```python
167-
>>> x = 1_00_000
168-
>>> x
169-
100000
170-
```
155+
Writing large numbers as value invites a difficulty of reading it later. Many
156+
times I myself put finger on the screen and count the places to get an idea of
157+
number. In this version, you can put _ for separating numbers for constructing
158+
more readable value. Below example will make more understanding of this.
171159

172-
Explanation:
160+
'''python
161+
&gt;&gt;&gt; x = 1_00_000
162+
&gt;&gt;&gt; x
163+
100000
164+
'''
173165

174-
The integer value of x is 1, 00, 000. Putting underscore at places of comma
175-
makes larger value more readable than writing entire value together.
166+
Explanation:
176167

168+
The integer value of x is 1, 00, 000. Putting underscore at places of a comma
169+
makes the larger value more readable than writing entire value together.
177170

178171
* Default arguments to Namedtuples:
179172

180-
Namedtuples are less known, but very helpful feature of Python. I use them
181-
whenever I find constructing a Class can be lengthy, still I want to mimic the
182-
Class behaviour.
183-
184-
Below is the example of putting a default arguments to a named tuple:
173+
Namedtuples are less known, but very helpful feature of Python. I use them
174+
whenever I find constructing a Class can be lengthy still, I want to mimic the
175+
Class behavior.
185176

186-
```python
187-
>>> from collections import namedtuple
188-
>>> Point = namedtuple("Point", ["x", "y"], defaults=[2,])
189-
>>> p = Point(3)
190-
>>> p
191-
Point(x=3, y=2)
192-
```
177+
Below is the example of putting a default arguments to a named tuple:
193178

194-
Explanation:
179+
'''python
180+
&gt;&gt;&gt; from collections import namedtuple
181+
&gt;&gt;&gt; Point = namedtuple("Point", ["x", "y"], defaults=[2,])
182+
&gt;&gt;&gt; p = Point(3)
183+
&gt;&gt;&gt; p
184+
Point(x=3, y=2)
185+
'''
195186

196-
The default arguments will be assigned in to attributes in the order from left
197-
to right. Here, the default argument 2 will be assigned to y, and not
198-
x.
187+
Explanation:
199188

189+
The default arguments will be assigned in the order from left to right. Here,
190+
the default argument 2 will be assigned to y, and not x.
200191

201-
* .pyc files are now diffed with hash approach:
192+
* .pyc files are now diffed with the hash approach:
202193

203-
.pyc are object files generated everytime you change your Python code file
204-
(.py). At present identifying the change in Python code is done by comparing
205-
meta fields like edited date. With this release, that functionality is
206-
improved by comparing files with hash based approach. Hashbased approach quick
207-
and more consistent than a meta data approach. Though this improvment is still
208-
considered unstable and CPython will continue with the meta data approach.
194+
.pyc are object files generated every time you change your Python code file
195+
(.py). At present identifying the change in Python code is done by comparing
196+
meta fields like an edited date and few other fields. With this release, that
197+
functionality is improved by comparing files with a hash-based approach. Hashed
198+
approach is quick and more consistent than a metadata approach. Though this
199+
improvement is still considered unstable and CPython will continue with the
200+
metadata approach with this release.

0 commit comments

Comments
 (0)