-
Notifications
You must be signed in to change notification settings - Fork 25
Add dict comprehension support #230
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
Conversation
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
1a93cf2
to
eead3fd
Compare
@Zehen-249 , thanks for working on that. please take a look into the errors in the CI pls |
eead3fd
to
47c8eb6
Compare
|
808526d
to
3c39c80
Compare
Hey @xmnlab can you review this PR now |
Heyy @Zehen-249 , Can you please remove the file |
libs/astx/src/astx/flows.py
Outdated
), | ||
} | ||
|
||
key = "DICT-COMPREHENSION" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add unique id to the key here for better representation.
3c39c80
to
2f2ff19
Compare
Added unique id for the key. |
@Zehen-249 , you can call your test function(suppose |
if I want to call repr function I must have defined the repr function, which I haven't. Also in the ASTx site it is mention how to implement str and get_structs method but nothing for repr method. I ran grep on the src code of astx to find implementation of repr for classes and no class implements this method. I also looked into some previously merged PRs no one has did anything like this . |
), | ||
], | ||
) | ||
generated_code = transpiler.visit(dict_comp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of using transpiler.visit, please use translate
(e.g. https://github.com/arxlang/astx/pull/232/files#diff-213fa69c2a6fc65240961486159b29cc98cffaab2968b96afbae37a47abc4624R52)
it uses the translate.visit + a function to check if the syntax is correct
key: Expr | ||
value: Expr | ||
|
||
def __init__( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi @Zehen-249
please consider the following structure from python:
"Module(body=[Expr(value=DictComp(key=Name(id='x', ctx=Load()), value=Name(id='x', ctx=Load()), generators=[comprehension(target=Name(id='x', ctx=Store()), iter=Call(func=Name(id='range', ctx=Load()), args=[Constant(value=10)], keywords=[]), ifs=[], is_async=0)]))], type_ignores=[])"
>>>
as you can see it just has the attributes: key, value, and generators.
so please update the class attributes and the arguments in __init__
in the attribute generators
it will be a list of Comprehension.
let me know if you have any questions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if generator is to be used the python syntax will be like
{x : x * x for x in ( ( x + x ) for x in [ 1, 2, 3, 4, 5, 6 ] if ( ( x % 2 ) == 0 ) ) }
which has generator ( ( x + x ) for x in [ 1, 2, 3, 4, 5, 6 ] if ( ( x % 2 ) == 0 ) ) but AStx does not have generator expr class implemented yet and using that will be more convincing i guess ???
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I am on right understanding ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @Zehen-249 , you should use the Comprehension class, instead of the Generator.
actually Generator should use comprehension as well. https://github.com/arxlang/astx/pull/232/files#diff-3ff6d1ecb86cdf40792ce4c94a5fc72d948eb8d389fb79dfce984b62819b9b39R748-R750
"Module(body=[Expr(value=GeneratorExp(elt=Name(id='x', ctx=Load()), generators=[comprehension(target=Name(id='x', ctx=Store()), iter=Call(func=Name(id='range', ctx=Load()), args=[Constant(value=10)], keywords=[]), ifs=[], is_async=0)]))], type_ignores=[])"
>>>
could you open a new PR to fix that pls?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
image Representaion
ASCIII Representation
String representation
DictComprehension[key=Variable[x], value=BinaryOp[+](Variable[x],Variable[x]), target=Variable[x], iterable=Variable[range(10)], conditions=['BinaryOp[>](Variable[x],LiteralInt32(5))', 'BinaryOp[<](Variable[x],LiteralInt32(7))'], is_async=False]
Trasnpiled Python Code
{x: (x + x) for x in range(10) if (x > 5) if (x < 7)}
2f2ff19
to
1614073
Compare
1614073
to
53fc9ec
Compare
hey @xmnlab, |
@Zehen-249 I just merged this PR: #245 let me know if it clear now. sorry for the confusion. |
Hey @xmnlab in the dictcompehension
How one can set the target variable to be key, value. One can use LiteralString maybe but thats not logically correct?? |
I tried using LiteralTuple for target but the problem is the elements of the LiteralTuple must be Literal types and so i can not use Variable or identifiers |
hi @Zehen-249 , I always use python ast as a reference: >>> import ast
>>> ast.dump(ast.parse("{key:value for key, value in [(a,1),(b,2),(c,3)]}"))
"Module(body=[Expr(value=DictComp(key=Name(id='key', ctx=Load()), value=Name(id='value', ctx=Load()), generators=[comprehension(target=Tuple(elts=[Name(id='key', ctx=Store()), Name(id='value', ctx=Store())], ctx=Store()), iter=List(elts=[Tuple(elts=[Name(id='a', ctx=Load()), Constant(value=1)], ctx=Load()), Tuple(elts=[Name(id='b', ctx=Load()), Constant(value=2)], ctx=Load()), Tuple(elts=[Name(id='c', ctx=Load()), Constant(value=3)], ctx=Load())], ctx=Load()), ifs=[], is_async=0)]))], type_ignores=[])"
|
let me know if that makes sense, pls |
Yeah I did this, but the same can't be replicated in ASTx bcoz, x,y is taken as tuple of Name type element and in ast it is allowed for Tuple node to store Name types but in ASTx implementation we only only have LiteralTuple in which we can only store Literal types, and Variable (which is counter for Name in ast i suppose) is not a Literal Type Both Literal and Variable inherit from DataTypeOb |
No, variables in Python are not subclasses of Literal. The Literal type in typing is used to annotate functions or values that are restricted to specific constant values — not to represent actual variable types. |
Sorry I missed your point. That is right, the current implementation is not correct. Good catch. If you want to work on that, please go ahead. Otherwise I will fix that soon. For now, probably it should be Expr. It seems that other collection classes are wrong. |
Hey @xmnlab, I would be happy to contribute. For now I believe changing the datatypes from Literals to DataTypeObs would do the job. I will do some more research and will raise an issue. |
thanks @Zehen-249 ! |
This pull request has been marked as stale because it has been |
This pull request has been marked as stale because it has been |
Pull Request description
Add Dictionary Comprehension Support
Solve #198
How to test these changes
Check Tutorials folder there is a notebook file for dict_comprehension
Pull Request checklists
Note:
proposed in this PR, in both image and ASCII formats. For more
information, check this Google Colab notebook:
https://colab.research.google.com/drive/1xXwHmOMkJKFSmhRvn4WYfSAsdDzMnawf?usp=sharing
This PR is a:
About this PR:
Author's checklist:
complexity.
Additional information
Dict Comprehension object

String Representation
{Variable[x]: BinaryOp[*](Variable[x],Variable[x]) for Variable[x] in Variable[range(10)] if BinaryOp[>](Variable[x],LiteralInt32(5)) if BinaryOp[<](Variable[x],LiteralInt32(7))}
Image Representation

Reviewer's checklist
Copy and paste this template for your review's note: