(Sep-07-2018, 03:33 PM)Mekire Wrote: [ -> ]One thing that jumps out is you don't need to interate through the file extensions.
You can store the file extension list as a set
and use os.path.splitext
to find the current file's extenstion.
Example:
import os
video_format = {
".3gp",".asf",".avi",".divx",".flv",".swf",".mp4",".mpeg", ".mpg",
".ogm",".wmv",".mov",".mkv",".nbr",".rm",".vob",".sfd",".webm",
".xvid"
}
some_files = ["something.mpg", "some.jpg", "this.avi", "that.doc"]
for file_name in some_files:
name, ext = os.path.splitext(file_name)
if ext in video_format:
print("Valid '{}' file '{}' found.".format(ext, file_name))
else:
print("'{}' is not a valid video format.".format(ext))
Output:
Valid '.mpg' file 'something.mpg' found.
'.jpg' is not a valid video format.
Valid '.avi' file 'this.avi' found.
'.doc' is not a valid video format.
If I understood correctly splittext() splits the string into two variables:
name --> the name of the file
ext --> the extension of the file
(Sep-07-2018, 03:33 PM)Mekire Wrote: [ -> ]One thing that jumps out is you don't need to interate through the file extensions. You can store the file extension list as a set
and use os.path.splitext
to find the current file's extenstion. Example: import os video_format = { ".3gp",".asf",".avi",".divx",".flv",".swf",".mp4",".mpeg", ".mpg", ".ogm",".wmv",".mov",".mkv",".nbr",".rm",".vob",".sfd",".webm", ".xvid" } some_files = ["something.mpg", "some.jpg", "this.avi", "that.doc"] for file_name in some_files: name, ext = os.path.splitext(file_name) if ext in video_format: print("Valid '{}' file '{}' found.".format(ext, file_name)) else: print("'{}' is not a valid video format.".format(ext))
Output:
Valid '.mpg' file 'something.mpg' found. '.jpg' is not a valid video format. Valid '.avi' file 'this.avi' found. '.doc' is not a valid video format.
(Sep-07-2018, 03:31 PM)Larz60+ Wrote: [ -> ]the following are pycodestyle (which checks for PEP8 compatibility) generated issues. Code can run perfectly well without being compliant, but you should strive to get there. Output:
PyAlex.py:3:10: E401 multiple imports on one line PyAlex.py:4:1: W293 blank line contains whitespace PyAlex.py:5:1: E265 block comment should start with '# ' PyAlex.py:5:80: E501 line too long (89 > 79 characters) PyAlex.py:6:1: E302 expected 2 blank lines, found 1 PyAlex.py:13:1: E305 expected 2 blank lines after class or function definition, found 0PyAlex.py:14:1: E265 block comment should start with '# ' PyAlex.py:14:80: E501 line too long (89 > 79 characters) PyAlex.py:15:1: W293 blank line contains whitespace PyAlex.py:18:23: E231 missing whitespace after ',' PyAlex.py:18:30: E231 missing whitespace after ',' PyAlex.py:18:37: E231 missing whitespace after ',' PyAlex.py:18:45: E231 missing whitespace after ',' PyAlex.py:18:52: E231 missing whitespace after ',' PyAlex.py:18:59: E231 missing whitespace after ',' PyAlex.py:18:66: E231 missing whitespace after ',' PyAlex.py:18:74: E231 missing whitespace after ',' PyAlex.py:18:80: E501 line too long (152 > 79 characters) PyAlex.py:18:81: E231 missing whitespace after ',' PyAlex.py:18:88: E231 missing whitespace after ',' PyAlex.py:18:95: E231 missing whitespace after ',' PyAlex.py:18:102: E231 missing whitespace after ',' PyAlex.py:18:109: E231 missing whitespace after ',' PyAlex.py:18:116: E231 missing whitespace after ',' PyAlex.py:18:122: E231 missing whitespace after ',' PyAlex.py:18:129: E231 missing whitespace after ',' PyAlex.py:18:136: E231 missing whitespace after ',' PyAlex.py:18:144: E231 missing whitespace after ',' PyAlex.py:21:1: W293 blank line contains whitespace PyAlex.py:22:1: E302 expected 2 blank lines, found 1 PyAlex.py:33:37: E231 missing whitespace after ',' PyAlex.py:35:80: E501 line too long (119 > 79 characters) PyAlex.py:37:1: W293 blank line contains whitespace PyAlex.py:38:1: E302 expected 2 blank lines, found 1 PyAlex.py:48:1: W293 blank line contains whitespace PyAlex.py:49:1: W293 blank line contains whitespace PyAlex.py:54:1: W293 blank line contains whitespace PyAlex.py:55:1: E302 expected 2 blank lines, found 1 PyAlex.py:60:80: E501 line too long (118 > 79 characters) PyAlex.py:60:109: E231 missing whitespace after ',' PyAlex.py:62:1: W293 blank line contains whitespace PyAlex.py:63:1: E305 expected 2 blank lines after class or function definition, found 1 PyAlex.py:64:18: E251 unexpected spaces around keyword / parameter equals PyAlex.py:64:20: E251 unexpected spaces around keyword / parameter equals PyAlex.py:65:18: E251 unexpected spaces around keyword / parameter equals PyAlex.py:65:20: E251 unexpected spaces around keyword / parameter equals PyAlex.py:65:40: W292 no newline at end of file
The following are pylint (only 2!) comments: Output:
{ "resource": ".../CodeReview/src/PyAlex.py", "owner": "python", "code": "W0612", "severity": 4, "message": "W0612:Unused variable 's_dirs'", "source": "pylint", "startLineNumber": 26, "startColumn": 15, "endLineNumber": 26, "endColumn": 15 { "resource": ".../CodeReview/src/PyAlex.py", "owner": "python", "code": "W0612", "severity": 4, "message": "W0612:Unused variable 's_dirs'", "source": "pylint", "startLineNumber": 41, "startColumn": 15, "endLineNumber": 41, "endColumn": 15 } }
(Sep-07-2018, 03:31 PM)Larz60+ Wrote: [ -> ]the following are pycodestyle (which checks for PEP8 compatibility) generated issues. Code can run perfectly well without being compliant, but you should strive to get there. Output:
PyAlex.py:3:10: E401 multiple imports on one line PyAlex.py:4:1: W293 blank line contains whitespace PyAlex.py:5:1: E265 block comment should start with '# ' PyAlex.py:5:80: E501 line too long (89 > 79 characters) PyAlex.py:6:1: E302 expected 2 blank lines, found 1 PyAlex.py:13:1: E305 expected 2 blank lines after class or function definition, found 0PyAlex.py:14:1: E265 block comment should start with '# ' PyAlex.py:14:80: E501 line too long (89 > 79 characters) PyAlex.py:15:1: W293 blank line contains whitespace PyAlex.py:18:23: E231 missing whitespace after ',' PyAlex.py:18:30: E231 missing whitespace after ',' PyAlex.py:18:37: E231 missing whitespace after ',' PyAlex.py:18:45: E231 missing whitespace after ',' PyAlex.py:18:52: E231 missing whitespace after ',' PyAlex.py:18:59: E231 missing whitespace after ',' PyAlex.py:18:66: E231 missing whitespace after ',' PyAlex.py:18:74: E231 missing whitespace after ',' PyAlex.py:18:80: E501 line too long (152 > 79 characters) PyAlex.py:18:81: E231 missing whitespace after ',' PyAlex.py:18:88: E231 missing whitespace after ',' PyAlex.py:18:95: E231 missing whitespace after ',' PyAlex.py:18:102: E231 missing whitespace after ',' PyAlex.py:18:109: E231 missing whitespace after ',' PyAlex.py:18:116: E231 missing whitespace after ',' PyAlex.py:18:122: E231 missing whitespace after ',' PyAlex.py:18:129: E231 missing whitespace after ',' PyAlex.py:18:136: E231 missing whitespace after ',' PyAlex.py:18:144: E231 missing whitespace after ',' PyAlex.py:21:1: W293 blank line contains whitespace PyAlex.py:22:1: E302 expected 2 blank lines, found 1 PyAlex.py:33:37: E231 missing whitespace after ',' PyAlex.py:35:80: E501 line too long (119 > 79 characters) PyAlex.py:37:1: W293 blank line contains whitespace PyAlex.py:38:1: E302 expected 2 blank lines, found 1 PyAlex.py:48:1: W293 blank line contains whitespace PyAlex.py:49:1: W293 blank line contains whitespace PyAlex.py:54:1: W293 blank line contains whitespace PyAlex.py:55:1: E302 expected 2 blank lines, found 1 PyAlex.py:60:80: E501 line too long (118 > 79 characters) PyAlex.py:60:109: E231 missing whitespace after ',' PyAlex.py:62:1: W293 blank line contains whitespace PyAlex.py:63:1: E305 expected 2 blank lines after class or function definition, found 1 PyAlex.py:64:18: E251 unexpected spaces around keyword / parameter equals PyAlex.py:64:20: E251 unexpected spaces around keyword / parameter equals PyAlex.py:65:18: E251 unexpected spaces around keyword / parameter equals PyAlex.py:65:20: E251 unexpected spaces around keyword / parameter equals PyAlex.py:65:40: W292 no newline at end of file
The following are pylint (only 2!) comments: Output:
{ "resource": ".../CodeReview/src/PyAlex.py", "owner": "python", "code": "W0612", "severity": 4, "message": "W0612:Unused variable 's_dirs'", "source": "pylint", "startLineNumber": 26, "startColumn": 15, "endLineNumber": 26, "endColumn": 15 { "resource": ".../CodeReview/src/PyAlex.py", "owner": "python", "code": "W0612", "severity": 4, "message": "W0612:Unused variable 's_dirs'", "source": "pylint", "startLineNumber": 41, "startColumn": 15, "endLineNumber": 41, "endColumn": 15 } }
I started writing in python 5 days ago and i did not know there was a pycodestyle.
what is the PEP8 combatability?
How can I make the code compatible with PEP8?