Skip to content

Instantly share code, notes, and snippets.

@davincif
Last active November 20, 2021 19:40
Show Gist options
  • Select an option

  • Save davincif/eb294a1e196225cfda263f2a36acb6af to your computer and use it in GitHub Desktop.

Select an option

Save davincif/eb294a1e196225cfda263f2a36acb6af to your computer and use it in GitHub Desktop.

Revisions

  1. Leonardo Da Vinci revised this gist Nov 20, 2021. No changes.
  2. Leonardo Da Vinci revised this gist Nov 20, 2021. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions lines.py
    Original file line number Diff line number Diff line change
    @@ -85,6 +85,14 @@ def main():
    for fext in fileExtentions
    }

    # delete empty lines
    toDelete = []
    for lang in stats:
    if(not stats[lang]['lines']):
    toDelete.append(lang)
    for deleteIt in toDelete:
    del stats[deleteIt]

    # show results
    print("Linhas:", total_lines)
    print(
  3. Leonardo Da Vinci created this gist Nov 2, 2021.
    97 changes: 97 additions & 0 deletions lines.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    #!/usr/bin/python3
    import os
    import json

    path = '../'

    fileExtentions = [
    'ts',
    'js',
    'json',
    'html',
    'css',
    'scss',
    'sass',
    'svg',
    'sh',
    ]
    fileExtentionsNames = {
    'ts': 'TypeScript',
    'js': 'JavaScript',
    'json': 'JavaScript Object',
    'html': 'HTML',
    'css': 'CSS',
    'scss': 'SCSS',
    'sass': 'SASS',
    'svg': 'SVG',
    'sh': 'ShellScript',
    }
    excludeFile = ['package-lock.json']
    excludeDir = [
    'node_modules',
    'dist',
    '.vscode',
    '.git',
    'trash',
    ]

    files = []
    total_lines = 0

    # initialize states
    stats = {file: 0 for file in fileExtentions}


    def main():
    global fileExtentions
    global excludeFile
    global excludeDir
    global files
    global total_lines
    global stats

    excludeDir = list(
    map(lambda dir: os.path.join(path, dir), excludeDir)
    )

    # r=root, d=directories, f = files
    for r, d, f in os.walk(path):
    if(True in map(lambda dir: r.startswith(dir), excludeDir)):
    continue

    for file in f:
    fname = file.split('/')[-1]
    fext = fname.split('.')[-1]
    if fext in fileExtentions and fname not in excludeFile:
    file_lines = sum(
    1 for line in open(
    os.path.join(r, file)
    )
    )
    stats[fext] += file_lines
    total_lines += file_lines


    if __name__ == '__main__':
    main()

    # create statistics
    stats = {
    fext: {
    'language': fileExtentionsNames[fext],
    'lines': stats[fext],
    "%": '%.2f%%' % (stats[fext] / total_lines * 100),
    }
    for fext in fileExtentions
    }

    # show results
    print("Linhas:", total_lines)
    print(
    "Statistics:",
    json.dumps(
    stats,
    indent=2,
    sort_keys=True
    )
    )