Skip to content

Instantly share code, notes, and snippets.

Ken Takagiwa giwa

Block or report user

Report or block giwa

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@giwa
giwa / file0.txt
Last active Apr 13, 2016
EP 24 Use `@classmethod` Polymorphism to Construct Objects Generically ref: http://qiita.com/giwa/items/fd563a93825714cffd70
View file0.txt
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 Mar 15, 2016
EP 21 Enforce Clarity with Keyword-Only Arguments ref: http://qiita.com/giwa/items/7b04ce6f7262c2003321
View file0.txt
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 Mar 13, 2016
EP19 Provide Optional Behavior with Keyword Arguments ref: http://qiita.com/giwa/items/c08c03452bb78fb916f8
View file0.txt
def remidner(number, divisor):
return number % divisor
@giwa
giwa / file0.txt
Created Mar 11, 2016
EP 17 Be Defensive When Iterating Over Arguments ref: http://qiita.com/giwa/items/3c386c906472c7d54f24
View file0.txt
def normalize(numbers):
total = sum(numbers)
result = []
for value in numbers:
percent = 100 * value / total
result.append(percent)
return result
View file0.txt
>>> 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 Mar 7, 2016
EP 16 Consider Generator Instead of Returning Lists ref: http://qiita.com/giwa/items/e20a575a0b3e6028d532
View file0.txt
def index_words(text):
result = []
if text:
result.append(0)
for index, letter in enumerate(text):
if letter == ' ':
result.append(index + 1)
return result
@giwa
giwa / file0.txt
Created Mar 7, 2016
EP 15 Know How Closures Interact with Variable Scope ref: http://qiita.com/giwa/items/ef67c710fd007039c48a
View file0.txt
def sort_priority(values, group):
def helper(x):
if x in group:
return (0, x)
return (1, x)
values.sort(key=helper)
@giwa
giwa / file0.txt
Created Mar 6, 2016
EP 14 Prefer Exceptions to Returning `None` ref: http://qiita.com/giwa/items/5ba9386970bcbccd0b80
View file0.txt
def divide(a, b):
try:
return a/b
except ZeroDivisionError:
return None
@giwa
giwa / file0.txt
Last active Feb 18, 2016
ER 6 Know How Ruby Build Inheritance Hierarachies ref: http://qiita.com/giwa/items/2390a3f93a9e8c474707
View file0.txt
BasicObject
<- Kernel (module)
<- PP::ObjectMixin(module)
<- Object
<- Person
<- Customer
@giwa
giwa / file0.rb
Last active Feb 3, 2016
Understand What Ruby considers to be True ref: http://qiita.com/giwa/items/8d083c48152bdb99f3fa
View file0.rb
[3] pry(main)> true.class
=> TrueClass
[4] pry(main)> false.class
=> FalseClass
You can’t perform that action at this time.