From 26c03761b9495a7d5e3435d88ead993734cc0ea5 Mon Sep 17 00:00:00 2001 From: cockroacher <163405488+cockroacher@users.noreply.github.com> Date: Tue, 9 Apr 2024 15:18:11 +0200 Subject: [PATCH 1/3] pylint #294 --- carbon-rating.py | 138 ---------- carbon_rating.py | 185 ++++++++++++++ tests/energy_efficiency_carbon_percentiles.py | 241 ++++++++++-------- ...nergy_efficiency_carbon_percentiles2021.py | 241 ++++++++++-------- ...nergy_efficiency_carbon_percentiles2022.py | 241 ++++++++++-------- 5 files changed, 572 insertions(+), 474 deletions(-) delete mode 100644 carbon-rating.py create mode 100644 carbon_rating.py diff --git a/carbon-rating.py b/carbon-rating.py deleted file mode 100644 index 42105dbb..00000000 --- a/carbon-rating.py +++ /dev/null @@ -1,138 +0,0 @@ -# -*- coding: utf-8 -*- -import sys -import getopt -import gettext -import math -import json -import gettext -from datetime import datetime - -FIELD_INDEX_DATE = 0 -FIELD_INDEX_DATA = 1 - - -def getPercentile(arr, percentile): - percentile = min(100, max(0, percentile)) - index = (percentile / 100) * (len(arr) - 1) - fractionPart = index - math.floor(index) - intPart = math.floor(index) - percentile = float(arr[intPart]) - - if fractionPart > 0: - percentile += fractionPart * \ - (float(arr[intPart + 1]) - float(arr[intPart])) - else: - percentile += 0 - - return percentile - - -def write(output_filename, content): - with open(output_filename, 'w') as outfile: - outfile.write(content) - - -def main(argv): - """ - WebPerf Core Carbon Percentiles - - - Usage: - * run webperf-core test on all websites you want to use for your percentiles (with json as output file) - * run this file against your output file, for example like this: carbon-rating.py -i data\carbon-references-2022.json -o tests\energy_efficiency_carbon_percentiles.py - - Options and arguments: - -h/--help\t\t\t: Help information on how to use script - -i/--input \t: input file path (.json) - -o/--output \t: output file path (.py) - """ - - output_filename = '' - input_filename = '' - langCode = 'en' - language = False - - # add support for default (en) language - language = gettext.translation( - 'webperf-core', localedir='locales', languages=[langCode]) - language.install() - _ = language.gettext - - try: - opts, args = getopt.getopt( - argv, "hi:o:", ["help", "input=", "output="]) - except getopt.GetoptError: - print(main.__doc__) - sys.exit(2) - - if (opts.__len__() == 0): - print(main.__doc__) - sys.exit(2) - - for opt, arg in opts: - if opt in ('-h', '--help'): # help - print(main.__doc__) - sys.exit(2) - elif opt in ("-i", "--input"): # input file path - input_filename = arg - from engines.json_engine import read_tests - elif opt in ("-o", "--output"): # output file path - output_filename = arg - pass - - tests = read_tests(input_filename, 0, -1) - generated_date = False - co2s = [] - - for test in tests: - if not generated_date: - generated_date = datetime.fromisoformat( - test[FIELD_INDEX_DATE]).strftime('%Y-%m-%d') - - str_data = test[FIELD_INDEX_DATA].replace('\'', '"') - data = json.loads(str_data) - print(str_data) - co2s.append(data['co2']) - - if not generated_date: - generated_date = datetime.today().strftime('%Y-%m-%d') - - output_content = "# This array was last generated with carbon-rating.py on {0}\n".format( - generated_date) - output_content += "def get_generated_date():\n" - output_content += "\treturn '{0}'\n".format( - generated_date) - output_content += "\n" - output_content += "def get_percentiles():\n" - output_content += "\treturn [\n" - - co2s_sorted = sorted(co2s) - - intervals = [] - - index = 1 - while (index <= 100): - percentile = getPercentile(co2s_sorted, index) - intervals.append(percentile) - position = index - 1 - if index < 100: - if position % 10 == 0 and position != 0: - output_content += "\t\t# {0} percentile\n".format(position) - - output_content += "\t\t{0},\n".format(percentile) - else: - output_content += "\t\t{0}\n".format(percentile) - index += 1 - - output_content += "\t]" - - print(output_content) - if (len(output_filename) > 0): - write(output_filename, output_content) - - -""" -If file is executed on itself then call a definition, mostly for testing purposes -""" -if __name__ == '__main__': - main(sys.argv[1:]) diff --git a/carbon_rating.py b/carbon_rating.py new file mode 100644 index 00000000..6b3b1bfe --- /dev/null +++ b/carbon_rating.py @@ -0,0 +1,185 @@ +# -*- coding: utf-8 -*- +import sys +import getopt +import math +import json +from datetime import datetime +from engines.json_engine import read_tests + +FIELD_INDEX_DATE = 0 +FIELD_INDEX_DATA = 1 + +def get_percentile(arr, percentile): + """ + Calculate the percentile of a list of values. + + Parameters: + arr (list): A list of numerical data. + percentile (float): The percentile to compute, which must be between 0 and 100 inclusive. + + Returns: + float: The percentile of the values in the `arr`. + + This function uses linear interpolation between data points if + the desired percentile lies between two data points `i` and `j`: + percentile = (1 - fraction) * arr[i] + fraction * arr[j] + where `fraction` is the fractional part of the index surrounded by `i` and `j`. + """ + percentile = min(100, max(0, percentile)) + index = (percentile / 100) * (len(arr) - 1) + fraction_part = index - math.floor(index) + int_part = math.floor(index) + percentile = float(arr[int_part]) + + if fraction_part > 0: + percentile += fraction_part * \ + (float(arr[int_part + 1]) - float(arr[int_part])) + else: + percentile += 0 + + return percentile + + +def write(output_filename, content): + """ + Write a string to a file. + + Parameters: + output_filename (str): The name of the file to write to. + content (str): The content to write to the file. + + This function will overwrite the existing content of the file. + """ + with open(output_filename, 'w', encoding='utf-8') as outfile: + outfile.write(content) + +def generate_content(co2s, generated_date): + """ + Generate a Python script that includes the date of generation and percentiles of CO2 data. + + Parameters: + co2s (list): A list of CO2 data. + generated_date (str): The date when the data was generated. + + Returns: + str: A string that represents a Python script. The script includes two functions: + - get_generated_date(): Returns the date when the data was generated. + - get_percentiles(): Returns a list of percentiles of the CO2 data. + + The percentiles are calculated for each integer from 1 to 100. + The percentile values are sorted in ascending order. + If the percentile position is a multiple of 10, + a comment indicating the percentile is added before the value. + """ + output_content = f"# This array was last generated with carbon-rating.py on {generated_date}\n" + output_content += "def get_generated_date():\n" + output_content += " \"\"\"\n" + output_content += " Get the date when the data was generated.\n" + output_content += "\n" + output_content += " Returns:\n" + output_content += " str: A string that represents the date when the data was generated.\n" + output_content += " The date is in the format 'YYYY-MM-DD'.\n" + output_content += " \"\"\"\n" + output_content += f" return '{generated_date}'\n" + output_content += "\n" + output_content += "def get_percentiles():\n" + output_content += " \"\"\"\n" + output_content += " Get the precomputed percentiles of CO2 data.\n" + output_content += "\n" + output_content += " Returns:\n" + output_content += " list: A list of precomputed percentiles of the CO2 data.\n" + output_content += " The list contains 100 elements,\n" + output_content += " each representing the percentile from 1 to 100.\n" + output_content += " The percentiles are sorted in ascending order.\n" + output_content += " The percentile values are floats.\n" + output_content += " \"\"\"\n" + output_content += " return [\n" + + co2s_sorted = sorted(co2s) + + intervals = [] + + index = 1 + while index <= 100: + percentile = get_percentile(co2s_sorted, index) + intervals.append(percentile) + position = index - 1 + if index < 100: + if position % 10 == 0 and position != 0: + output_content += f" # {position} percentile\n" + + output_content += f" {percentile},\n" + else: + output_content += f" {percentile}\n" + index += 1 + + output_content += " ]\n" + return output_content + + +def main(argv): + """ + WebPerf Core Carbon Percentiles + + Usage: + * run webperf-core test on all websites you want to + use for your percentiles (with json as output file) + * run this file against your output file, for example like this: + carbon-rating.py + -i data\\carbon-references-2022.json + -o tests\\energy_efficiency_carbon_percentiles.py + + Options and arguments: + -h/--help\t\t\t: Help information on how to use script + -i/--input \t: input file path (.json) + -o/--output \t: output file path (.py) + """ + + output_filename = '' + input_filename = '' + + try: + opts, _ = getopt.getopt( + argv, "hi:o:", ["help", "input=", "output="]) + except getopt.GetoptError: + print(main.__doc__) + sys.exit(2) + + if len(opts) == 0: + print(main.__doc__) + sys.exit(2) + + for opt, arg in opts: + if opt in ('-h', '--help'): # help + print(main.__doc__) + sys.exit(2) + elif opt in ("-i", "--input"): # input file path + input_filename = arg + elif opt in ("-o", "--output"): # output file path + output_filename = arg + + tests = read_tests(input_filename, 0, -1) + generated_date = False + co2s = [] + + for test in tests: + if not generated_date: + generated_date = datetime.fromisoformat( + test[FIELD_INDEX_DATE]).strftime('%Y-%m-%d') + + str_data = test[FIELD_INDEX_DATA].replace('\'', '"') + data = json.loads(str_data) + print(str_data) + co2s.append(data['co2']) + + if not generated_date: + generated_date = datetime.today().strftime('%Y-%m-%d') + + output_content = generate_content(co2s, generated_date) + print(output_content) + if len(output_filename) > 0: + write(output_filename, output_content) + + +if __name__ == '__main__': + main(sys.argv[1:]) diff --git a/tests/energy_efficiency_carbon_percentiles.py b/tests/energy_efficiency_carbon_percentiles.py index 14e41bde..87bae2e1 100644 --- a/tests/energy_efficiency_carbon_percentiles.py +++ b/tests/energy_efficiency_carbon_percentiles.py @@ -1,116 +1,133 @@ # This array was last generated with carbon-rating.py on 2024-03-31 def get_generated_date(): - return '2024-03-31' + """ + Get the date when the data was generated. + + Returns: + str: A string that represents the date when the data was generated. + The date is in the format 'YYYY-MM-DD'. + """ + return '2024-03-31' def get_percentiles(): - return [ - 0.08354936412472742, - 0.13657804459389297, - 0.18515692842479328, - 0.22692933166886212, - 0.2525221250193426, - 0.2874776061070385, - 0.31191066180402416, - 0.344436006239173, - 0.36667539186773357, - 0.39758132844476496, - # 10 percentile - 0.42584487272235566, - 0.4464283894000808, - 0.46531171471399724, - 0.48831765141088984, - 0.510851378373918, - 0.5317421617470216, - 0.5543698387214449, - 0.5791470562671542, - 0.6028803097745404, - 0.6186698679652763, - # 20 percentile - 0.631780596878333, - 0.6463543994801352, - 0.6655098084641156, - 0.6942654328065225, - 0.7170844340597977, - 0.7372893775268226, - 0.7558947352377232, - 0.7787478584792232, - 0.8010871503955684, - 0.8200965462073219, - # 30 percentile - 0.8394079517579637, - 0.8574583870709639, - 0.8799363854723051, - 0.8971731259099208, - 0.9300173361049964, - 0.9541632124678697, - 0.9763189208532217, - 0.9938500749687432, - 1.0212998279130319, - 1.0504173141319773, - # 40 percentile - 1.0768987260450609, - 1.0988630619226023, - 1.1215843754201895, - 1.1494796192552659, - 1.1679330798609882, - 1.196162212776602, - 1.2189920173166087, - 1.2507048967148173, - 1.2774594292205759, - 1.307289932363783, - # 50 percentile - 1.3392636821991764, - 1.3570009668838463, - 1.38218706954075, - 1.411758462603763, - 1.4452612874577753, - 1.4804685478239554, - 1.5103988125680712, - 1.5413658552356755, - 1.5866408142562025, - 1.6249815582929876, - # 60 percentile - 1.657838934993767, - 1.7014340311565435, - 1.7496255158773157, - 1.7901508147093466, - 1.8204601590682286, - 1.8672358344797044, - 1.9200499443119627, - 1.9582236473505616, - 2.024268769118678, - 2.087484008510713, - # 70 percentile - 2.159640824487759, - 2.228946874949638, - 2.3116927446237296, - 2.393050343723898, - 2.4580033448385072, - 2.540090069335118, - 2.6137265979916324, - 2.6977107944072927, - 2.7708533379551255, - 2.87857855274051, - # 80 percentile - 2.9831052229901087, - 3.0822705451690124, - 3.205326033772178, - 3.3414577193540285, - 3.4742567115516865, - 3.662649320810405, - 3.824050304002779, - 4.022925911478537, - 4.2230293800118375, - 4.470116463111714, - # 90 percentile - 4.695625259033334, - 4.9861721319525785, - 5.365176268398273, - 5.774669570875075, - 6.345150944315008, - 7.2068164399044585, - 8.434910226292136, - 10.216039042627969, - 13.186847352152784, - 40.041936754347404 - ] \ No newline at end of file + """ + Get the precomputed percentiles of CO2 data. + + Returns: + list: A list of precomputed percentiles of the CO2 data. + The list contains 100 elements, + each representing the percentile from 1 to 100. + The percentiles are sorted in ascending order. + The percentile values are floats. + """ + return [ + 0.08354936412472742, + 0.13657804459389297, + 0.18515692842479328, + 0.22692933166886212, + 0.2525221250193426, + 0.2874776061070385, + 0.31191066180402416, + 0.344436006239173, + 0.36667539186773357, + 0.39758132844476496, + # 10 percentile + 0.42584487272235566, + 0.4464283894000808, + 0.46531171471399724, + 0.48831765141088984, + 0.510851378373918, + 0.5317421617470216, + 0.5543698387214449, + 0.5791470562671542, + 0.6028803097745404, + 0.6186698679652763, + # 20 percentile + 0.631780596878333, + 0.6463543994801352, + 0.6655098084641156, + 0.6942654328065225, + 0.7170844340597977, + 0.7372893775268226, + 0.7558947352377232, + 0.7787478584792232, + 0.8010871503955684, + 0.8200965462073219, + # 30 percentile + 0.8394079517579637, + 0.8574583870709639, + 0.8799363854723051, + 0.8971731259099208, + 0.9300173361049964, + 0.9541632124678697, + 0.9763189208532217, + 0.9938500749687432, + 1.0212998279130319, + 1.0504173141319773, + # 40 percentile + 1.0768987260450609, + 1.0988630619226023, + 1.1215843754201895, + 1.1494796192552659, + 1.1679330798609882, + 1.196162212776602, + 1.2189920173166087, + 1.2507048967148173, + 1.2774594292205759, + 1.307289932363783, + # 50 percentile + 1.3392636821991764, + 1.3570009668838463, + 1.38218706954075, + 1.411758462603763, + 1.4452612874577753, + 1.4804685478239554, + 1.5103988125680712, + 1.5413658552356755, + 1.5866408142562025, + 1.6249815582929876, + # 60 percentile + 1.657838934993767, + 1.7014340311565435, + 1.7496255158773157, + 1.7901508147093466, + 1.8204601590682286, + 1.8672358344797044, + 1.9200499443119627, + 1.9582236473505616, + 2.024268769118678, + 2.087484008510713, + # 70 percentile + 2.159640824487759, + 2.228946874949638, + 2.3116927446237296, + 2.393050343723898, + 2.4580033448385072, + 2.540090069335118, + 2.6137265979916324, + 2.6977107944072927, + 2.7708533379551255, + 2.87857855274051, + # 80 percentile + 2.9831052229901087, + 3.0822705451690124, + 3.205326033772178, + 3.3414577193540285, + 3.4742567115516865, + 3.662649320810405, + 3.824050304002779, + 4.022925911478537, + 4.2230293800118375, + 4.470116463111714, + # 90 percentile + 4.695625259033334, + 4.9861721319525785, + 5.365176268398273, + 5.774669570875075, + 6.345150944315008, + 7.2068164399044585, + 8.434910226292136, + 10.216039042627969, + 13.186847352152784, + 40.041936754347404 + ] diff --git a/tests/energy_efficiency_carbon_percentiles2021.py b/tests/energy_efficiency_carbon_percentiles2021.py index c4f253ab..44242b9a 100644 --- a/tests/energy_efficiency_carbon_percentiles2021.py +++ b/tests/energy_efficiency_carbon_percentiles2021.py @@ -1,116 +1,133 @@ # This array was last generated with carbon-rating.py on 2021-12-12 def get_generated_date(): - return '2021-12-12' + """ + Get the date when the data was generated. + + Returns: + str: A string that represents the date when the data was generated. + The date is in the format 'YYYY-MM-DD'. + """ + return '2021-12-12' def get_percentiles(): - return [ - 0.0953836, - 0.1410492, - 0.195002, - 0.2358364, - 0.262904, - 0.3000236, - 0.3182312, - 0.349924, - 0.38022120000000004, - 0.40702600000000005, - # 10 percentile - 0.42752480000000004, - 0.4498392, - 0.4772428, - 0.5048804, - 0.5216, - 0.5333804, - 0.5468992, - 0.5644688, - 0.5863052000000001, - 0.6014560000000001, - # 20 percentile - 0.6192776, - 0.6405636, - 0.6593608, - 0.6915084, - 0.70698, - 0.7236624, - 0.7420403999999999, - 0.7592104000000001, - 0.7842159999999999, - 0.80426, - # 30 percentile - 0.8257704, - 0.8471128, - 0.865516, - 0.8822332, - 0.902168, - 0.9264883999999999, - 0.9473739999999998, - 0.9767252000000001, - 0.9999304000000001, - 1.0191520000000003, - # 40 percentile - 1.0347444000000001, - 1.0572148000000001, - 1.0867584000000001, - 1.1122888, - 1.143956, - 1.1648304, - 1.1913983999999997, - 1.2126664, - 1.2412051999999998, - 1.27171, - # 50 percentile - 1.3091504000000003, - 1.3313284, - 1.3584724, - 1.3886472, - 1.420338, - 1.4487428, - 1.4820103999999998, - 1.5078003999999998, - 1.5452476, - 1.592982, - # 60 percentile - 1.6304788, - 1.666354, - 1.7338556000000003, - 1.7800792, - 1.8304320000000005, - 1.8739607999999999, - 1.9291192000000001, - 1.9680304, - 2.020606, - 2.0735059999999996, - # 70 percentile - 2.1504748, - 2.1879891999999996, - 2.2389408, - 2.3014731999999998, - 2.37685, - 2.440808, - 2.5242876, - 2.5912212000000006, - 2.711244, - 2.8313860000000006, - # 80 percentile - 2.9067716000000017, - 3.0417359999999998, - 3.195211599999998, - 3.3553764, - 3.481357999999999, - 3.6491988, - 3.845197600000001, - 4.056775199999997, - 4.318432, - 4.593262, - # 90 percentile - 4.950937600000001, - 5.188025599999999, - 5.551064800000001, - 5.972769999999999, - 6.473571999999994, - 7.134348799999998, - 8.014240000000001, - 10.102379199999978, - 13.86074479999998, - 29.59855 - ] \ No newline at end of file + """ + Get the precomputed percentiles of CO2 data. + + Returns: + list: A list of precomputed percentiles of the CO2 data. + The list contains 100 elements, + each representing the percentile from 1 to 100. + The percentiles are sorted in ascending order. + The percentile values are floats. + """ + return [ + 0.0953836, + 0.1410492, + 0.195002, + 0.2358364, + 0.262904, + 0.3000236, + 0.3182312, + 0.349924, + 0.38022120000000004, + 0.40702600000000005, + # 10 percentile + 0.42752480000000004, + 0.4498392, + 0.4772428, + 0.5048804, + 0.5216, + 0.5333804, + 0.5468992, + 0.5644688, + 0.5863052000000001, + 0.6014560000000001, + # 20 percentile + 0.6192776, + 0.6405636, + 0.6593608, + 0.6915084, + 0.70698, + 0.7236624, + 0.7420403999999999, + 0.7592104000000001, + 0.7842159999999999, + 0.80426, + # 30 percentile + 0.8257704, + 0.8471128, + 0.865516, + 0.8822332, + 0.902168, + 0.9264883999999999, + 0.9473739999999998, + 0.9767252000000001, + 0.9999304000000001, + 1.0191520000000003, + # 40 percentile + 1.0347444000000001, + 1.0572148000000001, + 1.0867584000000001, + 1.1122888, + 1.143956, + 1.1648304, + 1.1913983999999997, + 1.2126664, + 1.2412051999999998, + 1.27171, + # 50 percentile + 1.3091504000000003, + 1.3313284, + 1.3584724, + 1.3886472, + 1.420338, + 1.4487428, + 1.4820103999999998, + 1.5078003999999998, + 1.5452476, + 1.592982, + # 60 percentile + 1.6304788, + 1.666354, + 1.7338556000000003, + 1.7800792, + 1.8304320000000005, + 1.8739607999999999, + 1.9291192000000001, + 1.9680304, + 2.020606, + 2.0735059999999996, + # 70 percentile + 2.1504748, + 2.1879891999999996, + 2.2389408, + 2.3014731999999998, + 2.37685, + 2.440808, + 2.5242876, + 2.5912212000000006, + 2.711244, + 2.8313860000000006, + # 80 percentile + 2.9067716000000017, + 3.0417359999999998, + 3.195211599999998, + 3.3553764, + 3.481357999999999, + 3.6491988, + 3.845197600000001, + 4.056775199999997, + 4.318432, + 4.593262, + # 90 percentile + 4.950937600000001, + 5.188025599999999, + 5.551064800000001, + 5.972769999999999, + 6.473571999999994, + 7.134348799999998, + 8.014240000000001, + 10.102379199999978, + 13.86074479999998, + 29.59855 + ] diff --git a/tests/energy_efficiency_carbon_percentiles2022.py b/tests/energy_efficiency_carbon_percentiles2022.py index 8e79ea05..cadc5f1f 100644 --- a/tests/energy_efficiency_carbon_percentiles2022.py +++ b/tests/energy_efficiency_carbon_percentiles2022.py @@ -1,116 +1,133 @@ # This array was last generated with carbon-rating.py on 2022-12-16 def get_generated_date(): - return '2022-12-16' + """ + Get the date when the data was generated. + + Returns: + str: A string that represents the date when the data was generated. + The date is in the format 'YYYY-MM-DD'. + """ + return '2022-12-16' def get_percentiles(): - return [ - 0.09973231387370034, - 0.16080354696745056, - 0.20362588258347242, - 0.2379187339514261, - 0.27656745606072947, - 0.30745744090969673, - 0.33184973107940746, - 0.3529047702283366, - 0.37534929773239417, - 0.39810352749424055, - # 10 percentile - 0.42508327717952193, - 0.4389412294057431, - 0.4562202545828302, - 0.4785825637696777, - 0.5068633860716945, - 0.5305798920731991, - 0.5477266613885061, - 0.5661989312880904, - 0.579879822949972, - 0.5960701396723743, - # 20 percentile - 0.6143280318508855, - 0.6330070592960808, - 0.6498430170875509, - 0.6694780148360645, - 0.6892955109640024, - 0.7101647842215606, - 0.7250250909520546, - 0.744704218197707, - 0.763463341885933, - 0.7899220982457044, - # 30 percentile - 0.8094173284721329, - 0.8283828359315404, - 0.8446715276722913, - 0.8657136655747891, - 0.884027165742591, - 0.9072240650011458, - 0.9295368189330097, - 0.9521169542114716, - 0.9772724314796505, - 1.0007404016001384, - # 40 percentile - 1.0238258919642773, - 1.0471724697959142, - 1.0710941779591143, - 1.0923557214516215, - 1.124570965740073, - 1.146183845138899, - 1.1746030242659617, - 1.1979125622573776, - 1.2310768439363453, - 1.2515047016501195, - # 50 percentile - 1.2795522645937627, - 1.3061630386311094, - 1.3351569502008265, - 1.3628026453467552, - 1.3917207530507585, - 1.4207824624790577, - 1.4529408809970128, - 1.4915141368433134, - 1.5291470606261284, - 1.554606947023887, - # 60 percentile - 1.5936143358412198, - 1.6352281140403822, - 1.6801844363885348, - 1.7187831209532685, - 1.7571613991771002, - 1.8028394920055058, - 1.8599011262235698, - 1.905627098350646, - 1.9640204546886495, - 2.023141995958402, - # 70 percentile - 2.0738455101034607, - 2.1401365513315884, - 2.2145383168173134, - 2.288088527695136, - 2.3726603939716004, - 2.450417219021078, - 2.5282657152636565, - 2.5990867940599562, - 2.7045361686886635, - 2.8158361741732807, - # 80 percentile - 2.8941146556964497, - 3.014464415320265, - 3.142870581337029, - 3.2905006896879754, - 3.43547236961074, - 3.6471893352084095, - 3.807560159901865, - 3.9759314212817936, - 4.240359033538238, - 4.474958891702117, - # 90 percentile - 4.721777182758359, - 5.040366506351629, - 5.525683098755043, - 5.968029039437439, - 6.511319842356704, - 7.102975344301667, - 8.126477029148848, - 9.81361661188916, - 12.361596299821109, - 46.09157958511321 - ] \ No newline at end of file + """ + Get the precomputed percentiles of CO2 data. + + Returns: + list: A list of precomputed percentiles of the CO2 data. + The list contains 100 elements, + each representing the percentile from 1 to 100. + The percentiles are sorted in ascending order. + The percentile values are floats. + """ + return [ + 0.09973231387370034, + 0.16080354696745056, + 0.20362588258347242, + 0.2379187339514261, + 0.27656745606072947, + 0.30745744090969673, + 0.33184973107940746, + 0.3529047702283366, + 0.37534929773239417, + 0.39810352749424055, + # 10 percentile + 0.42508327717952193, + 0.4389412294057431, + 0.4562202545828302, + 0.4785825637696777, + 0.5068633860716945, + 0.5305798920731991, + 0.5477266613885061, + 0.5661989312880904, + 0.579879822949972, + 0.5960701396723743, + # 20 percentile + 0.6143280318508855, + 0.6330070592960808, + 0.6498430170875509, + 0.6694780148360645, + 0.6892955109640024, + 0.7101647842215606, + 0.7250250909520546, + 0.744704218197707, + 0.763463341885933, + 0.7899220982457044, + # 30 percentile + 0.8094173284721329, + 0.8283828359315404, + 0.8446715276722913, + 0.8657136655747891, + 0.884027165742591, + 0.9072240650011458, + 0.9295368189330097, + 0.9521169542114716, + 0.9772724314796505, + 1.0007404016001384, + # 40 percentile + 1.0238258919642773, + 1.0471724697959142, + 1.0710941779591143, + 1.0923557214516215, + 1.124570965740073, + 1.146183845138899, + 1.1746030242659617, + 1.1979125622573776, + 1.2310768439363453, + 1.2515047016501195, + # 50 percentile + 1.2795522645937627, + 1.3061630386311094, + 1.3351569502008265, + 1.3628026453467552, + 1.3917207530507585, + 1.4207824624790577, + 1.4529408809970128, + 1.4915141368433134, + 1.5291470606261284, + 1.554606947023887, + # 60 percentile + 1.5936143358412198, + 1.6352281140403822, + 1.6801844363885348, + 1.7187831209532685, + 1.7571613991771002, + 1.8028394920055058, + 1.8599011262235698, + 1.905627098350646, + 1.9640204546886495, + 2.023141995958402, + # 70 percentile + 2.0738455101034607, + 2.1401365513315884, + 2.2145383168173134, + 2.288088527695136, + 2.3726603939716004, + 2.450417219021078, + 2.5282657152636565, + 2.5990867940599562, + 2.7045361686886635, + 2.8158361741732807, + # 80 percentile + 2.8941146556964497, + 3.014464415320265, + 3.142870581337029, + 3.2905006896879754, + 3.43547236961074, + 3.6471893352084095, + 3.807560159901865, + 3.9759314212817936, + 4.240359033538238, + 4.474958891702117, + # 90 percentile + 4.721777182758359, + 5.040366506351629, + 5.525683098755043, + 5.968029039437439, + 6.511319842356704, + 7.102975344301667, + 8.126477029148848, + 9.81361661188916, + 12.361596299821109, + 46.09157958511321 + ] From e8475e6077c9f8f07f572db8beb5356a87c61502 Mon Sep 17 00:00:00 2001 From: cockroacher <163405488+cockroacher@users.noreply.github.com> Date: Tue, 9 Apr 2024 15:19:00 +0200 Subject: [PATCH 2/3] pylint --- carbon_rating.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/carbon_rating.py b/carbon_rating.py index 6b3b1bfe..84e6b6bb 100644 --- a/carbon_rating.py +++ b/carbon_rating.py @@ -125,7 +125,7 @@ def main(argv): * run webperf-core test on all websites you want to use for your percentiles (with json as output file) * run this file against your output file, for example like this: - carbon-rating.py + carbon_rating.py -i data\\carbon-references-2022.json -o tests\\energy_efficiency_carbon_percentiles.py From 4f619d4e05f8a788476c278cb4b7663c0de0c558 Mon Sep 17 00:00:00 2001 From: cockroacher <163405488+cockroacher@users.noreply.github.com> Date: Tue, 9 Apr 2024 15:40:17 +0200 Subject: [PATCH 3/3] pylint docstring --- tests/energy_efficiency.py | 141 +++++++++++++++++++++++++++++-------- 1 file changed, 111 insertions(+), 30 deletions(-) diff --git a/tests/energy_efficiency.py b/tests/energy_efficiency.py index 5ab35e23..abb69de6 100644 --- a/tests/energy_efficiency.py +++ b/tests/energy_efficiency.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- from datetime import datetime -import tests.energy_efficiency_carbon_percentiles as energy_efficiency_carbon_percentiles +from models import Rating +from tests import energy_efficiency_carbon_percentiles import tests.energy_efficiency_carbon_percentiles2022 as energy_efficiency_carbon_percentiles_2022 from tests.performance_lighthouse import run_test as lighthouse_perf_run_test -from models import Rating from tests.utils import get_translation # Code below is built from: https://gitlab.com/wholegrain/carbon-api-2-0 @@ -13,37 +13,84 @@ FIRST_TIME_VIEWING_PERCENTAGE = 0.25 PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD = 0.02 CARBON_PER_KWG_GRID = 475 -# PER_KWG_RENEWABLE = 33.4 -# PERCENTAGE_OF_ENERGY_IN_DATACENTER = 0.1008 -# PERCENTAGE_OF_ENERGY_IN_TRANSMISSION_AND_END_USER = 0.8992 -# CO2_GRAMS_TO_LITRES = 0.5562 - def adjust_data_transfer(transfer_bytes): - return (transfer_bytes * RETURNING_VISITOR_PERCENTAGE) + (PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD * transfer_bytes * FIRST_TIME_VIEWING_PERCENTAGE) + """ + Adjusts the data transfer bytes considering the percentage of + returning visitors and first-time viewers. + + Parameters: + transfer_bytes (int): The original data transfer bytes. + Returns: + float: The adjusted data transfer bytes. + """ + return (transfer_bytes * RETURNING_VISITOR_PERCENTAGE)\ + + (PERCENTAGE_OF_DATA_LOADED_ON_SUBSEQUENT_LOAD *\ + transfer_bytes * FIRST_TIME_VIEWING_PERCENTAGE) -def energy_consumption(bytesAdjusted): - return bytesAdjusted * (KWG_PER_GB / 1073741824) +def energy_consumption(bytes_adjusted): + """ + Calculates the energy consumption based on the adjusted data transfer bytes. + Parameters: + bytes_adjusted (float): The adjusted data transfer bytes. + + Returns: + float: The energy consumption in kilowatt-grams per gigabyte. + """ + return bytes_adjusted * (KWG_PER_GB / 1073741824) def get_co2_grid(energy): + """ + Calculates the CO2 emissions based on the energy consumption. + + Parameters: + energy (float): The energy consumption in kilowatt-grams per gigabyte. + + Returns: + float: The CO2 emissions in the grid. + """ return energy * CARBON_PER_KWG_GRID +def convert_2_co2(transfer_bytes): + """ + Converts the data transfer bytes to CO2 emissions. -# def get_co2_renewable(energy): -# return ((energy * PERCENTAGE_OF_ENERGY_IN_DATACENTER) * CARBON_PER_KWG_RENEWABLE) + ((energy * PERCENTAGE_OF_ENERGY_IN_TRANSMISSION_AND_END_USER) * CARBON_PER_KWG_GRID) + This function adjusts the data transfer bytes, + calculates the energy consumption based on the adjusted bytes, + and then calculates the CO2 emissions based on the energy consumption. + Parameters: + transfer_bytes (int): The original data transfer bytes. -def convert_2_co2(transfer_bytes): - bytesAdjusted = adjust_data_transfer(transfer_bytes) - energy = energy_consumption(bytesAdjusted) - co2Grid = get_co2_grid(energy) - # co2Renewable = get_co2_renewable(energy) - return co2Grid + Returns: + float: The CO2 emissions in the grid. + """ + bytes_adjusted = adjust_data_transfer(transfer_bytes) + energy = energy_consumption(bytes_adjusted) + co2_grid = get_co2_grid(energy) + return co2_grid def cleaner_than(co2, year='current'): + """ + Determines how much cleaner a given CO2 value is compared to a set of percentiles. + + This function compares a given CO2 value to a set of percentiles that + represent the distribution of CO2 values for a particular year. + The percentiles are loaded from a file that is updated periodically with new data. + + Parameters: + co2 (float): The CO2 value to compare. + year (str, optional): The year for which to load the percentiles. + If not specified, the current year's percentiles are loaded. Defaults to 'current'. + + Returns: + float: The percentile of the given CO2 value. This is a value between 0 and 1, + where 1 means the CO2 value is cleaner than all the percentiles, + and 0 means it is not cleaner than any of them. + """ # This array needs to be updated periodically with new data. This was # originally calculated with a database query but that was too slow at # scale. We can look in to writing a cron job that will generate and export @@ -58,13 +105,25 @@ def cleaner_than(co2, year='current'): # this should always be exactly 100 number of values for item in percentiles: position += 1 - if(co2 < item): + if co2 < item: return (100 - position) / 100 return 0 def format_bytes(size): - # 2**10 = 1024 + """ + Converts a byte size into a human-readable format. + + This function takes a size in bytes and converts it into a more readable format, + using the appropriate unit (bytes, kilobytes, megabytes, gigabytes, or terabytes). + + Parameters: + size (int): The size in bytes to be converted. + + Returns: + tuple: A tuple containing the converted size and the unit used for the conversion. + The size is a float and the unit is a string. + """ power = 2**10 n = 0 power_labels = {0: '', 1: 'k', 2: 'm', 3: 'g', 4: 't'} @@ -87,9 +146,7 @@ def run_test(global_translation, lang_code, url): datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) result_dict = {} - lighthouse_perf_result = lighthouse_perf_run_test(global_translation, lang_code, url, True) - - transfer_bytes = lighthouse_perf_result[1]['total-byte-weight'] + transfer_bytes = get_total_bytes_for_url(global_translation, lang_code, url) result_dict['total-byte-weight'] = transfer_bytes co2 = convert_2_co2(transfer_bytes) @@ -101,7 +158,7 @@ def run_test(global_translation, lang_code, url): review = '' - points = float("{0:.2f}".format((5 * (cleaner / 100)))) + points = float(f"{(5 * (cleaner / 100)):.2f}") # handicap points if cleaner >= 95: @@ -119,20 +176,44 @@ def run_test(global_translation, lang_code, url): review = local_translation("TEXT_WEBSITE_IS_VERY_BAD") review += local_translation("TEXT_GRAMS_OF_CO2").format(round(co2, 2)) - review += local_translation("TEXT_BETTER_THAN").format(int(cleaner), - energy_efficiency_carbon_percentiles.get_generated_date()) - review += local_translation("TEXT_BETTER_THAN").format(int(cleaner_2022), - energy_efficiency_carbon_percentiles_2022.get_generated_date()) + latest_generated_date = energy_efficiency_carbon_percentiles.get_generated_date() + review += local_translation("TEXT_BETTER_THAN").format( + int(cleaner), + latest_generated_date) + + old_generated_date = energy_efficiency_carbon_percentiles_2022.get_generated_date() + review += local_translation("TEXT_BETTER_THAN").format( + int(cleaner_2022), + old_generated_date) transfer_info = format_bytes(transfer_bytes) review += local_translation("TEXT_TRANSFER_SIZE").format(transfer_info[0], transfer_info[1]) rating = Rating(global_translation) rating.set_overall(points, review) - # we use this line to recalibrate percentiles (See carbon-rating.py), comment out line above also - #rating.set_overall(points, '{0}'.format(co2)) print(global_translation('TEXT_TEST_END').format( datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) return (rating, result_dict) + +def get_total_bytes_for_url(global_translation, lang_code, url): + """ + Runs a Lighthouse performance test on a given URL and returns the total byte weight of the page. + + This function is specifically designed to work with multilingual websites. + It uses the 'global_translation' + and 'lang_code' parameters to handle the language-specific aspects of the website. + + Parameters: + global_translation (dict): A dictionary containing language-specific translations. + lang_code (str): The language code for the specific language version of the website to test. + url (str): The URL of the webpage to run the Lighthouse performance test on. + + Returns: + int: The total byte weight of the webpage as determined by the Lighthouse performance test. + """ + lighthouse_perf_result = lighthouse_perf_run_test(global_translation, lang_code, url, True) + + transfer_bytes = lighthouse_perf_result[1]['total-byte-weight'] + return transfer_bytes