Skip to content

Instantly share code, notes, and snippets.

View werediver's full-sized avatar
💭
🦀

Raman Fedaseyeu werediver

💭
🦀
View GitHub Profile
@werediver
werediver / singleton.py
Created December 28, 2012 09:51
A thread safe implementation of singleton pattern in Python. Based on tornado.ioloop.IOLoop.instance() approach.
import threading
# Based on tornado.ioloop.IOLoop.instance() approach.
# See https://github.com/facebook/tornado
class SingletonMixin(object):
__singleton_lock = threading.Lock()
__singleton_instance = None
@classmethod
@werediver
werediver / websse.py
Last active June 1, 2023 14:17
Simple demonstration of how to implement Server-sent events (SSE) in Python using Bottle micro web-framework. SSE require asynchronous request handling, but it's tricky with WSGI. One way to achieve that is to use gevent library as shown here.
"""
Simple demonstration of how to implement Server-sent events (SSE) in Python
using Bottle micro web-framework.
SSE require asynchronous request handling, but it's tricky with WSGI. One way
to achieve that is to use gevent library as shown here.
Usage: just start the script and open http://localhost:8080/ in your browser.
Based on:
@werediver
werediver / main.dart
Last active November 1, 2022 14:00
Container types and the `map()` function in Dart
void main() {
print(f(true).map((value) => value + 1));
print(f(false).map((value) => value + 1));
print([1, 2, 3].map((value) => "x ${value + 1}"));
print([].map((value) => value + 1));
}
// Methods like
// map, flatMap / expand, filter / where, firstWhere, reduce, fold
void main() async {
await variant1();
variant2();
print("xxx");
}
Future<void> variant1() async {
final result = await y();
// async gap
print("y: $result");
@werediver
werediver / default.yaml
Last active February 18, 2022 13:33
Lima/Podman configuration allowing connections to ports 80, 443 through non-loopback interfaces
# Based on https://github.com/lima-vm/lima/blob/943c90b13e38be32777b8f25be17c2491bb1421f/examples/podman.yaml
#
# Allows connections to ports 80, 443 through non-loopback interfaces.
# Example to use Podman instead of containerd & nerdctl
# $ limactl start ./podman.yaml
# $ limactl shell podman podman run -it -v $HOME:$HOME --rm docker.io/library/alpine
# To run `podman` on the host (assumes podman-remote is installed):
# $ export CONTAINER_HOST=$(limactl list podman --format 'unix://{{.Dir}}/sock/podman.sock')
@werediver
werediver / WiFiSsid.swift
Created July 14, 2016 12:47
Get the connected Wi-Fi network SSID on iOS (in Swift).
import Foundation
import SystemConfiguration.CaptiveNetwork
func getWiFiSsid() -> String? {
var ssid: String?
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
break
@werediver
werediver / CircularQueue.h
Created August 22, 2014 11:45
Simple implementation of circular queue in Objective-C (for ARC-enabled environment).
//
// CircularQueue.h
//
// Created on 9/21/13.
//
#import <Foundation/Foundation.h>
@interface CircularQueue : NSObject <NSFastEnumeration>
@werediver
werediver / resample_rlen.vex
Created October 10, 2020 15:22
Houdini VEX code to resample a (two point) curve into segments of random length (with restrictions)
// Resample into segments of random length
// Run over primitives
float seed = chf("seed");
float seg_len_min = chi("seg_len_min");
float seg_len_max = chi("seg_len_max");
float seg_padding = chf("padding");
if (seg_len_min <= 0 || seg_len_max < seg_len_min || seg_padding < 0) {
error("Make sure 0 < seg_len_min <= seg_len_max and 0 <= seg_padding");
@werediver
werediver / ViewTransition.swift
Last active July 8, 2020 10:19
View Transition mechanism for MVVM pattern (Swift, iOS)
import UIKit
// MARK: - Specification
protocol ViewType {
associatedtype ViewModel: ViewModelType
var viewModel: ViewModel { get }
@werediver
werediver / LazyServiceLocator.swift
Last active March 18, 2020 10:32
Service Locator pattern implementation in Swift 2 with lazy (on demand) service initialization.
protocol ServiceLocator {
func getService<T>() -> T?
}
final class LazyServiceLocator: ServiceLocator {
/// Registry record
enum RegistryRec {
case Instance(Any)