I've created a small python script to toggle between two files I'm using for testing.
My question is, what is a good Python format style for the following code:
import filecmp
import shutil
local = "local.txt"
remote = "remote.txt"
config_file = "C:\some\path\file.txt"
shutil.copyfile( remote if( filecmp.cmp(local, config_file ) ) else local, config_file )
Or
shutil.copyfile( remote
if( filecmp.cmp(local, config_file ) )
else local,
config_file )
Or
tocopy = remote if( filecmp.cmp( local, config_file ) ) else local
shutil.copyfile( tocopy, config_file )
Or what?
Also, what is the preferred way to name var in python for many-word names, is it "to_copy", "tocopy", "toCopy", "ToCopy"
-
There are a number of questions on related topics. See stackoverflow.com/questions/159720/… for example.S.Lott– S.Lott2008年12月01日 19:03:20 +00:00Commented Dec 1, 2008 at 19:03
-
Actually the question was mo tin the if:else: "ternary" for python, but once here, why not re-ask for var names. Thaks for the link thoughOscarRyz– OscarRyz2008年12月01日 19:17:38 +00:00Commented Dec 1, 2008 at 19:17
-
To me the 'y if x else z' is just way too perlish for me. Split it out into an if/else like Greg has shown.Brian C. Lane– Brian C. Lane2008年12月02日 01:02:29 +00:00Commented Dec 2, 2008 at 1:02
-
Careful with the backslashes - remember these are treated as escapes, so if your path was "C:\some\path\test.txt" you'll be scratching your head until you realise its trying to open "path[TAB]est.txt". Better to use forward slashes, or failing that, escape using \\ or raw strings.Brian– Brian2008年12月02日 02:06:22 +00:00Commented Dec 2, 2008 at 2:06
-
@Brian: Really? I though this was an advantage over other languages. Do you have any idea of why did this work ? ( didn't it? :-/ )OscarRyz– OscarRyz2008年12月03日 03:24:01 +00:00Commented Dec 3, 2008 at 3:24
5 Answers 5
For the conditional statement, I would probably go with:
if filecmp.cmp(local, config_file):
shutil.copyfile(remote, config_file)
else:
shutil.copyfile(local, config_file)
There's little need to use the inline y if x else z in this case, since the surrounding code is simple enough.
9 Comments
From the Python Style Guide:
With regard to listing out a compound expression:
Compound statements (multiple statements on the same line) are generally discouraged.
Yes:
if foo == 'blah':
do_blah_thing()
do_one()
do_two()
do_three()
Or for the code you supplied, Greg's example is a good one:
if filecmp.cmp(local, config_file):
shutil.copyfile(remote, config_file)
else:
shutil.copyfile(local, config_file)
Rather not:
if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()
Method Names and Instance Variables
Use the function naming rules: lowercase with words separated by underscores as necessary to improve readability.
Update: Per Oscar's request, also listed how his code would look in this fashion.
Comments
The third option looks the most natural to me, although your use of spaces in side parentheses and superfluous parentheses contradict the Python style guide.
That guide also answers the to_copy question, but I would probably use clearer names altogether.
I would write it as:
import filecmp
import shutil
local = "local.txt"
remote = "remote.txt"
destination = r"C:\some\path\file.txt"
source = remote if filecmp.cmp(local, destination) else local
shutil.copyfile(source, destination)
3 Comments
The most common naming I've seen is underscode separated words, to_copy.
As for the format style, I've seen no such agreement. I find
source = remote if filecmp.cmp(local, config_file) else local
shutil.copyfile(source, config_file)
to be the clearest among your options.
And seeing that everyone prefers to split the if I'd, at the very least, encapsulate the copyfile call in case you someday wish to change it:
def copy_to(source, destination):
shutil.copyfile(source,destination)
if filecmp.cmp(local, config_file):
copy_to(remote, config_file)
else:
copy_to(local, config_file)
7 Comments
What about:
import filecmp
import shutil
local = "local.txt"
remote = "remote.txt"
config_file = "C:\some\path\file.txt"
if filecmp.cmp( local, config_file):
to_copy = remote
else:
to_copy = local
shutil.copyfile( to_copy, config_file )
yikes, this open id screen name looks terrible.