Skip to content

Instantly share code, notes, and snippets.

View dasjestyr's full-sized avatar

Nunja dasjestyr

  • Scottsdale, AZ
View GitHub Profile
@dasjestyr
dasjestyr / ListMatcher.cs
Created July 19, 2013 16:24
See if one list is contained within another list. Basically, what this LINQ query does is check every element in the tested list (possessed) to see if one of them matches something in the larger list (required). If every test succeeds, then the result is true;
public class ListMatcher
{
public bool MeetsRequirements(IEnumerable<string> required, IEnumerable<string> possessed)
{
//possessed = new List<string> { "a", "b", "d", "g" };
//required = new List<string> { "b", "c" };
var meetsRequirements = required.All(r => possessed.Contains(r)); // false
return meetsRequirements;
}
@dasjestyr
dasjestyr / SqlFacade_BulkDemo.cs
Created July 23, 2013 02:57
Bulk insert method. Hold the connection open while it runs multiple inserts
public class SqlFacade
{
public static bool ExecuteBulkNonQuery<T>(string connectionString, CommandType commandType,
string commandText, IEnumerable<T> listItems, Func<T,SqlParameter[]> setParameters)
{
var fails = 0;
using (var conn = new SqlConnection(connectionString))
{
using (var comm = new SqlCommand(commandText, conn))
{
@dasjestyr
dasjestyr / RandomUnwatchedMovie.py
Created July 23, 2013 23:32
Movie randomizer. This will check your xbmc for unwatched movies and then pick one at random and start playing it. My first attempt at python
################################################################
# Script: Play random unwatched movie
# Description: Plays a random movie from your library that
# has not yet been played completely
# Written By: Jeremy Stafford
# Date: 11/15/2012
# Tested On: XBMC Eden
################################################################
import urllib2
@dasjestyr
dasjestyr / AdapterPatternExample.cs
Created July 29, 2013 20:10
Adapter pattern example
class Program
{
static void Main(string[] args)
{
const string messageFormat = "{0} gave you a {1}, which is a type of {2}";
// this operation expects to use a Character object
Character _char;
_char = new Character("Cecil");
@dasjestyr
dasjestyr / SingletonExample.cs
Created July 30, 2013 20:15
A thread-safe singleton example
void Main()
{
var inst1 = Employee.Instance;
Console.WriteLine(Employee.Count); //1
Console.WriteLine(Employee.Count); //1
var inst2 = Employee.Instance;
Console.WriteLine(Employee.Count); //2
Console.WriteLine(Employee.Count); //2
}
@dasjestyr
dasjestyr / FisherYatesShuffle.cs
Created August 26, 2013 21:07
Fisher-Yates Shuffle
private static IEnumerable<Card> Shuffle(IList<Card> cards)
{
var rnd = new Random();
for (var n = cards.Count - 1; n > 0; --n)
{
var newPosition = rnd.Next(n + 1);
var temp = cards[n];
cards[n] = cards[newPosition];
cards[newPosition] = temp;
}
@dasjestyr
dasjestyr / UnitOfWork.cs
Created November 14, 2013 04:21
Unit of Work example. Shows a single control class that will house a single context and all relevant repositories using the generic repository pattern
using System;
namespace mycompany.employee_manager.Core.Data.Repository
{
public class UnitOfWork : IDisposable
{
#region -- Fields --
private readonly MyContext _context = new MyContext();
private GenericRepository<User> _userRepository;
private GenericRepository<Department> _departmentRepository;
@dasjestyr
dasjestyr / GenericRepository.cs
Created November 14, 2013 04:24
Generic Repository example. Can be used as a single repository that will work for every entity.
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
namespace mycompany.employee_manager.Core.Data.Repository
{
public class GenericRepository<TEntity>
where TEntity : class
{
internal ProvausioEmployeeManagerContext Context;
@dasjestyr
dasjestyr / grid.css
Created December 28, 2013 00:46
A css grid system. The number represents a column span. The inherent width of each subsequent column (numbered) is equal to the column width plus the gutter width. Use gridulator.com to help with the calculations.
/*
Width: 1000px
# Columns : 12
Column width: 65px
Gutter : 20px
*/
.grid_1 { width: 65px; }
.grid_2 { width: 150px; }
.grid_3 { width: 235px; }
@dasjestyr
dasjestyr / string-formatter.js
Created January 7, 2014 19:02
Javascript string extension for formatted strings
String.prototype.format = String.prototype.f = function() {
var s = this,
i = arguments.length;
while (i--) {
s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
}
return s;
};