Skip to content

Instantly share code, notes, and snippets.

View motivic's full-sized avatar

Johnson Jia motivic

View GitHub Profile
@motivic
motivic / super_multi_inheritance.py
Created August 25, 2016 21:20
Using super in Python 2.7
# http://stackoverflow.com/questions/222877/how-to-use-super-in-python
class SomeBaseClass(object):
def __init__(self):
print('SomeBaseClass.__init__(self) called')
class ChildClass(SomeBaseClass):
def __init__(self):
print('ChildClass.__init__(self) called')
SomeBaseClass.__init__(self)
@motivic
motivic / property_and_inheritance.py
Created August 25, 2016 20:59
Properties and Inheritance in Python
# Property and inheritance
# http://stackoverflow.com/questions/1021464/how-to-call-a-property-of-the-base-class-if-this-property-is-being-overwritten-i
class Foo(object):
def __init__(self):
self._x = 'Foo'
@property
def x(self):
return self._x
@motivic
motivic / .gitignore
Created August 23, 2016 18:40 — forked from octocat/.gitignore
Some common .gitignore configurations
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
# Packages #
@motivic
motivic / keybase.md
Created August 17, 2016 10:53
PGP Keybase

Keybase proof

I hereby claim:

  • I am motivic on github.
  • I am motivic (https://keybase.io/motivic) on keybase.
  • I have a public key whose fingerprint is C8F7 82FA 8A76 6F7F 10C0 8A3B 224E 043B EB01 F627

To claim this, I am signing this object:

@motivic
motivic / c_pointer_casting.c
Created August 10, 2016 21:49
C Floating Pointer Casting
#include <stdio.h>
int main() {
double a = 100.0;
printf("double 100.0 into float: %lf \n", *(float*)(&a));
float b = 0.99;
printf("float 0.99 into double: %lf \n", *(double*)(&b));
float c = 100.0;
@motivic
motivic / each_permuted.rb
Created July 26, 2016 00:52
Engineering SaaS Proj 3.10 Ruby metaprogramming each_permuted
module Enumerable
def each_permuted
n = self.length
arr = *(0..n-1)
(n-1).downto(0) do |i|
j = rand(i)
arr[i], arr[j] = arr[j], arr[i]
end
arr.each do |x|
@motivic
motivic / each_with_flattening.rb
Created July 25, 2016 23:35
Engineering SaaS Proj 3.9 Ruby metaprogramming each_with_flattening
module Enumerable
def each_with_flattening(&block)
self.flatten.each do |x|
yield(x)
end
end
end
@motivic
motivic / FibSequence.rb
Created July 25, 2016 23:06
Engineering SaaS Proj 3.8 Ruby metaprogramming FibSequence
class FibSequence
include Enumerable
def initialize(n)
@n = n
@a1 = 0
@a2 = 1
end
def each
@motivic
motivic / each_with_custom_index.rb
Created July 25, 2016 22:41
Engineering SaaS Proj 3.7 Ruby metaprogramming each_with_custom_index
module Enumerable
def each_with_custom_index(start, step)
index = start
self.each do |x|
yield(x, index)
index += step
end
end
end
@motivic
motivic / attr_accessor_with_history.rb
Created July 23, 2016 20:05
Engineering SaaS Proj 3.6 Ruby metaprogramming attr_accessor_with_history
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s
raise ArgumentError, "'history' is a keyword" if attr_name == 'history'
#getter
self.class_eval("def #{attr_name};@#{attr_name};end")
#setter
self.class_eval %Q{