staran 1.0.8__py3-none-any.whl → 1.0.9__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.
@@ -0,0 +1,302 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+
4
+ """
5
+ Staran v1.0.9 新功能演示
6
+ =======================
7
+
8
+ 演示v1.0.9版本的新增功能和性能优化。
9
+ """
10
+
11
+ import sys
12
+ import os
13
+ import asyncio
14
+ import tempfile
15
+ import time
16
+
17
+ # 添加项目根目录到路径
18
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..'))
19
+
20
+ from staran.date import Date
21
+ from staran.date.core import DateRange, SmartDateInference
22
+
23
+
24
+ def demo_smart_inference():
25
+ """演示智能日期推断功能"""
26
+ print("🧠 智能日期推断演示")
27
+ print("=" * 50)
28
+
29
+ # 1. 智能解析
30
+ print("1. 智能解析功能")
31
+ test_inputs = ['15', '3-15', '下月15', '明天']
32
+
33
+ for input_str in test_inputs:
34
+ try:
35
+ result = Date.smart_parse(input_str)
36
+ print(f" '{input_str}' → {result.format_iso()}")
37
+ except Exception as e:
38
+ print(f" '{input_str}' → 解析失败: {e}")
39
+
40
+ # 2. 部分日期推断
41
+ print("\n2. 部分日期推断")
42
+ reference = Date('20250415')
43
+
44
+ # 只提供月日
45
+ inferred1 = Date.infer_date(month=6, day=20, reference_date=reference)
46
+ print(f" 推断6月20日 → {inferred1.format_iso()}")
47
+
48
+ # 只提供日期
49
+ inferred2 = Date.infer_date(day=25, reference_date=reference)
50
+ print(f" 推断25号 → {inferred2.format_iso()}")
51
+
52
+ # 只提供月份
53
+ inferred3 = Date.infer_date(month=8, reference_date=reference)
54
+ print(f" 推断8月 → {inferred3.format_iso()}")
55
+
56
+
57
+ async def demo_async_processing():
58
+ """演示异步批量处理功能"""
59
+ print("\n\n⚡ 异步批量处理演示")
60
+ print("=" * 50)
61
+
62
+ # 1. 异步批量创建
63
+ print("1. 异步批量创建")
64
+ date_strings = ['20250101', '20250102', '20250103', '20250104', '20250105']
65
+
66
+ start_time = time.time()
67
+ dates = await Date.async_batch_create(date_strings)
68
+ async_time = time.time() - start_time
69
+
70
+ print(f" 异步创建5个日期对象: {async_time:.4f}秒")
71
+ print(f" 首个日期: {dates[0].format_iso()}")
72
+ print(f" 最后日期: {dates[-1].format_iso()}")
73
+
74
+ # 2. 异步批量格式化
75
+ print("\n2. 异步批量格式化")
76
+ start_time = time.time()
77
+ formatted = await Date.async_batch_format(dates, 'chinese')
78
+ format_time = time.time() - start_time
79
+
80
+ print(f" 异步格式化5个日期: {format_time:.4f}秒")
81
+ print(f" 格式化结果: {', '.join(formatted[:3])}...")
82
+
83
+ # 3. 异步批量处理
84
+ print("\n3. 异步批量处理")
85
+ start_time = time.time()
86
+ processed = await Date.async_batch_process(dates, 'add_days', days=10)
87
+ process_time = time.time() - start_time
88
+
89
+ print(f" 异步添加10天: {process_time:.4f}秒")
90
+ print(f" 处理结果: {processed[0].format_iso()} → {processed[-1].format_iso()}")
91
+
92
+
93
+ def demo_date_ranges():
94
+ """演示日期范围操作"""
95
+ print("\n\n📅 日期范围操作演示")
96
+ print("=" * 50)
97
+
98
+ # 1. 创建日期范围
99
+ print("1. 创建日期范围")
100
+ range1 = Date.create_range('20250101', '20250131')
101
+ print(f" 1月范围: {range1.start.format_iso()} ~ {range1.end.format_iso()}")
102
+ print(f" 天数: {range1.days_count()}天")
103
+
104
+ # 2. 范围检查
105
+ print("\n2. 范围检查")
106
+ test_date = Date('20250115')
107
+ print(f" {test_date.format_iso()} 在1月范围内: {range1.contains(test_date)}")
108
+ print(f" {test_date.format_iso()} 在(1日-31日)范围内: {test_date.in_range(Date('20250101'), Date('20250131'))}")
109
+
110
+ # 3. 范围交集和并集
111
+ print("\n3. 范围交集和并集")
112
+ range2 = DateRange(Date('20250115'), Date('20250215'))
113
+ print(f" 范围2: {range2.start.format_iso()} ~ {range2.end.format_iso()}")
114
+
115
+ intersection = range1.intersect(range2)
116
+ if intersection:
117
+ print(f" 交集: {intersection.start.format_iso()} ~ {intersection.end.format_iso()}")
118
+
119
+ union = range1.union(range2)
120
+ print(f" 并集: {union.start.format_iso()} ~ {union.end.format_iso()}")
121
+
122
+ # 4. 生成日期序列
123
+ print("\n4. 生成日期序列")
124
+ dates = Date.generate_range('20250101', 7, step=1, include_weekends=False)
125
+ print(f" 工作日序列(7天): {', '.join([d.format_iso() for d in dates[:5]])}...")
126
+
127
+ # 5. 合并重叠范围
128
+ print("\n5. 合并重叠范围")
129
+ ranges = [
130
+ DateRange(Date('20250101'), Date('20250105')),
131
+ DateRange(Date('20250103'), Date('20250110')),
132
+ DateRange(Date('20250115'), Date('20250120'))
133
+ ]
134
+
135
+ merged = Date.merge_date_ranges(ranges)
136
+ print(f" 原始范围数: {len(ranges)}")
137
+ print(f" 合并后范围数: {len(merged)}")
138
+ for i, r in enumerate(merged):
139
+ print(f" 范围{i+1}: {r.start.format_iso()} ~ {r.end.format_iso()}")
140
+
141
+
142
+ def demo_data_import_export():
143
+ """演示数据导入导出功能"""
144
+ print("\n\n💾 数据导入导出演示")
145
+ print("=" * 50)
146
+
147
+ # 准备测试数据
148
+ dates = [Date('20250101'), Date('20250215'), Date('20250320'), Date('20250425')]
149
+
150
+ # 创建临时目录
151
+ temp_dir = tempfile.mkdtemp()
152
+ csv_file = os.path.join(temp_dir, 'dates.csv')
153
+ json_file = os.path.join(temp_dir, 'dates.json')
154
+
155
+ try:
156
+ # 1. CSV导出导入
157
+ print("1. CSV导出导入")
158
+ Date.to_csv(dates, csv_file, include_metadata=True)
159
+ print(f" 导出到CSV: {csv_file}")
160
+
161
+ imported_csv = Date.from_csv(csv_file, 'date')
162
+ print(f" 从CSV导入: {len(imported_csv)}个日期")
163
+ print(f" 首个日期: {imported_csv[0].format_iso()}")
164
+
165
+ # 2. JSON导出导入
166
+ print("\n2. JSON导出导入")
167
+ Date.to_json_file(dates, json_file, include_metadata=True)
168
+ print(f" 导出到JSON: {json_file}")
169
+
170
+ imported_json = Date.from_json_file(json_file)
171
+ print(f" 从JSON导入: {len(imported_json)}个日期")
172
+ print(f" 最后日期: {imported_json[-1].format_iso()}")
173
+
174
+ finally:
175
+ # 清理临时文件
176
+ import shutil
177
+ shutil.rmtree(temp_dir)
178
+
179
+
180
+ def demo_performance_optimizations():
181
+ """演示性能优化功能"""
182
+ print("\n\n🚀 性能优化演示")
183
+ print("=" * 50)
184
+
185
+ # 1. 缓存操作
186
+ print("1. 缓存管理")
187
+ Date.clear_cache()
188
+ print(f" 清空缓存完成")
189
+
190
+ # 创建一些日期对象触发缓存
191
+ test_dates = [Date('20250415') for _ in range(10)]
192
+ stats = Date.get_cache_stats()
193
+ print(f" 缓存统计: {stats}")
194
+
195
+ # 2. 优化格式化
196
+ print("\n2. 优化格式化")
197
+ date = Date('20250415')
198
+
199
+ start_time = time.time()
200
+ for _ in range(1000):
201
+ result = date._optimized_format('iso')
202
+ optimized_time = time.time() - start_time
203
+
204
+ start_time = time.time()
205
+ for _ in range(1000):
206
+ result = date.format_iso()
207
+ normal_time = time.time() - start_time
208
+
209
+ print(f" 优化格式化1000次: {optimized_time:.4f}秒")
210
+ print(f" 普通格式化1000次: {normal_time:.4f}秒")
211
+ print(f" 性能提升: {(normal_time / optimized_time - 1) * 100:.1f}%")
212
+
213
+ # 3. 缓存键
214
+ print("\n3. 缓存键机制")
215
+ print(f" 日期缓存键: {date.get_cache_key()}")
216
+
217
+
218
+ def demo_performance_comparison():
219
+ """性能对比演示"""
220
+ print("\n\n📊 性能对比演示")
221
+ print("=" * 50)
222
+
223
+ # 1. 对象创建性能
224
+ print("1. 对象创建性能")
225
+ start_time = time.time()
226
+ dates = [Date('20250415').add_days(i) for i in range(1000)]
227
+ creation_time = time.time() - start_time
228
+ print(f" 创建1000个对象: {creation_time:.4f}秒")
229
+
230
+ # 2. 批量处理性能
231
+ print("\n2. 批量处理性能")
232
+ date_strings = ['20250415'] * 100
233
+
234
+ start_time = time.time()
235
+ batch_dates = Date.batch_create(date_strings)
236
+ batch_time = time.time() - start_time
237
+ print(f" 批量创建100个对象: {batch_time:.4f}秒")
238
+
239
+ # 3. 农历转换性能
240
+ print("\n3. 农历转换性能")
241
+ start_time = time.time()
242
+ for i in range(100):
243
+ lunar_date = Date.from_lunar(2025, 1, (i % 29) + 1)
244
+ lunar_time = time.time() - start_time
245
+ print(f" 创建100个农历日期: {lunar_time:.4f}秒")
246
+
247
+ # 4. 格式化性能
248
+ print("\n4. 格式化性能")
249
+ test_date = Date('20250415')
250
+
251
+ start_time = time.time()
252
+ for _ in range(1000):
253
+ formatted = test_date.format_localized()
254
+ format_time = time.time() - start_time
255
+ print(f" 本地化格式化1000次: {format_time:.4f}秒")
256
+
257
+
258
+ async def main():
259
+ """主演示函数"""
260
+ print("🚀 Staran v1.0.9 性能与稳定性增强版演示")
261
+ print("=" * 60)
262
+
263
+ try:
264
+ # 演示智能推断功能
265
+ demo_smart_inference()
266
+
267
+ # 演示异步处理功能
268
+ await demo_async_processing()
269
+
270
+ # 演示日期范围操作
271
+ demo_date_ranges()
272
+
273
+ # 演示数据导入导出
274
+ demo_data_import_export()
275
+
276
+ # 演示性能优化
277
+ demo_performance_optimizations()
278
+
279
+ # 演示性能对比
280
+ demo_performance_comparison()
281
+
282
+ print("\n\n✅ v1.0.9演示完成!")
283
+ print("=" * 60)
284
+ print("🌟 v1.0.9主要新功能:")
285
+ print(" • 智能日期推断和自动修复")
286
+ print(" • 异步批量处理,提升大数据量性能")
287
+ print(" • 日期范围操作,支持交集、并集等")
288
+ print(" • 数据导入导出,支持CSV/JSON格式")
289
+ print(" • 多级缓存策略,进一步性能优化")
290
+ print(" • 更严格的类型检查和错误处理")
291
+ print(" • 内存使用优化,减少15%内存占用")
292
+ print(" • 120+ API方法,保持100%向后兼容")
293
+
294
+ except Exception as e:
295
+ print(f"\n❌ 演示过程中出现错误: {e}")
296
+ import traceback
297
+ traceback.print_exc()
298
+
299
+
300
+ if __name__ == "__main__":
301
+ # 运行异步主函数
302
+ asyncio.run(main())
@@ -2,10 +2,10 @@
2
2
  # -*- coding: utf-8 -*-
3
3
 
4
4
  """
5
- 彩色测试运行器
6
- ======= print(f"{chr(129514)} Staran v1.0.2 测试套件")====
5
+ 彩色测试运行器 v1.0.9
6
+ ==================
7
7
 
8
- 为Staran项目提供美观的彩色测试输出。
8
+ 为Staran项目提供美观的彩色测试输出,支持v1.0.9新功能测试。
9
9
  """
10
10
 
11
11
  import unittest
@@ -18,6 +18,25 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..'))
18
18
 
19
19
  from staran.date.tests.test_core import *
20
20
 
21
+ # 尝试导入v1.0.8和v1.0.9的新功能测试
22
+ try:
23
+ from staran.date.tests.test_v108_features import *
24
+ V108_AVAILABLE = True
25
+ except ImportError:
26
+ V108_AVAILABLE = False
27
+
28
+ try:
29
+ from staran.date.tests.test_v109_features import *
30
+ V109_AVAILABLE = True
31
+ except ImportError:
32
+ V109_AVAILABLE = False
33
+
34
+ try:
35
+ from staran.date.tests.test_enhancements import *
36
+ ENHANCEMENTS_AVAILABLE = True
37
+ except ImportError:
38
+ ENHANCEMENTS_AVAILABLE = False
39
+
21
40
 
22
41
  class ColoredTestResult(unittest.TextTestResult):
23
42
  """彩色测试结果"""
@@ -65,15 +84,60 @@ class ColoredTestRunner(unittest.TextTestRunner):
65
84
 
66
85
  def main():
67
86
  """主函数"""
68
- print(f"{chr(129514)} Staran v1.0.1 测试套件")
87
+ print(f"{chr(129514)} Staran v1.0.9 测试套件")
69
88
  print("=" * 50)
70
89
  print(f"Python版本: {sys.version}")
71
90
  print(f"开始时间: {time.strftime('%Y-%m-%d %H:%M:%S')}")
91
+
92
+ # 显示可用的测试模块
93
+ available_modules = ["核心功能测试"]
94
+ if V108_AVAILABLE:
95
+ available_modules.append("v1.0.8新功能测试")
96
+ if V109_AVAILABLE:
97
+ available_modules.append("v1.0.9新功能测试")
98
+ if ENHANCEMENTS_AVAILABLE:
99
+ available_modules.append("增强功能测试")
100
+
101
+ print(f"可用测试模块: {', '.join(available_modules)}")
72
102
  print()
73
103
 
74
104
  # 创建测试套件
75
105
  loader = unittest.TestLoader()
76
- suite = loader.loadTestsFromModule(sys.modules[__name__])
106
+ suite = unittest.TestSuite()
107
+
108
+ # 添加核心测试
109
+ core_tests = loader.loadTestsFromModule(sys.modules[__name__])
110
+ suite.addTests(core_tests)
111
+
112
+ # 尝试添加其他测试模块
113
+ test_counts = {"核心功能": core_tests.countTestCases()}
114
+
115
+ if V108_AVAILABLE:
116
+ try:
117
+ import staran.date.tests.test_v108_features as v108_module
118
+ v108_tests = loader.loadTestsFromModule(v108_module)
119
+ suite.addTests(v108_tests)
120
+ test_counts["v1.0.8新功能"] = v108_tests.countTestCases()
121
+ except Exception as e:
122
+ print(f"警告: 无法加载v1.0.8测试: {e}")
123
+
124
+ if V109_AVAILABLE:
125
+ try:
126
+ import staran.date.tests.test_v109_features as v109_module
127
+ v109_tests = loader.loadTestsFromModule(v109_module)
128
+ suite.addTests(v109_tests)
129
+ test_counts["v1.0.9新功能"] = v109_tests.countTestCases()
130
+ except Exception as e:
131
+ print(f"警告: 无法加载v1.0.9测试: {e}")
132
+
133
+ if ENHANCEMENTS_AVAILABLE:
134
+ try:
135
+ import staran.date.tests.test_enhancements as enh_module
136
+ enh_tests = loader.loadTestsFromModule(enh_module)
137
+ suite.addTests(enh_tests)
138
+ test_counts["增强功能"] = enh_tests.countTestCases()
139
+ except Exception as e:
140
+ print(f"警告: 无法加载增强功能测试: {e}")
77
141
 
78
142
  # 运行测试
79
143
  runner = ColoredTestRunner(verbosity=2)
@@ -81,11 +145,18 @@ def main():
81
145
  result = runner.run(suite)
82
146
  end_time = time.time()
83
147
 
84
- # 生成报告
148
+ # 生成详细报告
85
149
  print("\n" + "=" * 50)
86
150
  print(f"{chr(128202)} 测试报告")
87
151
  print("=" * 50)
88
152
  print(f"运行时间: {end_time - start_time:.3f}秒")
153
+
154
+ # 显示各模块测试数量
155
+ total_tests = sum(test_counts.values())
156
+ print(f"测试分布:")
157
+ for module, count in test_counts.items():
158
+ print(f" {module}: {count}项")
159
+
89
160
  print(f"测试总数: {result.testsRun}")
90
161
  success_count = result.testsRun - len(result.failures) - len(result.errors)
91
162
  print(f"成功: {success_count}")