-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced-python.html
More file actions
390 lines (338 loc) · 22.7 KB
/
advanced-python.html
File metadata and controls
390 lines (338 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Python</title>
<meta content="" name="description">
<meta content="" name="keywords">
<!-- Favicons -->
<link href="assets/img/Favicon-1.png" rel="icon">
<link href="assets/img/Favicon-1.png" rel="apple-touch-icon">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Raleway:300,300i,400,400i,500,500i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">
<!-- Vendor CSS Files -->
<link href="assets/vendor/aos/aos.css" rel="stylesheet">
<link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
<link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
<link href="assets/vendor/glightbox/css/glightbox.min.css" rel="stylesheet">
<link href="assets/vendor/swiper/swiper-bundle.min.css" rel="stylesheet">
<!-- Creating a python code section-->
<link rel="stylesheet" href="assets/css/prism.css">
<script src="assets/js/prism.js"></script>
<!-- Template Main CSS File -->
<link href="assets/css/style.css" rel="stylesheet">
<!-- To set the icon, visit https://fontawesome.com/account-->
<script src="https://kit.fontawesome.com/5d25c1efd3.js" crossorigin="anonymous"></script>
<!-- end of icon-->
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML">
</script>
<!-- =======================================================
* Template Name: iPortfolio
* Updated: Sep 18 2023 with Bootstrap v5.3.2
* Template URL: https://bootstrapmade.com/iportfolio-bootstrap-portfolio-websites-template/
* Author: BootstrapMade.com
* License: https://bootstrapmade.com/license/
======================================================== -->
</head>
<body>
<!-- ======= Mobile nav toggle button ======= -->
<i class="bi bi-list mobile-nav-toggle d-xl-none"></i>
<!-- ======= Header ======= -->
<header id="header">
<div class="d-flex flex-column">
<div class="profile">
<img src="assets/img/myphoto.jpeg" alt="" class="img-fluid rounded-circle">
<h1 class="text-light"><a href="index.html">Arun</a></h1>
<div class="social-links mt-3 text-center">
<a href="https://www.linkedin.com/in/arunp77/" target="_blank" class="linkedin"><i class="bx bxl-linkedin"></i></a>
<a href="https://github.com/arunp77" target="_blank" class="github"><i class="bx bxl-github"></i></a>
<a href="https://twitter.com/arunp77_" target="_blank" class="twitter"><i class="bx bxl-twitter"></i></a>
<a href="https://www.instagram.com/arunp77/" target="_blank" class="instagram"><i class="bx bxl-instagram"></i></a>
<a href="https://arunp77.medium.com/" target="_blank" class="medium"><i class="bx bxl-medium"></i></a>
</div>
</div>
<nav id="navbar" class="nav-menu navbar">
<ul>
<li><a href="index.html#hero" class="nav-link scrollto active"><i class="bx bx-home"></i> <span>Home</span></a></li>
<li><a href="index.html#about" class="nav-link scrollto"><i class="bx bx-user"></i> <span>About</span></a></li>
<li><a href="index.html#resume" class="nav-link scrollto"><i class="bx bx-file-blank"></i> <span>Resume</span></a></li>
<li><a href="index.html#portfolio" class="nav-link scrollto"><i class="bx bx-book-content"></i> <span>Portfolio</span></a></li>
<li><a href="index.html#skills-and-tools" class="nav-link scrollto"><i class="bx bx-wrench"></i> <span>Skills and Tools</span></a></li>
<li><a href="index.html#language" class="nav-link scrollto"><i class="bi bi-menu-up"></i> <span>Languages</span></a></li>
<li><a href="index.html#awards" class="nav-link scrollto"><i class="bi bi-award-fill"></i> <span>Awards</span></a></li>
<li><a href="index.html#professionalcourses" class="nav-link scrollto"><i class="bx bx-book-alt"></i> <span>Professional Certification</span></a></li>
<li><a href="index.html#publications" class="nav-link scrollto"><i class="bx bx-news"></i> <span>Publications</span></a></li>
<!-- <li><a href="index.html#extra-curricular" class="nav-link scrollto"><i class="bx bx-rocket"></i> <span>Extra-Curricular Activities</span></a></li> -->
<!-- <li><a href="#contact" class="nav-link scrollto"><i class="bx bx-envelope"></i> <span>Contact</span></a></li> -->
</ul>
</nav><!-- .nav-menu -->
</div>
</header><!-- End Header -->
<main id="main">
<!-- ======= Breadcrumbs ======= -->
<section id="breadcrumbs" class="breadcrumbs">
<div class="container">
<div class="d-flex justify-content-between align-items-center">
<h2>Python</h2>
<ol>
<li><a href="portfolio-details.html" class="clickable-box">Content section</a></li>
<li><a href="index.html#portfolio" class="clickable-box">Portfolio section</a></li>
</ol>
</div>
</div>
</section><!-- End Breadcrumbs -->
<!------ right dropdown menue ------->
<div class="right-side-list">
<div class="dropdown">
<button class="dropbtn"><strong>Shortcuts:</strong></button>
<div class="dropdown-content">
<ul>
<li><a href="cloud-compute.html"><i class="fas fa-cloud"></i> Cloud</a></li>
<li><a href="AWS-GCP.html"><i class="fas fa-cloud"></i> AWS-GCP</a></li>
<li><a href="amazon-s3.html"><i class="fas fa-cloud"></i> AWS S3</a></li>
<li><a href="ec2-confi.html"><i class="fas fa-server"></i> EC2</a></li>
<li><a href="Docker-Container.html"><i class="fab fa-docker" style="color: rgb(29, 27, 27);"></i> Docker</a></li>
<li><a href="Jupyter-nifi.html"><i class="fab fa-python" style="color: rgb(34, 32, 32);"></i> Jupyter-nifi</a></li>
<li><a href="snowflake-task-stream.html"><i class="fas fa-snowflake"></i> Snowflake</a></li>
<li><a href="data-model.html"><i class="fas fa-database"></i> Data modeling</a></li>
<li><a href="sql-basics.html"><i class="fas fa-table"></i> QL</a></li>
<li><a href="sql-basic-details.html"><i class="fas fa-database"></i> SQL</a></li>
<li><a href="Bigquerry-sql.html"><i class="fas fa-database"></i> Bigquery</a></li>
<li><a href="scd.html"><i class="fas fa-archive"></i> SCD</a></li>
<li><a href="sql-project.html"><i class="fas fa-database"></i> SQL project</a></li>
<!-- Add more subsections as needed -->
</ul>
</div>
</div>
</div>
<!-- ======= Portfolio Details Section ======= -->
<section id="portfolio-details" class="portfolio-details">
<div class="container">
<div class="row gy-4">
<h1>Advanced Python</h1>
<div class="col-lg-8">
<div class="portfolio-details-slider swiper">
<div class="swiper-wrapper align-items-center">
<figure>
<img src="assets/img/machine-ln/python-logo.png" alt="" style="max-width: 50%; max-height: auto;">
<figcaption></figcaption>
</figure>
</div>
<div class="swiper-pagination"></div>
</div>
</div>
<div class="col-lg-4 grey-box">
<div class="section-title">
<h3>Table of Contents</h3>
<ol>
<li><a href="#annotations">Annotations</a></li>
<li><a href="#decorators">Decorators</a></li>
<li><a href="#multithreading">Multithreading and multiprocessing in python</a></li>
<li><a href="#reference">Reference</a></li>
</ol>
</div>
</div>
</div>
<section>
<h3 id="annotations">Annotations</h3>
Annotations in Python are a way to add metadata to functions, methods, and variables. They provide additional information about the types of arguments, return values, and other aspects of the code. Annotations do not affect the runtime behavior of the code; they are primarily used for documentation and static analysis by tools like linters, type checkers, and IDEs. Here's an overview of how annotations can be used in Python:
<ul>
<li><strong>Function Annotations: </strong>Function annotations are specified using colons (<code>:</code>) after the parameter list, followed by the annotation expression. The annotation expression can be any valid Python expression.</li>
<pre class="language-python"><code>
def greet(name: str, age: int) -> str:
return f"Hello, {name}! You are {age} years old."
# Annotations are optional and not enforced by Python
# They are primarily used for documentation and type hinting
</code></pre>
<li><strong>Type Annotations: </strong>Type annotations specify the expected types of function arguments and return values. They are used for static type checking and documentation purposes.</li>
<pre class="language-python"><code>
def add(a: int, b: int) -> int:
return a + b
# Type annotations can be simple types (int, str, float, etc.)
# They can also be complex types (List, Tuple, Dict, etc.) from the typing module
</code></pre>
<li><strong>Variable Annotations:</strong> Variable annotations are similar to function annotations but are used to specify the type of variables. They can be defined inline or separately.</li>
<pre class="language-python"><code>
# Inline variable annotation
x: int = 10
# Separate variable annotation
y: str
y = "Hello"
</code></pre>
<li><strong>Annotations for Classes and Methods: </strong>Annotations can also be applied to class definitions and methods.</li>
<pre class="language-python"><code>
class MyClass:
def __init__(self, x: int, y: str) -> None:
self.x = x
self.y = y
def method(self, z: float) -> None:
pass
</code></pre>
<li><strong>Accessing Annotations:</strong>Annotations can be accessed at runtime using the <code>__annotations__</code> attribute of functions and classes.</li>
<pre class="language-python"><code>
print(greet.__annotations__) # {'name': <class 'str'>, 'age': <class 'int'>, 'return': <class 'str'>}
</code></pre>
<li><strong>Type Checking:</strong> Annotations can be used with type-checking tools like <code>mypy</code> to perform static type checking on Python code.</li>
<pre class="language-python"><code>
# Use mypy to perform static type checking
# Install mypy using pip: pip install mypy
# Run mypy on a Python file: mypy filename.py
</code></pre>
<li><strong>Documentation:</strong> Annotations serve as documentation for functions, methods, and variables, providing insight into the expected types of arguments and return values.</li>
<pre class="language-python"><code>
def calculate_area(radius: float) -> float:
"""Calculate the area of a circle given its radius.
Args:
radius: The radius of the circle.
Returns:
The area of the circle.
"""
return 3.14 * radius ** 2
</code></pre>
</ul>
<p>Annotations in Python are a powerful tool for improving code readability, facilitating type checking, and enhancing documentation. While they are not enforced by the Python interpreter, they are widely used in the Python community to improve code quality and maintainability.</p>
<!-------------------------->
<h3 id="decorators">Decorators</h3>
Decorators are a powerful feature in Python that allow you to modify or extend the behavior of functions or methods without changing their source code. Decorators are implemented using functions or classes, and they provide a clean and concise way to add functionality to existing code. Here's an overview of how decorators work in Python:
<ul>
<li><strong>Basic Decorator Syntax: </strong>Decorators are defined using the <code>@decorator_name</code> syntax, where <code>decorator_name</code> is the name of the decorator function or class.</li>
<pre class="language-python"><code>
def decorator(func):
def wrapper(*args, **kwargs):
print("Before calling the function")
result = func(*args, **kwargs)
print("After calling the function")
return result
return wrapper
@decorator
def my_function():
print("Inside the function")
my_function()
</code></pre>
<li><strong>Decorator Function: </strong>A decorator function is a regular Python function that takes another function as its argument and returns a new function. The new function typically adds some behavior before or after calling the original function.</li>
<pre class="language-python"><code>
def log_time(func):
def wrapper(*args, **kwargs):
import time
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
print(f"Execution time: {end_time - start_time} seconds")
return result
return wrapper
</code></pre>
<li><strong>Applying a Decorator:</strong> To apply a decorator to a function or method, simply place the <code>@decorator_name</code> line immediately before the function or method definition.</li>
<pre class="language-python"><code>
@log_time
def calculate_sum(n):
return sum(range(n+1))
calculate_sum(10000)
</code></pre>
<li><strong>Chaining Decorators: </strong>You can apply multiple decorators to a single function by stacking them on top of each other using multiple <code>@decorator_name</code> lines.</li>
<pre class="language-python"><code>
@decorator1
@decorator2
def my_function():
pass
</code></pre>
<p><strong>Example:</strong></p>
<pre><code>
def uppercase_decorator(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result.upper()
return wrapper
def bold_decorator(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return f"<b>{result}</b>"
return wrapper
@bold_decorator
@uppercase_decorator
def greet(name):
return f"Hello, {name}!"
print(greet("John"))
</code></pre>
<pre><strong>Output:</strong><b>HELLO JOHN</b></pre>
<li><strong>Class Decorators:</strong> In addition to functions, decorators can also be implemented using classes. To create a class decorator, the class must implement the <code>__call__</code> method.</li>
<pre class="language-python"><code>
class DecoratorClass:
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
print("Before calling the function")
result = self.func(*args, **kwargs)
print("After calling the function")
return result
</code></pre>
</ul>
<p><strong>Use cases for Decorators:</strong></p>
<ul>
<li>Logging and profiling</li>
<li>Authentication and authorization</li>
<li>Caching</li>
<li>Rate limiting</li>
<li>Error handling</li>
</ul>
Decorators are a versatile tool in Python that allow you to add cross-cutting concerns to your code in a modular and reusable way. By using decorators, you can keep your codebase clean and maintainable while adding functionality such as logging, caching, and error handling with minimal effort.
</section>
<!-------Reference ------->
<section id="reference">
<h3>References</h3>
<ol>
<li><a href="https://github.com/arunp77/Python-programming/blob/main/Python-fundamental/Classes-modules.ipynb" target="_blank"> Checkout my Jupyter notebook on these topics</a></li>
<li><a href="object-oriented-programming.html" target="_blank">For object oriented Programming in Python</a></li>
</ol>
</section>
<hr>
<div style="background-color: #f0f0f0; padding: 15px; border-radius: 5px;">
<h3>Some other interesting things to know:</h3>
<ul style="list-style-type: disc; margin-left: 30px;">
<li>Visit my website on <a href="sql-project.html">For Data, Big Data, Data-modeling, Datawarehouse, SQL, cloud-compute.</a></li>
<li>Visit my website on <a href="Data-engineering.html">Data engineering</a></li>
</ul>
</div>
<p></p>
<div class="navigation">
<a href="portfolio-details.html" class="clickable-box">
<span class="arrow-right">Content</span>
</a>
<!---------->
<a href="index.html#portfolio" class="clickable-box">
<span class="arrow-left">Portfolio section</span>
</a>
</div>
</div>
</section><!-- End Portfolio Details Section -->
</main><!-- End #main --
<!-- ======= Footer ======= -->
<footer id="footer">
<div class="container">
<div class="copyright">
© Copyright <strong><span>Arun</span></strong>
</div>
</div>
</footer><!-- End Footer -->
<a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>
<!-- Vendor JS Files -->
<script src="assets/vendor/purecounter/purecounter_vanilla.js"></script>
<script src="assets/vendor/aos/aos.js"></script>
<script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="assets/vendor/glightbox/js/glightbox.min.js"></script>
<script src="assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
<script src="assets/vendor/swiper/swiper-bundle.min.js"></script>
<script src="assets/vendor/typed.js/typed.umd.js"></script>
<script src="assets/vendor/waypoints/noframework.waypoints.js"></script>
<script src="assets/vendor/php-email-form/validate.js"></script>
<!-- Template Main JS File -->
<script src="assets/js/main.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
hljs.initHighlightingOnLoad();
});
</script>
</body>
</html>