persiantools 4.0.3__tar.gz → 4.1.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 (27) hide show
  1. persiantools-4.1.0/MANIFEST.in +1 -0
  2. {persiantools-4.0.3/persiantools.egg-info → persiantools-4.1.0}/PKG-INFO +1 -1
  3. {persiantools-4.0.3 → persiantools-4.1.0}/persiantools/__init__.py +1 -1
  4. persiantools-4.1.0/persiantools/characters.py +61 -0
  5. {persiantools-4.0.3 → persiantools-4.1.0}/persiantools/digits.py +123 -21
  6. {persiantools-4.0.3 → persiantools-4.1.0}/persiantools/jdatetime.py +367 -4
  7. persiantools-4.1.0/persiantools/utils.py +26 -0
  8. {persiantools-4.0.3 → persiantools-4.1.0/persiantools.egg-info}/PKG-INFO +1 -1
  9. {persiantools-4.0.3 → persiantools-4.1.0}/tests/test_characters.py +4 -0
  10. {persiantools-4.0.3 → persiantools-4.1.0}/tests/test_digits.py +16 -0
  11. {persiantools-4.0.3 → persiantools-4.1.0}/tests/test_jalalidate.py +207 -149
  12. {persiantools-4.0.3 → persiantools-4.1.0}/tests/test_jalalidatetime.py +220 -2
  13. persiantools-4.1.0/tests/test_utils.py +62 -0
  14. persiantools-4.0.3/MANIFEST.in +0 -1
  15. persiantools-4.0.3/persiantools/characters.py +0 -41
  16. persiantools-4.0.3/persiantools/utils.py +0 -10
  17. persiantools-4.0.3/tests/test_utils.py +0 -22
  18. {persiantools-4.0.3 → persiantools-4.1.0}/LICENSE +0 -0
  19. {persiantools-4.0.3 → persiantools-4.1.0}/README.md +0 -0
  20. {persiantools-4.0.3 → persiantools-4.1.0}/persiantools.egg-info/SOURCES.txt +0 -0
  21. {persiantools-4.0.3 → persiantools-4.1.0}/persiantools.egg-info/dependency_links.txt +0 -0
  22. {persiantools-4.0.3 → persiantools-4.1.0}/persiantools.egg-info/not-zip-safe +0 -0
  23. {persiantools-4.0.3 → persiantools-4.1.0}/persiantools.egg-info/requires.txt +0 -0
  24. {persiantools-4.0.3 → persiantools-4.1.0}/persiantools.egg-info/top_level.txt +0 -0
  25. {persiantools-4.0.3 → persiantools-4.1.0}/pyproject.toml +0 -0
  26. {persiantools-4.0.3 → persiantools-4.1.0}/setup.cfg +0 -0
  27. {persiantools-4.0.3 → persiantools-4.1.0}/setup.py +0 -0
@@ -0,0 +1 @@
1
+ include LICENSE README.md
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: persiantools
3
- Version: 4.0.3
3
+ Version: 4.1.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
@@ -7,7 +7,7 @@
7
7
 
8
8
  __title__ = "persiantools"
9
9
  __url__ = "https://github.com/majiidd/persiantools"
10
- __version__ = "4.0.3"
10
+ __version__ = "4.1.0"
11
11
  __build__ = __version__
12
12
  __author__ = "Majid Hajiloo"
13
13
  __author_email__ = "majid.hajiloo@gmail.com"
@@ -0,0 +1,61 @@
1
+ from persiantools import utils
2
+
3
+
4
+ def ar_to_fa(string: str) -> str:
5
+ """
6
+ Convert Arabic characters to Persian.
7
+
8
+ Usage::
9
+ from persiantools import characters
10
+ converted = characters.ar_to_fa("السلام عليكم")
11
+
12
+ Args:
13
+ string (str): The string to convert.
14
+
15
+ Returns:
16
+ str: The converted string with Arabic characters replaced by Persian characters.
17
+
18
+ Raises:
19
+ TypeError: If the input string is not of type str.
20
+ """
21
+ if not isinstance(string, str):
22
+ raise TypeError("Input must be of type str")
23
+
24
+ characters_map = {
25
+ "دِ": "د",
26
+ "بِ": "ب",
27
+ "زِ": "ز",
28
+ "ذِ": "ذ",
29
+ "شِ": "ش",
30
+ "سِ": "س",
31
+ "ى": "ی",
32
+ "ي": "ی",
33
+ "ك": "ک",
34
+ }
35
+
36
+ return utils.replace(string, characters_map)
37
+
38
+
39
+ def fa_to_ar(string: str) -> str:
40
+ """
41
+ Convert Persian characters to Arabic.
42
+
43
+ Usage::
44
+ from persiantools import characters
45
+ converted = characters.fa_to_ar("ای چرخ فلک خرابی از کینه تست")
46
+
47
+ Args:
48
+ string (str): The string to convert.
49
+
50
+ Returns:
51
+ str: The converted string with Persian characters replaced by Arabic characters.
52
+
53
+ Raises:
54
+ TypeError: If the input string is not of type str.
55
+ """
56
+ if not isinstance(string, str):
57
+ raise TypeError("Input must be of type str")
58
+
59
+ characters_map = {"ی": "ي", "ک": "ك"}
60
+
61
+ return utils.replace(string, characters_map)
@@ -1,4 +1,5 @@
1
1
  import re
2
+ from typing import Tuple
2
3
 
3
4
  EN_TO_FA_MAP = {
4
5
  "0": "۰",
@@ -98,58 +99,114 @@ class OutOfRangeException(Exception):
98
99
 
99
100
 
100
101
  def en_to_fa(string: str) -> str:
101
- """Convert EN digits to Persian
102
+ """
103
+ Convert English digits to Persian digits.
104
+
105
+ This function takes a string containing English digits and converts them to their
106
+ corresponding Persian digits.
107
+
108
+ Parameters:
109
+ string (str): A string containing English digits to be converted.
102
110
 
103
- Usage::
111
+ Returns:
112
+ str: A string with English digits converted to Persian digits.
113
+
114
+ Example:
104
115
  >>> from persiantools import digits
105
116
  >>> converted = digits.en_to_fa("0123456789")
106
-
107
- :param string: A string, will be converted
108
- :rtype: str
117
+ >>> print(converted)
118
+ ۰۱۲۳۴۵۶۷۸۹
109
119
  """
110
120
  return EN_TO_FA_REGEX.sub(lambda x: EN_TO_FA_MAP[x.group()], string)
111
121
 
112
122
 
113
123
  def ar_to_fa(string: str) -> str:
114
- """Convert Arabic digits to Persian
124
+ """
125
+ Convert Arabic digits to Persian digits.
115
126
 
116
- Usage::
127
+ This function takes a string containing Arabic digits and converts them to their
128
+ corresponding Persian digits.
129
+
130
+ Parameters:
131
+ string (str): A string containing Arabic digits to be converted.
132
+
133
+ Returns:
134
+ str: A string with Arabic digits converted to Persian digits.
135
+
136
+ Example:
117
137
  >>> from persiantools import digits
118
138
  >>> converted = digits.ar_to_fa("٠١٢٣٤٥٦٧٨٩")
119
-
120
- :param string: A string, will be converted
121
- :rtype: str
139
+ >>> print(converted)
140
+ ۰۱۲۳۴۵۶۷۸۹
122
141
  """
123
142
  return AR_TO_FA_REGEX.sub(lambda x: AR_TO_FA_MAP[x.group()], string)
124
143
 
125
144
 
126
145
  def fa_to_en(string: str) -> str:
127
- """Convert Persian digits to EN
146
+ """
147
+ Convert Persian digits to English digits.
128
148
 
129
- Usage::
149
+ This function takes a string containing Persian digits and converts them to their
150
+ corresponding English digits.
151
+
152
+ Parameters:
153
+ string (str): A string containing Persian digits to be converted.
154
+
155
+ Returns:
156
+ str: A string with Persian digits converted to English digits.
157
+
158
+ Example:
130
159
  >>> from persiantools import digits
131
160
  >>> converted = digits.fa_to_en("۰۱۲۳۴۵۶۷۸۹")
132
-
133
- :param string: A string, will be converted
134
- :rtype: str
161
+ >>> print(converted)
162
+ 0123456789
135
163
  """
136
164
  return FA_TO_EN_REGEX.sub(lambda x: FA_TO_EN_MAP[x.group()], string)
137
165
 
138
166
 
139
167
  def fa_to_ar(string: str) -> str:
140
- """Convert Persian digits to Arabic
168
+ """
169
+ Convert Persian digits to Arabic digits.
141
170
 
142
- Usage::
171
+ This function takes a string containing Persian digits and converts them to their
172
+ corresponding Arabic digits.
173
+
174
+ Parameters:
175
+ string (str): A string containing Persian digits to be converted.
176
+
177
+ Returns:
178
+ str: A string with Persian digits converted to Arabic digits.
179
+
180
+ Example:
143
181
  >>> from persiantools import digits
144
182
  >>> converted = digits.fa_to_ar("۰۱۲۳۴۵۶۷۸۹")
145
-
146
- :param string: A string, will be converted
147
- :rtype: str
183
+ >>> print(converted)
184
+ ٠١٢٣٤٥٦٧٨٩
148
185
  """
149
186
  return FA_TO_AR_REGEX.sub(lambda x: FA_TO_AR_MAP[x.group()], string)
150
187
 
151
188
 
152
189
  def _to_word(number: int, depth: bool) -> str:
190
+ """
191
+ Convert a number to its Persian word representation.
192
+
193
+ This function takes an integer and converts it to its Persian word representation.
194
+ It handles numbers up to 1,000,000,000,000,000 (one quadrillion).
195
+
196
+ Parameters:
197
+ number (int): The number to be converted.
198
+ depth (bool): A flag indicating if the function is called recursively.
199
+
200
+ Returns:
201
+ str: The Persian word representation of the number.
202
+
203
+ Raises:
204
+ OutOfRangeException: If the number is outside the supported range.
205
+
206
+ Example:
207
+ >>> print(_to_word(123, False))
208
+ یکصد و بیست و سه
209
+ """
153
210
  if number == 0:
154
211
  return ZERO if not depth else ""
155
212
 
@@ -169,6 +226,26 @@ def _to_word(number: int, depth: bool) -> str:
169
226
 
170
227
 
171
228
  def _floating_number_to_word(number: float, depth: bool) -> str:
229
+ """
230
+ Convert a floating-point number to its Persian word representation.
231
+
232
+ This function takes a floating-point number and converts it to its Persian word representation.
233
+ It handles floating-point numbers up to 14 decimal places.
234
+
235
+ Parameters:
236
+ number (float): The floating-point number to be converted.
237
+ depth (bool): A flag indicating if the function is called recursively.
238
+
239
+ Returns:
240
+ str: The Persian word representation of the floating-point number.
241
+
242
+ Raises:
243
+ OutOfRangeException: If the floating-point number has more than 14 decimal places.
244
+
245
+ Example:
246
+ >>> print(_floating_number_to_word(123.456, False))
247
+ یکصد و بیست و سه و چهارصد و پنجاه و شش هزارم
248
+ """
172
249
  left, right = str(abs(number)).split(".")
173
250
  if len(right) > 14:
174
251
  raise OutOfRangeException("You are allowed to use 14 digits for a floating point")
@@ -189,7 +266,32 @@ def _floating_number_to_word(number: float, depth: bool) -> str:
189
266
  return _to_word(int(left), False)
190
267
 
191
268
 
192
- def to_word(number: (float, int)) -> str:
269
+ def to_word(number: Tuple[float, int]) -> str:
270
+ """
271
+ Convert a number to its Persian word representation.
272
+
273
+ This function converts both integers and floating-point numbers to their
274
+ Persian word representations. It handles numbers up to 1 quadrillion (10^15)
275
+ for integers and up to 14 decimal places for floating-point numbers.
276
+
277
+ Parameters:
278
+ number (float or int): The number to be converted. It can be an integer or a floating-point number.
279
+
280
+ Returns:
281
+ str: The Persian word representation of the number.
282
+
283
+ Raises:
284
+ OutOfRangeException: If the number is greater than or equal to 1 quadrillion (10^15) or if the
285
+ floating-point number has more than 14 decimal places.
286
+ TypeError: If the input is not a float or an int.
287
+
288
+ Examples:
289
+ >>> digits.to_word(123)
290
+ 'یکصد و بیست و سه'
291
+
292
+ >>> digits.to_word(123.456)
293
+ 'یکصد و بیست و سه و چهارصد و پنجاه و شش هزارم'
294
+ """
193
295
  if isinstance(number, int):
194
296
  return _to_word(number, False)
195
297
  elif isinstance(number, float):