Simple log rotation scriptLocating matching files with input folder and file prefixBash Music PlayerSimple Log HandlerRead decibel level from a USB meter, display it as a live visualization, and send it via FTPSimple log writer classWriting to rotating log file via channelsWrite a message to a logScraping metrics from log filesTrace route based on Cisco routing table text outputSimple server log backup script utilising AWS

Idiom for 'person who gets violent when drunk"

Dedicated bike GPS computer over smartphone

Is fission/fusion to iron the most efficient way to convert mass to energy?

What does BREAD stand for while drafting?

Which are the methodologies for interpreting Vedas?

Why is isotope an issue in reading mass spectra?

Convert GE Load Center to main breaker

Why did the Death Eaters wait to reopen the Chamber of Secrets?

Why did Robert pick unworthy men for the White Cloaks?

Is it a good security practice to force employees hide their employer to avoid being targeted?

A life of PhD: is it feasible?

Approach sick days in feedback meeting

Does WiFi affect the quality of images downloaded from the internet?

Content Editor Web Part - SharePoint Online?

A team managed by my peer is close to melting down

Is tuition reimbursement a good idea if you have to stay with the job

My mom's return ticket is 3 days after I-94 expires

Fastest way from 8 to 7

Is there a frequency comparator device?

What plausible reason could I give for my FTL drive only working in space

Why would a car salesman tell me not to get my credit pulled again?

Part of my house is inexplicably gone

Can an open source licence be revoked if it violates employer's IP?

Why is my Taiyaki (Cake that looks like a fish) too hard and dry?



Simple log rotation script


Locating matching files with input folder and file prefixBash Music PlayerSimple Log HandlerRead decibel level from a USB meter, display it as a live visualization, and send it via FTPSimple log writer classWriting to rotating log file via channelsWrite a message to a logScraping metrics from log filesTrace route based on Cisco routing table text outputSimple server log backup script utilising AWS






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








8












$begingroup$


I created a simple script that implements log rotation. It checks the log file size (say, out.log) if it exceeds 5MB limit. If it does then the script copies it to out1.log and clears out.log. But it also copies out1.log to out2.log, etc. So log files are kind of 'shifted' and contents of the last one is discarded. In this implementation I keep only 5 files (initial out.log and out[1-4].log).



I am not a Python programmer, although I find this language extremely useful for such kinds of tasks. I would like my code to be as much idiomatic as possible, so what can I change/improve in my script for this?



from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser

SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5

class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix

def __str__(self):
return "[x].".format(self.suffix, self.prefix)

def __touch(self, file_name):
open(file_name, 'w').close()

def rotate(self):
files = [".".format(self.prefix, x + 1, self.suffix) for x in range(MAX_LOG_FILES_COUNT)]

[self.__touch(f) for f in files if not exists(f)]

current_log = ".".format(self.prefix, self.suffix)
if (getsize(current_log) < SIZE_5MB):
return

files.insert(0, current_log)

for i in range(len(files) - 1, 0, -1):
copyfile(files[i-1], files[i])

self.__touch(files[0])

if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")

args = parser.parse_args()

logRotator = LogRotator(args.prefix, args.suffix)
logRotator.rotate()









share|improve this question











$endgroup$











  • $begingroup$
    Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with self.prefix5.suffix.
    $endgroup$
    – Peilonrayz
    8 hours ago







  • 1




    $begingroup$
    @Peilonrayz yes, sure
    $endgroup$
    – xuesheng
    8 hours ago

















8












$begingroup$


I created a simple script that implements log rotation. It checks the log file size (say, out.log) if it exceeds 5MB limit. If it does then the script copies it to out1.log and clears out.log. But it also copies out1.log to out2.log, etc. So log files are kind of 'shifted' and contents of the last one is discarded. In this implementation I keep only 5 files (initial out.log and out[1-4].log).



I am not a Python programmer, although I find this language extremely useful for such kinds of tasks. I would like my code to be as much idiomatic as possible, so what can I change/improve in my script for this?



from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser

SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5

class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix

def __str__(self):
return "[x].".format(self.suffix, self.prefix)

def __touch(self, file_name):
open(file_name, 'w').close()

def rotate(self):
files = [".".format(self.prefix, x + 1, self.suffix) for x in range(MAX_LOG_FILES_COUNT)]

[self.__touch(f) for f in files if not exists(f)]

current_log = ".".format(self.prefix, self.suffix)
if (getsize(current_log) < SIZE_5MB):
return

files.insert(0, current_log)

for i in range(len(files) - 1, 0, -1):
copyfile(files[i-1], files[i])

self.__touch(files[0])

if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")

args = parser.parse_args()

logRotator = LogRotator(args.prefix, args.suffix)
logRotator.rotate()









share|improve this question











$endgroup$











  • $begingroup$
    Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with self.prefix5.suffix.
    $endgroup$
    – Peilonrayz
    8 hours ago







  • 1




    $begingroup$
    @Peilonrayz yes, sure
    $endgroup$
    – xuesheng
    8 hours ago













8












8








8





$begingroup$


I created a simple script that implements log rotation. It checks the log file size (say, out.log) if it exceeds 5MB limit. If it does then the script copies it to out1.log and clears out.log. But it also copies out1.log to out2.log, etc. So log files are kind of 'shifted' and contents of the last one is discarded. In this implementation I keep only 5 files (initial out.log and out[1-4].log).



I am not a Python programmer, although I find this language extremely useful for such kinds of tasks. I would like my code to be as much idiomatic as possible, so what can I change/improve in my script for this?



from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser

SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5

class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix

def __str__(self):
return "[x].".format(self.suffix, self.prefix)

def __touch(self, file_name):
open(file_name, 'w').close()

def rotate(self):
files = [".".format(self.prefix, x + 1, self.suffix) for x in range(MAX_LOG_FILES_COUNT)]

[self.__touch(f) for f in files if not exists(f)]

current_log = ".".format(self.prefix, self.suffix)
if (getsize(current_log) < SIZE_5MB):
return

files.insert(0, current_log)

for i in range(len(files) - 1, 0, -1):
copyfile(files[i-1], files[i])

self.__touch(files[0])

if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")

args = parser.parse_args()

logRotator = LogRotator(args.prefix, args.suffix)
logRotator.rotate()









share|improve this question











$endgroup$




I created a simple script that implements log rotation. It checks the log file size (say, out.log) if it exceeds 5MB limit. If it does then the script copies it to out1.log and clears out.log. But it also copies out1.log to out2.log, etc. So log files are kind of 'shifted' and contents of the last one is discarded. In this implementation I keep only 5 files (initial out.log and out[1-4].log).



I am not a Python programmer, although I find this language extremely useful for such kinds of tasks. I would like my code to be as much idiomatic as possible, so what can I change/improve in my script for this?



from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser

SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5

class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix

def __str__(self):
return "[x].".format(self.suffix, self.prefix)

def __touch(self, file_name):
open(file_name, 'w').close()

def rotate(self):
files = [".".format(self.prefix, x + 1, self.suffix) for x in range(MAX_LOG_FILES_COUNT)]

[self.__touch(f) for f in files if not exists(f)]

current_log = ".".format(self.prefix, self.suffix)
if (getsize(current_log) < SIZE_5MB):
return

files.insert(0, current_log)

for i in range(len(files) - 1, 0, -1):
copyfile(files[i-1], files[i])

self.__touch(files[0])

if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")

args = parser.parse_args()

logRotator = LogRotator(args.prefix, args.suffix)
logRotator.rotate()






python beginner python-3.x file logging






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 8 hours ago









dfhwze

2,258323




2,258323










asked 8 hours ago









xueshengxuesheng

1955




1955











  • $begingroup$
    Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with self.prefix5.suffix.
    $endgroup$
    – Peilonrayz
    8 hours ago







  • 1




    $begingroup$
    @Peilonrayz yes, sure
    $endgroup$
    – xuesheng
    8 hours ago
















  • $begingroup$
    Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with self.prefix5.suffix.
    $endgroup$
    – Peilonrayz
    8 hours ago







  • 1




    $begingroup$
    @Peilonrayz yes, sure
    $endgroup$
    – xuesheng
    8 hours ago















$begingroup$
Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with self.prefix5.suffix.
$endgroup$
– Peilonrayz
8 hours ago





$begingroup$
Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with self.prefix5.suffix.
$endgroup$
– Peilonrayz
8 hours ago





1




1




$begingroup$
@Peilonrayz yes, sure
$endgroup$
– xuesheng
8 hours ago




$begingroup$
@Peilonrayz yes, sure
$endgroup$
– xuesheng
8 hours ago










1 Answer
1






active

oldest

votes


















5












$begingroup$

  1. Return early, there's no need to touch files if the current file is <5mb.

  2. DRY your code, move the ".".format into it's own function.

  3. Don't use comprehensions for mutations. Use an explicit for loop for that.


  4. As you want idiomatic code, you should be aware that some people discourage the use of __ as it performs name mangling.



    If the function is intended to be private, not protected, then it should be ok.



  5. You can use the pairwise recipe to make the reversed pairwise loop. I think it's more readable, however you may not.

  6. It's unidiomatic to put brackets on if statements. Unless the statement spans multiple lines.

  7. It's idiomatic to put two newlines around top level classes and functions.

  8. I'm a bit confused why you want to touch everything.

  9. You may want to split all the different aspects of rotate into smaller functions to follow SRP.

Your code's pretty idiomatic otherwise, your naming conventions are good, you've used a main guard and you've got white space where it should be.



from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
import itertools


def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)


SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5


class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix

def __str__(self):
return "[x].".format(self.suffix, self.prefix)

def __touch(self, file_name):
open(file_name, 'w').close()

def _gen_file_name(self, name):
return ".".format(self.prefix, name, self.suffix)

def rotate(self):
current_log = self._gen_file_name('')
if getsize(current_log) < SIZE_5MB:
return

files = [
self._gen_file_name(i)
for i in range(1, MAX_LOG_FILES_COUNT + 1)
]

for file in files:
if not exests(file):
self.__touch(f)

for older, newer in pairwise(reversed([current_log] + files)):
copyfile(newer, older)

self.__touch(current_log)


if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")

args = parser.parse_args()

log_rotator = LogRotator(args.prefix, args.suffix)
log_rotator.rotate()





share|improve this answer









$endgroup$












  • $begingroup$
    "I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would be open(file_name, 'a').close()
    $endgroup$
    – Mathias Ettinger
    7 hours ago










  • $begingroup$
    @MathiasEttinger That's a good point. It makes sense if it's renamed to make_empty.
    $endgroup$
    – Peilonrayz
    7 hours ago










  • $begingroup$
    Also, 10. Using os.rename instead of shutil.copyfile since you don't need copies anyway.
    $endgroup$
    – Mathias Ettinger
    7 hours ago










  • $begingroup$
    @MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
    $endgroup$
    – Peilonrayz
    7 hours ago










  • $begingroup$
    No, I shouldn't even be here now, I’m a bit in a hurry now ;)
    $endgroup$
    – Mathias Ettinger
    7 hours ago











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f222089%2fsimple-log-rotation-script%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5












$begingroup$

  1. Return early, there's no need to touch files if the current file is <5mb.

  2. DRY your code, move the ".".format into it's own function.

  3. Don't use comprehensions for mutations. Use an explicit for loop for that.


  4. As you want idiomatic code, you should be aware that some people discourage the use of __ as it performs name mangling.



    If the function is intended to be private, not protected, then it should be ok.



  5. You can use the pairwise recipe to make the reversed pairwise loop. I think it's more readable, however you may not.

  6. It's unidiomatic to put brackets on if statements. Unless the statement spans multiple lines.

  7. It's idiomatic to put two newlines around top level classes and functions.

  8. I'm a bit confused why you want to touch everything.

  9. You may want to split all the different aspects of rotate into smaller functions to follow SRP.

Your code's pretty idiomatic otherwise, your naming conventions are good, you've used a main guard and you've got white space where it should be.



from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
import itertools


def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)


SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5


class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix

def __str__(self):
return "[x].".format(self.suffix, self.prefix)

def __touch(self, file_name):
open(file_name, 'w').close()

def _gen_file_name(self, name):
return ".".format(self.prefix, name, self.suffix)

def rotate(self):
current_log = self._gen_file_name('')
if getsize(current_log) < SIZE_5MB:
return

files = [
self._gen_file_name(i)
for i in range(1, MAX_LOG_FILES_COUNT + 1)
]

for file in files:
if not exests(file):
self.__touch(f)

for older, newer in pairwise(reversed([current_log] + files)):
copyfile(newer, older)

self.__touch(current_log)


if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")

args = parser.parse_args()

log_rotator = LogRotator(args.prefix, args.suffix)
log_rotator.rotate()





share|improve this answer









$endgroup$












  • $begingroup$
    "I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would be open(file_name, 'a').close()
    $endgroup$
    – Mathias Ettinger
    7 hours ago










  • $begingroup$
    @MathiasEttinger That's a good point. It makes sense if it's renamed to make_empty.
    $endgroup$
    – Peilonrayz
    7 hours ago










  • $begingroup$
    Also, 10. Using os.rename instead of shutil.copyfile since you don't need copies anyway.
    $endgroup$
    – Mathias Ettinger
    7 hours ago










  • $begingroup$
    @MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
    $endgroup$
    – Peilonrayz
    7 hours ago










  • $begingroup$
    No, I shouldn't even be here now, I’m a bit in a hurry now ;)
    $endgroup$
    – Mathias Ettinger
    7 hours ago















5












$begingroup$

  1. Return early, there's no need to touch files if the current file is <5mb.

  2. DRY your code, move the ".".format into it's own function.

  3. Don't use comprehensions for mutations. Use an explicit for loop for that.


  4. As you want idiomatic code, you should be aware that some people discourage the use of __ as it performs name mangling.



    If the function is intended to be private, not protected, then it should be ok.



  5. You can use the pairwise recipe to make the reversed pairwise loop. I think it's more readable, however you may not.

  6. It's unidiomatic to put brackets on if statements. Unless the statement spans multiple lines.

  7. It's idiomatic to put two newlines around top level classes and functions.

  8. I'm a bit confused why you want to touch everything.

  9. You may want to split all the different aspects of rotate into smaller functions to follow SRP.

Your code's pretty idiomatic otherwise, your naming conventions are good, you've used a main guard and you've got white space where it should be.



from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
import itertools


def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)


SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5


class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix

def __str__(self):
return "[x].".format(self.suffix, self.prefix)

def __touch(self, file_name):
open(file_name, 'w').close()

def _gen_file_name(self, name):
return ".".format(self.prefix, name, self.suffix)

def rotate(self):
current_log = self._gen_file_name('')
if getsize(current_log) < SIZE_5MB:
return

files = [
self._gen_file_name(i)
for i in range(1, MAX_LOG_FILES_COUNT + 1)
]

for file in files:
if not exests(file):
self.__touch(f)

for older, newer in pairwise(reversed([current_log] + files)):
copyfile(newer, older)

self.__touch(current_log)


if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")

args = parser.parse_args()

log_rotator = LogRotator(args.prefix, args.suffix)
log_rotator.rotate()





share|improve this answer









$endgroup$












  • $begingroup$
    "I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would be open(file_name, 'a').close()
    $endgroup$
    – Mathias Ettinger
    7 hours ago










  • $begingroup$
    @MathiasEttinger That's a good point. It makes sense if it's renamed to make_empty.
    $endgroup$
    – Peilonrayz
    7 hours ago










  • $begingroup$
    Also, 10. Using os.rename instead of shutil.copyfile since you don't need copies anyway.
    $endgroup$
    – Mathias Ettinger
    7 hours ago










  • $begingroup$
    @MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
    $endgroup$
    – Peilonrayz
    7 hours ago










  • $begingroup$
    No, I shouldn't even be here now, I’m a bit in a hurry now ;)
    $endgroup$
    – Mathias Ettinger
    7 hours ago













5












5








5





$begingroup$

  1. Return early, there's no need to touch files if the current file is <5mb.

  2. DRY your code, move the ".".format into it's own function.

  3. Don't use comprehensions for mutations. Use an explicit for loop for that.


  4. As you want idiomatic code, you should be aware that some people discourage the use of __ as it performs name mangling.



    If the function is intended to be private, not protected, then it should be ok.



  5. You can use the pairwise recipe to make the reversed pairwise loop. I think it's more readable, however you may not.

  6. It's unidiomatic to put brackets on if statements. Unless the statement spans multiple lines.

  7. It's idiomatic to put two newlines around top level classes and functions.

  8. I'm a bit confused why you want to touch everything.

  9. You may want to split all the different aspects of rotate into smaller functions to follow SRP.

Your code's pretty idiomatic otherwise, your naming conventions are good, you've used a main guard and you've got white space where it should be.



from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
import itertools


def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)


SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5


class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix

def __str__(self):
return "[x].".format(self.suffix, self.prefix)

def __touch(self, file_name):
open(file_name, 'w').close()

def _gen_file_name(self, name):
return ".".format(self.prefix, name, self.suffix)

def rotate(self):
current_log = self._gen_file_name('')
if getsize(current_log) < SIZE_5MB:
return

files = [
self._gen_file_name(i)
for i in range(1, MAX_LOG_FILES_COUNT + 1)
]

for file in files:
if not exests(file):
self.__touch(f)

for older, newer in pairwise(reversed([current_log] + files)):
copyfile(newer, older)

self.__touch(current_log)


if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")

args = parser.parse_args()

log_rotator = LogRotator(args.prefix, args.suffix)
log_rotator.rotate()





share|improve this answer









$endgroup$



  1. Return early, there's no need to touch files if the current file is <5mb.

  2. DRY your code, move the ".".format into it's own function.

  3. Don't use comprehensions for mutations. Use an explicit for loop for that.


  4. As you want idiomatic code, you should be aware that some people discourage the use of __ as it performs name mangling.



    If the function is intended to be private, not protected, then it should be ok.



  5. You can use the pairwise recipe to make the reversed pairwise loop. I think it's more readable, however you may not.

  6. It's unidiomatic to put brackets on if statements. Unless the statement spans multiple lines.

  7. It's idiomatic to put two newlines around top level classes and functions.

  8. I'm a bit confused why you want to touch everything.

  9. You may want to split all the different aspects of rotate into smaller functions to follow SRP.

Your code's pretty idiomatic otherwise, your naming conventions are good, you've used a main guard and you've got white space where it should be.



from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
import itertools


def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)


SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5


class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix

def __str__(self):
return "[x].".format(self.suffix, self.prefix)

def __touch(self, file_name):
open(file_name, 'w').close()

def _gen_file_name(self, name):
return ".".format(self.prefix, name, self.suffix)

def rotate(self):
current_log = self._gen_file_name('')
if getsize(current_log) < SIZE_5MB:
return

files = [
self._gen_file_name(i)
for i in range(1, MAX_LOG_FILES_COUNT + 1)
]

for file in files:
if not exests(file):
self.__touch(f)

for older, newer in pairwise(reversed([current_log] + files)):
copyfile(newer, older)

self.__touch(current_log)


if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")

args = parser.parse_args()

log_rotator = LogRotator(args.prefix, args.suffix)
log_rotator.rotate()






share|improve this answer












share|improve this answer



share|improve this answer










answered 8 hours ago









PeilonrayzPeilonrayz

28.5k344118




28.5k344118











  • $begingroup$
    "I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would be open(file_name, 'a').close()
    $endgroup$
    – Mathias Ettinger
    7 hours ago










  • $begingroup$
    @MathiasEttinger That's a good point. It makes sense if it's renamed to make_empty.
    $endgroup$
    – Peilonrayz
    7 hours ago










  • $begingroup$
    Also, 10. Using os.rename instead of shutil.copyfile since you don't need copies anyway.
    $endgroup$
    – Mathias Ettinger
    7 hours ago










  • $begingroup$
    @MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
    $endgroup$
    – Peilonrayz
    7 hours ago










  • $begingroup$
    No, I shouldn't even be here now, I’m a bit in a hurry now ;)
    $endgroup$
    – Mathias Ettinger
    7 hours ago
















  • $begingroup$
    "I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would be open(file_name, 'a').close()
    $endgroup$
    – Mathias Ettinger
    7 hours ago










  • $begingroup$
    @MathiasEttinger That's a good point. It makes sense if it's renamed to make_empty.
    $endgroup$
    – Peilonrayz
    7 hours ago










  • $begingroup$
    Also, 10. Using os.rename instead of shutil.copyfile since you don't need copies anyway.
    $endgroup$
    – Mathias Ettinger
    7 hours ago










  • $begingroup$
    @MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
    $endgroup$
    – Peilonrayz
    7 hours ago










  • $begingroup$
    No, I shouldn't even be here now, I’m a bit in a hurry now ;)
    $endgroup$
    – Mathias Ettinger
    7 hours ago















$begingroup$
"I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would be open(file_name, 'a').close()
$endgroup$
– Mathias Ettinger
7 hours ago




$begingroup$
"I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would be open(file_name, 'a').close()
$endgroup$
– Mathias Ettinger
7 hours ago












$begingroup$
@MathiasEttinger That's a good point. It makes sense if it's renamed to make_empty.
$endgroup$
– Peilonrayz
7 hours ago




$begingroup$
@MathiasEttinger That's a good point. It makes sense if it's renamed to make_empty.
$endgroup$
– Peilonrayz
7 hours ago












$begingroup$
Also, 10. Using os.rename instead of shutil.copyfile since you don't need copies anyway.
$endgroup$
– Mathias Ettinger
7 hours ago




$begingroup$
Also, 10. Using os.rename instead of shutil.copyfile since you don't need copies anyway.
$endgroup$
– Mathias Ettinger
7 hours ago












$begingroup$
@MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
$endgroup$
– Peilonrayz
7 hours ago




$begingroup$
@MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
$endgroup$
– Peilonrayz
7 hours ago












$begingroup$
No, I shouldn't even be here now, I’m a bit in a hurry now ;)
$endgroup$
– Mathias Ettinger
7 hours ago




$begingroup$
No, I shouldn't even be here now, I’m a bit in a hurry now ;)
$endgroup$
– Mathias Ettinger
7 hours ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f222089%2fsimple-log-rotation-script%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480