types-remover 1.0.0__py3-none-any.whl → 1.0.2__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.
types_remover/__init__.py CHANGED
@@ -1,6 +1,32 @@
1
+ from typing import Optional, Literal
2
+
1
3
  from ._types_remover_ast import remove_types_ast as remove_types_ast
2
4
  from ._types_remover_if import remove_types as remove_types_if
3
5
 
4
6
 
5
- __all__ = ['remove_types_if', 'remove_types_ast']
7
+ __all__ = ['remove_types','remove_types_if', 'remove_types_ast']
6
8
  __author__ = 'EasternFarmer'
9
+
10
+
11
+ def remove_types(
12
+ path: str,
13
+ *,
14
+ implementation: Literal['ast', 'if'] = 'ast',
15
+ return_str: bool = False,
16
+ output_file_path: Optional[str] = None
17
+ ) -> Optional[str]:
18
+
19
+ if implementation == 'ast':
20
+ return remove_types_ast(
21
+ path,
22
+ return_str = return_str,
23
+ output_file_path = output_file_path
24
+ )
25
+ elif implementation == 'if':
26
+ return remove_types_if(
27
+ path,
28
+ return_str = return_str,
29
+ output_file_path = output_file_path
30
+ )
31
+ else:
32
+ raise ValueError('Invalid implementation specified.')
@@ -1,24 +1,26 @@
1
1
  import ast
2
+ from typing import Optional
2
3
 
3
4
 
4
5
  class _DowngradeAnnAssign(ast.NodeTransformer):
5
6
  """Converts AnnAssign nodes (annotated assignment) to simpler Assign nodes."""
6
7
 
7
- def visit_AnnAssign(self, node: ast.AnnAssign) -> ast.Assign | None:
8
+ def visit_AnnAssign(self, node: ast.AnnAssign) -> Optional[ast.Assign]:
8
9
  if node.value is not None:
9
10
  return ast.Assign(targets = [node.target], value = node.value)
10
11
  return None # just so mypy can leave me alone
11
12
 
12
13
 
13
- def remove_types_ast(path: str, *, return_str: bool = False, output_file_path: str = 'output.py') -> None | str:
14
+ def remove_types_ast(path: str, *, return_str: bool = False, output_file_path: Optional[str] = None) -> Optional[str]:
14
15
  """
15
- Strips the types declared in the file at **path**
16
- Either one of return_str or output_file_path must be set
16
+ Strips the types declared in the file at **path** and outputs it to output_file_path
17
17
  :param path: Required parameter deciding what file to strip from types
18
- :param return_str: dictates if the function returns a str; can be mixed with output_file_path param
19
- :param output_file_path: dictates where the function saves the data; can be mixed with return_str param
18
+ :param return_str: dictates if the function returns a str
19
+ :param output_file_path: dictates where the function saves the data
20
20
  :return: str if return_str is True else None
21
21
  """
22
+ if output_file_path is None:
23
+ output_file_path = 'output.py'
22
24
 
23
25
  with open(path) as f:
24
26
  nodes = ast.parse(f.read())
@@ -35,8 +37,8 @@ def remove_types_ast(path: str, *, return_str: bool = False, output_file_path: s
35
37
  node.returns = None
36
38
  nodes = ast.fix_missing_locations(_DowngradeAnnAssign().visit(nodes))
37
39
 
38
- if return_str:
39
- return ast.unparse(nodes)
40
40
  with open(output_file_path, 'w') as f:
41
41
  f.writelines(ast.unparse(nodes))
42
+ if return_str:
43
+ return ast.unparse(nodes)
42
44
  return None # just so mypy can leave me alone
@@ -1,4 +1,5 @@
1
1
  import re
2
+ from typing import Optional
2
3
 
3
4
 
4
5
  def _parse_line(line: str) -> str | bool:
@@ -36,7 +37,17 @@ def _parse_def_statement(line: str) -> str:
36
37
  return f'{' ' * spaces}def {function_name}({', '.join(args2)}):'
37
38
 
38
39
 
39
- def remove_types(path: str, *, return_str: bool = False, output_file: str = 'output.py') -> None | str:
40
+ def remove_types(path: str, *, return_str: bool = False, output_file_path: Optional[str] = None) -> Optional[str]:
41
+ """
42
+ Strips the types declared in the file at **path** and outputs it to output_file_path
43
+ :param path: Required parameter deciding what file to strip from types
44
+ :param return_str: dictates if the function returns a str
45
+ :param output_file_path: dictates where the function saves the data. defaults to 'output.py'
46
+ :return: str if return_str is True else None
47
+ """
48
+ if output_file_path is None:
49
+ output_file_path = 'output.py'
50
+
40
51
  with open(path) as f:
41
52
  file = [line.rstrip('\n') for line in f.readlines()]
42
53
 
@@ -140,8 +151,8 @@ def remove_types(path: str, *, return_str: bool = False, output_file: str = 'out
140
151
  else: new_file.append(' ' * (len(tuple_line) - len(tuple_line.lstrip())) + ''.join(tuple_line.split()))
141
152
  tuple_line = ''
142
153
 
154
+ with open(output_file_path, 'w') as f:
155
+ f.writelines('\n'.join(new_file))
143
156
  if return_str:
144
157
  return '\n'.join(new_file)
145
- with open(output_file, 'w') as f:
146
- f.writelines('\n'.join(new_file))
147
158
  return None
types_remover/cli.py ADDED
@@ -0,0 +1,25 @@
1
+ """
2
+ The command-line interface for the ast version of the types remover
3
+ """
4
+ import argparse
5
+ from . import remove_types_ast
6
+
7
+
8
+ def main():
9
+ parser = argparse.ArgumentParser(
10
+ description="types remover"
11
+ )
12
+ parser.add_argument(
13
+ "path", type=str,
14
+ help="Path to a file that\' going to be stripped "
15
+ )
16
+ parser.add_argument(
17
+ "--output", "-o",
18
+ help="name of the output file. defaults to output.py"
19
+ )
20
+ args = parser.parse_args()
21
+ remove_types_ast(args.path, output_file_path=args.output)
22
+ print("types stripped successfully")
23
+
24
+ if __name__ == "__main__":
25
+ main()
@@ -1,17 +1,15 @@
1
- Metadata-Version: 2.4
1
+ Metadata-Version: 2.2
2
2
  Name: types_remover
3
- Version: 1.0.0
3
+ Version: 1.0.2
4
4
  Summary: A small package that removes types from a file
5
+ Author-email: EasternFarmer <franek.banas1@gmail.com>
5
6
  Project-URL: Homepage, https://github.com/EasternFarmer/types_remover
6
7
  Project-URL: Issues, https://github.com/EasternFarmer/types_remover/issues
7
- Author-email: EasternFarmer <franek.banas1@gmail.com>
8
- License-Expression: GPL-3.0-or-later
9
- License-File: LICENSE
10
- Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
11
- Classifier: Operating System :: OS Independent
12
8
  Classifier: Programming Language :: Python :: 3
9
+ Classifier: Operating System :: OS Independent
13
10
  Requires-Python: >=3.12
14
11
  Description-Content-Type: text/markdown
12
+ License-File: LICENSE
15
13
 
16
14
  # Types remover(s)
17
15
 
@@ -19,12 +17,12 @@ Description-Content-Type: text/markdown
19
17
 
20
18
 
21
19
  This packet has two available functions `remove_types_ast` and `remove_types_if`, that are doing almost the same thing
22
- with implementation. <br>
20
+ with different implementation. And one cmd command `remove-types` that uses argparse and `remove_types_ast`<br>
23
21
 
24
22
  From the tests I can tell you that `remove_types_ast` is on average 3,28 (average from 100 calls) times slower than
25
23
  `remove_types_if`
26
24
 
27
25
  ### IMPORTANT TO NOTE
28
26
 
29
- - The `remove_types_ast` function removes comments (not doc-strings)
30
- - Both functions turn multi-line data assignation into a one-line statement
27
+ - The `remove_types_ast` function and `remove-types` cmd command both remove comments (not doc-strings)
28
+ - Both functions turn multi-line data assignation into a one-line statement
@@ -0,0 +1,10 @@
1
+ types_remover/__init__.py,sha256=pjDIOIMn24mMYFYPMYFvQAJLdv9a9VovpPHqBFdvcAQ,891
2
+ types_remover/_types_remover_ast.py,sha256=OeBhPnoWwy06a43TumXLYx3ncVdk1P1f7hJdCmwdaRw,1650
3
+ types_remover/_types_remover_if.py,sha256=DLeQeVmclgPHm9NR0K21oPWTMLTGRQAa5nzlsQC0GYM,7562
4
+ types_remover/cli.py,sha256=Yackgc-XYvZ3pqv72Uj8z3K9CLQcmc8HGsx-xdQrEBM,624
5
+ types_remover-1.0.2.dist-info/LICENSE,sha256=ixuiBLtpoK3iv89l7ylKkg9rs2GzF9ukPH7ynZYzK5s,35148
6
+ types_remover-1.0.2.dist-info/METADATA,sha256=wNL1z3vdCoj86Uuusu_T52e7ad9dI_mwSvw2FcWEG38,1249
7
+ types_remover-1.0.2.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
8
+ types_remover-1.0.2.dist-info/entry_points.txt,sha256=tx3iH_DisPcRShwLxQYCdPh-MvuzzMqjsk16238Dwrk,56
9
+ types_remover-1.0.2.dist-info/top_level.txt,sha256=qG2PHosubBxZs6X2olkvKfJovD2lIDP670kEe1bVRMw,14
10
+ types_remover-1.0.2.dist-info/RECORD,,
@@ -1,4 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.27.0
2
+ Generator: setuptools (75.8.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ remove-types = types_remover.cli:main
@@ -0,0 +1 @@
1
+ types_remover
@@ -1,7 +0,0 @@
1
- types_remover/__init__.py,sha256=9rGAkBksywski5kmgbrPPvXS2Z9QrPoW90Kp1cc3Bv0,213
2
- types_remover/_types_remover_ast.py,sha256=Qe7puRK_lvTr0D9_COaW9Dub2_mHvVkNdpnIwLOi6l8,1652
3
- types_remover/_types_remover_if.py,sha256=m11y3dppo-KyTBIf_v4qtJoQyrhw6Va7NvcewSYdZ9o,7037
4
- types_remover-1.0.0.dist-info/METADATA,sha256=UU36FTgq9Ge95bmzopuHVFy-doMIgXI5g5WK5SxNAYw,1251
5
- types_remover-1.0.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
- types_remover-1.0.0.dist-info/licenses/LICENSE,sha256=ixuiBLtpoK3iv89l7ylKkg9rs2GzF9ukPH7ynZYzK5s,35148
7
- types_remover-1.0.0.dist-info/RECORD,,