bm-preprocessing 1.2.0__tar.gz → 1.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/PKG-INFO +1 -1
  2. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/USAGE.md +47 -73
  3. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/pyproject.toml +1 -1
  4. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/__init__.py +2 -1
  5. bm_preprocessing-1.3.0/src/bm_preprocessing/DM/python_doc.py +30 -0
  6. bm_preprocessing-1.3.0/src/bm_preprocessing/DM/sources/python_doc.py +174 -0
  7. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/.gitignore +0 -0
  8. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/README.md +0 -0
  9. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/adaboost.py +0 -0
  10. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/all.py +0 -0
  11. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/all_vis.py +0 -0
  12. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/apriori.py +0 -0
  13. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/bagging.py +0 -0
  14. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/hash.py +0 -0
  15. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/hunts.py +0 -0
  16. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/hunts_test.py +0 -0
  17. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/id3.py +0 -0
  18. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/id3_test.py +0 -0
  19. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/lib_doc.py +0 -0
  20. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/metrics.py +0 -0
  21. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/preprocessing.py +0 -0
  22. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/adaboost.py +0 -0
  23. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/all.py +0 -0
  24. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/all_hunts_tree +0 -0
  25. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/all_vis.py +0 -0
  26. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/apriori.py +0 -0
  27. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/bagging.py +0 -0
  28. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/data.csv +0 -0
  29. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/hash.py +0 -0
  30. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/hunts.py +0 -0
  31. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/hunts_test.py +0 -0
  32. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/id3.py +0 -0
  33. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/id3_test.py +0 -0
  34. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/lib_doc.py +0 -0
  35. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/metrics.py +0 -0
  36. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/preprocessing.py +0 -0
  37. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/DM/sources/tennis.csv +0 -0
  38. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/__init__.py +0 -0
  39. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/all.py +0 -0
  40. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/all_vis.py +0 -0
  41. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/eval_metrics.py +0 -0
  42. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/ndd.py +0 -0
  43. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/rel.py +0 -0
  44. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/sources/all.py +0 -0
  45. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/sources/all_vis.py +0 -0
  46. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/sources/eval_metrics.py +0 -0
  47. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/sources/ndd.py +0 -0
  48. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/IR/sources/rel.py +0 -0
  49. {bm_preprocessing-1.2.0 → bm_preprocessing-1.3.0}/src/bm_preprocessing/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bm-preprocessing
3
- Version: 1.2.0
3
+ Version: 1.3.0
4
4
  Summary: A package to preprocess text data
5
5
  Requires-Python: >=3.8
6
6
  Requires-Dist: build>=1.2.2.post1
@@ -8,52 +8,33 @@ pip install bm-preprocessing
8
8
 
9
9
  ---
10
10
 
11
- ## Usage in Python File
11
+ ## Usage in Python Scripts
12
12
 
13
- Create a file `example.py`:
13
+ You can directly import and utilize the predefined Data Mining (DM) and Information Retrieval (IR) configurations. Be sure to import from either `bm_preprocessing.DM` or `bm_preprocessing.IR` depending on the subject.
14
14
 
15
15
  ```python
16
- # Import modules
17
- from bm_preprocessing.IR import all, all_vis, eval_metrics, ndd, rel
18
- from bm_preprocessing.DM import adaboost, apriori, bagging, hash, hunts, hunts_test, id3, id3_test, lib_doc, metrics, preprocessing
16
+ # Import cohesive all-in-one modules
17
+ from bm_preprocessing.IR import all, all_vis
19
18
  from bm_preprocessing.DM import all, all_vis
20
19
 
21
- # Print the source code
20
+ # Import DM-specific algorithms
21
+ from bm_preprocessing.DM import adaboost, apriori, bagging, hash, hunts, hunts_test, id3, id3_test, lib_doc, metrics, preprocessing, python_doc
22
+
23
+ # Import IR-specific algorithms
24
+ from bm_preprocessing.IR import eval_metrics, ndd, rel
25
+
26
+ # Print the source code directly
22
27
  print("=== IR All Module ===")
23
28
  print(all)
24
29
 
25
- print("\n=== DM Apriori Module ===")
26
- print(apriori)
30
+ print("\n=== IR Near Duplicate Documents ===")
31
+ print(ndd)
27
32
 
28
33
  print("\n=== DM AdaBoost Module ===")
29
34
  print(adaboost)
30
-
31
- print("\n=== DM Bagging Module ===")
32
- print(bagging)
33
-
34
- print("\n=== DM Hash Module ===")
35
- print(hash)
36
-
37
- print("\n=== DM Hunts Module ===")
38
- print(hunts)
39
-
40
- print("\n=== DM Hunts Test Module ===")
41
- print(hunts_test)
42
-
43
- print("\n=== DM ID3 Module ===")
44
- print(id3)
45
-
46
- print("\n=== DM ID3 Test Module ===")
47
- print(id3_test)
48
-
49
- print("\n=== DM Metrics Module ===")
50
- print(metrics)
51
-
52
- print("\n=== DM Preprocessing Module ===")
53
- print(preprocessing)
54
35
  ```
55
36
 
56
- Run it:
37
+ Run it locally:
57
38
  ```bash
58
39
  python example.py
59
40
  ```
@@ -62,56 +43,45 @@ python example.py
62
43
 
63
44
  ## Usage in Terminal (Interactive Python)
64
45
 
46
+ If you just need quick access to the source code during an exam or practical, spin up the Python REPL:
47
+
65
48
  ```bash
66
49
  python
67
50
  ```
68
51
 
69
- Then in the Python REPL:
52
+ Then drop into the REPL to retrieve the code:
70
53
 
71
54
  ```python
55
+ # Returns entire IR source code cohesive module
72
56
  >>> from bm_preprocessing.IR import all
73
57
  >>> print(all)
74
- # Prints entire IR/all.py source code
75
-
76
- >>> from bm_preprocessing.DM import apriori
77
- >>> print(apriori)
78
- # Prints entire DM/apriori.py source code
79
58
 
59
+ # Returns Data Mining AdaBoost source code
80
60
  >>> from bm_preprocessing.DM import adaboost
81
61
  >>> print(adaboost)
82
- # Prints entire DM/adaboost.py source code
83
-
84
- >>> from bm_preprocessing.DM import bagging
85
- >>> print(bagging)
86
- # Prints entire DM/bagging.py source code
87
-
88
- >>> from bm_preprocessing.DM import hunts, hunts_test
89
- >>> print(hunts)
90
- # Prints entire DM/hunts.py source code
91
- >>> print(hunts_test)
92
- # Prints entire DM/hunts_test.py source code
93
-
94
- >>> from bm_preprocessing.DM import id3, id3_test
95
- >>> print(id3)
96
- # Prints entire DM/id3.py source code
97
- >>> print(id3_test)
98
- # Prints entire DM/id3_test.py source code
99
-
100
- >>> from bm_preprocessing.DM import metrics
101
- >>> print(metrics)
102
- # Prints entire DM/metrics.py source code
62
+
63
+ # Returns minhash and LSH source code
64
+ >>> from bm_preprocessing.IR import ndd
65
+ >>> print(ndd)
103
66
  ```
104
67
 
105
68
  ---
106
69
 
107
70
  ## One-liner in Terminal
108
71
 
72
+ If you want the terminal to automatically print the file contents for you without entering the REPL, you can execute these one-liners directly in your Bash/PowerShell:
73
+
74
+ ### Information Retrieval (IR)
109
75
  ```bash
110
76
  python -c "from bm_preprocessing.IR import all; print(all)"
111
77
  python -c "from bm_preprocessing.IR import all_vis; print(all_vis)"
112
- python -c "from bm_preprocessing.IR import eval_metrics; print(eval_metrics)"
113
78
  python -c "from bm_preprocessing.IR import ndd; print(ndd)"
114
79
  python -c "from bm_preprocessing.IR import rel; print(rel)"
80
+ python -c "from bm_preprocessing.IR import eval_metrics; print(eval_metrics)"
81
+ ```
82
+
83
+ ### Data Mining (DM)
84
+ ```bash
115
85
  python -c "from bm_preprocessing.DM import all; print(all)"
116
86
  python -c "from bm_preprocessing.DM import all_vis; print(all_vis)"
117
87
  python -c "from bm_preprocessing.DM import apriori; print(apriori)"
@@ -123,31 +93,35 @@ python -c "from bm_preprocessing.DM import hunts_test; print(hunts_test)"
123
93
  python -c "from bm_preprocessing.DM import id3; print(id3)"
124
94
  python -c "from bm_preprocessing.DM import id3_test; print(id3_test)"
125
95
  python -c "from bm_preprocessing.DM import metrics; print(metrics)"
126
- python -c "from bm_preprocessing.DM import lib_doc; print(lib_doc)"
127
96
  python -c "from bm_preprocessing.DM import preprocessing; print(preprocessing)"
97
+ python -c "from bm_preprocessing.DM import lib_doc; print(lib_doc)"
98
+ python -c "from bm_preprocessing.DM import python_doc; print(python_doc)"
128
99
  ```
129
100
 
130
101
  ---
131
102
 
132
- ## Available Modules
103
+ ## Available Modules Reference
133
104
 
134
- | Import | Description |
135
- |--------|-------------|
136
- | `from bm_preprocessing.IR import all` | Information Retrieval (MinHash, LSH, Rocchio, Jaccard, VS) |
137
- | `from bm_preprocessing.IR import all_vis` | IR algorithms with Matplotlib visualizations |
138
- | `from bm_preprocessing.IR import eval_metrics` | Jaccard, PRF, Compression Ratio, MAP metrics & plots |
105
+ | Import Path | Description |
106
+ |-------------|-------------|
107
+ | **Information Retrieval (IR)** | |
108
+ | `from bm_preprocessing.IR import all` | Cohesive IR File: MinHash, LSH, Rocchio, Jaccard, VS |
109
+ | `from bm_preprocessing.IR import all_vis` | Cohesive IR File + Matplotlib visualizations & Heatmaps |
139
110
  | `from bm_preprocessing.IR import ndd` | Near Duplicate Documents (MinHash & LSH) |
140
111
  | `from bm_preprocessing.IR import rel` | Relevance feedback & query expansion (Rocchio & LCA) |
141
- | `from bm_preprocessing.DM import all` | All DM algorithms (Hunt's, ID3, Bagging, AdaBoost, metrics) |
142
- | `from bm_preprocessing.DM import all_vis` | All DM algorithms + graphviz & full visualization |
112
+ | `from bm_preprocessing.IR import eval_metrics` | Jaccard, PRF, Compression Ratios, MAP metrics & plots |
113
+ | **Data Mining (DM)** | |
114
+ | `from bm_preprocessing.DM import all` | Cohesive DM File: Hunt's, ID3, Bagging, AdaBoost, Metrics |
115
+ | `from bm_preprocessing.DM import all_vis` | Cohesive DM File + Graphviz & Matplotlib visualizations |
143
116
  | `from bm_preprocessing.DM import apriori` | Apriori algorithm |
144
117
  | `from bm_preprocessing.DM import adaboost` | Bagging & AdaBoost ensemble classifiers |
145
118
  | `from bm_preprocessing.DM import bagging` | Bagging ensemble classifier |
146
119
  | `from bm_preprocessing.DM import hash` | Hash-based mining |
147
120
  | `from bm_preprocessing.DM import hunts` | Hunt's decision tree algorithm |
148
- | `from bm_preprocessing.DM import hunts_test` | Hunt's decision tree with visualization |
121
+ | `from bm_preprocessing.DM import hunts_test` | Hunt's decision tree with dataset visualization |
149
122
  | `from bm_preprocessing.DM import id3` | ID3 decision tree algorithm |
150
- | `from bm_preprocessing.DM import id3_test` | ID3 decision tree with visualization |
123
+ | `from bm_preprocessing.DM import id3_test` | ID3 decision tree with dataset visualization |
151
124
  | `from bm_preprocessing.DM import metrics` | Classification metrics & curves |
152
- | `from bm_preprocessing.DM import lib_doc` | Pandas/NumPy/Sklearn/DM/IR cheat sheet |
153
125
  | `from bm_preprocessing.DM import preprocessing` | Data preprocessing utilities |
126
+ | `from bm_preprocessing.DM import lib_doc` | Pandas, NumPy, Sklearn cheat sheet (DM & IR logic) |
127
+ | `from bm_preprocessing.DM import python_doc` | Python Basics cheat sheet (Sets, Dicts, Comprehensions, etc.) |
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "bm-preprocessing"
7
- version = "1.2.0"
7
+ version = "1.3.0"
8
8
  description = "A package to preprocess text data"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
@@ -13,6 +13,7 @@ from .id3_test import id3_test
13
13
  from .lib_doc import lib_doc
14
14
  from .metrics import metrics
15
15
  from .preprocessing import preprocessing
16
+ from .python_doc import python_doc
16
17
 
17
- __all__ = ["adaboost", "all", "all_vis", "apriori", "bagging", "hash", "hunts", "hunts_test", "id3", "id3_test", "lib_doc", "metrics", "preprocessing"]
18
+ __all__ = ["adaboost", "all", "all_vis", "apriori", "bagging", "hash", "hunts", "hunts_test", "id3", "id3_test", "lib_doc", "metrics", "preprocessing", "python_doc"]
18
19
 
@@ -0,0 +1,30 @@
1
+ """Source code loader for DM/python_doc.py"""
2
+
3
+ from pathlib import Path
4
+
5
+
6
+ class SourceCodeModule:
7
+ """A class that displays source code when printed."""
8
+
9
+ def __init__(self, name: str, source_path: Path):
10
+ self.name = name
11
+ self._source_path = source_path
12
+ self._source_code = None
13
+
14
+ @property
15
+ def source_code(self) -> str:
16
+ """Lazily load source code."""
17
+ if self._source_code is None:
18
+ self._source_code = self._source_path.read_text(encoding="utf-8")
19
+ return self._source_code
20
+
21
+ def __repr__(self) -> str:
22
+ return self.source_code
23
+
24
+ def __str__(self) -> str:
25
+ return self.source_code
26
+
27
+
28
+ # Get the path to the source file
29
+ _source_file = Path(__file__).parent / "sources" / "python_doc.py"
30
+ python_doc = SourceCodeModule("DM.python_doc", _source_file)
@@ -0,0 +1,174 @@
1
+ """
2
+ =============================================================================
3
+ PYTHON BASICS CHEAT SHEET
4
+ =============================================================================
5
+ A quick reference guide for core Python concepts, data structures, and features.
6
+ """
7
+
8
+ # =============================================================================
9
+ # 1. LISTS (Mutable, Ordered)
10
+ # =============================================================================
11
+ my_list = [1, 2, 3, 'a', 'b']
12
+
13
+ # Operations
14
+ my_list.append(4) # Add to end: [1, 2, 3, 'a', 'b', 4]
15
+ my_list.insert(0, 0) # Insert at index: [0, 1, 2, 3, 'a', 'b', 4]
16
+ my_list.extend([5, 6]) # Append multiple: [0, 1, 2, 3, 'a', 'b', 4, 5, 6]
17
+ my_list.pop() # Remove & return last item (6)
18
+ my_list.pop(1) # Remove & return item at index 1 (1)
19
+ my_list.remove('a') # Remove first occurrence of 'a'
20
+ my_list.reverse() # Reverse in place
21
+ # my_list.sort() # Sort in place (requires same types)
22
+ # sorted(my_list) # Return new sorted list
23
+ my_list.clear() # Empty the list
24
+ count = my_list.count(2) # Count occurrences
25
+ idx = my_list.index(3) # Find index of first occurrence
26
+
27
+ # Slicing: list[start:stop:step]
28
+ # my_list[1:4] (index 1 to 3), my_list[::-1] (reverse)
29
+
30
+ # =============================================================================
31
+ # 2. SETS (Mutable, Unordered, Unique Elements)
32
+ # =============================================================================
33
+ my_set = {1, 2, 3}
34
+ empty_set = set() # Note: {} creates an empty dict, not a set
35
+
36
+ # Operations
37
+ my_set.add(4) # Add element
38
+ my_set.update([5, 6]) # Add multiple elements
39
+ my_set.remove(6) # Remove element (raises KeyError if not found)
40
+ my_set.discard(10) # Remove element (safe, no error if not found)
41
+ my_set.pop() # Remove & return arbitrary element
42
+ my_set.clear() # Empty the set
43
+
44
+ set_a, set_b = {1, 2}, {2, 3}
45
+ union = set_a | set_b # {1, 2, 3} (or set_a.union(set_b))
46
+ intersection = set_a & set_b # {2} (or set_a.intersection(set_b))
47
+ diff = set_a - set_b # {1} (or set_a.difference(set_b))
48
+ sym_diff = set_a ^ set_b # {1, 3} (or set_a.symmetric_difference(set_b))
49
+
50
+ # =============================================================================
51
+ # 3. TUPLES (Immutable, Ordered)
52
+ # =============================================================================
53
+ my_tuple = (1, 2, 3, 2)
54
+ single_tuple = (1,) # Comma needed for single-element tuple
55
+
56
+ # Operations (Very limited since immutable)
57
+ count = my_tuple.count(2) # Count occurrences (2)
58
+ idx = my_tuple.index(3) # Find index of first occurrence (2)
59
+ # Tuples support unpacking: a, b, c, d = my_tuple
60
+
61
+ # =============================================================================
62
+ # 4. DICTIONARIES (Mutable, Key-Value Pairs, Unordered before Python 3.7)
63
+ # =============================================================================
64
+ my_dict = {'name': 'Alice', 'age': 25}
65
+
66
+ # Operations
67
+ my_dict['city'] = 'NYC' # Add or update key
68
+ val = my_dict.get('age') # Safe get (returns None if not found, instead of KeyError)
69
+ val = my_dict.get('x', 0) # Safe get with default value
70
+ keys = my_dict.keys() # dict_keys(['name', 'age', 'city'])
71
+ values = my_dict.values() # dict_values(['Alice', 25, 'NYC'])
72
+ items = my_dict.items() # dict_items([('name', 'Alice'), ...])
73
+
74
+ # Removal
75
+ popped_val = my_dict.pop('age') # Remove key 'age' and return value
76
+ popped_item = my_dict.popitem() # Remove & return last key-value pair as tuple
77
+ # del my_dict['name'] # Delete key
78
+ my_dict.clear() # Empty dict
79
+ my_dict.update({'a': 1, 'b': 2}) # Merge / Update with another dict
80
+
81
+ # =============================================================================
82
+ # 5. LIST / DICT / SET COMPREHENSIONS
83
+ # =============================================================================
84
+ # List Comprehension: [expression for item in iterable if condition]
85
+ squares = [x**2 for x in range(10) if x % 2 == 0] # [0, 4, 16, 36, 64]
86
+
87
+ # Dict Comprehension: {key_expr: val_expr for item in iterable if condition}
88
+ sq_dict = {x: x**2 for x in range(5)} # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
89
+
90
+ # Set Comprehension: {expression for item in iterable if condition}
91
+ sq_set = {x**2 for x in [-1, 1, 2]} # {1, 4}
92
+
93
+ # Generator Expression: (expression for item in iterable if condition)
94
+ gen = (x**2 for x in range(10)) # Lazy evaluation
95
+
96
+ # =============================================================================
97
+ # 6. LAMBDA FUNCTIONS, MAP, FILTER, REDUCE
98
+ # =============================================================================
99
+ # lambda arguments: expression
100
+ add = lambda x, y: x + y
101
+ print(add(2, 3)) # 5
102
+
103
+ nums = [1, 2, 3, 4]
104
+ # map: apply function to all items
105
+ mapped = list(map(lambda x: x*2, nums)) # [2, 4, 6, 8]
106
+
107
+ # filter: keep items where function returns True
108
+ filtered = list(filter(lambda x: x % 2 == 0, nums)) # [2, 4]
109
+
110
+ # reduce (requires functools): cumulative application
111
+ from functools import reduce
112
+ product = reduce(lambda x, y: x * y, nums) # 24
113
+
114
+ # Sort with lambda key
115
+ words = ["apple", "banana", "cherry"]
116
+ words.sort(key=lambda w: len(w)) # Sort by length
117
+
118
+ # =============================================================================
119
+ # 7. CLASSES AND OBJECTS (OOP)
120
+ # =============================================================================
121
+ class Animal:
122
+ """Base class for animals."""
123
+ species_count = 0 # Class attribute
124
+
125
+ def __init__(self, name):
126
+ self.name = name # Instance attribute
127
+ Animal.species_count += 1
128
+
129
+ def speak(self):
130
+ """Instance method"""
131
+ return "Some sound"
132
+
133
+ @classmethod
134
+ def get_count(cls):
135
+ """Class method: takes class as first arg"""
136
+ return cls.species_count
137
+
138
+ @staticmethod
139
+ def is_alive():
140
+ """Static method: no implicit self or cls args"""
141
+ return True
142
+
143
+ # Inheritance
144
+ class Dog(Animal):
145
+ def __init__(self, name, breed):
146
+ super().__init__(name) # Call parent constructor
147
+ self.breed = breed
148
+
149
+ def speak(self): # Method Overriding
150
+ return "Woof!"
151
+
152
+ dog = Dog("Buddy", "Golden Retriever")
153
+ print(dog.speak()) # "Woof!"
154
+ print(Animal.get_count()) # 1
155
+
156
+ # =============================================================================
157
+ # 8. FILE HANDLING
158
+ # =============================================================================
159
+ # Using 'with' is a best practice, as it automatically closes the file
160
+ # Modes: 'r' (read), 'w' (write, truncates), 'a' (append), 'r+' (read & write), 'b' (binary)
161
+
162
+ # Write to file
163
+ with open("example.txt", "w", encoding="utf-8") as file:
164
+ file.write("Hello World\nLine 2")
165
+
166
+ # Read from file
167
+ with open("example.txt", "r", encoding="utf-8") as file:
168
+ content = file.read() # Read entire file
169
+ # file.seek(0) # Reset cursor to start
170
+ # lines = file.readlines() # Read lines into a list
171
+ # for line in file: # Iterate line by line (memory efficient)
172
+ # print(line.strip())
173
+
174
+ # Note: file is automatically closed outside the 'with' block.