Skip to content

Commit 75e7f29

Browse files
authored
Remove unused/unnecessary functions in pyccel.parser.utilities (pyccel#1846)
Remove unused functions in `pyccel.parser.utilities`. In addition the function `read_file` is also removed. It is unnecessary to have a dedicated function for such a standard operation that can be written in 2 lines.
1 parent 4483af9 commit 75e7f29

File tree

3 files changed

+29
-83
lines changed

3 files changed

+29
-83
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ All notable changes to this project will be documented in this file.
7575
- \[INTERNALS\] Removed unused and undocumented function `get_function_from_ast`.
7676
- \[INTERNALS\] Remove unused parameters `expr`, `status` and `like` from `pyccel.ast.core.Assign`.
7777
- \[INTERNALS\] Remove `pyccel.ast.utilities.builtin_functions`.
78+
- \[INTERNALS\] Remove unused/unnecessary functions in `pyccel.parser.utilities` : `read_file`, `header_statement`, `accelerator_statement`, `get_module_name`, `view_tree`.
7879

7980
## \[1.11.2\] - 2024-03-05
8081

pyccel/parser/syntactic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666

6767
from pyccel.parser.base import BasicParser
6868
from pyccel.parser.extend_tree import extend_tree
69-
from pyccel.parser.utilities import read_file
7069
from pyccel.parser.utilities import get_default_path
7170

7271
from pyccel.parser.syntax.headers import parse as hdr_parse, types_meta
@@ -135,7 +134,8 @@ def __init__(self, inputs, **kwargs):
135134
# we don't use is_valid_filename_py since it uses absolute path
136135
# file extension
137136

138-
code = read_file(inputs)
137+
with open(inputs, 'r', encoding="utf-8") as file:
138+
code = file.read()
139139

140140
self._code = code
141141
self._context = []

pyccel/parser/utilities.py

Lines changed: 26 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
"""This file contains different utilities for the Parser."""
88
import os
99

10-
from sympy import srepr
11-
1210
from pyccel.ast.variable import DottedName
13-
from pyccel.parser.extend_tree import CommentLine
1411
from pyccel.ast.internals import PyccelSymbol
1512

16-
__all__ = ('pyccel_external_lib', 'read_file', 'is_valid_filename_py',
17-
'is_valid_filename_pyh', 'header_statement', 'accelerator_statement',
18-
'get_module_name', 'view_tree', 'get_default_path')
13+
__all__ = ('is_valid_filename_py',
14+
'is_valid_filename_pyh',
15+
'get_default_path',
16+
'pyccel_external_lib')
1917

2018
pyccel_external_lib = {"mpi4py" : "pyccel.stdlib.external.mpi4py",
2119
"scipy.linalg.lapack": "pyccel.stdlib.external.lapack",
@@ -26,13 +24,6 @@
2624

2725
#==============================================================================
2826

29-
def read_file(filename):
30-
"""Returns the source code from a filename."""
31-
f = open(filename)
32-
code = f.read()
33-
f.close()
34-
return code
35-
3627
#  ... checking the validity of the filenames, using absolute paths
3728
def _is_valid_filename(filename, ext):
3829
"""Returns True if filename has the extension ext and exists."""
@@ -55,76 +46,30 @@ def is_valid_filename_pyh(filename):
5546
return _is_valid_filename(filename, 'pyh')
5647
#  ...
5748

58-
#  ...
59-
def header_statement(stmt, accel):
60-
"""Returns stmt if a header statement. otherwise it returns None.
61-
this function can be used as the following
62-
>>> if header_statement(stmt):
63-
# do stuff
64-
...
65-
49+
def get_default_path(name):
6650
"""
67-
if not isinstance(stmt, CommentLine): return None
68-
if not stmt.value.startswith('#$'): return None
69-
70-
header = stmt.value[2:].lstrip()
71-
if not header.startswith('header'): return None
72-
73-
return stmt.value
74-
#  ...
75-
76-
# ... utilities for parsing OpenMP/OpenACC directives
77-
def accelerator_statement(stmt, accel):
78-
"""Returns stmt if an accelerator statement. otherwise it returns None.
79-
this function can be used as the following
80-
>>> if accelerator_statement(stmt, 'omp'):
81-
# do stuff
82-
...
83-
84-
In general you can use the functions omp_statement and acc_statement
51+
Get the full path to the Pyccel description of the imported library.
52+
53+
This function takes the name of an import source. If the imported library is in
54+
stdlib, it returns the full Python path to the stdlib equivalent library.
55+
Otherwise the original name is returned. This equivalent library should be a
56+
header file which describes all the functions which are supported by Pyccel.
57+
58+
Parameters
59+
----------
60+
name : PyccelSymbol | DottedName
61+
The name of the source file for the import.
62+
63+
Returns
64+
-------
65+
PyccelSymbol | DottedName
66+
The name of the Pyccel-compatible source file for the import.
8567
"""
86-
assert(accel in ['omp', 'acc'])
87-
88-
if not isinstance(stmt, CommentLine): return None
89-
if not stmt.value.startswith('#$'): return None
90-
91-
directive = stmt.value[2:].lstrip()
92-
if not directive.startswith(accel): return None
93-
94-
return stmt.value
95-
96-
omp_statement = lambda x: accelerator_statement(x, 'omp')
97-
acc_statement = lambda x: accelerator_statement(x, 'acc')
98-
# ...
99-
100-
def get_module_name( dotted_as_node ):
101-
code_name = dotted_as_node.target
102-
if (code_name != ""):
103-
return [ code_name ]
68+
name_ = str(name)
69+
name = pyccel_external_lib.get(name_, name_).split('.')
70+
if len(name)>1:
71+
return DottedName(*name)
10472
else:
105-
import_name = dotted_as_node.value
106-
return import_name.dumps().split('.')
107-
108-
109-
#  ... utilities
110-
def view_tree(expr):
111-
"""Views a sympy expression tree."""
112-
print (srepr(expr))
113-
#  ...
114-
115-
def get_default_path(name):
116-
"""this function takes a an import name
117-
and returns the path full bash of the library
118-
if the library is in stdlib"""
119-
name_ = name
120-
if isinstance(name, (DottedName, PyccelSymbol)):
121-
name_ = str(name)
122-
if name_ in pyccel_external_lib.keys():
123-
name = pyccel_external_lib[name_].split('.')
124-
if len(name)>1:
125-
return DottedName(*name)
126-
else:
127-
return name[0]
128-
return name
73+
return name[0]
12974

13075

0 commit comments

Comments
 (0)