HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonMinor

Python script to convert asn to .hpp

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptconverthpppythonasn

Problem

I have written a script that turns an asn file into a hpp file, but it look very dirty even though I have made changes based on pylint.

I think I make use of too many lists, for-loops and try except blocks.

I would like to get a review of my code, and I am particularly interested in making my code look more organized and make my code more efficient.


Sample input

SerError ::= CHO
{
   app-ref [0] IMPLICIT ENUMERATED
   {
      --  provider only
      other (0),
      ti-ela (1)

   },
   ha-res [1] IMPLICIT ENUMERATED
   {
      --  hardware 
      other (0),
      me-unavailable (1)

   }
}




Sample output

enum ESererror//SSerError ::= CHO
{
   eSererrorAppRef = 0, 
   eSererrorHaRes = 1
};

enum EAppRef//pp-ref [0] IMPLICIT ENUMERATED
{
   //--  provider only
   eAppRefOther = 0,
   eAppRefTiEla = 1
};

enum EHaRes//ha-res [1] IMPLICIT ENUMERATED
{
   //-- VDE hardware troubles
   eHaResOther = 0,
   eHaResMeUnavailable = 1
};


This is the script I would like to have reviewed:

```
'''
**

@brief
@version 1.0

**
'''
import re
from collections import deque
import sys
import inflection

class Convert(object):
'''To do: add data'''
def __init__(self):
'''To do: add data'''
self.plist = []
self.slist = []
self.tlist = []
self.llist = []
self.lines = []
self.line = None
self.open_braces = []
self.close_braces = []
self.outputfile = None
self.i = None
self.open_brace_list = []
self.close_brace_list = []
self.file_name = None
self.split_character = None
self.length = None
self.enumvariable_flag = None
self.inner_variable_prefix=""

def start_tag(self, split_character, line):
'''To do: add data'''
self.split_character = split_characte

Solution

You should also never have a bare except. Always use at least except Exception. Because otherwise you can't e.g. abort with CTRL-C anymore if it is stuck.

You should also try to limit their scope as small as possible to guard only the one operation which may fail. Otherwise it becomes a lot harder to debug as well.

Context

StackExchange Code Review Q#139501, answer score: 5

Revisions (0)

No revisions yet.