-
-
Notifications
You must be signed in to change notification settings - Fork 15
str and repr implementation for the py5 class. #251
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
Thank you, @AsadNizami , for this PR. It is off to a good start. I like how you put these new methods right after the Opinions on how to implement str and repr methods vary somewhat, but I like to have something display that is related to the instance in some way so that if multiple are printed out, they aren't all the same. Something like this: class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f"Person(name='{self.name}', age={self.age})"
def __repr__(self):
return self.__str__() Observe that (As an aside, observe that Anyhow, back to py5. The py5 classes are more complex in that it isn't totally obvious about what should be displayed. I would look into the various methods to see if there is anything interesting or relevant to display to the user. For For |
Thanks for the feedback. I am facing some issues with string formatting when implementing the class Py5Font:
"""$classdoc_Py5Font
"""
_cls = jpype.JClass('processing.core.PFont')
CHARSET = _cls.CHARSET
_py5_object_cache = weakref.WeakSet()
def __new__(cls, pfont):
for o in cls._py5_object_cache:
if pfont == o._instance:
return o
else:
o = object.__new__(Py5Font)
o._instance = pfont
cls._py5_object_cache.add(o)
return o
def __str__(self):
return "Py5Font(name='{}', size={})".format(self.get_name(), self.get_size())
KeyError with the other way of string formatting. class Py5Font:
"""$classdoc_Py5Font
"""
_cls = jpype.JClass('processing.core.PFont')
CHARSET = _cls.CHARSET
_py5_object_cache = weakref.WeakSet()
def __new__(cls, pfont):
for o in cls._py5_object_cache:
if pfont == o._instance:
return o
else:
o = object.__new__(Py5Font)
o._instance = pfont
cls._py5_object_cache.add(o)
return o
def __str__(self):
return f"Py5Font(name='{self.get_name()}', size={self.get_size()})"
I'll be grateful if you can help me here. |
Oh, I see. I forget about this issue all the time. To understand the issue, first I should explain how py5generator works. It is basically a custom template engine that uses data and templates to create the actual py5 code. All of the files in py5_resources/py5_module are the templates. The data comes from csv files, the output of The file that you are editing, font.py, is itself a string that gets used as a format string. The All of this works great but it gets tripped up if I try to write code in a template that uses |
I've made the changes, please review them. |
It is looking pretty good! Thank you for your efforts. I appreciate the Also, Py5Graphics and Py5Image (in graphics.py and image.py) should be similar to what you added to sketch.py. |
Please tell me how to create objects for those classes requiring additional arguments in the |
There's no way to create any of these objects without creating a py5 Sketch and running it. |
Ok, thanks! |
Thank you, @AsadNizami ! Almost there, just a few minor tweaks.
|
Please review |
This is great, @AsadNizami , thank you for your perseverance and for working with me to create a solid PR. I will merge this now... |
Thank you for helping me through this. |
Fix for #209. Tested the changes. I'll implement these methods for other classes also if this looks fine. Please let me know if there are any changes to make.