Spaces:
Sleeping

File size: 4,011 Bytes
a289188
8625b3d
a289188
 
 
 
 
 
 
 
 
 
 
 
8625b3d
 
 
a289188
 
 
 
 
 
 
 
 
8625b3d
 
a289188
 
 
8625b3d
a289188
 
 
8625b3d
 
a289188
 
 
 
 
 
 
 
 
8625b3d
 
 
a289188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8625b3d
a289188
 
 
8625b3d
a289188
 
 
 
 
8625b3d
a289188
 
 
 
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
{% extends "base.html" %}
{% block title %}Data Flow & Encoding - Data Science Lab{% endblock %}

{% block content %}
<div class="page-header">
    <h1>πŸ”„ Data Flow & Encoding</h1>
    <p class="breadcrumb">Home / Data Flow β€” Serialization, encoding, and data movement</p>
</div>

<div class="concept-section">
    <div class="concept-header"><h3>πŸ“– Data Serialization Formats</h3></div>
    <div class="concept-body">
        <p>Data must be serialized (converted to bytes) for storage and transmission. The format choice impacts performance, readability, and interoperability.</p>
        <table class="table">
            <tr><th>Format</th><th>Type</th><th>Pros</th><th>Cons</th><th>Use Case</th></tr>
            <tr><td>CSV</td><td>Text</td><td>Human-readable, universal</td><td>No schema, slow for big data</td><td>open data portal downloads</td></tr>
            <tr><td>JSON</td><td>Text</td><td>Flexible, API-friendly</td><td>Verbose, no native types</td><td>open data portal API responses</td></tr>
            <tr><td>Parquet</td><td>Binary</td><td>Columnar, compressed, fast</td><td>Not human-readable</td><td>Large-scale analytics</td></tr>
            <tr><td>Avro</td><td>Binary</td><td>Schema evolution, row-based</td><td>Complex setup</td><td>Kafka event streams</td></tr>
            <tr><td>Protocol Buffers</td><td>Binary</td><td>Compact, fast, typed</td><td>Requires .proto files</td><td>gRPC microservices</td></tr>
        </table>

        <div class="code-block"><span class="lang-label">Python</span><pre>import pandas as pd
import json

# CSV: Human-readable flat files
df = pd.read_csv('data/housing_data.csv')
df.to_csv('output/data_clean.csv', index=False)

# JSON: API-compatible structured data
data = df.head(5).to_dict(orient='records')
with open('output/data_sample.json', 'w') as f:
    json.dump(data, f, indent=2)

# Parquet: Columnar storage for analytics (10x smaller, 100x faster)
df.to_parquet('output/housing_data.parquet', engine='pyarrow')
df_parquet = pd.read_parquet('output/housing_data.parquet')

# Size comparison (typical for 1M rows):
# CSV: ~150 MB | JSON: ~250 MB | Parquet: ~15 MB</pre></div>
    </div>
</div>

<div class="concept-section">
    <div class="concept-header"><h3>πŸ“– Character Encoding</h3></div>
    <div class="concept-body">
        <p>Multilingual environments makes encoding critical.</p>
        <div class="code-block"><span class="lang-label">Python</span><pre># Always specify UTF-8 for data
df = pd.read_csv('data/housing_data.csv', encoding='utf-8')

# Common encoding issues:
# - Street names with Chinese characters
# - Malay place names with diacritics
# - Tamil school names

# Safe reading pattern
for encoding in ['utf-8', 'utf-8-sig', 'cp1252', 'latin-1']:
    try:
        df = pd.read_csv(filepath, encoding=encoding)
        break
    except UnicodeDecodeError:
        continue</pre></div>
    </div>
</div>

<div class="concept-section">
    <div class="concept-header"><h3>πŸ“– Data Flow Patterns</h3></div>
    <div class="concept-body">
        <div class="code-block"><span class="lang-label">Concept</span><pre>Request-Response (Synchronous):
  Client ──request──> Server ──response──> Client
  Example: GET /api/housing β†’ JSON response

Publish-Subscribe (Async):
  Producer ──event──> Message Queue ──event──> Consumer(s)
  Example: transit crowding event β†’ Kafka topic β†’ Dashboard + Alert service

Batch File Transfer:
  Source ──file──> Staging ──process──> Warehouse
  Example: Monthly housing CSV β†’ Python ETL β†’ SQLite star schema</pre></div>
    </div>
</div>

<div class="exercise-box">
    <h5>πŸ§ͺ Try It Yourself</h5>
    <p>1. Convert the housing CSV data to JSON format and compare file sizes.</p>
    <p>2. Create a data flow diagram for: API β†’ Validate β†’ Transform β†’ Store β†’ Query β†’ Chart.</p>
    <p>3. What encoding would you use for a dataset containing both English and Chinese street names?</p>
</div>
{% endblock %}