99---
1010
1111
12- In this post, I will try to discuss improvements done in Core Python version 3.7. Below is the summary of features convered in this post.
13-
14- * Fstrings
12+ In this post, I will try to discuss improvements done in Core Python version
13+ 3.7. Below is the summary of features convered in this post.
1514
1615* Breakpoints
1716
18- * Disabling breakpoints using environment variable
19-
20- * namedtuples
21-
2217* subprocess
2318
2419* dataclass
@@ -27,108 +22,69 @@ In this post, I will try to discuss improvements done in Core Python version 3.7
2722
2823* __ dir__ and __ attr__ at module level. Inside __ init__ .py file
2924
25+ * namedtuples
3026
31- * Fstring: Fstring is one of the coolest feature introduced with this version of
32- Python. F-String is yet another way of re-presenting formatted strings. At
33- present, Python already have more than two ways to format string. This version
34- do introduce yet another way to format the strings. In my view, this option is
35- more concise than the other available options.
3627
37- ``` python
38- >> > name = " Jay"
39- >> > print (f " Hello { name} " )
40- Hello Jay
41- >> > name = " Vijay"
42- >> > print (f " Hello { name} " )
43- Hello Vijay
44- ```
28+ * Breakpoint:
4529
46- You have to append ** f** before the starting of the formatted string. The
47- variable substitution will be done by looking for the value of that variable
48- inside the present scope. If, the variable isn't defined that it will raise a
49- NameError.
30+ * breakpoint():
5031
32+ Breakpoints are extream important feature for debugging. Since I
33+ started learning Python, I am using the same API of putting breakpoints. With
34+ latest improvements, you can put breakpoints by calling the function
35+ "breakpoint()". The ```breakpoint`` is a built in function. Calling buildting
36+ function is more handy than its previous way which is importing a
37+ ``` set_trace ``` method from the ``` pdb ``` module.
5138
52- * Breakpoint: Breakpoints are extream important feature for debugging. Since, I
53- started learning Python I am using the same API of putting breakpoints. With
54- latest improvements, you can put breakpoints by calling the function
55- "breakpoint()". Yes, ```breakpoint`` is a built in function now. You don't
56- need to import ``` pdb ``` and call ``` pdb.set_trace ``` anymore.
39+ Below is the example of using breakpoints in your program:
5740
58- Example
59- ``` python
60- >> > for i in range (5 ):
61- ... if i % 2 == 0 :
62- ... breakpoint ()
63- ... else :
64- ... print (i)
65- ...
66- > < stdin> (1 )< module> ()
67- (Pdb) c
68- 1
69- > < stdin> (1 )< module> ()
70- (Pdb) c
71- 3
72- > < stdin> (1 )< module> ()
73- (Pdb) c
74- ```
41+ # Note: A Gif will be more batter example for this.
42+ ``` python
43+ >> > for i in range (5 ):
44+ ... if i % 2 == 0 :
45+ ... breakpoint ()
46+ ... else :
47+ ... print (i)
48+ ...
49+ ```
7550
76- * Disabling breakpoint with environment variable: Have you felt anytime that you
77- are debugging your code and you have added lost of breakpoints. Now, you want
78- to disable them for a moment. Removing them is time consuming and putting them
79- back is too. Now, it is possible to disable all the breakpoints of your code
80- by just setting an environment variable named "PYTHONBREAKPOINT=0". When you
81- want to enable them again, you can either unset the value of this environment
82- variable or set the value of it to "1".
51+ * PYTHONBREAKPOINT environment variable:
8352
84- # TODO: Add video demonstration or gif demonstration for this.
53+ You can disable all the breakpoints by setting this environment variable to
54+ " 1" . Sometimes debugging can lead to a level where number of breakpoints are
55+ too many. This environment variable can provide a great way of toggling them
56+ in one shot.
8557
58+ # TODO : Add demonstration of gif for this point.
8659
87- * Default arguments to Namedtuples: Namedtuples are less known, but very helpful
88- feature of Python. I use them whenever I constructing a Class is lengthy, but
89- I want to expose it as an instance .
60+ I advise to set " PYTHONBREAKPOINT=0 " at production environment. That will
61+ prevent long holding if someone someone forgets to remove a breakpoint from
62+ their code and deploys it to production. I have seen people doing this .
9063
91- ``` python
92- >> > from collections import namedtuple
93- >> > Point = namedtuple(" Point" , [" x" , " y" ], defaults = [2 ,])
94- >> > p = Point(3 )
95- >> > p
96- Point(x = 3 , y = 2 )
97- ```
9864
99- One important point to observe here is that the default arguments will be
100- assigned in the order of left to right. Here, the default argument ** 2** will
101- be assigned to ** y** , and not ** x** .
65+ * Subprocess got new parameter capture_output:
10266
103- * Subprocess got new parameter named * capture_output* : We all use
104- ``` subprocess.run ``` a lot for running various commands. For capturing the
105- output of Standard output stream (stdout) and Standard error stream, we have
106- to pipe them with existing subprocess module. Like below
67+ You can pipe the output of Standard Output stream (stdout) and Standard Error
68+ stream (stderr) by enabling capture_output parameter of subprocess.run
69+ command.
10770
108- ``` python
109- >> > import subprocess
110- >> > ls = subprocess.run([" ls" , " -l" , " /var" ], stdout = subprocess.PIPE , stderr = subprocess.PIPE )
111- >> > print (ls.stdout.decode())
112- total 28
113- drwxr- xr- x 11 root root 4096 Apr 24 15 :14 cache
114- drwxr- xr- x 4 root root 4096 Aug 12 15 :28 db
115- drwxr- xr- x 2 root root 4096 Jul 2 13 :47 empty
116- drwxr- xr- x 34 root root 4096 Jul 20 04 :22 lib
117- lrwxrwxrwx 1 root root 9 Oct 12 2017 lock -> / run/ lock
118- drwxr- xr- x 11 root root 4096 Aug 12 14 :52 log
119- lrwxrwxrwx 1 root root 15 Apr 23 14 :08 mail -> / var/ spool/ mail
120- lrwxrwxrwx 1 root root 4 Oct 12 2017 run -> / run
121- drwxr- xr- x 7 root root 4096 Jun 15 09 :22 spool
122- drwxrwxrwt 3 root root 4096 Aug 10 15 :15 tmp
71+ Without this parameter, capturing the Standard stream was lengthy using
72+ subprocess.PIPE parameter like below
12373
124- >> > print (ls.stderr.decode())
74+ ```python
75+ subprocess.run(
76+ [" ls" , " -l" , " /var" ], stdout = subprocess.PIPE , stderr = subprocess.PIPE
77+ )
78+ ```
12579
80+ With capture_output parameter, this will become shorten like below
12681
82+ ``` python
83+ subprocess.run([" ls" , " -l" , " /var" ], capture_output = True )
12784 ```
12885
129- With this version, you can get exact output by just passing this option
130- ``` capture_output=True ``` to ``` subprocess.run(...) ``` . Below is the example
131- for this.
86+ Below is the detailed example of using captured_true parameter with
87+ subprocess.run command.
13288
13389 ``` python
13490 >> > import subprocess
@@ -151,11 +107,14 @@ In this post, I will try to discuss improvements done in Core Python version 3.7
151107 ```
152108
153109
154- * Dataclass decorator: This new decorator will reduce many lines of your code.
155- Python is known for such improvements. For understanding this more, you should
156- be aware with Python's latest feature of providing type hints. I will try to
157- write a tutorial on type hints in up coming days. Until there, I will suggest
158- to grab any inital tutorial from your Google search.
110+ * Dataclasses:
111+
112+ The new class level decorator "@dataclass " will reduce many lines of your
113+ code. Python is well known for developing features which allows to achieve
114+ more by writing less. For understanding this feature batter, you should be
115+ aware of type hints. I will try to write a tutorial on type hints in up coming
116+ days. Until there, I will suggest to grab any available tutorial from your
117+ Google search.
159118
160119 Below is the usual way to define a Class and creating an object from it.
161120
@@ -170,7 +129,8 @@ In this post, I will try to discuss improvements done in Core Python version 3.7
170129 < __main__.Point object at 0x 7f3c73688b00>
171130 ```
172131
173- Above lines can be reduced with using ``` @dataclass ``` decorator.
132+ Converting above example with``` @dataclass ``` decorator below and then I will
133+ discuss some more advantages of using a dataclass decorator.
174134
175135 ``` python
176136 >> > from dataclasses import dataclass
@@ -184,32 +144,35 @@ In this post, I will try to discuss improvements done in Core Python version 3.7
184144 Point(x = 2 , y = 4 )
185145 ```
186146
187- Those lines of putting construct for obious things is gone! Additionally, the
188- class level decorator ``` @dataclass ``` is defining dundre methods like
189- ``` __repr__() ``` and ``` __eq__() ``` and ``` __hash__() ``` for us.
147+ Those lines of putting obious construct code is gone! Additionally, the class
148+ level decorator ``` @dataclass ``` is authomatically defining behaviours for
149+ dundre methods ``` __repr__() ``` , ``` __eq__() ``` and ``` __hash__() ```
150+ for us. I will advise of using this decorator for defining your Classes.
190151
191152
192- * Putting underscores in defining Integers: Writing large numbers as value in
193- code is difficult to read. I myself put finger on screen and count the places
194- to get an idea of number. In this version, you can add put * _ * for values.
195- Below example will make it more clear.
153+ * Putting underscores in defining Integers:
154+
155+ Writing large numbers as value in code is difficult to read. I myself put
156+ finger on screen and count the places to get an idea of number. In this
157+ version, you can add put * _ * for values. Below example will make it more
158+ clear.
196159
197160 ``` python
198161 >> > x = 1_00_000
199162 >> > x
200163 100000
201164 ```
202165
203- * Module level definition of dunder methods __ dir__ () and __ attr__ (): I was
204- facing this issue of hinding actual imports of my module with user. It was
205- always confusing when someone calls "dir()" on my modules. There is already
206- dunder method called "__ dir__ ()" to override this behaviour, but this method
207- was limited to Classes. With this version, it is possible to override the
208- behaviour of module by defining module level ``` __dir__() ``` method.
166+ * Module level definition of dunder methods __ dir__ () and __ attr__ ():
209167
168+ I was facing this issue of hinding actual imports of my module with user. It
169+ was always confusing when someone calls "dir()" on my modules. There is
170+ already dunder method called "__ dir__ ()" to override this behaviour, but this
171+ method was limited to Classes. With this version, it is possible to override
172+ the behaviour of module by defining module level ``` __dir__() ``` method.
210173
211- # TODO: Write the example of them here.
212174
175+ # TODO: Write the example of them here.
213176
214177* __ attr__ : You can override this method for various purposes like firing an
215178 API deprication warning. The method will behave just as it was behaving on
@@ -222,3 +185,25 @@ In this post, I will try to discuss improvements done in Core Python version 3.7
222185 important point, or something is confusing I request you to write me at
223186 [ jaysinhp@gmail.com ] ( mailto:jaysinhp@gmail.com ) . I will be happy to find
224187 improvements on me. Thanks for reading :)
188+
189+ * Default arguments to Namedtuples:
190+
191+ Namedtuples are less known, but very helpful feature of Python. I use them
192+ whenever I find constructing a Class can be lengthy, still I want to mimic the
193+ Class behaviour.
194+
195+ Below is the example of putting a default arguments to a named tuple:
196+
197+ ``` python
198+ >> > from collections import namedtuple
199+ >> > Point = namedtuple(" Point" , [" x" , " y" ], defaults = [2 ,])
200+ >> > p = Point(3 )
201+ >> > p
202+ Point(x = 3 , y = 2 )
203+ ```
204+
205+ Explanation:
206+
207+ The default arguments will be assigned in to attributes in the order from left
208+ to right. Here, the default argument 2 will be assigned to y, and not
209+ x.
0 commit comments