Skip to content

Instantly share code, notes, and snippets.

View PoisonousJohn's full-sized avatar

Ivan Fateev PoisonousJohn

View GitHub Profile
@PoisonousJohn
PoisonousJohn / Service-offline-emulation.cs
Last active February 20, 2018 16:39
Offline emulation mode per Service's method
public static class Services
{
public static bool isOffline
{
get; set;
}
}
public class APIService
{
@PoisonousJohn
PoisonousJohn / Service-offline-emulation-virtual.cs
Created February 20, 2018 16:34
Offline emulation using virtual methods
public class APIService {
public virtual string UserId {
get {
return _userId;
}
}
public virtual void Login()
{
// send request to server here
using System;
public static class Services
{
public static IAPIService api { get { return _api.Value; } }
public static bool isOffline { get; set; }
private static Lazy<IAPIService> _api = new Lazy<IAPIService>(() => isOffline
? (IAPIService)new OfflineAPIService()
: new APIService());
@PoisonousJohn
PoisonousJohn / Lazy.cs
Last active February 20, 2018 18:00
Reference Lazy<T> implementation for Unity
using System;
public class Lazy<T> {
public Lazy(Func<T> factory)
{
if (factory == null)
{
throw new ArgumentNullException("Lazy<T> doesn't accept null factory");
}
@PoisonousJohn
PoisonousJohn / cross_dependency.cs
Last active February 21, 2018 08:28
Solving cross-dependency with Lazy
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Services
{
public static Lazy<ILogger> log = new Lazy<ILogger>(() =>
{
return new UserIdUnityLogger(api);
});
@PoisonousJohn
PoisonousJohn / incorrect_dependency_injection.cs
Created February 21, 2018 09:28
Incorrect dependency injection
public class APIService : IAPIService
{
private readonly Lazy<ILogger> _logger;
public APIService()
{
_logger = Services.log;
}
public string UserId { get; private set; }
@PoisonousJohn
PoisonousJohn / copy.ps1
Last active March 30, 2018 15:24
Copy Azure managed image to a different region. Source: https://michaelcollier.wordpress.com/2017/05/03/copy-managed-images/
az account set --subscription $SubscriptionID
snapshotId=$(az snapshot show -g $ResourceGroupName -n $snapshotName --query "id" -o tsv )
# Get the SAS for the snapshotId
snapshotSasUrl=$(az snapshot grant-access -g $ResourceGroupName -n $snapshotName --duration-in-seconds 3600 -o tsv)
# Setup the target storage account in another region
targetStorageAccountKey=$(az storage account keys list -g $ResourceGroupName --account-name $targetStorageAccountName --query "[:1].value" -o tsv)
storageSasToken=$(az storage account generate-sas --expiry 2017-05-02'T'12:00'Z' --permissions aclrpuw --resource-types sco --services b --https-only --account-name $targetStorageAccountName --account-key $targetStorageAccountKey -o tsv)
@PoisonousJohn
PoisonousJohn / log.log
Created October 8, 2018 20:45
Motion log with rtsp
[alarm@alarmpi records]$ motion
[37881728:motion] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion/motion.conf
[37881728:motion] [NTC] [ALL] motion_startup: Motion 4.1.1 Started
[37881728:motion] [NTC] [ALL] motion_startup: Logging to syslog
[37881728:motion] [NTC] [ALL] motion_startup: Using log type (ALL) log level (DBG)
[37881728:motion] [INF] [ALL] conf_output_parms: Writing configuration parameters from all files (1):
[37881728:motion] [INF] [ALL] Thread 0 - Config file: /etc/motion/motion.conf
[37881728:motion] [INF] [ALL] daemon off
[37881728:motion] [INF] [ALL] process_id_file /var/run/motion/motion.pid
[37881728:motion] [INF] [ALL] setup_mode off
@PoisonousJohn
PoisonousJohn / tasks.json
Created January 18, 2019 14:03
vscode pylint problem matcher
{
"problemMatcher": {
"owner": "python",
"fileLocation": [
"relative",
"${workspaceFolder}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(W|E).+:\\s+(.*)$",
"file": 1,
@PoisonousJohn
PoisonousJohn / crop.sh
Last active February 1, 2019 22:06
ImageMagick crop image to square adding stripes on the sides, same names
#!/bin/bash
mkdir -p out
convert *.JPG -background white -resize "1500x1500" -gravity Center -extent 1500x1500 -rotate 90 -set filename:f '%t' out/'%[filename:f].png'