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

[DICT] Add Python support for reversed #1890

Open
Tracked by #1570
EmilyBourne opened this issue May 16, 2024 · 4 comments
Open
Tracked by #1570

[DICT] Add Python support for reversed #1890

EmilyBourne opened this issue May 16, 2024 · 4 comments
Labels
Containers tuples/lists/sets/maps enhancement

Comments

@EmilyBourne
Copy link
Member

Describe the feature

Syntactic and semantic support should be added for reversed. This should be tested by implementing Python printing

Test Code

Provide code which does not currently work but which should do when this issue is fixed:

def foo():
    a = {1,1.0, 2:2.0}
    b : int[int]
    for bi in reversed(a):
        b.append(bi)
    return a, b
@EmilyBourne EmilyBourne mentioned this issue Dec 25, 2023
20 tasks
@shoaib-moeen
Copy link

Hi @EmilyBourne , I can start working on this. Could you give me a sample output for the above code?

@EmilyBourne
Copy link
Member Author

Hi @shoaib-moeen
For the moment this issue is just about translating Python to Python. We do this as a first step to get the syntactic and semantic stages working before printing in C or Fortran. For the Python support all the issues from #1880-#1890 should be somewhat similar. There is already an open PR for #1886, see #1891. You may want to fork that branch to avoid conflicts/repeat code.
There are also several examples of similar merged PRs for lists and sets. Eg #1798

Just to warn you though. A PR related to this issue is likely to stay open for a few weeks as it will be dependent on my PRs and the person who merges my PRs is not available at the moment

@EmilyBourne
Copy link
Member Author

Hello again @shoaib-moeen , I've had another think about this, this morning. I forget that reversed is a little special compared to other dictionary methods because it is an iterator. The other dictionary methods should be a bit simpler to handle. For reversed you will need to define it similarly to pyccel.ast.itertools.Product. Specifically the equivalent to the following methods will need to be implemented:

def __getitem__(self, indices):
return [elem[idx] for idx, elem in zip(indices, self.elements)]
def to_range(self):
""" Get the range iterator(s) needed to access all elements of Product
"""
lengths = [getattr(e, '__len__',
getattr(e, 'length', PythonLen(e))) for e in self.elements]
lengths = [l() if callable(l) else l for l in lengths]
return [PythonRange(l) for l in lengths]
@property
def n_indices(self):
""" Number of indices required
"""
return len(self._elements)

But I'm not sure how the to_range method should be implemented here.

Soon we will add the C implementation. It will be based on STC (https://github.com/stclib/STC/blob/master/docs/smap_api.md). I think the answer about how to implement this is probably related to how this would be written in C but I haven't found anything about reversing the order of things in the docs so far.

@shoaib-moeen
Copy link

Thanks @EmilyBourne. I ll look into other issues. Maybe start working on #1887

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Containers tuples/lists/sets/maps enhancement
Projects
None yet
Development

No branches or pull requests

2 participants