Skip to content

Instantly share code, notes, and snippets.

View giwa's full-sized avatar

Ken Takagiwa giwa

  • Nombre Premier
  • Tokyo
View GitHub Profile
@giwa
giwa / file0.txt
Last active April 26, 2016 21:08
Fetch PR from Github filtered by labels using PyGithub ref: http://qiita.com/giwa/items/50167309774927a5a203
from github import Github
token = 'YOUR_TOKEN'
g = Github(token)
repos = ['YOUR_REPO', 'YOUR_REPO']
# You can change get_organization to get_user()
org = g.get_organization('YOUR_ORG')
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/go-sql-driver/mysql"
"github.com/gocraft/dbr"
)
@giwa
giwa / file0.txt
Created April 14, 2016 06:34
EP 26 Use Multiple Inheritance Only for Mix-in Utility Classes ref: http://qiita.com/giwa/items/a3a92b56049b84b9965a
import time
class ToDictMixin:
def to_dict(self):
return self._traverse_dict(self.__dict__)
def _traverse_dict(self, instance_dict):
output = {}
for key, value in instance_dict.items():
output[key] = self._traverse(key, value)
@giwa
giwa / file0.txt
Last active April 13, 2016 13:05
EP 24 Use `@classmethod` Polymorphism to Construct Objects Generically ref: http://qiita.com/giwa/items/fd563a93825714cffd70
import os
from threading import Thread
class InputData:
def read(self):
raise NotImplementedError
class PathInputData(InputData):
def __init__(self, path):
super().__init__()
@giwa
giwa / file0.txt
Created April 13, 2016 12:52
EP 25 Initialize Parent Classes with `super` ref: http://qiita.com/giwa/items/1e8bc71050a2490ce95c
from pprint import pprint
class MyBaseClass:
def __init__(self, value):
self.value = value
class TimesTwo:
def __init__(self):
self.value *= 2
@giwa
giwa / file0.txt
Created March 15, 2016 16:22
EP 21 Enforce Clarity with Keyword-Only Arguments ref: http://qiita.com/giwa/items/7b04ce6f7262c2003321
def safe_division(number, divisor, ignore_overflow, ignore_zero_division):
try:
return number / divisor
except OverflowError:
if ignore_overflow:
return 0
else:
raise
except ZeroDivisionError:
if ignore_zero_division:
@giwa
giwa / file0.txt
Created March 13, 2016 05:53
EP19 Provide Optional Behavior with Keyword Arguments ref: http://qiita.com/giwa/items/c08c03452bb78fb916f8
def remidner(number, divisor):
return number % divisor
>>> sum(range(11))
55
>>> sum([i for i in range(1,11)])
55
>>> sum(i for i in range(1,11))
55
>>> import functools
>>> from functools import reduce
>>> reduce(lambda x,y: x + y, range(1,11))
55
@giwa
giwa / file0.txt
Created March 11, 2016 14:50
EP 17 Be Defensive When Iterating Over Arguments ref: http://qiita.com/giwa/items/3c386c906472c7d54f24
def normalize(numbers):
total = sum(numbers)
result = []
for value in numbers:
percent = 100 * value / total
result.append(percent)
return result
@giwa
giwa / file0.txt
Created March 7, 2016 05:45
EP 16 Consider Generator Instead of Returning Lists ref: http://qiita.com/giwa/items/e20a575a0b3e6028d532
def index_words(text):
result = []
if text:
result.append(0)
for index, letter in enumerate(text):
if letter == ' ':
result.append(index + 1)
return result