My first Python program! Does anyone want to review it to help me improve?

I just wrote my first Python program and it works! This is a program that renames subtitle files to corresponding video files so that media players collect subtitles (for example, it renames "The_Office_3x01.str" to "The.Office.S03E01.str" if a file named ". Office.S03E01.avi" was present in catalog).

I would really like someone to comment / criticize my code and help me improve my coding style and make it more Python-y. The code can be found at http://subtitle-renamer.googlecode.com/hg/renombrador.py

As I said, this is my first Python program, so feel free to comment on something like:

  • Style (indentation, variable names, conventions, etc.)
  • Design
  • Python functions i have to use that i don't
  • My use of libraries

Thank!

+3
source share
4 answers

1) The hash line only works if it is the first line in the file.

2) The names of functions and variables are usually allocated only at the bottom, not camelCase.

3) Dockstones usually use triple quotation marks, even for single-line dockers.

4) Your coding style is very functional, with many lambdas, maps, abbreviations, etc. and one copy of a four-line three-pole list comprehension. I find it difficult to understand this style and definitely deploy some of them.

5) , (4), , - :

match = [regex.match(str) for regex in episodeRegExes if regex.match(str)]

def getEpisodeTuple(iterable):
    episodeTuple = [episodeChunk(chunk)
        for chunk in iterable
        if episodeChunk(chunk)]
    if episodeTuple:
        assert len(episodeTuple) == 1
        return episodeTuple[0]
    else:
        return None

, (chunk) , , .

() (b) , , . :

def getEpisodeTuple(iterable):
    for chunk in iterable:
        echunk = episodeChunk(chunk)
        if echunk:
            return echunk

6) . , :

def splitWithAny(string, delimiters):
    "Splits the string with any of the strings of delimiters"
    return reduce(
        lambda iterable, delim: reduce(
            lambda lst,chunk: lst + chunk.split(delim),
            iterable,
            []),
        delimiters,
        [string])

def splitName(fileName):
    "Splits the fileName into smaller and hopefully significant chunks"
    delimiters = [" ", ".", "_", "-"]
    return filter(None, splitWithAny(fileName, delimiters))

( , - ( ( ()))) ..) :

def splitName(fileName):
    return re.split("[ ._-]+", fileName)
+4

pep8, ( pep8).

, - pyLint . Python , :) ( ).

+1

:

  • .

  • Regex , , ?

  • :

return [
        (getEpisodeTuple(chunks),
        [chunk for chunk in chunks if not episodeChunk(chunk)])
        for chunks in [splitName(string) for string in stringiterable]]

, . , - , . , . , , , , loooooong.

, , :

splitNames = [splitName(string) for string in stringiterable]
chunkChunks = lambda chunks: [chunk for chunk in chunks if not episodeChunk(chunk)]

return [(getEpisodeTuple(chunks),chunkChunks(chunks) for chunks in splitNames]
+1
  • #!/usr/bin/env python
  • [docstrings] [1] .
  • , , .
  • , .
0

Source: https://habr.com/ru/post/1791400/


All Articles