6.8. Sharing and Downloading#

This section covers some tools to share and download your data.

6.8.1. gdown: Download a File from Google Drive in Python#

Hide code cell content
!pip install gdown 

If you want to download a file from Google Drive in Python, use gdown. All you need to specify is the URL link.

import gdown

# Format of url: https://drive.google.com/uc?id=YOURFILEID
url = "https://drive.google.com/uc?id=1jI1cmxqnwsmC-vbl8dNY6b4aNBtBbKy3"
output = "Twitter.zip"

gdown.download(url, output, quiet=False)
Hide code cell output
Downloading...
From: https://drive.google.com/uc?id=1jI1cmxqnwsmC-vbl8dNY6b4aNBtBbKy3
To: /home/khuyen/book/book/Chapter4/Twitter.zip
120MB [00:09, 12.1MB/s] 
'Twitter.zip'

Link to gdown.

6.8.2. pyserde: Effortless Serialization and Deserialization of Dataclass Objects#

Hide code cell content
!pip install pyserde

Dataclasses provide a concise syntax for defining data-holding classes. Paired with pyserde, you can easily serialize and deserialize dataclass objects.

Serialization converts dataclass data into a serialized format (e.g., JSON, YAML) for easy storage and transmission. Deserialization reconstructs the dataclass object from serialized data.

from dataclasses import dataclass
from serde import serde
from serde.json import from_json, to_json
from serde.yaml import from_yaml, to_yaml


@serde
@dataclass
class User:
    name: str
    age: int


user1 = User(name="user1", age=20)
print(to_json(user1))
{"name":"user1","age":20}
from_json(User, '{"name":"user1","age":20}')
User(name='user1', age=20)
print(to_yaml(user1))
age: 20
name: user1
from_yaml(User, "age: 20\nname: user1\n")
User(name='user1', age=20)

Link to pyserde.

6.8.3. ItsDangerous: Safely Pass Trusted Data to Untrusted Environments and Back#

Hide code cell content
!pip install -U itsdangerous

When passing data between different web requests, there is a risk of malicious code injection.

To ensure the safety of passing data to untrusted environments, use ItsDangerous. ItsDangerous adds a unique signature to the data to verify that the data has not been tampered with during transmission.

from itsdangerous import URLSafeSerializer

auth_s = URLSafeSerializer("some key")
token = auth_s.dumps({"id": 5, "name": "khuyentran"})
token
'eyJpZCI6NSwibmFtZSI6ImtodXllbnRyYW4ifQ.3cQlkHP1MyeUS8jNQmGV_mbrxsQ'
# Get the data back from the token using the secret key
data = auth_s.loads(token)
data["name"]
'khuyentran'

Link to ItsDangerous.