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

feat: ImportExpr and ImportFromExpr #122

Merged
merged 24 commits into from
Oct 15, 2024
Merged

Conversation

apkrelling
Copy link
Contributor

@apkrelling apkrelling commented Oct 11, 2024

Pull Request description

This PR adds ImportExpr and ImportFromExpr.
This PR is an attempt to solve #88 .

  • added new ASTKind's
  • created ImportExpr and ImportFromExpr classes
  • updated __init__.py to include the new imports and classes in the __all__ list
  • added the new classes ad visit methods in transpilers/python.py
  • added tests in tests/transpilers/test_python.py
  • added tests in tests/test_packages.py

How to test these changes

from astx import ImportExpr, ImportFromExpr, AliasExpr
from astx.transpilers import python as astx2py

# ImportExpr
# Create alias expressions
alias1 = AliasExpr(name="pi")
alias2 = AliasExpr(name="sqrt", asname="square_root")

# Create an import expression
import_expr = ImportExpr([alias1])
import_expr.get_struct()

# Initialize the generator
generator = astx2py.ASTxPythonTranspiler()

# Generate Python code
generated_code = generator.visit(import_expr)

# print generated code
generated_code

# Create an import-from expression
import_from_expr = ImportFromExpr(module="math", names=[alias1, alias2])

import_from_expr.get_struct(simplified=True)

# Initialize the generator
generator = astx2py.ASTxPythonTranspiler()

# Generate Python code
generated_code = generator.visit(import_from_expr)

# print generated code
generated_code

Pull Request checklists

This PR is a:

  • bug-fix
  • new feature
  • maintenance

About this PR:

  • it includes tests.
  • the tests are executed on CI.
  • the tests generate log file(s) (path).
  • pre-commit hooks were executed locally.
  • this PR requires a project documentation update.

Author's checklist:

  • I have reviewed the changes and it contains no misspelling.
  • The code is well commented, especially in the parts that contain more
    complexity.
  • New and old tests passed locally.

Additional information

Reviewer's checklist

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

## Reviewer's Checklist

- [ ] I managed to reproduce the problem locally from the `main` branch
- [ ] I managed to test the new changes locally
- [ ] I confirm that the issues mentioned were fixed/resolved .

@apkrelling apkrelling marked this pull request as draft October 11, 2024 13:58
@apkrelling
Copy link
Contributor Author

The ascii AST viz has been fixed. now we have something like the outputs below:

PR3
PR1
PR2
PR4

Examples of how to test this:

from astx.packages import (
    AliasExpr,
    ImportFromStmt,
    ImportStmt,
    ImportExpr, 
    ImportFromExpr
)

# AliasExpr
alias1 = AliasExpr(name="pyplot", asname="plt")
alias1
alias1.get_struct()

# ImportStmt
alias1 = AliasExpr(name="math")
alias2 = AliasExpr(name="matplotlib", asname="mtlb")
# Create an import statement
import_stmt = ImportStmt(names=[alias1, alias2])
import_stmt
import_stmt.get_struct()

# ImportFromStmt
alias4 = AliasExpr(name="pyplot",asname="plt")
import_from_stmt = ImportFromStmt(module="matplotlib",names=[alias4], level=0)
import_from_stmt
import_from_stmt.get_struct()

# ImportFromStmt
alias2 = AliasExpr(name="matplotlib", asname="mtlb")
import_from_stmt = ImportFromStmt(names=[alias2], level=1)
import_from_stmt
import_from_stmt.get_struct()

# ImportExpr
alias1 = AliasExpr(name="sqrt", asname="square_root")
alias2 = AliasExpr(name="pi")
import_expr = ImportExpr([alias1, alias2])
import_expr
import_expr.get_struct()

# ImportFromExpr
alias1 = AliasExpr(name="pyplot", asname="plt")
import_from_expr = ImportFromExpr(module="matplotlib",names=[alias1])
import_from_expr
import_from_expr.get_struct()

I believe now it's good to go.

@apkrelling apkrelling marked this pull request as ready for review October 14, 2024 12:38
@apkrelling
Copy link
Contributor Author

apkrelling commented Oct 14, 2024

@xmnlab the problem with graphviz AST viz has been solved.
PR_graphviz

@xmnlab xmnlab merged commit 7785ba7 into arxlang:main Oct 15, 2024
12 checks passed
Copy link

🎉 This PR is included in version 0.16.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants