Skip to content

Instantly share code, notes, and snippets.

View yamaneko1212's full-sized avatar

KANEKO Ryosuke yamaneko1212

View GitHub Profile
@yamaneko1212
yamaneko1212 / dump.py
Created November 8, 2011 03:34
日本語を含むUnicodeのJSON文字列を得る. ref: http://qiita.com/items/935
import json
data = {u'title': u'ほげ'}
json_data = json.dumps(data, ensure_ascii=False)
@yamaneko1212
yamaneko1212 / seri.py
Created November 9, 2011 04:30
JSON変換するクラス ref: http://qiita.com/items/970
import json
from unittest import TestCase
import unittestclass JSONSerializable(object):
"""Serialize to json. """ def to_json(self, exclude=tuple()):
"""
exclude: List or tuple of attributes that is excluded to serialize.
"""
data = self.to_serializable_dict(exclude=exclude)
return json.dumps(data, ensure_ascii=False) def to_serializable_dict(self, exclude=tuple()):
"""Convert to dict that is json serializable.
@yamaneko1212
yamaneko1212 / dec.py
Created November 14, 2011 09:29
[Revised] 値をキャッシュするpropertyデコレータ ref: http://qiita.com/items/1057
from functools import wraps
import mox
from unittest import TestCasedef cached_property(f):
prefix = '_cached_property_'
@wraps(f)
def _cached_property(self):
key = prefix + f.__name__
if hasattr(self, key):
return getattr(self, key)
value = f(self)
# -*- coding: utf-8
from unittest import TestCase
import unittest
from mongoengine import connect, Document, EmbeddedDocument, StringField, IntField, \
FloatField, BooleanField, DateTimeField, EmbeddedDocumentField, ListField, \
DictField, ObjectIdField, ReferenceField, MapField, DecimalField, \
ComplexDateTimeField, URLField, GenericReferenceField, FileField, BinaryField, \
SortedListField, EmailField, GeoPointField, ImageField, SequenceField, UUIDField, \
GenericEmbeddedDocumentField
# -*- coding: utf-8
from unittest import TestCase
import unittest
from mongoengine import connect, Document, StringField, IntField, FloatField, \
ComplexDateTimeField
import datetime
class Test(TestCase):
def setUp(self):
@yamaneko1212
yamaneko1212 / gist:1431439
Created December 4, 2011 22:06
なんかよくわからんが, すごい短い間隔でDBをドロップすると, GridFSの部分で失敗する. あとで細かいことを検証しよう.
# -*- coding: utf-8
# This code doesn't work correctly.
# Reproduced on Python 2.7, Mongodb 1.8.4, pymongo 2.0.1 and mongoengine 0.5
from mongoengine import Document, FileField, connect
import pymongo
db = 'my_test'
# -*- coding: utf-8
from paste.httpserver import serve
from pyramid.configuration import Configurator
from pyramid.response import Response
from pyramid.events import subscriber, ApplicationCreated, NewRequest
from pyramid.session import UnencryptedCookieSessionFactoryConfig
from pyramid.exceptions import NotFound
from pyramid.httpexceptions import HTTPFound
from mongoengine import connect, Document, StringField, \
@yamaneko1212
yamaneko1212 / patch.py
Created December 8, 2011 02:09
モンキーパッチを管理する ref: http://qiita.com/items/1321
# -*- coding: utf-8
from unittest import TestCase
class PatchManager(object):
def __init__(self):
self._storage = {}
def attach(self, target, name, patch):
key = '%s.%s'%(target.__name__, name)
@yamaneko1212
yamaneko1212 / file0.txt
Created December 18, 2011 10:52
Objective-Cモードでのインデント設定 ref: http://qiita.com/items/1402
(add-hook 'c-mode-common-hook
'(lambda()
(c-set-style "cc-mode")
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)))
@yamaneko1212
yamaneko1212 / file0.txt
Created December 20, 2011 12:41
7つの言語Clojure1日目セルフスタディ2 ref: http://qiita.com/items/1421
(def obj-map {(class {}) :map (class []) :vector (class '()) :list (class '(nil)) :list})
(defn collection-type [col] (obj-map (class col)))