Python Forum
Python for syntax conversion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python for syntax conversion
#11
The __repr__ gives a nice representation of the object when you try to display it, for example
>>> class Statement:
...     def __init__(self, data):
...         self.data = data
...     def __repr__(self):
...         return "{}({})".format(self.__class__.__name__, self.data)
... 
>>> class Storey(Statement):
...     pass
... 
>>> element = Storey(('STOREY', 'foo', 'BAR', 'baz'))
>>> 
>>> element
Storey(('STOREY', 'foo', 'BAR', 'baz'))
If I don't include the __repr__ in the class definition, the element is displayed as so
>>> class Statement:
...     def __init__(self, data):
...         self.data = data
... 
>>> class Storey(Statement):
...     pass
... 
>>> element = Storey(('STOREY', 'foo', 'BAR', 'baz'))
>>> element
<__main__.Storey object at 0x7ff163ad3898>
Using classes for the various statements will help you in the second step when you will transform these statements in the second language or in intermediary steps if you want to manipulate the data easily. To start with. you can simply define a subclass of Statement for each of the kinds of statements that you meet in the input file. For example if there are statements for doors, then use a class
class Door(Statement):
    pass
These classes are empty for now but you will be free to add features to them later.

It would be a good idea to include an example of a typical input file if you can do that.
Reply


Messages In This Thread
Python for syntax conversion - by kingsman - Dec-20-2019, 02:52 PM
RE: Python for syntax conversion - by ichabod801 - Dec-20-2019, 03:58 PM
RE: Python for syntax conversion - by kingsman - Dec-20-2019, 05:33 PM
RE: Python for syntax conversion - by Gribouillis - Dec-20-2019, 06:59 PM
RE: Python for syntax conversion - by kingsman - Dec-21-2019, 04:49 PM
RE: Python for syntax conversion - by Gribouillis - Dec-21-2019, 05:16 PM
RE: Python for syntax conversion - by kingsman - Dec-22-2019, 10:02 AM
RE: Python for syntax conversion - by buran - Dec-21-2019, 05:47 PM
RE: Python for syntax conversion - by Gribouillis - Dec-22-2019, 10:58 AM
RE: Python for syntax conversion - by kingsman - Dec-23-2019, 04:44 PM
RE: Python for syntax conversion - by Gribouillis - Dec-23-2019, 05:13 PM
RE: Python for syntax conversion - by kingsman - Dec-24-2019, 11:55 AM
RE: Python for syntax conversion - by Gribouillis - Dec-24-2019, 01:03 PM
RE: Python for syntax conversion - by kingsman - Dec-26-2019, 12:48 PM
RE: Python for syntax conversion - by Gribouillis - Dec-26-2019, 05:50 PM
RE: Python for syntax conversion - by kingsman - Dec-27-2019, 01:07 PM
RE: Python for syntax conversion - by Gribouillis - Dec-27-2019, 01:47 PM
RE: Python for syntax conversion - by kingsman - Dec-27-2019, 02:05 PM
RE: Python for syntax conversion - by Gribouillis - Dec-27-2019, 02:15 PM
RE: Python for syntax conversion - by kingsman - Dec-27-2019, 03:49 PM
RE: Python for syntax conversion - by kingsman - Apr-27-2020, 09:26 AM
RE: Python for syntax conversion - by Gribouillis - Dec-27-2019, 04:18 PM
RE: Python for syntax conversion - by kingsman - Dec-27-2019, 04:26 PM
RE: Python for syntax conversion - by kingsman - Apr-27-2020, 03:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python format conversion bluefrog 2 2,790 Jul-22-2018, 03:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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