Skip to content

Commit

Permalink
Code cleanup and optimisation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Pircher committed May 3, 2017
1 parent 01dc9b1 commit 0076907
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 235 deletions.
16 changes: 0 additions & 16 deletions crc_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,12 @@
>>> print("{0:#x}".format(crc.table_driven("123456789")))
"""

# Class Crc
###############################################################################
class Crc(object):
"""
A base class for CRC routines.
"""
# pylint: disable=too-many-instance-attributes

# Class constructor
###############################################################################
def __init__(self, width, poly, reflect_in, xor_in, reflect_out, xor_out, table_idx_width=None, slice_by=1):
"""The Crc constructor.
Expand Down Expand Up @@ -93,8 +89,6 @@ def __init__(self, width, poly, reflect_in, xor_in, reflect_out, xor_out, table_
self.crc_shift = 0


# function __get_nondirect_init
###############################################################################
def __get_nondirect_init(self, init):
"""
return the non-direct init if the direct algorithm has been selected.
Expand All @@ -110,8 +104,6 @@ def __get_nondirect_init(self, init):
return crc & self.mask


# function reflect
###############################################################################
def reflect(self, data, width):
"""
reflect a data word, i.e. reverts the bit order.
Expand All @@ -125,8 +117,6 @@ def reflect(self, data, width):
return res


# function bit_by_bit
###############################################################################
def bit_by_bit(self, in_data):
"""
Classic simple and slow CRC implementation. This function iterates bit
Expand Down Expand Up @@ -158,8 +148,6 @@ def bit_by_bit(self, in_data):
return (reg ^ self.xor_out) & self.mask


# function bit_by_bit_fast
###############################################################################
def bit_by_bit_fast(self, in_data):
"""
This is a slightly modified version of the bit-by-bit algorithm: it
Expand Down Expand Up @@ -187,8 +175,6 @@ def bit_by_bit_fast(self, in_data):
return reg ^ self.xor_out


# function gen_table
###############################################################################
def gen_table(self):
"""
This function generates the CRC table used for the table_driven CRC
Expand Down Expand Up @@ -218,8 +204,6 @@ def gen_table(self):
return tbl


# function table_driven
###############################################################################
def table_driven(self, in_data):
"""
The Standard table_driven CRC algorithm.
Expand Down
221 changes: 76 additions & 145 deletions crc_codegen.py

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions crc_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
print('"{}" -> "{}"'.format(expr, expr.simplify()))
"""

DELETEME_no_simplify = True # TODO delete
#DELETEME_no_simplify = False # TODO delete

def _classify(val):
"""
Expand Down Expand Up @@ -114,7 +112,6 @@ def simplify(self):
"""
Return a simplified version of this sub-expression.
"""
if DELETEME_no_simplify: return self
args = [arg.simplify() for arg in self.args]
return FunctionCall(self.name, args)

Expand All @@ -133,7 +130,6 @@ def simplify(self):
"""
Return a simplified version of this sub-expression.
"""
if DELETEME_no_simplify: return self
val = self.val.simplify()
if type(val) is Terminal:
return val
Expand Down Expand Up @@ -161,7 +157,6 @@ def simplify(self):
"""
Return a simplified version of this sub-expression.
"""
if DELETEME_no_simplify: return self
lhs = self.lhs.simplify()
rhs = self.rhs.simplify()
if lhs.is_int() and rhs.is_int():
Expand Down Expand Up @@ -194,7 +189,6 @@ def simplify(self):
"""
Return a simplified version of this sub-expression.
"""
if DELETEME_no_simplify: return self
lhs = self.lhs.simplify()
rhs = self.rhs.simplify()
if lhs.is_int() and rhs.is_int():
Expand Down Expand Up @@ -227,7 +221,6 @@ def simplify(self):
"""
Return a simplified version of this sub-expression.
"""
if DELETEME_no_simplify: return self
lhs = self.lhs.simplify()
rhs = self.rhs.simplify()
if lhs.is_int() and rhs.is_int():
Expand Down Expand Up @@ -262,7 +255,6 @@ def simplify(self):
"""
Return a simplified version of this sub-expression.
"""
if DELETEME_no_simplify: return self
lhs = self.lhs.simplify()
rhs = self.rhs.simplify()
if lhs.is_int() and rhs.is_int():
Expand Down Expand Up @@ -295,7 +287,6 @@ def simplify(self):
"""
Return a simplified version of this sub-expression.
"""
if DELETEME_no_simplify: return self
lhs = self.lhs.simplify()
rhs = self.rhs.simplify()
if lhs.is_int() and rhs.is_int():
Expand Down Expand Up @@ -328,7 +319,6 @@ def simplify(self):
"""
Return a simplified version of this sub-expression.
"""
if DELETEME_no_simplify: return self
lhs = self.lhs.simplify()
rhs = self.rhs.simplify()
if lhs.is_int() and rhs.is_int():
Expand Down Expand Up @@ -361,7 +351,6 @@ def simplify(self):
"""
Return a simplified version of this sub-expression.
"""
if DELETEME_no_simplify: return self
lhs = self.lhs.simplify()
rhs = self.rhs.simplify()
if lhs.is_int() and rhs.is_int():
Expand Down Expand Up @@ -392,7 +381,6 @@ def simplify(self):
"""
Return a simplified version of this sub-expression.
"""
if DELETEME_no_simplify: return self
lhs = self.lhs.simplify()
rhs = self.rhs.simplify()
if lhs.is_int() and rhs.is_int():
Expand Down
35 changes: 13 additions & 22 deletions crc_models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pycrc -- parameterisable CRC calculation utility and C source code generator
#
# Copyright (c) 2006-2015 Thomas Pircher <tehpeh-web@tty1.net>
# Copyright (c) 2006-2017 Thomas Pircher <tehpeh-web@tty1.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
Expand All @@ -26,27 +26,25 @@
To print the parameters of a particular model:
from crc_models import CrcModels
import crc_models
models = CrcModels()
print(models.get_list())
models = crc_models.CrcModels()
print(", ".join(models.names()))
m = models.get_params("crc-32")
if m != None:
print("Width: {0:d}".format(m['width']))
print("Poly: {0:#x}".format(m['poly']))
print("ReflectIn: {0}".format(m['reflect_in']))
print("XorIn: {0:#x}".format(m['xor_in']))
print("ReflectOut: {0}".format(m['reflect_out']))
print("XorOut: {0:#x}".format(m['xor_out']))
print("Check: {0:#x}".format(m['check']))
print("Width: {width:d}".format(**m))
print("Poly: {poly:#x}".format(**m))
print("ReflectIn: {reflect_in}".format(**m))
print("XorIn: {xor_in:#x}".format(**m))
print("ReflectOut: {reflect_out}".format(**m))
print("XorOut: {xor_out:#x}".format(**m))
print("Check: {check:#x}".format(**m))
else:
print("model not found.")
"""



# Class CrcModels
###############################################################################
class CrcModels(object):
"""
CRC Models.
Expand Down Expand Up @@ -318,20 +316,13 @@ class CrcModels(object):
})


# function get_list
###############################################################################
def get_list(self):
def names(self):
"""
This function returns the list of supported CRC models.
"""
models = []
for i in self.models:
models.append(i['name'])
return models
return [model['name'] for model in self.models]


# function get_params
###############################################################################
def get_params(self, model):
"""
This function returns the parameters of a given model.
Expand Down
22 changes: 2 additions & 20 deletions crc_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
from crc_models import CrcModels


# Class Options
###############################################################################
class Options(object):
"""
The options parsing and validating class.
Expand Down Expand Up @@ -66,8 +64,6 @@ class Options(object):
action_generate_table = 0x07


# Class constructor
###############################################################################
def __init__(self):
self.width = None
self.poly = None
Expand All @@ -94,8 +90,6 @@ def __init__(self):
self.undefined_crc_parameters = False


# function parse
###############################################################################
def parse(self, argv=None):
"""
Parses and validates the options given as arguments
Expand All @@ -119,7 +113,7 @@ def parse(self, argv=None):
--width --poly --reflect-in --xor-in --reflect-out --xor-out"""

models = CrcModels()
model_list = ", ".join(models.get_list())
model_list = ", ".join(models.names())
parser = OptionParser(option_class=MyOption, usage=usage, version=self.version_str)
parser.add_option(
"-v", "--verbose",
Expand Down Expand Up @@ -400,8 +394,6 @@ def parse(self, argv=None):



# function __warning
###############################################################################
def __warning(self, message):
"""
Print a warning message to stderr.
Expand All @@ -411,8 +403,6 @@ def __warning(self, message):



# function _error
###############################################################################
def __error(self, message):
"""
Print a error message to stderr and terminate the program.
Expand All @@ -422,8 +412,6 @@ def __error(self, message):
sys.exit(1)


# function _model_cb
##############################################################################
def _model_cb(option, opt_str, value, parser):
"""
This function sets up the single parameters if the 'model' option has been selected
Expand All @@ -441,14 +429,12 @@ def _model_cb(option, opt_str, value, parser):
setattr(parser.values, 'xor_out', model['xor_out'])
else:
models = CrcModels()
model_list = ", ".join(models.get_list())
model_list = ", ".join(models.names())
raise OptionValueError(
"unsupported model {0:s}. Supported models are: {1:s}."
.format(value, model_list))


# function _check_hex
###############################################################################
def _check_hex(dummy_option, opt, value):
"""
Checks if a value is given in a decimal integer of hexadecimal reppresentation.
Expand All @@ -464,8 +450,6 @@ def _check_hex(dummy_option, opt, value):
"option {0:s}: invalid integer or hexadecimal value: {1:s}.".format(opt, value))


# function _check_bool
###############################################################################
def _check_bool(dummy_option, opt, value):
"""
Checks if a value is given as a boolean value (either 0 or 1 or "true" or "false")
Expand All @@ -481,8 +465,6 @@ def _check_bool(dummy_option, opt, value):
raise OptionValueError("option {0:s}: invalid boolean value: {1:s}.".format(opt, value))


# Class MyOption
###############################################################################
class MyOption(Option):
"""
New option parsing class extends the Option class
Expand Down
3 changes: 1 addition & 2 deletions crc_symtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
print(sym['crc_width'])
print('width: {crc_width}, poly: {crc_poly}'.format(**sym))
"""
#pylint: disable=too-many-lines

from crc_algorithms import Crc
import collections
Expand Down Expand Up @@ -146,7 +145,7 @@ def _pretty_str(value):
return str(value)


def _pretty_hex(value, width=None): # TODO return 0x????u
def _pretty_hex(value, width=None):
"""
Return a value of width bits as a pretty hexadecimal formatted string.
"""
Expand Down
12 changes: 0 additions & 12 deletions pycrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,13 @@
import sys


# function print_parameters
###############################################################################
def print_parameters(opt):
"""
Generate a string with the options pretty-printed (used in the --verbose mode).
"""
return str(cg.ParamBlock(opt, ''))


# function check_string
###############################################################################
def check_string(opt):
"""
Return the calculated CRC sum of a string.
Expand Down Expand Up @@ -106,8 +102,6 @@ def check_string(opt):
return crc


# function check_hexstring
###############################################################################
def check_hexstring(opt):
"""
Return the calculated CRC sum of a hex string.
Expand All @@ -130,8 +124,6 @@ def check_hexstring(opt):
return check_string(opt)


# function crc_file_update
###############################################################################
def crc_file_update(alg, register, check_bytes):
"""
Update the CRC using the bit-by-bit-fast CRC algorithm.
Expand All @@ -154,8 +146,6 @@ def crc_file_update(alg, register, check_bytes):
return register


# function check_file
###############################################################################
def check_file(opt):
"""
Calculate the CRC of a file.
Expand Down Expand Up @@ -192,8 +182,6 @@ def check_file(opt):
return register


# function write_file
###############################################################################
def write_file(filename, out_str):
"""
Write the content of out_str to filename.
Expand Down
Loading

0 comments on commit 0076907

Please sign in to comment.