Skip to main content
Fixed typos
Source Link

In case you do not know it yet, I suggest you to use pylintpylint to parse your code and have automatic feedbacks. In fact some of my notes comes actuallycome from pylint.

  • Constant names like var "directory" require uppercase style PEP8
  • Function and variable names require snake_case style PEP8
  • Functions require docstring, as well as the module itself PEP8

In case you do not know it yet, I suggest you to use pylint to parse your code and have automatic feedbacks. In fact some of my notes comes actually from pylint.

  • Constant names like var "directory" require uppercase style
  • Function and variable names require snake_case style
  • Functions require docstring, as well as the module itself

In case you do not know it yet, I suggest you to use pylint to parse your code and have automatic feedbacks. In fact some of my notes come from pylint.

  • Constant names like var "directory" require uppercase style PEP8
  • Function and variable names require snake_case style PEP8
  • Functions require docstring, as well as the module itself PEP8
Source Link

In case you do not know it yet, I suggest you to use pylint to parse your code and have automatic feedbacks. In fact some of my notes comes actually from pylint.

Styling notes:

  • Constant names like var "directory" require uppercase style
  • Function and variable names require snake_case style
  • Functions require docstring, as well as the module itself

Inline notes

#NOTE: 'from datetime import datetime' allows a shorter line below
import datetime
import time
import os
import sys

# get user's current directory

# NOTE: use os.path.join for concat filesystem paths for cross compatibility (also below)
directory = os.path.dirname(os.path.realpath(__file__)) + '/logs'


def createNewLog():

    # ensure log folder exists

    if not os.path.exists(directory):
        # NOTE: what if user has no permission to create files/directory in current directory? (also below)
        # HINT: give a way to choose where to store logs
        os.mkdir(directory)

    # get timestamp for naming the log

    timeInSeconds = time.time()
    # NOTE: fromtimestamp may raise an exception which is not not managed here
    timestamp = \
        datetime.datetime.fromtimestamp(timeInSeconds).strftime('%Y-%m-%d %H:%M:%S'
            )

    logFile = directory + '/' + timestamp + '.txt'
    mostRecentLog = directory + '/mostRecentLog.txt'

    # create log file and file with the log file's name

    # NOTE: redefining name 'log' (it's also a function name)
    # NOTE: this 'log' is not used. What it's that for?
    with open(logFile, 'w') as log:
        with open(mostRecentLog, 'w') as recentLog:
            recentLog.write(timestamp)


def findMostRecentLog():

    # ensure logger has been intiated

    try:
        with open(directory + '/mostRecentLog.txt', 'r') as logFile:
            logName = logFile.read()
    except FileNotFoundError:
        print("Must initiate logger first!")
        # NOTE: kind of rugh, I suggest returning an error and managing at caller level
        sys.exit(1)

    return directory + '/' + logName + '.txt'


def log(comment):
    # NOTE: why forcing the type?
    comment = str(comment)

    # write to log file retriving most recent log from corresponding file

    # NOTE: this dependency will make hard testing this function alone.
    with open(findMostRecentLog(), 'a') as log:
        log.write(comment + '\n')