pam-python 0.1.30__tar.gz → 0.1.32__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 (39) hide show
  1. {pam_python-0.1.30 → pam_python-0.1.32}/PKG-INFO +1 -1
  2. {pam_python-0.1.30 → pam_python-0.1.32}/pam/cli.py +2 -0
  3. pam_python-0.1.32/pam/templates/init/AGENT.md +197 -0
  4. {pam_python-0.1.30 → pam_python-0.1.32}/pam_python.egg-info/PKG-INFO +1 -1
  5. {pam_python-0.1.30 → pam_python-0.1.32}/pam_python.egg-info/SOURCES.txt +1 -0
  6. {pam_python-0.1.30 → pam_python-0.1.32}/setup.py +1 -1
  7. {pam_python-0.1.30 → pam_python-0.1.32}/LICENSE.txt +0 -0
  8. {pam_python-0.1.30 → pam_python-0.1.32}/README.md +0 -0
  9. {pam_python-0.1.30 → pam_python-0.1.32}/pam/__init__.py +0 -0
  10. {pam_python-0.1.30 → pam_python-0.1.32}/pam/api.py +0 -0
  11. {pam_python-0.1.30 → pam_python-0.1.32}/pam/interface_task_manager.py +0 -0
  12. {pam_python-0.1.30 → pam_python-0.1.32}/pam/models/__init__.py +0 -0
  13. {pam_python-0.1.30 → pam_python-0.1.32}/pam/models/request_command.py +0 -0
  14. {pam_python-0.1.30 → pam_python-0.1.32}/pam/server.py +0 -0
  15. {pam_python-0.1.30 → pam_python-0.1.32}/pam/service.py +0 -0
  16. {pam_python-0.1.30 → pam_python-0.1.32}/pam/sqlite.py +0 -0
  17. {pam_python-0.1.30 → pam_python-0.1.32}/pam/task_manager.py +0 -0
  18. {pam_python-0.1.30 → pam_python-0.1.32}/pam/temp_file_utils.py +0 -0
  19. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/buildcmd/pamb +0 -0
  20. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/buildcmd/pamb-base.sh +0 -0
  21. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/docker/Dockerfile +0 -0
  22. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/init/dockerignore.tmpl +0 -0
  23. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/init/gitignore.tmpl +0 -0
  24. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/init/main.tmpl +0 -0
  25. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/init/pylintrc.tmpl +0 -0
  26. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/init/run_unit_test.bat +0 -0
  27. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/init/run_unit_test.ps1 +0 -0
  28. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/init/run_unit_test.sh +0 -0
  29. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/service/functions.tmpl +0 -0
  30. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/service/service.test.tmpl +0 -0
  31. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/service/service.yaml +0 -0
  32. {pam_python-0.1.30 → pam_python-0.1.32}/pam/templates/service/service_class.tmpl +0 -0
  33. {pam_python-0.1.30 → pam_python-0.1.32}/pam/tester_task.py +0 -0
  34. {pam_python-0.1.30 → pam_python-0.1.32}/pam/utils.py +0 -0
  35. {pam_python-0.1.30 → pam_python-0.1.32}/pam_python.egg-info/dependency_links.txt +0 -0
  36. {pam_python-0.1.30 → pam_python-0.1.32}/pam_python.egg-info/entry_points.txt +0 -0
  37. {pam_python-0.1.30 → pam_python-0.1.32}/pam_python.egg-info/requires.txt +0 -0
  38. {pam_python-0.1.30 → pam_python-0.1.32}/pam_python.egg-info/top_level.txt +0 -0
  39. {pam_python-0.1.30 → pam_python-0.1.32}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pam-python
3
- Version: 0.1.30
3
+ Version: 0.1.32
4
4
  Summary: Pam Python Library
5
5
  Home-page: https://github.com/heart/pam-python
6
6
  Author: Narongrit Kanhanoi
@@ -74,6 +74,7 @@ def create_service(name):
74
74
  cpy("service/service.yaml", os.path.join(name, "service.yaml"))
75
75
  cpy("service/functions.tmpl", os.path.join(name, "functions.py"))
76
76
  cpy("service/service.test.tmpl", os.path.join(name, f"test_{name}.py"))
77
+
77
78
 
78
79
  replace_template_content(name, to_pascal_case(
79
80
  name)+"Svc", to_pascal_case(name)+"Svc.py")
@@ -96,6 +97,7 @@ def init_project():
96
97
  cpy("init/run_unit_test.sh", "run_unit_test.sh")
97
98
  cpy("init/run_unit_test.bat", "run_unit_test.bat")
98
99
  cpy("init/run_unit_test.ps1", "run_unit_test.ps1")
100
+ cpy("init/AGENT.md", "AGENT.md")
99
101
 
100
102
  if not os.path.exists("requirements.txt"):
101
103
  open("requirements.txt", 'a', encoding='utf-8').close()
@@ -0,0 +1,197 @@
1
+ # AGENT
2
+
3
+ This document explains the data plugin service lifecycle so developers can create new services and understand runtime behavior inside the CDP environment.
4
+
5
+ ## Project Overview
6
+
7
+ 1. This project is a data plugin that will be connected to the main system in production.
8
+ 2. The data plugin processes data from CDP and is created as separate services by domain. For example, if we process RFM, we create a new service dedicated to that domain.
9
+
10
+ ## Create a New Service
11
+
12
+ 1. Create a service with:
13
+ `pam new service [service name]`
14
+ 2. The system creates a new folder named `[service name]` with sample code and basic templates.
15
+ 3. Example:
16
+ `pam new service rfm_segment`
17
+ This generates `rfm_segment/Rfm_segmentSvc.py`, which is the starting point of the service.
18
+ 4. `rfm_segment/service.yaml` registers the service automatically.
19
+ The value `endpoint: /Rfm_segmentSvc` means CDP will call:
20
+ `http://ip-of-this-k8s-pod/services/Rfm_segmentSvc`
21
+
22
+ ### Code Organization Guidelines
23
+
24
+ - Separate each lifecycle step into clear, dedicated functions. Add comments at critical points so humans can review and audit later.
25
+ - Avoid nested functions unless absolutely necessary.
26
+ - Put service logic in `[service_name]/functions.py`, then import and use it from the service class to keep the overall code easy to read.
27
+
28
+ ## Service Lifecycle
29
+
30
+ CDP spawns a service instance when a job starts and terminates it after the job completes. This design supports parallel execution. For example, two runs will spawn two separate instances. The only risk is shared local files (e.g., sqlite) which can cause conflicts.
31
+
32
+ The lifecycle is wired in `rfm_segment/Rfm_segmentSvc.py` (example service name).
33
+
34
+ ### on_start
35
+
36
+ `on_start` runs at startup. Use it to read runtime parameters from CDP and initialize work.
37
+
38
+ Example:
39
+
40
+ ```python
41
+ def on_start(self):
42
+ log("on_start")
43
+
44
+ some_param = self.request.runtime_parameters.get("some_param", "")
45
+ self._request_data()
46
+ ```
47
+
48
+ `runtime_parameters` are configured by CDP to control service behavior without code changes (e.g., `period_day="7"`).
49
+
50
+ Notes
51
+
52
+ - All values are strings. Cast with defaults to avoid errors.
53
+ - `dry_run` is the only built-in parameter. If missing, it runs for real. If `dry_run="true"`, the service runs fully but does not upload results to CDP.
54
+ - You do not need to check `dry_run` manually. Always call `_upload_result`; the system handles it.
55
+
56
+ **Important:** `on_start` must return quickly because CDP calls via HTTP. If it hangs, the client may time out and the job will be terminated. If work is long-running, move it to a thread and return from `on_start` first. This applies to all lifecycle functions.
57
+
58
+ ### Request Data from CDP
59
+
60
+ Call `self._request_data()` to ask CDP for input data. CDP returns up to 5 CSV files (each file is a separate event).
61
+
62
+ - File order is guaranteed per agreement, so you can index by position.
63
+ - Column names and record counts are defined by CDP configuration.
64
+
65
+ CSV example:
66
+
67
+ ```csv
68
+ _id,id,data1,data2,data3
69
+ xx,yy,zz,nn,mm
70
+ ```
71
+
72
+ Column meaning
73
+
74
+ - `_id` = event id for dedup/processing checks. Often unused and can be dropped.
75
+ - `id` = customer id used by CDP as a key.
76
+ - Other columns depend on the agreement.
77
+
78
+ Date format is always:
79
+ `"02/08/2026, 18:23:54"`
80
+ Timezone may vary by customer data (usually Bangkok time).
81
+ You may sometimes be asked for the exact time zone if it is logically important for the program.
82
+
83
+ ### on_data_input
84
+
85
+ When CDP finishes collecting data, it calls `on_data_input`.
86
+
87
+ Example:
88
+
89
+ ```python
90
+ def on_data_input(self, req: RequestCommand):
91
+ log(f"on_data_input req.is_end = {req.is_end}")
92
+
93
+ # RUN IN A SEPARATED THREAD TO PREVENT HTTP REQUEST BLOCKING
94
+ thread = threading.Thread(
95
+ target=lambda: self.__run_process_data_in_thread(req),
96
+ daemon=True,
97
+ )
98
+ thread.start()
99
+
100
+
101
+ def __run_process_data_in_thread(self, req: RequestCommand):
102
+ dataframe = self.__process_data(req.input_files)
103
+ self._upload_result(dataframe)
104
+
105
+ if not req.is_end:
106
+ self._request_data(req.next)
107
+ else:
108
+ self._exit()
109
+ ```
110
+
111
+ Why a thread: to end the HTTP request quickly and avoid timeouts.
112
+
113
+ `req.input_files` is the ordered list of CSV paths.
114
+
115
+ ### Upload Results to CDP
116
+
117
+ After processing, call `_upload_result` with a dataframe. Example output:
118
+
119
+ ```csv
120
+ id,data_x,rfm
121
+ xxx,20,hero
122
+ ```
123
+
124
+ - `id` is required for CDP matching.
125
+ - Other columns are defined by agreement.
126
+
127
+ Sometimes you may include an `event` column to create per-user events:
128
+
129
+ ```csv
130
+ id,data_x,event
131
+ xxx,20,purchase
132
+ yyy,50,refund
133
+ ```
134
+
135
+ If `event` is missing, CDP uses the default event configured in the system.
136
+
137
+ ### Pagination
138
+
139
+ In the example:
140
+
141
+ ```python
142
+ if not req.is_end:
143
+ self._request_data(req.next)
144
+ else:
145
+ self._exit()
146
+ ```
147
+
148
+ - `req.is_end` indicates whether this is the last page.
149
+ - If not end, call `self._request_data(req.next)` for the next page.
150
+ - If end, call `self._exit()` to terminate the instance.
151
+
152
+ CDP will not send the next page until you call `_request_data`, so the flow is serialized and no two threads will handle the same data unless you explicitly design it that way.
153
+
154
+ ### on_terminate
155
+
156
+ Before termination, the system calls:
157
+
158
+ ```python
159
+ def on_terminate(self):
160
+ log("on_terminate")
161
+ ```
162
+
163
+ Use it for cleanup.
164
+
165
+ ## Persist State Between Runs (sqlite/duckdb)
166
+
167
+ Services can be killed and restarted at any time. If you need state across runs, use sqlite or duckdb via the provided API.
168
+
169
+ Example (sqlite):
170
+
171
+ ```python
172
+ SQLITE_FILE_NAME = "mydata.sqlite"
173
+
174
+ sqlite_file = self._request_sqlite(file_name=SQLITE_FILE_NAME, is_shared=False)
175
+ if sqlite_file is not None:
176
+ pass
177
+
178
+ self._upload_sqlite(
179
+ file_name=SQLITE_FILE_NAME,
180
+ is_shared=False,
181
+ sqlite_file="/local/path/to/upload/sqlite.sqlite",
182
+ )
183
+ ```
184
+
185
+ Notes
186
+
187
+ - `_request_sqlite` downloads from the server and is automatically scoped to this plugin. It will not mix with other plugins even if the name matches.
188
+ - The call is synchronous and returns the local path.
189
+ - If the return value is `None`, no file exists on the server; you can create a new one.
190
+ - `_upload_sqlite` always overwrites the previous file.
191
+
192
+ `is_shared` behavior
193
+
194
+ - `False` (recommended): isolate per service. Multiple instances get their own copies.
195
+ - `True`: shared storage. If another plugin updates the file, this plugin can download the latest version.
196
+
197
+ Note: `file_name` acts like a server key, while `sqlite_file` is the local file path.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pam-python
3
- Version: 0.1.30
3
+ Version: 0.1.32
4
4
  Summary: Pam Python Library
5
5
  Home-page: https://github.com/heart/pam-python
6
6
  Author: Narongrit Kanhanoi
@@ -17,6 +17,7 @@ pam/models/request_command.py
17
17
  pam/templates/buildcmd/pamb
18
18
  pam/templates/buildcmd/pamb-base.sh
19
19
  pam/templates/docker/Dockerfile
20
+ pam/templates/init/AGENT.md
20
21
  pam/templates/init/dockerignore.tmpl
21
22
  pam/templates/init/gitignore.tmpl
22
23
  pam/templates/init/main.tmpl
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="pam-python",
5
- version="0.1.30",
5
+ version="0.1.32",
6
6
  author="Narongrit Kanhanoi",
7
7
  author_email="narongrit@pams.ai",
8
8
  description="Pam Python Library",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes