Skip to content

Instantly share code, notes, and snippets.

@eungju
eungju / LookAndSay.exs
Created October 31, 2014 02:24
Look and Say
[1] |> Stream.iterate(fn(x) -> x |> Enum.chunk_by(&+/1) |> Enum.flat_map(fn([x|_]=c) -> [x, Enum.count(c)] end) end) |> Enum.take(10)
@eungju
eungju / download-chapters.hs
Last active April 9, 2019 08:31
Download The Art of Science and Engineering
import Control.Applicative
import Control.Monad
import System.Directory
import System.FilePath
import System.Exit
import System.Process
import Text.Printf
type URL = String
@eungju
eungju / Cleave.hs
Created April 11, 2014 11:32
Factor cleave words in Haskell
bi :: (a -> b) -> (a -> c) -> a -> (b, c)
bi f g (x) = (f x, g x)
bi2 :: (a -> b -> c) -> (a -> b -> d) -> (a, b) -> (c, d)
bi2 f g (x, y) = (f x y, g x y)
tri :: (a -> b) -> (a -> c) -> (a -> d) -> a -> (b, c, d)
tri f g h (x) = (f x, g x, h x)
tri2 :: (a -> b -> c) -> (a -> b -> d) -> (a -> b -> e) -> (a, b) -> (c, d, e)
@eungju
eungju / otp.hs
Last active December 21, 2022 11:09
Google Authenticator in Haskell
#!/usr/bin/env runghc
import qualified Codec.Binary.Base32 as Base32
import Codec.Utils (i2osp, fromTwosComp)
import qualified Control.Arrow as Arrow
import Data.Bits
import Data.Char
import Data.Functor
import Data.HMAC
import Data.List.Split
@eungju
eungju / elpa-obsolete.hs
Last active August 29, 2015 13:58
ELPA utility
#!/usr/bin/env runghc
import Control.Monad
import Data.Function
import Data.Functor
import Data.List
import System.Directory
import System.FilePath
data ELPA = ELPA FilePath
@eungju
eungju / thrifthelper.py
Last active January 19, 2016 05:22
Thrift HA
class ThriftConnection(object):
def __init__(self, service, host, port):
socket = TSocket.TSocket(host, port)
self.transport = TTransport.TFramedTransport(socket)
protocol = TBinaryProtocolAccelerated(self.transport)
self.client = service.Client(protocol)
self.transport.open()
self.open_time = time.time()
self.access_time = self.open_time
self.str = "%s#%d(%s:%d/%s)" % (self.__class__.__name__, id(self), host, port, service.__name__.rsplit(".", 1)[-1])
@eungju
eungju / threaddump.md
Last active March 6, 2017 10:28
파이썬에서 데드락 해결하기 위해 쓰레드 덤프 얻는 방법.

데드락이 걸렸는데 코드를 봐도 어디인지 모를 때가 있다. 자바는 JVM이 쓰레드 덤프 기능을 제공해서 어떤 쓰레드가 어디서 멈춰 있는지 확인하기 쉬웠는데 파이썬은 쓰레드 덤프를 얻으려면 약간의 코딩이 필요하다.

아래와 같이 모든 쓰레드의 스택 트레이스를 볼 수 있다.

for ident, stack in sys._current_frames().items():
    logger.info(("%d" % ident) + "".join(traceback.format_list(traceback.extract_stack(stack))))

어떤 쓰레드가 멈춰 있는지 안다면 그 쓰레드의 스택 트레이스만 볼 수 있다.

@eungju
eungju / thrift_pool.py
Last active January 1, 2016 22:19
Thrift Client Pooling.
class ThriftClientPool(object):
def __init__(self, service, host, port, pool_conf=None):
self.service = service
self.host = host
self.port = port
self.pool_conf = {"max": 0, "min": 1, "idle_expire": 30, "expire": 3 * 60}
if pool_conf:
self.pool_conf.update(pool_conf)
self.pool = Queue(self.pool_conf["max"])
for i in range(self.pool_conf["min"]):
@eungju
eungju / kyotocabinet-mavericks.patch
Created November 8, 2013 04:35
A patch for kyotocabinet.
diff -ru kyotocabinet-1.2.76/kccommon.h kyotocabinet-1.2.76-mavericks/kccommon.h
--- kyotocabinet-1.2.76/kccommon.h 2012-05-25 01:27:59.000000000 +0900
+++ kyotocabinet-1.2.76-mavericks/kccommon.h 2013-11-08 13:22:57.000000000 +0900
@@ -82,7 +82,7 @@
using ::snprintf;
}
-#if __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(_MSC_VER)
+#if __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(_MSC_VER) || defined(_LIBCPP_VERSION)
@eungju
eungju / days.py
Last active December 25, 2015 10:19
휴일 표현과 계산.
import datetime
class DayExpression(object):
def include(self, date):
raise NotImplementedError
class Union(DayExpression):
def __init__(self, expressions):