Repository: einsteinpy/einsteinpy

Issue:
Add __repr__ and __str__ for Geodesic Class
🐞 **Problem**

The Geodesic class is missing these methods. More can be discusses in comments.

🎯 **Goal**

<!--- Why is this change important to you? How would you use it? -->
<!--- How can it benefit other users? -->

💡 **Possible solutions**

<!--- Not obligatory, but suggest an idea for implementing addition or change -->

📋  **Steps to solve the problem**

 * Comment below about what you've started working on.
 * Add, commit, push your changes
 * Submit a pull request and add this in comments - `Addresses #<put issue number here>`
 * Ask for a review in comments section of pull request
 * Celebrate your contribution to this project 🎉


Discussion:
pep8speaks: Hello @Bhavam! Thanks for opening this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:

* In the file [`src/einsteinpy/geodesic.py`](https://github.com/einsteinpy/einsteinpy/blob/514576453f59eac704d8c54109797e5e0908664d/src/einsteinpy/geodesic.py):

> [Line 36:23](https://github.com/einsteinpy/einsteinpy/blob/514576453f59eac704d8c54109797e5e0908664d/src/einsteinpy/geodesic.py#L36): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 36:48](https://github.com/einsteinpy/einsteinpy/blob/514576453f59eac704d8c54109797e5e0908664d/src/einsteinpy/geodesic.py#L36): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ':'
> [Line 36:65](https://github.com/einsteinpy/einsteinpy/blob/514576453f59eac704d8c54109797e5e0908664d/src/einsteinpy/geodesic.py#L36): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ','
> [Line 36:89](https://github.com/einsteinpy/einsteinpy/blob/514576453f59eac704d8c54109797e5e0908664d/src/einsteinpy/geodesic.py#L36): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ','
> [Line 36:121](https://github.com/einsteinpy/einsteinpy/blob/514576453f59eac704d8c54109797e5e0908664d/src/einsteinpy/geodesic.py#L36): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (123 > 120 characters)
> [Line 39:44](https://github.com/einsteinpy/einsteinpy/blob/514576453f59eac704d8c54109797e5e0908664d/src/einsteinpy/geodesic.py#L39): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ','
> [Line 39:85](https://github.com/einsteinpy/einsteinpy/blob/514576453f59eac704d8c54109797e5e0908664d/src/einsteinpy/geodesic.py#L39): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ','
> [Line 39:119](https://github.com/einsteinpy/einsteinpy/blob/514576453f59eac704d8c54109797e5e0908664d/src/einsteinpy/geodesic.py#L39): [E203](https://duckduckgo.com/?q=pep8%20E203) whitespace before ','
> [Line 39:121](https://github.com/einsteinpy/einsteinpy/blob/514576453f59eac704d8c54109797e5e0908664d/src/einsteinpy/geodesic.py#L39): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (163 > 120 characters)


shreyasbapat: Hi @Bhavam You need to fix the code quality.

Run `tox -e reformat` 

while in the repo.
shreyasbapat: Also, can you define a example geodesic and show how the representation looks like? Pick anyone from the example notebook
Bhavam: Yes sure ill do that and get back to you.
shreyasbapat: Good. Now I believe, the representations won't pop up. So, please try to run it once.
codecov[bot]: # [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/383?src=pr&el=h1) Report
> Merging [#383](https://codecov.io/gh/einsteinpy/einsteinpy/pull/383?src=pr&el=desc) into [master](https://codecov.io/gh/einsteinpy/einsteinpy/commit/4df5a19e51da5aabff19c50e231d6f8b83e3d5a7?src=pr&el=desc) will **decrease** coverage by `0.09%`.
> The diff coverage is `50%`.

[![Impacted file tree graph](https://codecov.io/gh/einsteinpy/einsteinpy/pull/383/graphs/tree.svg?width=650&token=H3oDVYA11f&height=150&src=pr)](https://codecov.io/gh/einsteinpy/einsteinpy/pull/383?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master     #383     +/-   ##
=========================================
- Coverage   94.13%   94.04%   -0.1%     
=========================================
  Files          52       52             
  Lines        1843     1847      +4     
=========================================
+ Hits         1735     1737      +2     
- Misses        108      110      +2
```


| [Impacted Files](https://codecov.io/gh/einsteinpy/einsteinpy/pull/383?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/einsteinpy/geodesic.py](https://codecov.io/gh/einsteinpy/einsteinpy/pull/383/diff?src=pr&el=tree#diff-c3JjL2VpbnN0ZWlucHkvZ2VvZGVzaWMucHk=) | `85.71% <50%> (-14.29%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/383?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/einsteinpy/einsteinpy/pull/383?src=pr&el=footer). Last update [4df5a19...363c4f1](https://codecov.io/gh/einsteinpy/einsteinpy/pull/383?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

shreyasbapat: And later you will have to write up a unit test that tests this change to take care of the failing coverage check.
Bhavam: Sure and ill follow up with the example geodesic representation also.
Bhavam: > And later you will have to write up a unit test that tests this change to take care of the failing coverage check.

Do I insert the unit tests in the testing module or somewhere else
ritzvik: > > And later you will have to write up a unit test that tests this change to take care of the failing coverage check.
> 
> Do I insert the unit tests in the testing module or somewhere else

Kindly put in new tests in `tests/test_geodesic.py`
Also, run `tox -e reformat` to lint the code and pass quality checks :)
shreyasbapat: I guess all you have to do is,
Squash this in one commit.

And rebase.

If you need any help in that front, ping me and I'll be happy to help.

Patch:
diff --git a/src/einsteinpy/geodesic.py b/src/einsteinpy/geodesic.py
index 31b0700..7537371 100644
--- a/src/einsteinpy/geodesic.py
+++ b/src/einsteinpy/geodesic.py
@@ -32,6 +32,22 @@ class Geodesic:
             return_cartesian=True,
         )[1]
 
+    def __repr__(self):
+        return "body name= ({0}) , metric=({1}) , parent name=({2}) , parent mass=({3})".format(
+            self.body.name,
+            self.metric.name,
+            self.body.parent.name,
+            self.body.parent.mass,
+        )
+
+    def __str__(self):
+        return "body name= ({0}) , metric=({1}) , parent name=({2}) , parent mass=({3})".format(
+            self.body.name,
+            self.metric.name,
+            self.body.parent.name,
+            self.body.parent.mass,
+        )
+
     @property
     def trajectory(self):
         return self._trajectory
diff --git a/src/einsteinpy/metric/kerr.py b/src/einsteinpy/metric/kerr.py
index c5230b3..b2a3467 100644
--- a/src/einsteinpy/metric/kerr.py
+++ b/src/einsteinpy/metric/kerr.py
@@ -20,6 +20,7 @@ class Kerr:
     @u.quantity_input(time=u.s, M=u.kg)
     def __init__(self, bl_coords, M, time):
         self.input_coords = bl_coords
+        self.name = "Kerr"
         self.M = M
         self.a = self.input_coords.a.to(u.m)
         self.time = time
diff --git a/src/einsteinpy/metric/kerrnewman.py b/src/einsteinpy/metric/kerrnewman.py
index 5a6f9e2..0c8cde9 100644
--- a/src/einsteinpy/metric/kerrnewman.py
+++ b/src/einsteinpy/metric/kerrnewman.py
@@ -22,6 +22,7 @@ class KerrNewman:
     @u.quantity_input(time=u.s, M=u.kg, Q=u.C)
     def __init__(self, bl_coords, q, M, Q, time):
         self.input_coords = bl_coords
+        self.name = "KerrNewman"
         self.M = M
         self.a = self.input_coords.a.to(u.m)
         self.Q = Q
diff --git a/src/einsteinpy/metric/schwarzschild.py b/src/einsteinpy/metric/schwarzschild.py
index b446b28..a49e126 100644
--- a/src/einsteinpy/metric/schwarzschild.py
+++ b/src/einsteinpy/metric/schwarzschild.py
@@ -19,6 +19,7 @@ class Schwarzschild:
 
     @u.quantity_input(time=u.s, M=u.kg)
     def __init__(self, sph_coords, M, time):
+        self.name = "Schwarzschild"
         self.M = M
         self.input_coords = sph_coords
         self.time = time


Test patch:
diff --git a/src/einsteinpy/tests/test_geodesics.py b/src/einsteinpy/tests/test_geodesics.py
index 7050e40..97b93c6 100644
--- a/src/einsteinpy/tests/test_geodesics.py
+++ b/src/einsteinpy/tests/test_geodesics.py
@@ -37,3 +37,21 @@ def test_Geodesics_has_trajectory(dummy_data):
     body, t, _, end_lambda, stepsize = dummy_data
     geo = Geodesic(body, time=t, end_lambda=end_lambda, step_size=stepsize)
     assert isinstance(geo.trajectory, np.ndarray)
+
+
+def test_Geodesics_str_returns_members(dummy_data):
+    body, t, _, end_lambda, stepsize = dummy_data
+    geo = Geodesic(body, time=t, end_lambda=end_lambda, step_size=stepsize)
+    assert (
+        geo.__str__()
+        == "body name= (obj) , metric=(Schwarzschild) , parent name=(attractor) , parent mass=(6e+24 kg)"
+    )
+
+
+def test_Geodesics_repr_returns_members(dummy_data):
+    body, t, _, end_lambda, stepsize = dummy_data
+    geo = Geodesic(body, time=t, end_lambda=end_lambda, step_size=stepsize)
+    assert (
+        geo.__repr__()
+        == "body name= (obj) , metric=(Schwarzschild) , parent name=(attractor) , parent mass=(6e+24 kg)"
+    )