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

A source Code Counter

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

Problem

This is from my project codecount I use personally that is very similar to cloc.exe or SLOCCount. The part that I am questioning is where I am calculating when I am in a comment block and have deep nesting, I basically am replacing the sections with blank. I would like to deal with fixed format files at some point (RPGLE, CLP, etc) where the comments need to have characters in a specific position.

Any review/comments/suggestions would be appreciated.

```
def scanfile(self, filename):
""" The heart of the codecounter, Scans a file to identify
and collect the metrics based on the classification. """
strblock = None
endblock = None
inblock = False
endcode = False
sha256 = hashlib.sha256()

if(filename.size == 0):
logging.info("Skipping File : " + filename.name)
return

# Identify language
for l in self.langs:
if(filename.extension in self.langs[l]["ext"]):
filename.lang = l
break

# Unknown files don't need processed
if(filename.lang is None):
logging.info("Skipping File : " + filename.name)
return

# Using the with file opening in order to ensure no GC issues.
with open(filename.path + "/" + filename.name,
encoding="utf-8", errors='ignore') as fp:

for line in fp:
sha256.update(line.encode("utf-8"))
filename.lines += 1
line = line.strip()
identified = False

if(line == ""):
logging.info(" blak " + str(filename.lines))
filename.blanks += 1
continue

if(endcode):
filename.comments += 1
continue

# Check to see if it is a block or was an opening block
# ex1 = "/ / if x;" = Code, not inblock
# ex2 = "/ if x; /" = Code, inblock
# ex3 = " if x; /*" = Code, inblock
# ex4 = "/ / if x;

Solution

A couple of points:

In Python there is no need to place parentheses after if. For example,

if(filename.size == 0):


Can be replaced with

if filename.size == 0:


In fact, the latter is the preferred style.

Also, using format is preferred to using + to append strings. For example,

logging.info("Total  "
        + " " + str(filename.blanks)
        + " " + str(filename.comments)
        + " " + str(filename.code))


Can be replaced with:

log_message = 'Total  {blanks} {comments} {code}'.format(
    blanks=filename.blanks,
    comments=filename.comments,
    code=filename.code)
logging.info(log_message)


This has a few advantages: more readable, easier to internationalize (one single string token instead of multiple), has descriptive named place holders, and finally, will allow for easier formatting if you decide to add it at some point. For example, you can change {comments} to {comments:05d} to format it as a fixed-width number with width 5 and zeros padded on the left if necessary.

Code Snippets

if(filename.size == 0):
if filename.size == 0:
logging.info("Total  "
        + " " + str(filename.blanks)
        + " " + str(filename.comments)
        + " " + str(filename.code))
log_message = 'Total  {blanks} {comments} {code}'.format(
    blanks=filename.blanks,
    comments=filename.comments,
    code=filename.code)
logging.info(log_message)

Context

StackExchange Code Review Q#47311, answer score: 10

Revisions (0)

No revisions yet.