Skip to content

Instantly share code, notes, and snippets.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Collections.Concurrent;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@Irfy
Irfy / 9303362.py
Created February 17, 2012 00:47
Implement a cutoff window and a circular window on top of numpy's matrix.
"""
Demonstrates creating windows or views of two-dimensional arrays, in two ways:
* Cutting off parts of window that is outside
* Continuing windows on the opposite side (circular sliding)
'numpy' is a very useful numerical library for python which *already* supports
creating windows/views (actually sub-arrays, for multidimensional arrays). This
code demonstrates how to "wrap" such an underlying implementation to provide
specialized functionality.
@Irfy
Irfy / so9336486.java
Created February 18, 2012 01:12
An alternative to late binding of template arguments to template arguments, by pulling the early bound template arguments to bind later, at the same time with their template arguments.
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Vector;
interface Wrapped<T> {
void commonWrappedMethod();
}
class Wrapped1<T> implements Wrapped<T> {
public class User {
public String[] usernames;
public User(String[] names) {
usernames = names;
}
public String[] getUsernames() {
return usernames;
}
#include <iostream>
#include <vector>
#include <cstdio>
#include <unistd.h>
#include <pthread.h>
#include <termios.h>
using std::cout;
using std::getchar;
@Irfy
Irfy / move.cc
Created November 29, 2015 20:07
Unintuitive move ctor behavior (gcc 5.2 in gnu++11 mode)
#define IMPLICIT_DELETE_MEMBER 0
#define DEFAULT_KEYWORD_INSIDE 0
#include <utility>
#include <iostream>
struct Explicit {
Explicit() = default;
Explicit(const Explicit&) { std::cout<<"Explicit-Copy called\n"; }
Explicit(Explicit&&) noexcept { std::cout<<"Explicit-Move called\n"; }
@Irfy
Irfy / x.cc
Created November 30, 2015 10:23
Demoing defaulted move ctor neither being generated nor called (see ImplicitDelete)
#include <utility>
#include <iostream>
using namespace std;
struct Explicit {
// prints whether the containing class's move or copy constructor was called
// in practice this would be the expensive vector<string>
string owner;
Explicit(string owner) : owner(owner) {};
Explicit(const Explicit& o) { cout << o.owner << " is actually copying\n"; }
$ cat x.cc
#include<type_traits>
#include<utility>
using namespace std;
struct T {
T() = default;
T(const T&) {}
};
static_assert(is_move_constructible<T>(), "");
T a;
import inspect
import ctypes
from time import sleep
from threading import Thread
def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exctype))
@Irfy
Irfy / so9269338.py
Last active February 19, 2019 10:51
Demonstrates a way to run blocking functions which require Ctrl-C handling capability on the main thread via queues from other threads in Python
from Queue import Queue, Empty
from functools import wraps
from threading import Thread, Semaphore
ctrlc_queue = Queue()
exit_sem = Semaphore(0) # Read: there are 0 ctrlc threads on start-up
def needs_ctrlc(fun):
@wraps(fun)
def wrapped(*args, **kwargs):