Skip to content

Commit

Permalink
Added the dunder str to repr.py and updated the file name (#27)
Browse files Browse the repository at this point in the history
* Added the dunder str to repr.py and updated the file name

- Added the __str_() in the repr.py
- Updated the repr.py filename to str_and_repr.py

* Added the ascii function and its f-string example in the code

* Added comment requested by trag1c

* Made black not cry

* Corrected Doc-String for repr

Corrected Doc-String for repr based from PR feedback

* Elaborated on eval and repr

Elaborated on when evaling a repr should give us identical object and also the cases when it doesn't

* Fixed formatting issues

Made black not cry

* Edit __repr__ doc str

- Edited __repr__ doc 
- Added EOL in gitignore

* 2nd attempt at adding EOL

* Code in comment is now uncommented

- Removed extra line from .gitignore
- Converted lambda to function since ruff was complaining

* Removed docs and comments

Removed docs, comments, and some code, so the file can fit on screen.

* Removed args only limitation from init

Removed args only limitation from init
  • Loading branch information
isFakeAccount authored Jan 22, 2024
1 parent 123fcd5 commit 013e378
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,4 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
.idea/
17 changes: 0 additions & 17 deletions src/middle-earth/repr.py

This file was deleted.

32 changes: 32 additions & 0 deletions src/middle-earth/str_and_repr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import re


class MyCoolClass:
def __init__(self, value: str) -> None:
self.value = value

def __repr__(self) -> str:
return f"MyCoolClass(value={self.value!r})"

def __str__(self) -> str:
return f"My Cool Class Value is: {self.value}"


if __name__ == "__main__":
my_class = MyCoolClass(value="∫ f(x) dx")
print("Printing the output from __repr__()")
print("The repr is:", repr(my_class))
print(f"Calling the repr using f-string: {my_class!r}")
print("The evaled repr is:", repr(eval(repr(my_class))))

print(repr(re.compile(r"\d"))) # Can't eval repr in these examples
o = object()
print(repr(o))

print("The ascii repr of my_class is:", ascii(my_class))
print(f"Calling the ascii using f-string: {my_class!a}\n")

print("Printing the output from __str__()")
print(my_class)
print("The str() output is:", str(my_class))
print(f"Calling the str using f-string: {my_class!s} == {my_class}")

0 comments on commit 013e378

Please sign in to comment.