Skip to content

Instantly share code, notes, and snippets.

@osya
osya / .py
Created February 20, 2018 10:08
Execute `yapf` for all files in a directory #Python
import glob
import subprocess
files = glob.iglob('E:/Work/web-frameworks/Python/**/*.py', recursive=True)
files_i_care_about = filter(lambda x: all(e not in x for e in ('venv', 'DjangoRESTPostgres')), files)
for filename in files_i_care_about:
subprocess.call(['yapf', '-vv', '-i', filename])
@osya
osya / search_replace.py
Last active February 17, 2018 10:26
Search & Replace in files #Python
import glob
replace = 'https://github.com/osya'
replacement = 'git@github.com:osya'
for filename in glob.iglob('E:/Work/**/.git/config', recursive=True):
with open(filename) as f:
s = f.read()
if replace in s:
print(filename)
s = s.replace(replace, replacement)
@osya
osya / phantomjs_test.js
Last active June 30, 2017 21:15
JavaScript script for some PhantomJS automation #JavaScript
/**
* Created by info_000 on 30-Jun-17.
*/
"use strict";
var page = require('webpage').create(),
fs = require('fs');
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36';
page.onError = function (msg, trace) {
console.log(msg);
trace.forEach(function(item) {
@osya
osya / gist:f9625a0d3649db30b68e609fa8ad07de
Last active June 2, 2024 04:00
Python boilerplate for AWS Lambda #Python
from __future__ import print_function
import boto3
import json
print('Loading function')
def handler(event, context):
'''Provide an event that contains the following keys:
@osya
osya / gist:4a9e6d822b636e20fe5ac7c5b1426297
Last active December 7, 2016 01:17
Dynamic LINQ Expression with Generic Any<T>() function Example #CSharp
private static MethodBase GetGenericMethod(Type type, string name, Type[] typeArgs, Type[] argTypes, BindingFlags flags)
{
var typeArity = typeArgs.Length;
var methods = type.GetMethods()
.Where(m => m.Name == name)
.Where(m => m.GetGenericArguments().Length == typeArity)
.Select(m => m.MakeGenericMethod(typeArgs));
return Type.DefaultBinder.SelectMethod(flags, methods.ToArray(), argTypes, null);
}
@osya
osya / gist:78a3853a2cd4bda68eaf29775602c873
Created October 24, 2016 11:00
Mocking ExternalLoginCallbackTest #CSharp
[TestMethod]
public async Task ExternalLoginCallbackTest()
{
// Arrange
var authManager = new Mock<IAuthenticationManager>();
var externalUser = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.NameIdentifier, "10154549708179846", "http://www.w3.org/2001/XMLSchema#string", "Facebook"),
new Claim(ClaimTypes.Name, DummyModel.UserName, "http://www.w3.org/2001/XMLSchema#string", "Facebook")
});
@osya
osya / gist:f963056860864b6e4fb408da9775a453
Last active October 20, 2016 09:47
ValidateMessageFor which shows multiple messages #CSharp
public static class ValidateMessagesFor
{
public static MvcHtmlString ValidationMessagesFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
{
var propertyName = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).PropertyName;
var modelState = htmlHelper.ViewData.ModelState;
if (!modelState.ContainsKey(propertyName) || modelState[propertyName].Errors.Count <= 1)
return htmlHelper.ValidationMessageFor(expression, null,
@osya
osya / get_distro.sh
Created February 18, 2016 14:46
Detect Operation System from Bash
get_distro() {
ARCH=$(uname -m | sed 's/x86_/amd/;s/i[3-6]86/x86/')
if [ $ARCH == "amd64" ]; then
T_ARCH="x86_64"
WK_ARCH="amd64"
else
T_ARCH="i386"
WK_ARCH="i386"
fi
@osya
osya / gist:fd2db3456b61844d1e9e
Created November 27, 2015 11:26
Base64 C# #CSharp
static byte[] AnotherDecode64(string base64Decoded)
{
string temp = base64Decoded.TrimEnd('=');
int asciiChars = temp.Length - temp.Count(c => Char.IsWhiteSpace(c));
switch (asciiChars % 4)
{
case 1:
//This would always produce an exception!!
//Regardless what (or what not) you attach to your string!
//Better would be some kind of throw new Exception()
@osya
osya / gist:1707c0812cfc9b803b7b
Last active December 2, 2015 20:50
XAML Reader Loop Examples #XAML #CSharp
XmlReader xmlReader = XmlReader.Create(input);
XamlXmlReader reader = new XamlXmlReader(xmlReader, System.Windows.Markup.XamlReader.GetWpfSchemaContext());
XamlObjectWriter writer = new XamlObjectWriter(reader.SchemaContext);
while (reader.Read())
{
switch (reader.NodeType)
{
case XamlNodeType.StartObject:
if (!reader.Type.Name.Equals("Window"))