persiantools 3.0.1__tar.gz → 5.2.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.
- {persiantools-3.0.1 → persiantools-5.2.0}/LICENSE +2 -2
- persiantools-5.2.0/MANIFEST.in +1 -0
- {persiantools-3.0.1/persiantools.egg-info → persiantools-5.2.0}/PKG-INFO +69 -48
- {persiantools-3.0.1 → persiantools-5.2.0}/README.md +51 -42
- {persiantools-3.0.1 → persiantools-5.2.0}/persiantools/__init__.py +2 -2
- persiantools-5.2.0/persiantools/characters.py +64 -0
- persiantools-5.2.0/persiantools/digits.py +264 -0
- {persiantools-3.0.1 → persiantools-5.2.0}/persiantools/jdatetime.py +830 -146
- persiantools-5.2.0/persiantools/utils.py +25 -0
- {persiantools-3.0.1 → persiantools-5.2.0/persiantools.egg-info}/PKG-INFO +69 -48
- {persiantools-3.0.1 → persiantools-5.2.0}/persiantools.egg-info/SOURCES.txt +7 -1
- persiantools-5.2.0/persiantools.egg-info/requires.txt +1 -0
- {persiantools-3.0.1 → persiantools-5.2.0}/setup.py +6 -5
- persiantools-5.2.0/tests/test_characters.py +60 -0
- persiantools-5.2.0/tests/test_digits.py +92 -0
- persiantools-5.2.0/tests/test_jalalidate.py +584 -0
- persiantools-5.2.0/tests/test_jalalidatetime.py +639 -0
- persiantools-5.2.0/tests/test_utils.py +62 -0
- persiantools-3.0.1/MANIFEST.in +0 -1
- persiantools-3.0.1/persiantools/characters.py +0 -41
- persiantools-3.0.1/persiantools/digits.py +0 -191
- persiantools-3.0.1/persiantools/utils.py +0 -10
- {persiantools-3.0.1 → persiantools-5.2.0}/persiantools.egg-info/dependency_links.txt +0 -0
- {persiantools-3.0.1 → persiantools-5.2.0}/persiantools.egg-info/not-zip-safe +0 -0
- {persiantools-3.0.1 → persiantools-5.2.0}/persiantools.egg-info/top_level.txt +0 -0
- {persiantools-3.0.1 → persiantools-5.2.0}/pyproject.toml +0 -0
- {persiantools-3.0.1 → persiantools-5.2.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2016-
|
|
3
|
+
Copyright (c) 2016-2025 Majid Hajiloo
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
include LICENSE README.md
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.2
|
|
2
2
|
Name: persiantools
|
|
3
|
-
Version:
|
|
3
|
+
Version: 5.2.0
|
|
4
4
|
Summary: Jalali date and datetime with other tools
|
|
5
5
|
Home-page: https://github.com/majiidd/persiantools
|
|
6
6
|
Author: Majid Hajiloo
|
|
@@ -13,11 +13,11 @@ Classifier: Natural Language :: Persian
|
|
|
13
13
|
Classifier: Operating System :: OS Independent
|
|
14
14
|
Classifier: Programming Language :: Python
|
|
15
15
|
Classifier: Programming Language :: Python :: 3
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.6
|
|
17
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
18
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
19
16
|
Classifier: Programming Language :: Python :: 3.9
|
|
20
17
|
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
21
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
22
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
23
23
|
Classifier: Topic :: Software Development
|
|
@@ -25,9 +25,21 @@ Classifier: Topic :: Software Development :: Libraries
|
|
|
25
25
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
26
26
|
Classifier: Topic :: Software Development :: Localization
|
|
27
27
|
Classifier: Topic :: Utilities
|
|
28
|
-
Requires-Python: >=3.
|
|
28
|
+
Requires-Python: >=3.9
|
|
29
29
|
Description-Content-Type: text/markdown
|
|
30
30
|
License-File: LICENSE
|
|
31
|
+
Requires-Dist: pytz
|
|
32
|
+
Dynamic: author
|
|
33
|
+
Dynamic: author-email
|
|
34
|
+
Dynamic: classifier
|
|
35
|
+
Dynamic: description
|
|
36
|
+
Dynamic: description-content-type
|
|
37
|
+
Dynamic: home-page
|
|
38
|
+
Dynamic: keywords
|
|
39
|
+
Dynamic: license
|
|
40
|
+
Dynamic: requires-dist
|
|
41
|
+
Dynamic: requires-python
|
|
42
|
+
Dynamic: summary
|
|
31
43
|
|
|
32
44
|
# PersianTools
|
|
33
45
|
|
|
@@ -38,72 +50,78 @@ License-File: LICENSE
|
|
|
38
50
|
[](https://pypi.org/project/persiantools/)
|
|
39
51
|
[](https://pypi.org/project/persiantools/)
|
|
40
52
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
- Convert Arabic and Persian characters/digits to each other
|
|
48
|
-
- Convert numbers to words
|
|
53
|
+
PersianTools provides comprehensive tools for handling Jalali (Shamsi or Persian) dates, date-time functionalities, and more.
|
|
54
|
+
- Conversion between Jalali and Gregorian dates/datetimes using Python's native datetime module.
|
|
55
|
+
- Full support for operations like `+`, `-`, `==`, and `>=`.
|
|
56
|
+
- Timezone-aware date and datetime handling.
|
|
57
|
+
- Conversion between Persian and Arabic characters and digits.
|
|
58
|
+
- Conversion of numbers to their Persian word representation.
|
|
49
59
|
|
|
50
60
|
## Install Package
|
|
51
|
-
|
|
61
|
+
You can install the package using pip with the following command:
|
|
52
62
|
```bash
|
|
53
63
|
python -m pip install persiantools
|
|
54
64
|
```
|
|
55
|
-
Persiantools supports Python 3.6+. (_for python 2.7 and 3.5 use [1.5.x](https://github.com/majiidd/persiantools/tree/1.5.x) version_)
|
|
56
65
|
|
|
57
|
-
##
|
|
66
|
+
## Usage Guide
|
|
58
67
|
|
|
59
|
-
### Date
|
|
68
|
+
### Date Operations
|
|
60
69
|
|
|
61
70
|
```python
|
|
62
71
|
>>> from persiantools.jdatetime import JalaliDate
|
|
63
72
|
>>> import datetime
|
|
64
73
|
|
|
74
|
+
# Get today's date in Jalali
|
|
65
75
|
>>> JalaliDate.today()
|
|
66
|
-
JalaliDate(
|
|
76
|
+
JalaliDate(1403, 8, 18, Jomeh)
|
|
67
77
|
|
|
68
|
-
>>> JalaliDate(
|
|
69
|
-
JalaliDate(
|
|
78
|
+
>>> JalaliDate(1367, 2, 14)
|
|
79
|
+
JalaliDate(1367, 2, 14, Chaharshanbeh)
|
|
70
80
|
|
|
71
|
-
|
|
72
|
-
JalaliDate(
|
|
81
|
+
# Convert Gregorian to Jalali
|
|
82
|
+
>>> JalaliDate(datetime.date(1988, 5, 4))
|
|
83
|
+
JalaliDate(1367, 2, 14, Chaharshanbeh)
|
|
73
84
|
|
|
74
|
-
|
|
85
|
+
# Convert from Gregorian to Jalali using method
|
|
86
|
+
>>> JalaliDate.to_jalali(2013, 9, 16)
|
|
75
87
|
JalaliDate(1392, 6, 25, Doshanbeh)
|
|
76
88
|
|
|
77
|
-
|
|
89
|
+
# Convert from Jalali to Gregorian
|
|
90
|
+
>>> JalaliDate(1392, 6, 25).to_gregorian()
|
|
78
91
|
datetime.date(2013, 9, 16)
|
|
79
92
|
|
|
80
|
-
|
|
93
|
+
# Create a Jalali date from a Unix timestamp
|
|
94
|
+
>>> JalaliDate.fromtimestamp(578707200)
|
|
81
95
|
JalaliDate(1367, 2, 14, Chaharshanbeh)
|
|
82
96
|
```
|
|
83
97
|
|
|
84
|
-
### Datetime
|
|
98
|
+
### Datetime Operations
|
|
85
99
|
|
|
86
100
|
```python
|
|
87
101
|
>>> from persiantools.jdatetime import JalaliDateTime
|
|
88
102
|
>>> import datetime, pytz
|
|
89
103
|
|
|
104
|
+
# Get the current Jalali datetime
|
|
90
105
|
>>> JalaliDateTime.now()
|
|
91
|
-
JalaliDateTime(
|
|
106
|
+
JalaliDateTime(1403, 8, 18, 12, 48, 54, 569082)
|
|
92
107
|
|
|
93
|
-
|
|
94
|
-
|
|
108
|
+
# Convert Jalali datetime to Gregorian
|
|
109
|
+
>>> JalaliDateTime.now().to_gregorian()
|
|
110
|
+
datetime.datetime(2024, 11, 8, 12, 48, 54, 569082)
|
|
95
111
|
|
|
96
|
-
|
|
112
|
+
# Convert Gregorian datetime to Jalali
|
|
113
|
+
>>> JalaliDateTime.to_jalali(datetime.datetime(1988, 5, 4, 14, 0, 0, 0))
|
|
97
114
|
JalaliDateTime(1367, 2, 14, 14, 0)
|
|
98
115
|
|
|
99
|
-
|
|
116
|
+
# Create a timezone-aware Jalali datetime from a timestamp
|
|
117
|
+
>>> JalaliDateTime.fromtimestamp(578723400, pytz.timezone("Asia/Tehran"))
|
|
100
118
|
JalaliDateTime(1367, 2, 14, 8, 0, tzinfo=<DstTzInfo 'Asia/Tehran' +0330+3:30:00 STD>)
|
|
101
119
|
|
|
102
120
|
>>> JalaliDateTime.now(pytz.utc)
|
|
103
121
|
JalaliDateTime(1395, 4, 17, 21, 23, 53, 474618, tzinfo=<UTC>)
|
|
104
122
|
```
|
|
105
123
|
|
|
106
|
-
###
|
|
124
|
+
### Formatting
|
|
107
125
|
|
|
108
126
|
Based on python `strftime()` behavior
|
|
109
127
|
|
|
@@ -111,45 +129,50 @@ Based on python `strftime()` behavior
|
|
|
111
129
|
>>> from persiantools.jdatetime import JalaliDate, JalaliDateTime
|
|
112
130
|
>>> import pytz
|
|
113
131
|
|
|
132
|
+
# ISO formatting
|
|
114
133
|
>>> JalaliDate(1367, 2, 14).isoformat()
|
|
115
134
|
'1367-02-14'
|
|
116
135
|
|
|
136
|
+
# Custom date formatting
|
|
117
137
|
>>> JalaliDate(1395, 3, 1).strftime("%Y/%m/%d")
|
|
118
138
|
'1395/03/01'
|
|
119
139
|
|
|
140
|
+
# Custom datetime formatting
|
|
120
141
|
>>> JalaliDateTime(1369, 7, 1, 14, 0, 10, 0, pytz.utc).strftime("%c")
|
|
121
142
|
'Yekshanbeh 01 Mehr 1369 14:00:10'
|
|
122
|
-
|
|
123
|
-
>>> JalaliDateTime.now(pytz.utc).strftime("%I:%M:%S.%f %p %z %Z")
|
|
124
|
-
'01:49:22.518523 PM +0000 UTC'
|
|
125
143
|
```
|
|
126
144
|
|
|
127
|
-
### Digits
|
|
145
|
+
### Digits and Character Conversion
|
|
128
146
|
|
|
129
147
|
```python
|
|
130
148
|
>>> from persiantools import characters, digits
|
|
131
149
|
|
|
150
|
+
# Convert English digits to Persian
|
|
132
151
|
>>> digits.en_to_fa("0987654321")
|
|
133
152
|
'۰۹۸۷۶۵۴۳۲۱'
|
|
134
153
|
|
|
154
|
+
# Convert Arabic digits to Persian
|
|
135
155
|
>>> digits.ar_to_fa("٠٩٨٧٦٥٤٣٢١")
|
|
136
156
|
'۰۹۸۷۶۵۴۳۲۱'
|
|
137
157
|
|
|
158
|
+
# Convert Persian digits to English
|
|
138
159
|
>>> digits.fa_to_en("۰۹۸۷۶۵۴۳۲۱")
|
|
139
160
|
'0987654321'
|
|
140
161
|
|
|
162
|
+
# Convert numbers to Persian words
|
|
141
163
|
>>> digits.to_word(9512026)
|
|
142
164
|
'نه میلیون و پانصد و دوازده هزار و بیست و شش'
|
|
143
165
|
|
|
144
166
|
>>> digits.to_word(15.007)
|
|
145
167
|
'پانزده و هفت هزارم'
|
|
146
168
|
|
|
169
|
+
# Convert Arabic to Persian characters
|
|
147
170
|
>>> characters.ar_to_fa("كيك")
|
|
148
171
|
'کیک'
|
|
149
172
|
```
|
|
150
173
|
|
|
151
174
|
### Operators
|
|
152
|
-
|
|
175
|
+
The package supports various operators for date and time manipulations. Here are some examples:
|
|
153
176
|
```python
|
|
154
177
|
>>> from persiantools.jdatetime import JalaliDate, JalaliDateTime
|
|
155
178
|
>>> import datetime
|
|
@@ -157,7 +180,7 @@ Based on python `strftime()` behavior
|
|
|
157
180
|
>>> JalaliDate(1367, 2, 14) == JalaliDate(datetime.date(1988, 5, 4))
|
|
158
181
|
True
|
|
159
182
|
|
|
160
|
-
>>> JalaliDateTime(1367, 2, 14, 4, 30) >= JalaliDateTime(
|
|
183
|
+
>>> JalaliDateTime(1367, 2, 14, 4, 30) >= JalaliDateTime(1368, 2, 14, 1, 0)
|
|
161
184
|
False
|
|
162
185
|
|
|
163
186
|
>>> JalaliDate(1367, 2, 14) == datetime.date(1988, 5, 4)
|
|
@@ -170,21 +193,19 @@ JalaliDate(1395, 3, 21, Jomeh)
|
|
|
170
193
|
datetime.timedelta(365)
|
|
171
194
|
```
|
|
172
195
|
|
|
173
|
-
### Serializing and
|
|
196
|
+
### Serializing and Deserializing
|
|
174
197
|
|
|
175
198
|
```python
|
|
176
199
|
>>> from persiantools.jdatetime import JalaliDate
|
|
177
200
|
>>> import pickle
|
|
178
201
|
|
|
179
|
-
|
|
180
|
-
>>>
|
|
181
|
-
>>>
|
|
182
|
-
>>> file.close()
|
|
202
|
+
# Serialize a Jalali date to a file
|
|
203
|
+
>>> with open("save.p", "wb") as file:
|
|
204
|
+
>>> pickle.dump(JalaliDate(1367, 2, 14), file)
|
|
183
205
|
|
|
184
|
-
|
|
185
|
-
>>>
|
|
186
|
-
>>>
|
|
187
|
-
>>> file.close()
|
|
206
|
+
# Deserialize from a file
|
|
207
|
+
>>> with open("save.p", "rb") as file:
|
|
208
|
+
>>> jalali = pickle.load(file)
|
|
188
209
|
>>> jalali
|
|
189
210
|
JalaliDate(1367, 2, 14, Chaharshanbeh)
|
|
190
211
|
```
|
|
@@ -7,72 +7,78 @@
|
|
|
7
7
|
[](https://pypi.org/project/persiantools/)
|
|
8
8
|
[](https://pypi.org/project/persiantools/)
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
- Convert Arabic and Persian characters/digits to each other
|
|
17
|
-
- Convert numbers to words
|
|
10
|
+
PersianTools provides comprehensive tools for handling Jalali (Shamsi or Persian) dates, date-time functionalities, and more.
|
|
11
|
+
- Conversion between Jalali and Gregorian dates/datetimes using Python's native datetime module.
|
|
12
|
+
- Full support for operations like `+`, `-`, `==`, and `>=`.
|
|
13
|
+
- Timezone-aware date and datetime handling.
|
|
14
|
+
- Conversion between Persian and Arabic characters and digits.
|
|
15
|
+
- Conversion of numbers to their Persian word representation.
|
|
18
16
|
|
|
19
17
|
## Install Package
|
|
20
|
-
|
|
18
|
+
You can install the package using pip with the following command:
|
|
21
19
|
```bash
|
|
22
20
|
python -m pip install persiantools
|
|
23
21
|
```
|
|
24
|
-
Persiantools supports Python 3.6+. (_for python 2.7 and 3.5 use [1.5.x](https://github.com/majiidd/persiantools/tree/1.5.x) version_)
|
|
25
22
|
|
|
26
|
-
##
|
|
23
|
+
## Usage Guide
|
|
27
24
|
|
|
28
|
-
### Date
|
|
25
|
+
### Date Operations
|
|
29
26
|
|
|
30
27
|
```python
|
|
31
28
|
>>> from persiantools.jdatetime import JalaliDate
|
|
32
29
|
>>> import datetime
|
|
33
30
|
|
|
31
|
+
# Get today's date in Jalali
|
|
34
32
|
>>> JalaliDate.today()
|
|
35
|
-
JalaliDate(
|
|
33
|
+
JalaliDate(1403, 8, 18, Jomeh)
|
|
36
34
|
|
|
37
|
-
>>> JalaliDate(
|
|
38
|
-
JalaliDate(
|
|
35
|
+
>>> JalaliDate(1367, 2, 14)
|
|
36
|
+
JalaliDate(1367, 2, 14, Chaharshanbeh)
|
|
39
37
|
|
|
40
|
-
|
|
41
|
-
JalaliDate(
|
|
38
|
+
# Convert Gregorian to Jalali
|
|
39
|
+
>>> JalaliDate(datetime.date(1988, 5, 4))
|
|
40
|
+
JalaliDate(1367, 2, 14, Chaharshanbeh)
|
|
42
41
|
|
|
43
|
-
|
|
42
|
+
# Convert from Gregorian to Jalali using method
|
|
43
|
+
>>> JalaliDate.to_jalali(2013, 9, 16)
|
|
44
44
|
JalaliDate(1392, 6, 25, Doshanbeh)
|
|
45
45
|
|
|
46
|
-
|
|
46
|
+
# Convert from Jalali to Gregorian
|
|
47
|
+
>>> JalaliDate(1392, 6, 25).to_gregorian()
|
|
47
48
|
datetime.date(2013, 9, 16)
|
|
48
49
|
|
|
49
|
-
|
|
50
|
+
# Create a Jalali date from a Unix timestamp
|
|
51
|
+
>>> JalaliDate.fromtimestamp(578707200)
|
|
50
52
|
JalaliDate(1367, 2, 14, Chaharshanbeh)
|
|
51
53
|
```
|
|
52
54
|
|
|
53
|
-
### Datetime
|
|
55
|
+
### Datetime Operations
|
|
54
56
|
|
|
55
57
|
```python
|
|
56
58
|
>>> from persiantools.jdatetime import JalaliDateTime
|
|
57
59
|
>>> import datetime, pytz
|
|
58
60
|
|
|
61
|
+
# Get the current Jalali datetime
|
|
59
62
|
>>> JalaliDateTime.now()
|
|
60
|
-
JalaliDateTime(
|
|
63
|
+
JalaliDateTime(1403, 8, 18, 12, 48, 54, 569082)
|
|
61
64
|
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
# Convert Jalali datetime to Gregorian
|
|
66
|
+
>>> JalaliDateTime.now().to_gregorian()
|
|
67
|
+
datetime.datetime(2024, 11, 8, 12, 48, 54, 569082)
|
|
64
68
|
|
|
65
|
-
|
|
69
|
+
# Convert Gregorian datetime to Jalali
|
|
70
|
+
>>> JalaliDateTime.to_jalali(datetime.datetime(1988, 5, 4, 14, 0, 0, 0))
|
|
66
71
|
JalaliDateTime(1367, 2, 14, 14, 0)
|
|
67
72
|
|
|
68
|
-
|
|
73
|
+
# Create a timezone-aware Jalali datetime from a timestamp
|
|
74
|
+
>>> JalaliDateTime.fromtimestamp(578723400, pytz.timezone("Asia/Tehran"))
|
|
69
75
|
JalaliDateTime(1367, 2, 14, 8, 0, tzinfo=<DstTzInfo 'Asia/Tehran' +0330+3:30:00 STD>)
|
|
70
76
|
|
|
71
77
|
>>> JalaliDateTime.now(pytz.utc)
|
|
72
78
|
JalaliDateTime(1395, 4, 17, 21, 23, 53, 474618, tzinfo=<UTC>)
|
|
73
79
|
```
|
|
74
80
|
|
|
75
|
-
###
|
|
81
|
+
### Formatting
|
|
76
82
|
|
|
77
83
|
Based on python `strftime()` behavior
|
|
78
84
|
|
|
@@ -80,45 +86,50 @@ Based on python `strftime()` behavior
|
|
|
80
86
|
>>> from persiantools.jdatetime import JalaliDate, JalaliDateTime
|
|
81
87
|
>>> import pytz
|
|
82
88
|
|
|
89
|
+
# ISO formatting
|
|
83
90
|
>>> JalaliDate(1367, 2, 14).isoformat()
|
|
84
91
|
'1367-02-14'
|
|
85
92
|
|
|
93
|
+
# Custom date formatting
|
|
86
94
|
>>> JalaliDate(1395, 3, 1).strftime("%Y/%m/%d")
|
|
87
95
|
'1395/03/01'
|
|
88
96
|
|
|
97
|
+
# Custom datetime formatting
|
|
89
98
|
>>> JalaliDateTime(1369, 7, 1, 14, 0, 10, 0, pytz.utc).strftime("%c")
|
|
90
99
|
'Yekshanbeh 01 Mehr 1369 14:00:10'
|
|
91
|
-
|
|
92
|
-
>>> JalaliDateTime.now(pytz.utc).strftime("%I:%M:%S.%f %p %z %Z")
|
|
93
|
-
'01:49:22.518523 PM +0000 UTC'
|
|
94
100
|
```
|
|
95
101
|
|
|
96
|
-
### Digits
|
|
102
|
+
### Digits and Character Conversion
|
|
97
103
|
|
|
98
104
|
```python
|
|
99
105
|
>>> from persiantools import characters, digits
|
|
100
106
|
|
|
107
|
+
# Convert English digits to Persian
|
|
101
108
|
>>> digits.en_to_fa("0987654321")
|
|
102
109
|
'۰۹۸۷۶۵۴۳۲۱'
|
|
103
110
|
|
|
111
|
+
# Convert Arabic digits to Persian
|
|
104
112
|
>>> digits.ar_to_fa("٠٩٨٧٦٥٤٣٢١")
|
|
105
113
|
'۰۹۸۷۶۵۴۳۲۱'
|
|
106
114
|
|
|
115
|
+
# Convert Persian digits to English
|
|
107
116
|
>>> digits.fa_to_en("۰۹۸۷۶۵۴۳۲۱")
|
|
108
117
|
'0987654321'
|
|
109
118
|
|
|
119
|
+
# Convert numbers to Persian words
|
|
110
120
|
>>> digits.to_word(9512026)
|
|
111
121
|
'نه میلیون و پانصد و دوازده هزار و بیست و شش'
|
|
112
122
|
|
|
113
123
|
>>> digits.to_word(15.007)
|
|
114
124
|
'پانزده و هفت هزارم'
|
|
115
125
|
|
|
126
|
+
# Convert Arabic to Persian characters
|
|
116
127
|
>>> characters.ar_to_fa("كيك")
|
|
117
128
|
'کیک'
|
|
118
129
|
```
|
|
119
130
|
|
|
120
131
|
### Operators
|
|
121
|
-
|
|
132
|
+
The package supports various operators for date and time manipulations. Here are some examples:
|
|
122
133
|
```python
|
|
123
134
|
>>> from persiantools.jdatetime import JalaliDate, JalaliDateTime
|
|
124
135
|
>>> import datetime
|
|
@@ -126,7 +137,7 @@ Based on python `strftime()` behavior
|
|
|
126
137
|
>>> JalaliDate(1367, 2, 14) == JalaliDate(datetime.date(1988, 5, 4))
|
|
127
138
|
True
|
|
128
139
|
|
|
129
|
-
>>> JalaliDateTime(1367, 2, 14, 4, 30) >= JalaliDateTime(
|
|
140
|
+
>>> JalaliDateTime(1367, 2, 14, 4, 30) >= JalaliDateTime(1368, 2, 14, 1, 0)
|
|
130
141
|
False
|
|
131
142
|
|
|
132
143
|
>>> JalaliDate(1367, 2, 14) == datetime.date(1988, 5, 4)
|
|
@@ -139,21 +150,19 @@ JalaliDate(1395, 3, 21, Jomeh)
|
|
|
139
150
|
datetime.timedelta(365)
|
|
140
151
|
```
|
|
141
152
|
|
|
142
|
-
### Serializing and
|
|
153
|
+
### Serializing and Deserializing
|
|
143
154
|
|
|
144
155
|
```python
|
|
145
156
|
>>> from persiantools.jdatetime import JalaliDate
|
|
146
157
|
>>> import pickle
|
|
147
158
|
|
|
148
|
-
|
|
149
|
-
>>>
|
|
150
|
-
>>>
|
|
151
|
-
>>> file.close()
|
|
159
|
+
# Serialize a Jalali date to a file
|
|
160
|
+
>>> with open("save.p", "wb") as file:
|
|
161
|
+
>>> pickle.dump(JalaliDate(1367, 2, 14), file)
|
|
152
162
|
|
|
153
|
-
|
|
154
|
-
>>>
|
|
155
|
-
>>>
|
|
156
|
-
>>> file.close()
|
|
163
|
+
# Deserialize from a file
|
|
164
|
+
>>> with open("save.p", "rb") as file:
|
|
165
|
+
>>> jalali = pickle.load(file)
|
|
157
166
|
>>> jalali
|
|
158
167
|
JalaliDate(1367, 2, 14, Chaharshanbeh)
|
|
159
168
|
```
|
|
@@ -7,9 +7,9 @@
|
|
|
7
7
|
|
|
8
8
|
__title__ = "persiantools"
|
|
9
9
|
__url__ = "https://github.com/majiidd/persiantools"
|
|
10
|
-
__version__ = "
|
|
10
|
+
__version__ = "5.2.0"
|
|
11
11
|
__build__ = __version__
|
|
12
12
|
__author__ = "Majid Hajiloo"
|
|
13
13
|
__author_email__ = "majid.hajiloo@gmail.com"
|
|
14
14
|
__license__ = "MIT"
|
|
15
|
-
__copyright__ = "Copyright (c) 2016-
|
|
15
|
+
__copyright__ = "Copyright (c) 2016-2025 Majid Hajiloo"
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
CHARACTER_MAP_AR_TO_FA = {
|
|
4
|
+
"دِ": "د",
|
|
5
|
+
"بِ": "ب",
|
|
6
|
+
"زِ": "ز",
|
|
7
|
+
"ذِ": "ذ",
|
|
8
|
+
"شِ": "ش",
|
|
9
|
+
"سِ": "س",
|
|
10
|
+
"ى": "ی",
|
|
11
|
+
"ي": "ی",
|
|
12
|
+
"ك": "ک",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
CHARACTER_MAP_FA_TO_AR = {"ی": "ي", "ک": "ك"}
|
|
16
|
+
|
|
17
|
+
AR_TO_FA_PATTERN = re.compile("|".join(map(re.escape, CHARACTER_MAP_AR_TO_FA.keys())))
|
|
18
|
+
FA_TO_AR_PATTERN = re.compile("|".join(map(re.escape, CHARACTER_MAP_FA_TO_AR.keys())))
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def ar_to_fa(string: str) -> str:
|
|
22
|
+
"""
|
|
23
|
+
Convert Arabic characters to Persian.
|
|
24
|
+
|
|
25
|
+
Usage::
|
|
26
|
+
from persiantools import characters
|
|
27
|
+
converted = characters.ar_to_fa("السلام عليكم")
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
string (str): The string to convert.
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
str: The converted string with Arabic characters replaced by Persian characters.
|
|
34
|
+
|
|
35
|
+
Raises:
|
|
36
|
+
TypeError: If the input string is not of type str.
|
|
37
|
+
"""
|
|
38
|
+
if not isinstance(string, str):
|
|
39
|
+
raise TypeError("Input must be of type str")
|
|
40
|
+
|
|
41
|
+
return AR_TO_FA_PATTERN.sub(lambda match: CHARACTER_MAP_AR_TO_FA[match.group(0)], string)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def fa_to_ar(string: str) -> str:
|
|
45
|
+
"""
|
|
46
|
+
Convert Persian characters to Arabic.
|
|
47
|
+
|
|
48
|
+
Usage::
|
|
49
|
+
from persiantools import characters
|
|
50
|
+
converted = characters.fa_to_ar("ای چرخ فلک خرابی از کینه تست")
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
string (str): The string to convert.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
str: The converted string with Persian characters replaced by Arabic characters.
|
|
57
|
+
|
|
58
|
+
Raises:
|
|
59
|
+
TypeError: If the input string is not of type str.
|
|
60
|
+
"""
|
|
61
|
+
if not isinstance(string, str):
|
|
62
|
+
raise TypeError("Input must be of type str")
|
|
63
|
+
|
|
64
|
+
return FA_TO_AR_PATTERN.sub(lambda match: CHARACTER_MAP_FA_TO_AR[match.group(0)], string)
|