Python Forum
Formating for __repr__
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Formating for __repr__
#1
I am banging my head against the wall. I have tried so many different format things to get this to work and cannot figure it out. 
I am not so sharp at making strings look exactly like I want them to. Hopefully someone can help me. Thanks. Here's the code I've tried in it's current format.
#I need it to look exactly like this:
#"3kg (0,6.37e+06,0) (0,0,0)"
formated = '({:.3g},{:.3g},{:.3g})'.format(self.mass, self.position, self.velocity)
return formated

#Also tried this:
formatted = '"{1:8.2f},{1:8.2f},{1:8.2f}"'.format(self.mass, self.position, self.velocity)
Reply
#2
So, what did you get that was not to your liking?

I don't know what specification g means, but in line 7 you'll only get self.position - 3 times. Number before colon - may be omitted in most cases - tells which positional argument to use that specification for. Either remove it - or replace with 0, 1, 2 respectively
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
It would help if we could see the data you are trying to format. It looks from your desired output that position and velocity are tuples of the three dimensional components of position and velocity. You will have to format each tuple separately, and then combine the results in with the formatting of any non-tuple data.
position = (801, 23, 2.718281828)
position_text = '({:3g}, {:3g}, {:3g})'.format(*position)
The * syntax converts a sequence into a sequence of parameters.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Maybe it would help if I give you the entire __repr__ function.  The data being passed as parameters is mass = 3, position = (0,6.37e+06,0), and velocity = (0,0,0)
def __repr__(self):
        #Representation Call
        #"3kg (0,6.37e+06,0) (0,0,0)"
        formatted = '{1:.6f},({1:.6f},{1:.6f},{1:.6f}),({1:.6f},{1:.6f},{1:.6f})'.format(self.mass, self.position, self.velocity)
        
        print(formatted) #This was for testing. 
        return formatted
Reply
#5
I mostly have it now. Thought I would follow up for anyone else and to see how to align it all correctly. Here's the code:

formatted = '{0!s:6s},{1!s:6s},{2!s:6s}'.format(mass, position, velocity)
Input


str(melon) == "3kg (0,6.37e+06,0) (0,0,0)"

I get 3kg (0,6.37e+06,0) (0,0,0), which is correct.


When I pass this
str(earth) == "5.97e+24kg (0,0,0) (0,0,0)"

I get 5.9736kg (0,0,0) (0,0,0), which you can see is not correct.
Reply
#6
Most of what you are doing is unnecessary. Take {0!s:6s}. The 0 says take the first parameter, which is what it's going to do anyway for the first set of braces. The !s says to use string formatting, which is the default. The second 's' says (again) to use string formatting. So all you have really asked for is :6, which means width of six. If you want only exponential format, you want :6e. If you want only two digits of precision, you want :6.2e. It is all explained in the string formatting documentation: https://docs.python.org/3/library/string...ing-syntax
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Display Formating Jrvelandia 2 2,881 Apr-23-2018, 11:54 PM
Last Post: Jrvelandia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020