Professional Documents
Culture Documents
Get unlimited access to all of Medium for less than $1/week. Become a member
1. Using a virtual environment: This shows that you isolate environments to avoid
issues with dependencies and package versions. Also if we stick to requirements
& constraints files, it shows that we care about how the app should be run in
another place.
pip install -r requirements.txt -c constraints.txt
2. Using type hints: Type hints make our code more readable. This helps with ease
of maintenance and also debugging.
def demo(x: type_x, y: type_y, z: type_z= 100) -> type_return :
8. Using zip (itertools): While working with multiple list zip method simplifies the
iteration and items are available straight out of the box.
for item1, item2 in zip(list1, list2) . Zip is only indicative, knowing other
functions from itertools gives an idea that the coder is aware of tools that
simplify the process.
10. Using else with for & while: This is the pythonic way of telling that all items
were processed in their natural course.
12. Using the items method when working with dictionaries: This is a more
pythonic way of accessing the key and item rather than looping over keys and
then accessing value with get or subscript.
for key, val in my_dict.items()
13. Using dict.get() method: This avoids error when the key is not present and
also allows us to return a default value when the key is not present.
14. Using the isinstance keyword when doing type equality of objects: Comparing
object type with will give wrong results in case of inherited objects.
15. Using is for equality with singleton: Using is to check None, True & False . The
is operator checks if the two items reference the same object (i.e. present in
the same memory location). The == operator checks for value equality.
16. Using the “or”, “and”, “not” keyword when working with Boolean values: This
helps in deciding the correct flow of operations. Also using and over or helps in
the early breaking of code where we have too many false compared to true. In
OR, conditions are checked till it keep encountering false, moment it receives a
true it break, in AND as soon as the false condition is met the rest condition are
not checked. Note: If you need to be sure of all conditions evaluate to true or any
one is true you can use any and all function over iterable list of boolean
values.
18. Using functional programming techniques (like map, reduce, and filter): This
makes code efficient.
squared = map(lambda x : x*x, [1, 2, 3]) This is faster than running a for loop.
Also map loads each item one by one, unlike for loop which loads all items in
memory, this makes it more memory efficient. Although we should prefer
generator as far as possible.
19. Using Decorators: This helps in writing reusable code. Decorators like @retry
@properties @lru_cache etc help in extending the functionality of code. There are
frameworks like Flask which has @route decorator. Airflow has @task operator.
20. Using the right built-in libraries: Using the right built-in packages can reduce a
lot of boilerplate code. Also since specific packages are efficient at doing thing
specific tasks. It makes sense to use the right packages. E.g. os vs pathlib &
os.system vs subprocess .
21. Using correct naming conventions: The PEP-8 is a popular guideline among
Python programmers, which tells us how to name our variables, classes, and
functions properly.
22. Sticking to a style guide: Sticking to your chosen rules makes refactoring
moments like a breeze and its easier to understand later on.
23. Using Docstring and comment: Docstring help in making user understand what
the function/module intends to do. Its input, output, and their types, sample
values. Also, it helps if we put single-line comments before and expression of
what it signifies.
Python Programming
View list
Follow
PySpark Styleguide
Common practices to make PySpark code elegant
48 1
770 3
ELT is dead, and EtLT will be the end of modern data processing
architecture
When talking about data processing, people often abbreviate it as “ETL.” However, if we look
closely, data processing has undergone several…
344 4
Amit Singh Rathore in Dev Genius
75
1.2K 9
Evenword
Creating a Machine Learning Based Web Application Using Django
Deploy your Machine Learning model on the web using Python’s famous Django framework
and increase its visibility.
124 3
Lists
ChatGPT
21 stories · 109 saves
Shubhadeep Chattopadhyay
784 23
1.3K 32
Denat Hoxha
1.1K 25
1.3K 24