Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List support: pop method #1710

Merged
merged 62 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
2fcb2de
fix documentation : correcting wrapper stage link in overview.md
mustapha-belbiad Jan 19, 2024
5975f85
Revert "fix documentation : correcting wrapper stage link in overview…
mustapha-belbiad Jan 19, 2024
9e8be09
getting last devel updates into my branch
Farouk-Echaref Jan 26, 2024
454d0cb
Example of Lists Methods
mustapha-belbiad Jan 26, 2024
82bffa9
feat: List class_def structure & List Methods structre
Farouk-Echaref Jan 26, 2024
a23c504
Merge branch 'devel' into feat/list_support_in_Pyccel
Farouk-Echaref Jan 29, 2024
81927c7
for merge
mustapha-belbiad Jan 30, 2024
bd3b880
Merge branch 'devel' of https://github.com/pyccel/pyccel into devel
mustapha-belbiad Jan 30, 2024
752554c
Merge branch 'devel' into List_Methods
mustapha-belbiad Jan 30, 2024
8ebf4c1
feat: list classdef structure
Farouk-Echaref Jan 30, 2024
4d69c1a
feat: PythonList in type inference
Farouk-Echaref Jan 30, 2024
e270e92
start append class doc
Farouk-Echaref Jan 30, 2024
ea42345
check on List type in classdef
Farouk-Echaref Jan 31, 2024
3e5c3f4
add pop method for list
mustapha-belbiad Feb 2, 2024
e3e6c66
Merge branch 'devel' of https://github.com/pyccel/pyccel into devel
mustapha-belbiad Feb 2, 2024
1f9f8a3
Merge branch 'devel' into List_Methods
mustapha-belbiad Feb 2, 2024
3c967b4
delete test files
mustapha-belbiad Feb 2, 2024
8559b9c
Fix issues from merge step
mustapha-belbiad Feb 2, 2024
531b565
Fix some issues
mustapha-belbiad Feb 2, 2024
4f1ea19
Fix some issues
mustapha-belbiad Feb 2, 2024
215940c
Fix some issues
mustapha-belbiad Feb 2, 2024
9369e06
Fix some issues
mustapha-belbiad Feb 2, 2024
9fd1dd2
Add tests to check the pop method
mustapha-belbiad Feb 2, 2024
bdcb2b0
Add tests to check the pop method
mustapha-belbiad Feb 2, 2024
6c2a947
Merge branch 'List_Methods' of https://github.com/pyccel/pyccel into …
mustapha-belbiad Feb 5, 2024
8adafb4
fixed some minor issues in the printer
mustapha-belbiad Feb 5, 2024
74886f7
fix condition to get the base class of an object
mustapha-belbiad Feb 5, 2024
f37199f
Update pyccel/codegen/printing/pycode.py
mustapha-belbiad Feb 6, 2024
39a483c
new changes for the pop method
mustapha-belbiad Feb 9, 2024
371681c
Merge branch 'devel' of https://github.com/pyccel/pyccel into devel
mustapha-belbiad Feb 9, 2024
7d91c04
Merge branch 'devel' into List_Methods
mustapha-belbiad Feb 9, 2024
928e9e6
chore: add ddescription to this file
mustapha-belbiad Feb 9, 2024
848a7c2
chore: Remove unnecessary changes in these files
mustapha-belbiad Feb 9, 2024
db4132f
chore: revert file permissions
mustapha-belbiad Feb 9, 2024
ceff486
chore: Fix the name of the attributes in the pop class.
mustapha-belbiad Feb 9, 2024
a147d42
chore: fixe Documentation
mustapha-belbiad Feb 9, 2024
d65f532
chore: fix documentation
mustapha-belbiad Feb 9, 2024
540283d
Add quote
mustapha-belbiad Feb 12, 2024
8d649bf
Remove parentheses
mustapha-belbiad Feb 12, 2024
61433ec
Modify the names in the list class
mustapha-belbiad Feb 12, 2024
7bd0193
Remove the condition because it is handled by 'elsif isinstance(expr,…
mustapha-belbiad Feb 12, 2024
8e921a7
Save the results to avoid repeating calls in the test functions
mustapha-belbiad Feb 12, 2024
07ed125
Remove unused-import
mustapha-belbiad Feb 12, 2024
7c3d396
correct documentation
mustapha-belbiad Feb 12, 2024
8ce387e
Merge branch 'devel' into List_Methods
EmilyBourne Feb 12, 2024
b90e8f4
Merge branch 'devel' into List_Methods
EmilyBourne Feb 12, 2024
2a2e6c2
Merge branch 'devel' of https://github.com/pyccel/pyccel into devel
mustapha-belbiad Feb 12, 2024
8987845
change name to List_variable
mustapha-belbiad Feb 13, 2024
0f683b7
Merge branch 'devel' into List_Methods
mustapha-belbiad Feb 13, 2024
e02a491
correct documentation
mustapha-belbiad Feb 13, 2024
ba53aab
Corrected from last_variable to list_variable
mustapha-belbiad Feb 13, 2024
b3ff23f
Merge branch 'devel' into List_Methods
EmilyBourne Feb 13, 2024
30aff5c
Merge branch 'devel' into List_Methods
mustapha-belbiad Feb 13, 2024
9f8e6d7
set _attribute_nodes for the ListPop class and delete passing args to…
mustapha-belbiad Feb 13, 2024
54d0846
add brackets to super call
mustapha-belbiad Feb 13, 2024
cff5c6a
Merge branch 'devel' of https://github.com/pyccel/pyccel into devel
mustapha-belbiad Feb 14, 2024
fcc8169
Merge branch 'devel' into List_Methods
mustapha-belbiad Feb 14, 2024
762af71
include the title of the issue in the unreleased section
mustapha-belbiad Feb 14, 2024
ebb7c4e
Merge branch 'devel' of https://github.com/pyccel/pyccel into devel
mustapha-belbiad Feb 15, 2024
91e2135
Merge branch 'devel' into List_Methods
mustapha-belbiad Feb 15, 2024
a018f3a
Merge branch 'devel' of https://github.com/pyccel/pyccel into devel
mustapha-belbiad Feb 15, 2024
be18da4
Merge branch 'devel' into List_Methods
mustapha-belbiad Feb 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ Contributors
* Mudit Loya
* Priyabrata Mondal
* Farouk Ech-Charef
* Mustapha Belbiad
yguclu marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ All notable changes to this project will be documented in this file.
- #1605 : Add support for methods and interfaces in classes (including `__init__` and `__del__`).
- #1618 : Add support for class instance attributes.
- #1680 : Add support for `typing.Final`.
- #1690 : Add support for `pop()` method of list class
EmilyBourne marked this conversation as resolved.
Show resolved Hide resolved
- Add a `--time_execution` flag to allow detailed investigation of critical sections of code.
- #1659 : Add multi-file support for classes.
- #1708 : Allow returning pointers to arguments from functions.
Expand Down
63 changes: 63 additions & 0 deletions pyccel/ast/builtin_objects/list_functions.py
yguclu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# coding: utf-8
#------------------------------------------------------------------------------------------#
# This file is part of Pyccel which is released under MIT License. See the LICENSE file or #
# go to https://github.com/pyccel/pyccel/blob/master/LICENSE for full license details. #
#------------------------------------------------------------------------------------------#

"""
The List container has a number of built-in methods that are
always available.

This module contains objects which describe these methods within Pyccel's AST.
"""

from pyccel.ast.internals import PyccelInternalFunction
from pyccel.ast.datatypes import NativeHomogeneousList

class ListPop(PyccelInternalFunction) :
"""
Represents a call to the .pop() method.

Represents a call to the .pop() method which
removes the item at the specified index.
The method also returns the removed item.

Parameters
----------
list_variable : TypedAstNode
The name of the list.

index_element : TypedAstNode
The current index value for the element to be popped.
"""
__slots__ = ('_dtype','_precision', '_index','_list_variable')
_attribute_nodes = ('_index','_list_variable')
_rank = 0
_order = None
_shape = None
_class_type = NativeHomogeneousList()
name = 'pop'
def __init__(self, list_variable, index_element=None):
yguclu marked this conversation as resolved.
Show resolved Hide resolved
self._index = index_element
self._list_variable = list_variable
self._dtype = list_variable.dtype
self._precision = list_variable.precision
super().__init__()

@property
def pop_index(self):
"""
The current index value for the element to be popped.

The current index value for the element to be popped.
"""
return self._index

@property
def list_variable(self):
"""
Provide the name of the list as the return value.

Provide the name of the list as the return value.
"""
return self._list_variable
19 changes: 16 additions & 3 deletions pyccel/ast/class_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,29 @@
"""
This module contains all types which define a python class which is automatically recognised by pyccel
"""

from pyccel.ast.builtin_objects.list_functions import ListPop
from .builtins import PythonImag, PythonReal, PythonConjugate
from .core import ClassDef, PyccelFunctionDef
from .c_concepts import CStackArray
from .datatypes import (NativeBool, NativeInteger, NativeFloat,
NativeComplex, NativeString, NativeNumeric,
NativeTuple, CustomDataType)
NativeComplex, NativeString, NativeNumericTypes,
NativeTuple, CustomDataType, NativeHomogeneousList)
from .numpyext import (NumpyShape, NumpySum, NumpyAmin, NumpyAmax,
NumpyImag, NumpyReal, NumpyTranspose,
NumpyConjugate, NumpySize, NumpyResultType,
NumpyArray, NumpyNDArrayType)

__all__ = ('BooleanClass',
'IntegerClass',
'FloatClass',
'ComplexClass',
'StringClass',
'NumpyArrayClass',
'TupleClass',
'ListClass',
'literal_classes',
'get_cls_base')

Check warning on line 30 in pyccel/ast/class_defs.py

View check run for this annotation

Pyccel Bot / Pyccel best practices (pyccel_lint, 3.8)

pyccel/ast/class_defs.py#L21-L30

Sort the __all__ attribute of `class_defs`

#=======================================================================================

Expand Down Expand Up @@ -130,6 +133,14 @@

#=======================================================================================

ListClass = ClassDef('list', class_type = NativeHomogeneousList(),
methods=[
PyccelFunctionDef('pop', func_class = ListPop,
decorators = {}),
])

#=======================================================================================

TupleClass = ClassDef('tuple', class_type = NativeTuple(),
methods=[
#index
Expand Down Expand Up @@ -217,10 +228,12 @@
return None
if precision in (-1, 0, None) and container_type is dtype:
return literal_classes[dtype]
elif dtype in NativeNumeric or isinstance(container_type, NumpyNDArrayType):
elif isinstance(container_type, (*NativeNumericTypes, NumpyNDArrayType)):
return NumpyArrayClass
elif isinstance(container_type, NativeTuple):
return TupleClass
elif isinstance(container_type, NativeHomogeneousList):
return ListClass
else:
if container_type:
type_name = str(container_type)
Expand Down
7 changes: 7 additions & 0 deletions pyccel/codegen/printing/pycode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,13 @@ def _print_TypingFinal(self, expr):
annotation = self._print(expr.arg)
return f'const {annotation}'

def _print_ListPop(self, expr):
args = ""
if expr.pop_index:
args = self._print(expr.pop_index)
yguclu marked this conversation as resolved.
Show resolved Hide resolved
name = self._print(expr.list_variable)
return f"{name}.pop({args})"

#==============================================================================
def pycode(expr, assign_to=None, **settings):
""" Converts an expr to a string of Python code
Expand Down
92 changes: 92 additions & 0 deletions tests/epyccel/test_epyccel_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# pylint: disable=missing-function-docstring, missing-module-docstring
import pytest
from pyccel.epyccel import epyccel

@pytest.mark.parametrize( 'language', (
pytest.param("fortran", marks = [
pytest.mark.xfail(reason="Function in function not implemented in fortran"),
pytest.mark.fortran]),
pytest.param("c", marks = [
pytest.mark.xfail(reason="Function in function not implemented in C"),
pytest.mark.c]
),
pytest.param("python", marks = pytest.mark.python)
)
)

def test_pop_last_element(language) :
def pop_last_element():
a = [1,3,45]
return a.pop()
epyc_last_element = epyccel(pop_last_element, language = language)
pyccel_result = epyc_last_element()
python_result = pop_last_element()
assert isinstance(python_result, type(pyccel_result))
assert python_result == pyccel_result

@pytest.mark.parametrize( 'language', (
pytest.param("fortran", marks = [
pytest.mark.xfail(reason="method pop for list not implemented in fortran"),
pytest.mark.fortran]),
pytest.param("c", marks = [
pytest.mark.xfail(reason="method pop for list not implemented in C"),
pytest.mark.c]
),
pytest.param("python", marks = pytest.mark.python)
)
)

def test_pop_specific_index(language) :
def pop_specific_index():
a = [1j,3j,45j]
return a.pop(1)
epyc_specific_index = epyccel(pop_specific_index, language = language)
python_result = pop_specific_index()
pyccel_result = epyc_specific_index()
assert isinstance(python_result, type(pyccel_result))
assert python_result == pyccel_result

@pytest.mark.parametrize( 'language', (
pytest.param("fortran", marks = [
pytest.mark.xfail(reason="method pop for list not implemented in fortran"),
pytest.mark.fortran]),
pytest.param("c", marks = [
pytest.mark.xfail(reason="method pop for list not implemented in C"),
pytest.mark.c]
),
pytest.param("python", marks = pytest.mark.python)
)
)

def test_pop_negative_index(language) :
def pop_negative_index():
a = [1j,3j,45j]
return a.pop(-1)
epyc_negative_index = epyccel(pop_negative_index, language = language)
python_result = pop_negative_index()
pyccel_result = epyc_negative_index()
assert isinstance(python_result, type(pyccel_result))
assert python_result == pyccel_result

@pytest.mark.parametrize( 'language', (
pytest.param("fortran", marks = [
pytest.mark.xfail(reason="method pop for list not implemented in fortran"),
pytest.mark.fortran]),
pytest.param("c", marks = [
pytest.mark.xfail(reason="method pop for list not implemented in C"),
pytest.mark.c]
),
pytest.param("python", marks = pytest.mark.python)
)
)

def test_pop_2(language) :
def pop_2():
a = [1.7,2.7,45.0]
a.pop()
return a.pop(-1)
pop_2_epyc = epyccel(pop_2, language = language)
python_result = pop_2()
pyccel_result = pop_2_epyc()
assert isinstance(python_result, type(pyccel_result))
assert python_result == pyccel_result
Loading