Skip to content

Commit

Permalink
Merge pull request #98 from Leo-Corporation/vNext
Browse files Browse the repository at this point in the history
Version 1.5.0.2304
  • Loading branch information
lpeyr authored Apr 2, 2023
2 parents 3d3115c + 4470599 commit 39549c8
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 42 deletions.
17 changes: 4 additions & 13 deletions PeyrSharp.Core/PeyrSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Title>PeyrSharp.Core</Title>
<Version>1.4.0.2303</Version>
<Version>1.5.0.2304</Version>
<Authors>Léo Corporation</Authors>
<Description>Core methods and features of PeyrSharp.</Description>
<Copyright>© 2023</Copyright>
Expand All @@ -16,16 +16,7 @@
<PackageReadmeFile>NUGET_README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReleaseNotes>- 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)</PackageReleaseNotes>
<PackageReleaseNotes></PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand All @@ -40,8 +31,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="PeyrSharp.Enums" Version="1.4.0.2303" />
<PackageReference Include="PeyrSharp.Exceptions" Version="1.4.0.2303" />
<PackageReference Include="PeyrSharp.Enums" Version="1.5.0.2304" />
<PackageReference Include="PeyrSharp.Exceptions" Version="1.5.0.2304" />
</ItemGroup>

</Project>
62 changes: 62 additions & 0 deletions PeyrSharp.Enums/LogLevel.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Specifies the severity level of a log message.
/// </summary>
public enum LogLevel
{
/// <summary>
/// Debug-level messages provide verbose information for debugging purposes.
/// </summary>
Debug,

/// <summary>
/// Info-level messages provide informational messages about the application's state.
/// </summary>
Info,

/// <summary>
/// Warning-level messages indicate a potential problem or non-critical issue.
/// </summary>
Warning,

/// <summary>
/// Error-level messages indicate an error has occurred in the application.
/// </summary>
Error,

/// <summary>
/// Critical-level messages indicate a critical error has occurred that requires immediate attention.
/// </summary>
Critical,

/// <summary>
/// Misc-level messages are for miscellaneous use cases and are not defined in the logging specification.
/// </summary>
Misc
}
}
4 changes: 3 additions & 1 deletion PeyrSharp.Enums/PeyrSharp.Enums.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
<PackageProjectUrl>https://peyrsharp.leocorporation.dev/</PackageProjectUrl>
<RepositoryUrl>https://github.com/Leo-Corporation/PeyrSharp</RepositoryUrl>
<PackageTags>enums;c-sharp;dotnet;vb;peyrsharp;leo corp</PackageTags>
<Version>1.4.0.2303</Version>
<Version>1.5.0.2304</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>logo.png</PackageIcon>
<PackageReadmeFile>NUGET_README.md</PackageReadmeFile>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>- Added the ``LogLevel`` enumeration (#90)
</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
41 changes: 41 additions & 0 deletions PeyrSharp.Env/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -51,5 +52,45 @@ public static void Log(string message, string filePath, DateTime dateTime)

File.AppendAllText(filePath, $"[{dateTime}] {message}"); // Log
}

/// <summary>
/// Logs a formatted message to a file.
/// </summary>
/// <param name="message">The log message to be written.</param>
/// <param name="filePath">The path to the log file. Must contain an extension.</param>
/// <param name="formatString">An object array that contains zero or more objects to format.</param>
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
}

/// <summary>
/// Logs a message with the specified severity level, file path, and timestamp.
/// </summary>
/// <param name="message">The message to log.</param>
/// <param name="filePath">The path to the file where the message was logged.</param>
/// <param name="dateTime">The timestamp for the log message.</param>
/// <param name="logLevel">The severity level for the log message.</param>
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"
};
}
}
7 changes: 4 additions & 3 deletions PeyrSharp.Env/PeyrSharp.Env.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Title>PeyrSharp.Env</Title>
<Version>1.4.0.2303</Version>
<Version>1.5.0.2304</Version>
<Authors>Léo Corporation</Authors>
<Description>Environment-related methods of PeyrSharp.</Description>
<Copyright>© 2023</Copyright>
Expand All @@ -16,7 +16,8 @@
<PackageReadmeFile>NUGET_README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageReleaseNotes>- Added the possibility to format the logged message (#89)
- Added the possibility to generate a log message depending on the `LogLevel` (#91)</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand All @@ -31,6 +32,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="PeyrSharp.Enums" Version="1.4.0.2303" />
<PackageReference Include="PeyrSharp.Enums" Version="1.5.0.2304" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion PeyrSharp.Exceptions/PeyrSharp.Exceptions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>PeyrSharp.Exceptions</Title>
<Version>1.4.0.2303</Version>
<Version>1.5.0.2304</Version>
<Company>Léo Corporation</Company>
<Description>Exceptions of PeyrSharp.</Description>
<Copyright>© 2023</Copyright>
Expand Down
80 changes: 80 additions & 0 deletions PeyrSharp.Extensions/DoubleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -199,5 +200,84 @@ public static class DoubleExtensions
/// <param name="d">The <see cref="double"/> to convert.</param>
/// <returns>An <see cref="int"/> value.</returns>
public static int ToInt(this double d) => (int)Math.Round(d);

/// <summary>
/// Calculates the mean (average) of a dataset.
/// </summary>
/// <param name="values">The dataset to calculate the mean of.</param>
/// <returns>The mean of the dataset.</returns>
/// <exception cref="ArgumentException">Thrown if the dataset is empty.</exception>
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;
}

/// <summary>
/// Calculates the median of a dataset.
/// </summary>
/// <param name="values">The dataset to calculate the median of.</param>
/// <returns>The median of the dataset.</returns>
/// <exception cref="ArgumentException">Thrown if the dataset is empty.</exception>
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;
}

/// <summary>
/// Calculates the mode of a dataset.
/// </summary>
/// <param name="values">The dataset to calculate the mode of.</param>
/// <returns>The mode of the dataset.</returns>
/// <exception cref="ArgumentException">Thrown if the dataset is empty.</exception>
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;
}
}
}
80 changes: 80 additions & 0 deletions PeyrSharp.Extensions/IntExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -64,5 +65,84 @@ public static int[] GetDivisors(this int number)

return ds.ToArray();
}

/// <summary>
/// Calculates the mean (average) of a dataset.
/// </summary>
/// <param name="values">The dataset to calculate the mean of.</param>
/// <returns>The mean of the dataset.</returns>
/// <exception cref="ArgumentException">Thrown if the dataset is empty.</exception>
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;
}

/// <summary>
/// Calculates the median of a dataset.
/// </summary>
/// <param name="values">The dataset to calculate the median of.</param>
/// <returns>The median of the dataset.</returns>
/// <exception cref="ArgumentException">Thrown if the dataset is empty.</exception>
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;
}

/// <summary>
/// Calculates the mode of a dataset.
/// </summary>
/// <param name="values">The dataset to calculate the mode of.</param>
/// <returns>The mode of the dataset.</returns>
/// <exception cref="ArgumentException">Thrown if the dataset is empty.</exception>
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;
}
}
}
Loading

0 comments on commit 39549c8

Please sign in to comment.