diff --git a/PeyrSharp.Core/PeyrSharp.Core.csproj b/PeyrSharp.Core/PeyrSharp.Core.csproj index 09c60ce..49e65cb 100644 --- a/PeyrSharp.Core/PeyrSharp.Core.csproj +++ b/PeyrSharp.Core/PeyrSharp.Core.csproj @@ -4,7 +4,7 @@ net5.0;net6.0;net7.0 True PeyrSharp.Core - 1.4.0.2303 + 1.5.0.2304 Léo Corporation Core methods and features of PeyrSharp. © 2023 @@ -16,16 +16,7 @@ NUGET_README.md MIT True - - Added `CaloriesToJoules()` method (#79) -- Added `JoulesToCalories()` method (#79) -- Added `KnotsToKilometersPerHour()` method (#80) -- Added `KilometersPerHourToKnots()` method (#80) -- Added `KnotsToMilesPerHour()` method (#81) -- Added `MilesPerHourToKnots()` method (#81) -- Added `KilometersPerHourToMetersPerSecond()` method (#82) -- Added `MetersPerSecondToKilometersPerHour()` method (#82) -- Added `MilesPerHourToKilometersPerHour()` (#83) -- Added `KilometersPerHourToMilesPerHour()` (#83) + @@ -40,8 +31,8 @@ - - + + diff --git a/PeyrSharp.Enums/LogLevel.cs b/PeyrSharp.Enums/LogLevel.cs new file mode 100644 index 0000000..7d3f728 --- /dev/null +++ b/PeyrSharp.Enums/LogLevel.cs @@ -0,0 +1,62 @@ +/* +MIT License + +Copyright (c) Léo Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +namespace PeyrSharp.Enums +{ + /// + /// Specifies the severity level of a log message. + /// + public enum LogLevel + { + /// + /// Debug-level messages provide verbose information for debugging purposes. + /// + Debug, + + /// + /// Info-level messages provide informational messages about the application's state. + /// + Info, + + /// + /// Warning-level messages indicate a potential problem or non-critical issue. + /// + Warning, + + /// + /// Error-level messages indicate an error has occurred in the application. + /// + Error, + + /// + /// Critical-level messages indicate a critical error has occurred that requires immediate attention. + /// + Critical, + + /// + /// Misc-level messages are for miscellaneous use cases and are not defined in the logging specification. + /// + Misc + } +} diff --git a/PeyrSharp.Enums/PeyrSharp.Enums.csproj b/PeyrSharp.Enums/PeyrSharp.Enums.csproj index 68df1fd..043468f 100644 --- a/PeyrSharp.Enums/PeyrSharp.Enums.csproj +++ b/PeyrSharp.Enums/PeyrSharp.Enums.csproj @@ -11,13 +11,15 @@ https://peyrsharp.leocorporation.dev/ https://github.com/Leo-Corporation/PeyrSharp enums;c-sharp;dotnet;vb;peyrsharp;leo corp - 1.4.0.2303 + 1.5.0.2304 True logo.png NUGET_README.md True MIT git + - Added the ``LogLevel`` enumeration (#90) + diff --git a/PeyrSharp.Env/Logger.cs b/PeyrSharp.Env/Logger.cs index 6f7a7b9..5b5a781 100644 --- a/PeyrSharp.Env/Logger.cs +++ b/PeyrSharp.Env/Logger.cs @@ -21,6 +21,7 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +using PeyrSharp.Enums; using System; using System.IO; @@ -51,5 +52,45 @@ public static void Log(string message, string filePath, DateTime dateTime) File.AppendAllText(filePath, $"[{dateTime}] {message}"); // Log } + + /// + /// Logs a formatted message to a file. + /// + /// The log message to be written. + /// The path to the log file. Must contain an extension. + /// An object array that contains zero or more objects to format. + public static void Log(string message, string filePath, params object[] formatString) + { + if (!Directory.Exists(filePath)) + { + Directory.CreateDirectory(Path.GetDirectoryName(filePath)); + } + + if (!File.Exists(filePath)) + { + File.Create(filePath); // Create file + } + + File.AppendAllText(filePath, string.Format(message, formatString)); // Log + } + + /// + /// Logs a message with the specified severity level, file path, and timestamp. + /// + /// The message to log. + /// The path to the file where the message was logged. + /// The timestamp for the log message. + /// The severity level for the log message. + public static void Log(string message, string filePath, DateTime dateTime, LogLevel logLevel) => Log($"[{LogLevelToString(logLevel)}] {message}", filePath, dateTime); + + private static string LogLevelToString(LogLevel level) => level switch + { + LogLevel.Error => "Error", + LogLevel.Warning => "Warning", + LogLevel.Info => "Info", + LogLevel.Critical => "Critical", + LogLevel.Debug => "Debug", + _ => "Misc" + }; } } diff --git a/PeyrSharp.Env/PeyrSharp.Env.csproj b/PeyrSharp.Env/PeyrSharp.Env.csproj index 9e9bdf1..3c52383 100644 --- a/PeyrSharp.Env/PeyrSharp.Env.csproj +++ b/PeyrSharp.Env/PeyrSharp.Env.csproj @@ -4,7 +4,7 @@ net5.0;net6.0;net7.0 True PeyrSharp.Env - 1.4.0.2303 + 1.5.0.2304 Léo Corporation Environment-related methods of PeyrSharp. © 2023 @@ -16,7 +16,8 @@ NUGET_README.md MIT True - + - Added the possibility to format the logged message (#89) +- Added the possibility to generate a log message depending on the `LogLevel` (#91) @@ -31,6 +32,6 @@ - + diff --git a/PeyrSharp.Exceptions/PeyrSharp.Exceptions.csproj b/PeyrSharp.Exceptions/PeyrSharp.Exceptions.csproj index 3f49368..d8a7439 100644 --- a/PeyrSharp.Exceptions/PeyrSharp.Exceptions.csproj +++ b/PeyrSharp.Exceptions/PeyrSharp.Exceptions.csproj @@ -6,7 +6,7 @@ enable True PeyrSharp.Exceptions - 1.4.0.2303 + 1.5.0.2304 Léo Corporation Exceptions of PeyrSharp. © 2023 diff --git a/PeyrSharp.Extensions/DoubleExtensions.cs b/PeyrSharp.Extensions/DoubleExtensions.cs index 01df874..7000e78 100644 --- a/PeyrSharp.Extensions/DoubleExtensions.cs +++ b/PeyrSharp.Extensions/DoubleExtensions.cs @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ using PeyrSharp.Enums; using System; +using System.Linq; namespace PeyrSharp.Extensions { @@ -199,5 +200,84 @@ public static class DoubleExtensions /// The to convert. /// An value. public static int ToInt(this double d) => (int)Math.Round(d); + + /// + /// Calculates the mean (average) of a dataset. + /// + /// The dataset to calculate the mean of. + /// The mean of the dataset. + /// Thrown if the dataset is empty. + public static double Mean(this double[] values) + { + // Check for empty input + if (values.Length == 0) + { + throw new ArgumentException("Cannot calculate mean of empty dataset", "values"); + } + + // Calculate sum of values + double sum = values.Sum(); + + // Calculate mean + double mean = sum / values.Length; + + return mean; + } + + /// + /// Calculates the median of a dataset. + /// + /// The dataset to calculate the median of. + /// The median of the dataset. + /// Thrown if the dataset is empty. + public static double Median(this double[] values) + { + // Check for empty input + if (values.Length == 0) + { + throw new ArgumentException("Cannot calculate median of empty dataset", "values"); + } + + // Sort values + var list = values.ToList(); + list.Sort(); + + // Calculate median + double median; + int n = list.Count; + if (n % 2 == 1) + { + // Odd number of elements + median = list[n / 2]; + } + else + { + // Even number of elements + median = (list[n / 2 - 1] + list[n / 2]) / 2; + } + + return median; + } + + /// + /// Calculates the mode of a dataset. + /// + /// The dataset to calculate the mode of. + /// The mode of the dataset. + /// Thrown if the dataset is empty. + public static double Mode(this double[] values) + { + // Check for empty input + if (values.Length == 0) + { + throw new ArgumentException("Cannot calculate mode of empty dataset", "values"); + } + + // Group values by frequency + var frequencyGroups = values.GroupBy(x => x).OrderByDescending(g => g.Count()); + + // Return most common value (if more than one, return first) + return frequencyGroups.First().Key; + } } } diff --git a/PeyrSharp.Extensions/IntExtensions.cs b/PeyrSharp.Extensions/IntExtensions.cs index 12a7c3f..fb5ee29 100644 --- a/PeyrSharp.Extensions/IntExtensions.cs +++ b/PeyrSharp.Extensions/IntExtensions.cs @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */ using System; using System.Collections.Generic; +using System.Linq; namespace PeyrSharp.Extensions { @@ -64,5 +65,84 @@ public static int[] GetDivisors(this int number) return ds.ToArray(); } + + /// + /// Calculates the mean (average) of a dataset. + /// + /// The dataset to calculate the mean of. + /// The mean of the dataset. + /// Thrown if the dataset is empty. + public static double Mean(this int[] values) + { + // Check for empty input + if (values.Length == 0) + { + throw new ArgumentException("Cannot calculate mean of empty dataset", "values"); + } + + // Calculate sum of values + double sum = values.Sum(); + + // Calculate mean + double mean = sum / values.Length; + + return mean; + } + + /// + /// Calculates the median of a dataset. + /// + /// The dataset to calculate the median of. + /// The median of the dataset. + /// Thrown if the dataset is empty. + public static double Median(this int[] values) + { + // Check for empty input + if (values.Length == 0) + { + throw new ArgumentException("Cannot calculate median of empty dataset", "values"); + } + + // Sort values + var list = values.ToList(); + list.Sort(); + + // Calculate median + double median; + int n = list.Count; + if (n % 2 == 1) + { + // Odd number of elements + median = list[n / 2]; + } + else + { + // Even number of elements + median = (list[n / 2 - 1] + list[n / 2]) / 2; + } + + return median; + } + + /// + /// Calculates the mode of a dataset. + /// + /// The dataset to calculate the mode of. + /// The mode of the dataset. + /// Thrown if the dataset is empty. + public static double Mode(this int[] values) + { + // Check for empty input + if (values.Length == 0) + { + throw new ArgumentException("Cannot calculate mode of empty dataset", "values"); + } + + // Group values by frequency + var frequencyGroups = values.GroupBy(x => x).OrderByDescending(g => g.Count()); + + // Return most common value (if more than one, return first) + return frequencyGroups.First().Key; + } } } diff --git a/PeyrSharp.Extensions/PeyrSharp.Extensions.csproj b/PeyrSharp.Extensions/PeyrSharp.Extensions.csproj index 302510f..581117e 100644 --- a/PeyrSharp.Extensions/PeyrSharp.Extensions.csproj +++ b/PeyrSharp.Extensions/PeyrSharp.Extensions.csproj @@ -4,7 +4,7 @@ net5.0;net6.0;net7.0 True PeyrSharp.Extensions - 1.4.0.2303 + 1.5.0.2304 Léo Corporation Extensions methods of PeyrSharp. © 2023 @@ -16,7 +16,9 @@ NUGET_README.md MIT True - - Added the ``Reverse()`` extension method (string) (#78) + - Added Mean() extension method for int[] and double[] (#92) +- Added Median() extension method for int[] and double[] (#93) +- Added Mode() extension method for int[] and double[] (#94) @@ -31,7 +33,7 @@ - + diff --git a/PeyrSharp.UiHelpers/PeyrSharp.UiHelpers.csproj b/PeyrSharp.UiHelpers/PeyrSharp.UiHelpers.csproj index 9e29c0d..024c7ec 100644 --- a/PeyrSharp.UiHelpers/PeyrSharp.UiHelpers.csproj +++ b/PeyrSharp.UiHelpers/PeyrSharp.UiHelpers.csproj @@ -8,7 +8,7 @@ true True PeyrSharp.UiHelpers - 1.4.0.2303 + 1.5.0.2304 Léo Corporation Useful helpers for Windows Forms and Windows Presentation Framework. © 2023 @@ -34,7 +34,7 @@ - + diff --git a/PeyrSharp/PeyrSharp.cs b/PeyrSharp/PeyrSharp.cs index e5ad02c..daac84b 100644 --- a/PeyrSharp/PeyrSharp.cs +++ b/PeyrSharp/PeyrSharp.cs @@ -32,6 +32,6 @@ public static class PeyrSharp /// /// The current version of PeyrSharp. /// - public static string Version => "1.4.0.2303"; + public static string Version => "1.5.0.2304"; } } diff --git a/PeyrSharp/PeyrSharp.csproj b/PeyrSharp/PeyrSharp.csproj index d690d43..ee71e8b 100644 --- a/PeyrSharp/PeyrSharp.csproj +++ b/PeyrSharp/PeyrSharp.csproj @@ -4,7 +4,7 @@ net5.0;net6.0;net5.0-windows;net6.0-windows;net7.0;net7.0-windows True PeyrSharp - 1.4.0.2303 + 1.5.0.2304 Léo Corporation © 2023 A C# library designed to make developers' job easier. @@ -16,17 +16,12 @@ NUGET_README.md MIT True - - Added the ``Reverse()`` extension method (string) (#78) -- Added `CaloriesToJoules()` method (#79) -- Added `JoulesToCalories()` method (#79) -- Added `KnotsToKilometersPerHour()` method (#80) -- Added `KilometersPerHourToKnots()` method (#80) -- Added `KnotsToMilesPerHour()` method (#81) -- Added `MilesPerHourToKnots()` method (#81) -- Added `KilometersPerHourToMetersPerSecond()` method (#82) -- Added `MetersPerSecondToKilometersPerHour()` method (#82) -- Added `MilesPerHourToKilometersPerHour()` (#83) -- Added `KilometersPerHourToMilesPerHour()` (#83) + - Added the possibility to format the logged message (#89) +- Added the LogLevel enumeration (#90) +- Added the possibility to generate a log message depending on the LogLevel (#91) +- Added Mean() extension method for int[] and double[] (#92) +- Added Median() extension method for int[] and double[] (#93) +- Added Mode() extension method for int[] and double[] (#94) @@ -41,12 +36,12 @@ - - - - - - + + + + + +