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

Better __repr__ or .info() method #181

Open
MarkWieczorek opened this issue Apr 5, 2024 · 4 comments
Open

Better __repr__ or .info() method #181

MarkWieczorek opened this issue Apr 5, 2024 · 4 comments
Labels
enhancement Idea or request for a new feature

Comments

@MarkWieczorek
Copy link
Contributor

MarkWieczorek commented Apr 5, 2024

Description of the desired feature:
Printing the contents of a boule ellipsoid gives the following:

In [1]: print(boule.WGS84)
Ellipsoid(name='WGS84', semimajor_axis=6378137, flattening=0.0033528106647474805, geocentric_grav_const=398600441800000.0, angular_velocity=7.292115e-05, long_name='World Geodetic System (1984)', reference='Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy (2nd, corr. ed. 2006 edition ed.). Wien\u202f; New York: Springer.')

As this is a really long string, it is somewhat difficult to read if you a looking for a particular element. I propose that we provide an easier way to print the entirety of the ellipsoid contents for human readibility. This could either be by explicitly defining __repr__ for each ellispoid class or by providing a simple method such as Ellipsoid.info(). The output would look something like this:

In [1]: print(boule.WGS84)
Class: Ellipsoid
name = 'WGS84'
long_name = 'World Geodetic System (1984)'
semimajor_axis = 6378137
flattening = 0.0033528106647474805
geocentric_grav_const = 398600441800000.0
angular_velocity = 7.292115e-05
reference = 'Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy (2nd, corr. ed. 2006 edition ed.). Wien\u202f; New York: Springer.'

If desired, we could also append the units, but if we did this, we should probably define the units for each attribute in the class.

Are you willing to help implement and maintain this feature?
After we decide on how to do this, the changes would be trivial to make...

@MarkWieczorek MarkWieczorek added the enhancement Idea or request for a new feature label Apr 5, 2024
@VascoSch92
Copy link

VascoSch92 commented Apr 13, 2024

Suggestion:

In [1]: print(boule.WGS84)
Class: Ellipsoid
 - name = 'WGS84'
 - long name = 'World Geodetic System (1984)'
 - semimajor axis = 6378137 km
 - flattening = 0.0033528106647474805
 - geocentric gravitational constant = 398600441800000.0
 - angular velocity = 7.292115e-05
 - reference = 'Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy (2nd, corr. ed. 2006 edition ed.). Wien\u202f; New York: Springer.'

Three other things:

  1. in reference you can see that you have \u202f after Wien. Is it possible that there is no a regular space between Wien and ; ?
  2. could be also interesting to add a method info() which print all the information about the ellipsoid, i.e., all the property are executed and printed in a nice way :-)
  3. printing directly in a table format could also be interesting

@leouieda
Copy link
Member

Completely agree that we need a better print(bl.WGS84). The __repr__ is created automatically by attrs and I'd prefer to leave it as is, since __repr__ is supposed to allow eval(a.__repr__) == a which is what ours currently does (see this stackoverflow answer). But there is also __str__ which is used to make a nice representation of an object. If __str__ is defined, then print(a) will call a.__str__ instead of __repr__.

If implementing __str__, we can make the output nicer by not needing the key : value format:

>>> print(bl.WGS49)
World Geodetic System (1984) - WGS84
Oblate ellipsoid:
  •  Semi-major axis: 6378137 m
  •       Flattening: 0.0033528106647474805
  •               GM: 398600441800000.0 m³/s²
  • Angular velocity: 7.292115e-05 rad/s
From: Hofmann-Wellenhof, B., & Moritz, H. (2006). Physical Geodesy 
      (2nd, corr. ed. 2006 edition ed.). Wien; New York: Springer.

Another thing we can add in the future is a __repr_html__ to display the ellipsoid using HTML in Jupyter. But that's for another PR when we have this one implemented.

@VascoSch92
Copy link

I agree with the ouput format proposed by @leouieda . The only "problem" I see is that formatting the reference in a nice way can be tricky as we don't know where is what. Option can be

  1. the reference is printed just on one line
  2. the reference is given with some structure, see under for examples, such that we can control the various part of the reference and we can pretty print it

For point 2:

reference = {
    "author": "Hofmann-Wellenhof, B., & Moritz, H.",
    "title": "Physical Geodesy",
    "edition": "2nd, corr. ed. 2006",
    "publisher": "Springer",
    "year": 2006,
    "location": "Wien; New York"
}

or using a small class

Reference(
    author="Hofmann-Wellenhof, B., & Moritz, H.", 
    title="Physical Geodesy", 
    edition="2nd, corr. ed. 2006",
    publisher="Springer", 
    year=2006, 
    location="Wien; New York"
)

@MarkWieczorek
Copy link
Contributor Author

Another problem is that the new ellipsoids can have up to 4 references (one for each of the input parameters). One possible solution would be not to print the reference string at all for __str__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Idea or request for a new feature
Projects
None yet
Development

No branches or pull requests

3 participants