0% found this document useful (0 votes)
11 views6 pages

3 Data Science Tips That You Might Have Missed 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

3 Data Science Tips That You Might Have Missed 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Weekend Read: 3 Data

Science Tips that You Might


Have Missed
Don't Hard-Code. User Hydra Instead
When writing code, it is a good practice to put the values that you
might change in a separate file from your original script. This practice
not only saves you from wasting time searching for a specific variable
in your scripts but also makes your scripts more reproducible.

My favorite tool to handle config files is Hydra. The code below shows
how to get values from a config file using Hydra.

# config.yml
data: data1
variables:
drop_features: ['iid', 'id', 'idg', 'wave']

categorical_vars: ['undergra', 'zipcode']

# main.py
import hydra

@hydra.main(config_name='config.yml')
def main(config):
print(f'Process {config.data}')
print(f'Drop features:
{config.variables.drop_features}')

if __name__ == '__main__':
main()
Output:

Process data1
Drop features: ['iid', 'id', 'idg', 'wave'

Link to the source code.

Link to my article about Hydra.


getattr: a Better Way to Get the
Attribute of a Class
If you want to get a default value when calling an attribute that is not
in a class, use getattr() method.

The getattr(class, attribute_name) method simply gets the


value of an attribute of a class. However, if the attribute is not found in
a class, it returns the default value provided to the function.

class Food:

def __init__(self, name: str, color: str):


self.name = name

self.color = color

apple = Food('apple', 'red')

print("The color of apple is", getattr(apple,


'color', 'yellow'))

# The color of apple is red

print("The flavor of apple is", getattr(apple,


'flavor', 'sweet'))

# The flavor of apple is sweet

print("The flavor of apple is", apple.sweet)

# AttributeError: 'Food' object has no attribute


'sweet'

Link to the source code.


Python Clean Code: 6 Best Practices
to Make Your Python Functions More
Readable

Have you ever looked at a function you wrote one month earlier and
found it difficult to understand in 3 minutes? If that is the case, it is
time to refactor your code.

If you want your code to be reusable, you want it to be readable.


Writing clean code is especially important to data scientists who
collaborate with other team members in different roles.

Specifically, you want your Python function to:

be small
do one thing
contain code with the same level of abstraction
have fewer than 4 arguments
have no duplication
use descriptive names

In this article, you will learn how to utilize 6 practices mentioned above
to write better Python functions.

Link to the source code.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy