Skip to content

Commit

Permalink
Merge pull request #10 from imohitmayank/nov_23
Browse files Browse the repository at this point in the history
Moved Python section to DS tools and more.
  • Loading branch information
imohitmayank authored Nov 7, 2023
2 parents 5a2602b + 2d8841c commit 39c8dfc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,41 @@ logger.setLevel(logging.INFO)
!!! note
Remember to switch to another VE when you start working on another project or/and to deactivate the VE when you want to move to base VE.

### Type Hints and Data Validation

- Python comes with dynamic variable type support, i.e. you can execute code `x=1` and `x='a'` one after the another and the it will still work. This is in contrast with languages like Java and C++ where you have to specify the type explicitly, for example `int a = 1`. While this feature makes Python easier to use and more dynamic, it comes at the cost of clarity and complexity. Now, let's talk about some good practices to mitigate this.

- [Type Hints](https://docs.python.org/3/library/typing.html), as the name suggests, is an inbuilt functionality in Python to provide hints about the variable or parameter types in Python. One example is shown below, where `name: str` denotes that the function `greeting` expects a string input, and `-> str` denotes that it returns a string output.

```python linenums="1"
def greeting(name: str) -> str:
return 'Hello ' + name
```

!!! Note
Python runtime does not enforce type checks by default, type hints are there for cleaner code and are used by 3rd party tools like IDEs and linters.

- For Data validation purpose, we can use [Pydantic](https://docs.pydantic.dev/latest/) that provides multiple ready-made validation, custom validation and json serialization support. One example is shown below, where we define the model `Delivery` class *(that inherits from `BaseModel`)* with `timestamp`, `dimensions` and `enum` fields and provide their respective type hints. Here, Pydantic enforces that `timestamp` should be of type `datetime`, `dimensions` should be of type `tuple` of two `int` and `enum` will be positive integer.

```python linenums="1"
from datetime import datetime
from typing import Tuple
from pydantic import BaseModel, PositiveInt

# class definition
class Delivery(BaseModel):
timestamp: datetime
dimensions: Tuple[int, int]
enum: PositiveInt


m = Delivery(timestamp='2020-01-02T03:04:05Z', dimensions=['10', '20'], enum=1)
print(repr(m.timestamp))
#> datetime.datetime(2020, 1, 2, 3, 4, 5, tzinfo=TzInfo(UTC))
print(m.dimensions)
#> (10, 20)
```

## References

- [PEP8 Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/)
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 4 additions & 6 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,14 @@ nav:
- 'Introduction': 'network_science/kg_introduction.md'
- 'KG Embedding Algorithms': 'network_science/kg_embedding_algorithms.md'

- 'Python':
- 'python/good_practices.md'
- 'python/python_snippets.md'
- 'Blogs':
- 'python/scraping_websites.md'

- 'Data Science Tools':
- 'data_science_tools/introduction.md'
- 'data_science_tools/python_snippets.md'
- 'data_science_tools/python_good_practices.md'
- 'data_science_tools/version_control.md'
- 'data_science_tools/compute_and_ai_services.md'
- 'Blogs':
- 'data_science_tools/scraping_websites.md'

- 'Machine Learning':
- 'machine_learning/introduction.md'
Expand Down

0 comments on commit 39c8dfc

Please sign in to comment.