Skip to content

Instantly share code, notes, and snippets.

View IJsWorkshop's full-sized avatar
💯
#Working

IanJ IJsWorkshop

💯
#Working
View GitHub Profile
@IJsWorkshop
IJsWorkshop / gist:ef9564b7a72b203709f237a5304bfce6
Created July 7, 2023 19:51
Basic custom generic DeSerializer
public static T DeSerializer<T>(Dictionary<string, IStorageElement> element)
{
var o = (T)Activator.CreateInstance<T>();
var props = o.GetType().GetProperties();
foreach (var prop in props)
{
// match element name with property name set value
if (element.TryGetValue(prop.Name, out var e))
@IJsWorkshop
IJsWorkshop / gist:e5b7268f86b76a743b9f290be704d80d
Created July 7, 2023 19:49
Basic custom generic Serializer to add your data to a file
public static string Serializer<T>(this T obj)
{
var sb = new StringBuilder();
sb.AppendLine($"<record>");
var props = obj.GetType().GetProperties();
// add values
foreach (var prop in props)
@IJsWorkshop
IJsWorkshop / gist:88b5dbd6cbe0ab4d07b6b2657dec15e3
Created July 6, 2023 09:14
cleaner way to generate a unique id
private static readonly RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
private string GenerateUniqueID(int length)
{
// We chose an encoding that fits 6 bits into every character,
// so we can fit length*6 bits in total.
// Each byte is 8 bits, so...
int sufficientBufferSizeInBytes = (length * 6 + 7) / 8;
var buffer = new byte[sufficientBufferSizeInBytes];
public static string GetUniqueId()
{
// get unique id
var builder = new StringBuilder();
Enumerable
.Range(65, 26)
.Select(e => ((char)e).ToString())
.Concat(Enumerable.Range(97, 26).Select(e => ((char)e).ToString()))
.Concat(Enumerable.Range(0, 10).Select(e => e.ToString()))
@IJsWorkshop
IJsWorkshop / gist:b1d945b9344cb50b998cc7e427f82820
Created July 2, 2023 07:25
Convert string into a DateTime string with formating
public static string ToDateTime(this string datestring, string format="ddd dd")
{
var dstring = datestring;
if (System.DateTime.TryParse(datestring, out System.DateTime dTime))
{
dstring = dTime.ToString(format);
}
return dstring;
@IJsWorkshop
IJsWorkshop / gist:213466e5850d75cc0cbdd0c066f511a8
Created July 2, 2023 07:22
Split a List<T> into a List<List<T>> of nth size
public static List<List<T>> ChunkInto<T>(this IList<T> source, int chunkSize)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / chunkSize)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
@IJsWorkshop
IJsWorkshop / gist:9462094f4ee19c605513401f61fa4268
Created June 30, 2023 00:46
Using Selenium to access Gmail - Correction for ChromeDriver to fix wait feature and element is visible.
static void Delay(TimeSpan ts)
{
var stopwatch = Stopwatch.StartNew();
stopwatch.Start();
while (stopwatch.Elapsed.TotalSeconds <= ts.Seconds) { }
stopwatch.Stop();
}
public static bool IsVisible(this IWebDriver webDriver, By locator, TimeSpan timeOut)
{