0

I am a beginner. I wanted to write a script to clean-up old Google Chrome application versions from its folder and I was wondering how to "sort" application version numbers in an efficient way. What's the best way to do this?

Here my "spring cleaning" script:

import os
import sys
import re
import operator
import shutil


GOOGLE_CHROME_APPLICATION_VERSIONS_DIRECTORY=r'C:\Documents and Settings\Administrator\Local Settings\Application Data\Google\Chrome\Application'

REGEX_GOOGLE_CHROME_APPLICATION_VERSION = '^(\d+)\.(\d+)\.(\d+)\.(\d+)$'


def sorted_by_version_number(versions_list):
  version_elements = len(versions_list[0])
  for index in range(version_elements-1, 0, -1):
    key = operator.itemgetter(index)
    versions_list.sort(key=key, reverse = True)
  return versions_list


def delete_old_google_chrome_versions():
  REGEX = re.compile(REGEX_GOOGLE_CHROME_APPLICATION_VERSION)
  application_versions = []
  for filename in os.listdir(GOOGLE_CHROME_APPLICATION_VERSIONS_DIRECTORY):
    filepath = os.path.join(GOOGLE_CHROME_APPLICATION_VERSIONS_DIRECTORY,
                            filename)
    if os.path.isdir(filepath):
      match = REGEX.match(filename)
      if match:
        version = (v1, v2, v3, v4) = [int(match.group(i)) for i in range(1, 5)]
        application_versions.append(version)
  versions_to_delete = sorted_by_version_number(application_versions)[1:]
  for application_version in versions_to_delete:
    directory_name = '.'.join([str(v) for v in application_version])
    directory_path = os.path.join(GOOGLE_CHROME_APPLICATION_VERSIONS_DIRECTORY,
                                  directory_name)
    print('Deleting Google Chrome application directory: {}...'.format(
      directory_path))
    # shutil.rmtree(directory_path)


def main():
  delete_old_google_chrome_versions()


if __name__ == '__main__':
  main()

Optionally, how could this script be improved? Any advice would help as I am really useless in Python.

Robottinosino
  • 10,384
  • 17
  • 59
  • 97

1 Answers1

0

My answer to this question converts directly into Python:

How to compare software version number using js? (only number)

You can use this comparison algorithm to sort the list.

Community
  • 1
  • 1
Joe
  • 46,419
  • 33
  • 155
  • 245
  • My intuition was that my stable sorting of dotted string values, from the least significant to the most, considering them as int values, is more efficient than running your "Comparator" function many times as an auxiliary method to typical "collections" sorting? (e.g. Quicksort for certain types, Mergesort for other types, etc.) – Robottinosino Feb 21 '13 at 13:37
  • You could convert them into comparable values if you knew how many sections there were to the number. But unless you're doing thousands this won't have much impact at all. Computers are made for calculating things! – Joe Feb 21 '13 at 13:54
  • I am a beginner with high-school only education. I practice computer science as a hobby, I'll never get a job in it. Since it's for fun, I wanna learn to do it right. I really dislike the idea of just "throwing things at computers to calculate" if I can exercise my brain and find a solution there, or learn it from other users here, just because computers can do wasteful computation.. "fast" :) – Robottinosino Feb 21 '13 at 13:59
  • I absolutely understand. Everyone feels like that. What I am suggesting is a correct way to do it. There many be many correct ways to do it. There's always a trade-off (even if you're doing it for fun) between how long it takes you to do it and how long it takes the computer to do it. Two hours of your time to save two microseconds of computation time is worth considering. Programming skill also includes making decisions like this! – Joe Feb 21 '13 at 14:15
  • Comparing two integers that fit into CPU registers will take minimal O(1) time. Everything else requires a sequence of operations, possibly recursive, possibly complex. Sorting a list of strings, for example, requires lots of string comparisons which can be O(N) per string. – Joe Feb 21 '13 at 14:18
  • If you wanted to optimise the algorithm you could go through the list and convert them into tuples of integers to represent the points. This could perform all the string operations up-top and then rely on sorting tuples of integers. – Joe Feb 21 '13 at 14:19
  • Agree. I find two hours of time invested in understanding something more fun than a microsecond getting the right answer with no clue about why/how/etc.. I may change my mind as my life draws to a close and time becomes more clearly limited.. – Robottinosino Feb 21 '13 at 14:19
  • Don't I do the "to tuple of ints" conversion already in my code? Have you read the code? :) – Robottinosino Feb 21 '13 at 14:20
  • In true SO style, don't give me an approach, give me an _example_ of a more optimised algorithm, please! :) I am here to learn.. – Robottinosino Feb 21 '13 at 14:21
  • Yes you are indeed doing that optimisation in splitting the strings. The JS code I linked to explains exactly how and why. If you do implement it then you'll get an idea about recursion. Now *that's* a fun topic. – Joe Feb 21 '13 at 14:35
  • (I just checked. I didn't write it in a recursive style, but you could.) – Joe Feb 21 '13 at 14:53
  • Thanks for the tick @Robottinosino, sorry you didn't find a way that you found more intellectually satisfying. – Joe Apr 05 '13 at 08:41