This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
update reference calculator example #529
Open
TharukaCkasthuri
wants to merge
8
commits into
main
Choose a base branch
from
ref_ex
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HI @TharukaCkasthuri, I think we should change up this example, I've coached chat gpt to create the following in python, can you take this base example and create the jac equivilant to replace the calendar example? from abc import ABC, abstractmethod
from functools import wraps
from typing import Callable, Any, Dict, List
# Custom Decorator
def log_decorator(func: Callable) -> Callable:
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
print(f"Calling {func.__name__} with {args} and {kwargs}")
result = func(*args, **kwargs)
print(f"{func.__name__} returned {result}")
return result
return wrapper
# Standalone function
@log_decorator
def standalone_function(book_title: str) -> str:
print(f"Standalone function: Adding book '{book_title}' to the library.")
return book_title
# Abstract Base Class
class LibraryItem(ABC):
@abstractmethod
def item_info(self) -> str:
pass
# Derived Class with overridden abstract method
class Book(LibraryItem):
def __init__(self, title: str, author: str) -> None:
self.title = title
self.author = author
def item_info(self) -> str:
return f"Book Title: {self.title}, Author: {self.author}"
# Static method
@staticmethod
def get_item_type() -> str:
return "Book"
# Class method
@classmethod
def create_from_dict(cls, info: Dict[str, str]) -> 'Book':
return cls(info['title'], info['author'])
# Class with instance method and custom decorator
class Library:
def __init__(self) -> None:
self.catalog: List[LibraryItem] = []
@log_decorator
def add_item(self, item: LibraryItem) -> None:
self.catalog.append(item)
print(f"Item '{item.item_info()}' added to the library catalog.")
def display_catalog(self) -> None:
for item in self.catalog:
print(item.item_info())
# Using the various functions and methods
if __name__ == "__main__":
# Using standalone function
book_title: str = standalone_function("Python Programming")
# Creating a Book instance using a class method
book_info: Dict[str, str] = {"title": "Python Programming", "author": "John Doe"}
book: Book = Book.create_from_dict(book_info)
# Using static method
print(f"Item Type: {Book.get_item_type()}")
# Creating a Library instance and adding a book to the catalog
library: Library = Library()
library.add_item(book)
# Displaying the catalog
library.display_catalog() |
marsninja
suggested changes
Aug 1, 2024
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.
see comment.
@marsninja I assume there is a bug in jaclang classmethod implementation, I implemented it in this way;
but it doesn't recognize cls. is there any other way to do that. I couldn't find any example. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.