kalachacha 0.1.0__py3-none-any.whl

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.
kalachacha/__init__.py ADDED
@@ -0,0 +1,59 @@
1
+ """kalachacha - call exp1() or exp2() to get the full code of each experiment.
2
+
3
+ from kalachacha import exp1, exp2
4
+ exp1() # prints the full code
5
+ exp1(run=True) # runs it (needs `pip install ollama` and a running Ollama)
6
+ code = exp1(show=False) # returns the code as a string without printing
7
+ """
8
+ from importlib import resources
9
+
10
+ __version__ = "0.1.0"
11
+ __all__ = ["exp1", "exp2"]
12
+
13
+
14
+ def _source(name):
15
+ return resources.files(__name__).joinpath(f"_{name}.py").read_text(encoding="utf-8")
16
+
17
+
18
+ def _exp(name, run, show):
19
+ code = _source(name)
20
+ if run:
21
+ try:
22
+ import ollama # noqa: F401
23
+ except ImportError:
24
+ raise SystemExit("Running needs ollama: pip install ollama") from None
25
+ exec(compile(code, f"{name}.py", "exec"), {"__name__": "__main__"})
26
+ elif show:
27
+ print(code)
28
+ return None if (show or run) else code
29
+
30
+
31
+ def exp1(run=False, show=True):
32
+ """Experiment 1: generate sum_even_numbers with Ollama and test it."""
33
+ return _exp("exp1", run, show)
34
+
35
+
36
+ def exp2(run=False, show=True):
37
+ """Experiment 2: selection sort code generation + testing pipeline."""
38
+ return _exp("exp2", run, show)
39
+
40
+
41
+ def _cli(default=None):
42
+ import sys
43
+ args = sys.argv[1:]
44
+ run = "--run" in args
45
+ names = [a for a in args if a in ("exp1", "exp2")]
46
+ name = default or (names[0] if names else None)
47
+ if name is None:
48
+ print("usage: kalachacha exp1|exp2 [--run]")
49
+ return 1
50
+ _exp(name, run, True)
51
+ return 0
52
+
53
+
54
+ def _cli_exp1():
55
+ return _cli("exp1")
56
+
57
+
58
+ def _cli_exp2():
59
+ return _cli("exp2")
kalachacha/_exp1.py ADDED
@@ -0,0 +1,54 @@
1
+ import ollama
2
+
3
+
4
+ def generate_function():
5
+ prompt = """
6
+ Write a Python function named sum_even_numbers that takes an array
7
+ and returns the sum of all even numbers in it.
8
+ Return 0 if there are no even numbers.
9
+ Return only the function.
10
+ """
11
+ response = ollama.generate(model="qwen2.5-coder:7b", prompt=prompt)
12
+ return response["response"].replace("```python", "").replace("```", "").strip()
13
+
14
+
15
+ def test_function(code):
16
+ try:
17
+ namespace = {}
18
+ exec(code, namespace)
19
+ sum_even_numbers = namespace["sum_even_numbers"]
20
+
21
+ tests = [([1, 2, 3, 4, 5, 6], 12),
22
+ ([2, 4, 6], 12),
23
+ ([1, 3, 5], 0),
24
+ ([10, 15, 20], 30),
25
+ ([], 0)]
26
+
27
+ print("\nTesting Generated Function")
28
+ print("-" * 40)
29
+
30
+ passed = 0
31
+ for arr, expected in tests:
32
+ result = sum_even_numbers(arr)
33
+ print(f"{arr} = {result} (Expected: {expected})")
34
+ print("PASS" if result == expected else "FAIL")
35
+ passed += result == expected
36
+
37
+ print("-" * 40)
38
+ print(f"Test Cases Passed: {passed}/{len(tests)}")
39
+ print("Overall Result:", "CORRECT" if passed == len(tests) else "INCORRECT")
40
+
41
+ except Exception as e:
42
+ print("Error:", e)
43
+
44
+
45
+ print("Ollama Python Function Generator")
46
+ print("=" * 40)
47
+
48
+ code = generate_function()
49
+ print("\nGenerated Python Function:")
50
+ print("-" * 40)
51
+ print(code)
52
+ print("-" * 40)
53
+
54
+ test_function(code)
kalachacha/_exp2.py ADDED
@@ -0,0 +1,145 @@
1
+ import ollama
2
+ import ast
3
+ import re
4
+
5
+
6
+ class CodeGenerationTesterPipeline:
7
+
8
+ def __init__(self):
9
+
10
+ self.real_code_dataset = [
11
+ """def selection_sort(arr):
12
+ for i in range(len(arr)):
13
+ m = i
14
+ for j in range(i+1, len(arr)):
15
+ if arr[j] < arr[m]:
16
+ m = j
17
+ arr[i], arr[m] = arr[m], arr[i]
18
+ return arr""",
19
+
20
+ """def selection_sort(arr):
21
+ n = len(arr)
22
+ for i in range(n):
23
+ min_idx = i
24
+ for j in range(i+1, n):
25
+ if arr[min_idx] > arr[j]:
26
+ min_idx = j
27
+ arr[i], arr[min_idx] = arr[min_idx], arr[i]
28
+ return arr"""
29
+ ]
30
+
31
+ self.prompts = """
32
+ Generate a Python program to implement selection sort
33
+ with function name selection_sort().
34
+
35
+ Requirements:
36
+ 1. Implement selection sort manually.
37
+ 2. Do not use sorted() or .sort().
38
+ 3. Sort the array in ascending order.
39
+ 4. Return the sorted array.
40
+ 5. Do not include comments or explanations.
41
+ 6. Generate only the function.
42
+ """
43
+
44
+ self.model = "qwen2.5-coder:7b"
45
+
46
+ def generate(self):
47
+
48
+ c = ollama.generate(
49
+ model=self.model,
50
+ prompt=self.prompts
51
+ )["response"]
52
+
53
+ c = c.strip().replace("```python", "").replace("```", "")
54
+
55
+ print("\nGenerated Code:\n")
56
+ print(c)
57
+
58
+ with open("newcodes.txt", "w") as f:
59
+ f.write(c)
60
+
61
+ self.codes = c
62
+
63
+ print("\nCode saved to newcodes.txt")
64
+
65
+ def testcase_testing(self):
66
+
67
+ print("\nTest Case Testing")
68
+
69
+ ns = {}
70
+ exec(self.codes, ns)
71
+
72
+ testcases = [
73
+ ([5, 2, 4, 3, 1], [1, 2, 3, 4, 5]),
74
+ ([3, 4, 2, 1, 2], [1, 2, 2, 3, 4]),
75
+ ([78, 43, 67, 34, 21], [21, 34, 43, 67, 78])
76
+ ]
77
+
78
+ count = 0
79
+
80
+ for i, (arr, actual) in enumerate(testcases, 1):
81
+
82
+ expected = ns["selection_sort"](arr)
83
+
84
+ if expected == actual:
85
+ count += 1
86
+ print(f"Test case {i}: PASS")
87
+ else:
88
+ print(f"Test case {i}: FAIL")
89
+
90
+ print("Passed:", count)
91
+ print("Failed:", len(testcases) - count)
92
+
93
+ def checksyntax(self):
94
+
95
+ print("\nSyntax Checking")
96
+
97
+ try:
98
+ ast.parse(self.codes)
99
+ print("Syntax is correct")
100
+
101
+ except SyntaxError:
102
+ print("Syntax error found")
103
+
104
+ def checkalgo(self):
105
+
106
+ print("\nAlgorithm Checking")
107
+
108
+ if (
109
+ "for" in self.codes
110
+ and "if" in self.codes
111
+ and ".sort(" not in self.codes
112
+ and "sorted(" not in self.codes
113
+ ):
114
+ print("Algorithm check passed")
115
+
116
+ else:
117
+ print("Algorithm check failed")
118
+
119
+ def similarity(self):
120
+
121
+ print("\nSimilarity Checking")
122
+
123
+ words1 = set(re.findall(r"[a-z_]+", self.codes))
124
+
125
+ scores = []
126
+
127
+ for code in self.real_code_dataset:
128
+
129
+ words2 = set(re.findall(r"[a-z_]+", code))
130
+
131
+ common = len(words1 & words2)
132
+ total = len(words1 | words2)
133
+
134
+ scores.append((common / total) * 100)
135
+
136
+ print("Similarity Score:", round(max(scores), 2), "%")
137
+
138
+
139
+ obj = CodeGenerationTesterPipeline()
140
+
141
+ obj.generate()
142
+ obj.testcase_testing()
143
+ obj.checksyntax()
144
+ obj.checkalgo()
145
+ obj.similarity()
@@ -0,0 +1,21 @@
1
+ Metadata-Version: 2.4
2
+ Name: kalachacha
3
+ Version: 0.1.0
4
+ Summary: exp1() and exp2(): Ollama code-generation experiments
5
+ Requires-Python: >=3.9
6
+ Description-Content-Type: text/markdown
7
+ Provides-Extra: run
8
+ Requires-Dist: ollama; extra == "run"
9
+
10
+ # kalachacha
11
+
12
+ pip install kalachacha-0.1.0-py3-none-any.whl
13
+
14
+ ```python
15
+ from kalachacha import exp1, exp2
16
+ exp1() # prints the full code of experiment 1
17
+ exp2() # prints the full code of experiment 2
18
+ exp1(run=True) # runs it (needs: pip install ollama, and Ollama running with qwen2.5-coder:7b)
19
+ ```
20
+
21
+ Terminal: `exp1`, `exp2`, or `exp1 --run`.
@@ -0,0 +1,8 @@
1
+ kalachacha/__init__.py,sha256=yiTcW6294L7fsQI7mj4g42egixl_4XMu7JqkE-sqxLs,1605
2
+ kalachacha/_exp1.py,sha256=Y9axboLxk0jnqOvlzS0t7STvM1tZiTAv005roOqbWEk,1485
3
+ kalachacha/_exp2.py,sha256=1lBTkdXtADoXuRKnh-i_GSLi9afLnaqOZ0M_oA6x2-4,3270
4
+ kalachacha-0.1.0.dist-info/METADATA,sha256=2wMZseOwDF8ax2DsrkA7LVXzcfb4VyUa6H00vbmVaCs,605
5
+ kalachacha-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
6
+ kalachacha-0.1.0.dist-info/entry_points.txt,sha256=Mr47axxJnf5sIx8YcRQu1CqKiPLVv6mvGiQd9MsL9QI,103
7
+ kalachacha-0.1.0.dist-info/top_level.txt,sha256=uLjes8rrW0d8icKoyn8FWvBoZER1vc8pIRtw3LFCcG4,11
8
+ kalachacha-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,4 @@
1
+ [console_scripts]
2
+ exp1 = kalachacha:_cli_exp1
3
+ exp2 = kalachacha:_cli_exp2
4
+ kalachacha = kalachacha:_cli
@@ -0,0 +1 @@
1
+ kalachacha