pythonism 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -1
- data/bin/pythonism +2 -0
- data/lib/pythonism.rb +30 -2
- data/lib/pythonism/classes/array.rb +7 -2
- data/lib/pythonism/functions.rb +14 -0
- data/lib/pythonism/functions/builtin.rb +118 -0
- data/lib/pythonism/pythonize/statement.rb +1 -23
- data/lib/pythonism/version.rb +2 -1
- data/spec/pythonism/classes/array_spec.rb +4 -0
- data/spec/pythonism/classes/fixnum_spec.rb +6 -0
- data/spec/pythonism/classes/string_spec.rb +10 -6
- metadata +6 -4
- data/lib/pythonism/methods.rb +0 -1
- data/lib/pythonism/methods/kernel.rb +0 -55
data/README.md
CHANGED
data/bin/pythonism
ADDED
data/lib/pythonism.rb
CHANGED
@@ -1,7 +1,35 @@
|
|
1
1
|
require 'pythonism/version'
|
2
2
|
require 'pythonism/pythonize'
|
3
3
|
require 'pythonism/classes'
|
4
|
-
require 'pythonism/
|
4
|
+
require 'pythonism/functions'
|
5
5
|
|
6
6
|
# Namespace for Pythonism
|
7
|
-
module Pythonism
|
7
|
+
module Pythonism
|
8
|
+
# Zen of Python
|
9
|
+
# @author Tim Peters
|
10
|
+
# @see http://www.python.org/dev/peps/pep-0020/
|
11
|
+
# @note licensed under Public Domain
|
12
|
+
ZEN_OF_PYTHON = <<-EOT.freeze
|
13
|
+
The Zen of Python, by Tim Peters
|
14
|
+
|
15
|
+
Beautiful is better than ugly.
|
16
|
+
Explicit is better than implicit.
|
17
|
+
Simple is better than complex.
|
18
|
+
Complex is better than complicated.
|
19
|
+
Flat is better than nested.
|
20
|
+
Sparse is better than dense.
|
21
|
+
Readability counts.
|
22
|
+
Special cases aren't special enough to break the rules.
|
23
|
+
Although practicality beats purity.
|
24
|
+
Errors should never pass silently.
|
25
|
+
Unless explicitly silenced.
|
26
|
+
In the face of ambiguity, refuse the temptation to guess.
|
27
|
+
There should be one-- and preferably only one --obvious way to do it.
|
28
|
+
Although that way may not be obvious at first unless you're Dutch.
|
29
|
+
Now is better than never.
|
30
|
+
Although never is often better than *right* now.
|
31
|
+
If the implementation is hard to explain, it's a bad idea.
|
32
|
+
If the implementation is easy to explain, it may be a good idea.
|
33
|
+
Namespaces are one honking great idea -- let's do more of those!
|
34
|
+
EOT
|
35
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
# Pythonized +
|
2
|
-
class
|
1
|
+
# Pythonized +Array+ class
|
2
|
+
class Array
|
3
3
|
include Pythonism::Pythonize::Basic
|
4
4
|
|
5
5
|
# @return [Boolean]
|
@@ -12,6 +12,11 @@ class String
|
|
12
12
|
self
|
13
13
|
end
|
14
14
|
|
15
|
+
# @return [String]
|
16
|
+
def to_s
|
17
|
+
"[#{self.join(', ')}]"
|
18
|
+
end
|
19
|
+
|
15
20
|
# @return [String]
|
16
21
|
def self.to_s; 'list'; end
|
17
22
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# Provide Python like functions
|
2
|
+
module Pythonism::Functions
|
3
|
+
#
|
4
|
+
IMPORT_PROCS = {
|
5
|
+
builtin: proc{ include Pythonism::Functions::Builtin },
|
6
|
+
}
|
7
|
+
|
8
|
+
# @param [Symbol]
|
9
|
+
def self.import (key)
|
10
|
+
::Object.class_eval(&IMPORT_PROCS[key.to_sym])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
require 'pythonism/functions/builtin'
|
14
|
+
Pythonism::Functions::import(:builtin)
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# Define Pythonic Built-in Functions
|
2
|
+
# @see http://docs.python.org/2/library/functions.html
|
3
|
+
module Pythonism::Functions::Builtin
|
4
|
+
# @param [Object] obj
|
5
|
+
# @return [Boolean,Method]
|
6
|
+
def bool (*obj)
|
7
|
+
case obj.size
|
8
|
+
when 0
|
9
|
+
method(:bool)
|
10
|
+
when 1
|
11
|
+
obj[0].__nonzero__
|
12
|
+
else
|
13
|
+
raise ArgumentError
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param [Object] obj
|
18
|
+
# @return [Class]
|
19
|
+
def type (*obj)
|
20
|
+
case obj.size
|
21
|
+
when 0
|
22
|
+
Class
|
23
|
+
when 1
|
24
|
+
obj[0].__class__
|
25
|
+
else
|
26
|
+
raise ArgumentError
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# @param [Object] obj
|
31
|
+
# @return [Array,Class]
|
32
|
+
def list (*obj)
|
33
|
+
case obj.size
|
34
|
+
when 0
|
35
|
+
Array
|
36
|
+
when 1
|
37
|
+
obj[0].to_a
|
38
|
+
else
|
39
|
+
raise ArgumentError
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# @param [Object] obj
|
44
|
+
# @return [Fixnum,Bignum,Class]
|
45
|
+
def int (*obj)
|
46
|
+
case obj.size
|
47
|
+
when 0
|
48
|
+
Fixnum
|
49
|
+
when 1
|
50
|
+
obj[0].__int__
|
51
|
+
else
|
52
|
+
raise ArgumentError
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# @param [Object] obj
|
57
|
+
# @return [Fixnum,Bignum,Class]
|
58
|
+
def long (*obj)
|
59
|
+
case obj.size
|
60
|
+
when 0
|
61
|
+
Bignum
|
62
|
+
when 1
|
63
|
+
obj[0].__long__
|
64
|
+
else
|
65
|
+
raise ArgumentError
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# @param [Object] obj
|
70
|
+
# @return [Float,Class]
|
71
|
+
def float (*obj)
|
72
|
+
case obj.size
|
73
|
+
when 0
|
74
|
+
Float
|
75
|
+
when 1
|
76
|
+
obj[0].__float__
|
77
|
+
else
|
78
|
+
raise ArgumentError
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# @param [Object] obj
|
83
|
+
# @return [String,Class]
|
84
|
+
def str (*obj)
|
85
|
+
case obj.size
|
86
|
+
when 0
|
87
|
+
String
|
88
|
+
when 1
|
89
|
+
obj[0].__str__
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# @param [Object] obj
|
94
|
+
# @return [String,Class]
|
95
|
+
def hex (*obj)
|
96
|
+
case obj.size
|
97
|
+
when 0
|
98
|
+
method(:hex)
|
99
|
+
when 1
|
100
|
+
obj[0].__hex__
|
101
|
+
else
|
102
|
+
raise ArgumentError
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# @param [Object] obj
|
107
|
+
# @return [String,Class]
|
108
|
+
def oct (*obj)
|
109
|
+
case obj.size
|
110
|
+
when 0
|
111
|
+
method(:oct)
|
112
|
+
when 1
|
113
|
+
obj[0].__oct__
|
114
|
+
else
|
115
|
+
raise ArgumentError
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
@@ -5,29 +5,7 @@ module Pythonism::Pythonize::Statement
|
|
5
5
|
def import (obj)
|
6
6
|
case obj
|
7
7
|
when :this
|
8
|
-
puts
|
9
|
-
The Zen of Python, by Tim Peters
|
10
|
-
|
11
|
-
Beautiful is better than ugly.
|
12
|
-
Explicit is better than implicit.
|
13
|
-
Simple is better than complex.
|
14
|
-
Complex is better than complicated.
|
15
|
-
Flat is better than nested.
|
16
|
-
Sparse is better than dense.
|
17
|
-
Readability counts.
|
18
|
-
Special cases aren't special enough to break the rules.
|
19
|
-
Although practicality beats purity.
|
20
|
-
Errors should never pass silently.
|
21
|
-
Unless explicitly silenced.
|
22
|
-
In the face of ambiguity, refuse the temptation to guess.
|
23
|
-
There should be one-- and preferably only one --obvious way to do it.
|
24
|
-
Although that way may not be obvious at first unless you're Dutch.
|
25
|
-
Now is better than never.
|
26
|
-
Although never is often better than *right* now.
|
27
|
-
If the implementation is hard to explain, it's a bad idea.
|
28
|
-
If the implementation is easy to explain, it may be a good idea.
|
29
|
-
Namespaces are one honking great idea -- let's do more of those!
|
30
|
-
EOT
|
8
|
+
puts Pythonism::ZEN_OF_PYTHON
|
31
9
|
nil
|
32
10
|
else
|
33
11
|
require obj.to_s
|
data/lib/pythonism/version.rb
CHANGED
@@ -3,11 +3,15 @@ require File.expand_path('../../../spec_helper', __FILE__)
|
|
3
3
|
describe Array do
|
4
4
|
context '' do
|
5
5
|
let(:ary){ [1, 2, 3] }
|
6
|
+
it{ expect( ary ).to be_an_instance_of list }
|
7
|
+
it{ expect( str(ary) ).to eq '[1, 2, 3]' }
|
6
8
|
it{ expect( bool(ary) ).to be_true }
|
7
9
|
it{ expect( list(ary) ).to eq ary }
|
8
10
|
end
|
9
11
|
context 'empty' do
|
10
12
|
let(:ary){ [] }
|
13
|
+
it{ expect( ary ).to be_an_instance_of list }
|
14
|
+
it{ expect( str(ary) ).to eq '[]' }
|
11
15
|
it{ expect( bool(ary) ).to be_false }
|
12
16
|
it{ expect( list(ary) ).to eq ary }
|
13
17
|
end
|
@@ -3,12 +3,18 @@ require File.expand_path('../../../spec_helper', __FILE__)
|
|
3
3
|
describe Fixnum do
|
4
4
|
context 'non-zero number' do
|
5
5
|
let(:num){ 1 }
|
6
|
+
it{ expect( num ).to be_an_instance_of int }
|
7
|
+
it{ expect( type(num) ).to eq int }
|
6
8
|
it{ expect( bool(num) ).to be_true }
|
7
9
|
it{ expect( int(num) ).to eq num }
|
10
|
+
it{ expect( str(num) ).to eq '1' }
|
8
11
|
end
|
9
12
|
context 'zero' do
|
10
13
|
let(:num){ 0 }
|
14
|
+
it{ expect( num ).to be_an_instance_of int }
|
15
|
+
it{ expect( type(num) ).to eq int }
|
11
16
|
it{ expect( bool(num) ).to be_false }
|
12
17
|
it{ expect( int(num) ).to eq num }
|
18
|
+
it{ expect( str(num) ).to eq '0' }
|
13
19
|
end
|
14
20
|
end
|
@@ -2,13 +2,17 @@ require File.expand_path('../../../spec_helper', __FILE__)
|
|
2
2
|
|
3
3
|
describe String do
|
4
4
|
context 'string' do
|
5
|
-
let(:
|
6
|
-
it{ expect(
|
7
|
-
it{ expect(
|
5
|
+
let(:string){ 'str' }
|
6
|
+
it{ expect( string ).to be_an_instance_of str }
|
7
|
+
it{ expect( type(string) ).to eq str }
|
8
|
+
it{ expect( bool(string) ).to be_true }
|
9
|
+
it{ expect( list(string) ).to eq string.each_char.to_a }
|
8
10
|
end
|
9
11
|
context 'empty string' do
|
10
|
-
let(:
|
11
|
-
it{ expect(
|
12
|
-
it{ expect(
|
12
|
+
let(:string){ '' }
|
13
|
+
it{ expect( string ).to be_an_instance_of str }
|
14
|
+
it{ expect( type(string) ).to eq str }
|
15
|
+
it{ expect( bool(string) ).to be_false }
|
16
|
+
it{ expect( list(string) ).to eq string.each_char.to_a }
|
13
17
|
end
|
14
18
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pythonism
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,8 @@ dependencies: []
|
|
14
14
|
description: This gem was made for friendship with Pythonistas.
|
15
15
|
email:
|
16
16
|
- tadsan@zonu.me
|
17
|
-
executables:
|
17
|
+
executables:
|
18
|
+
- pythonism
|
18
19
|
extensions: []
|
19
20
|
extra_rdoc_files: []
|
20
21
|
files:
|
@@ -23,6 +24,7 @@ files:
|
|
23
24
|
- LICENSE.txt
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|
27
|
+
- bin/pythonism
|
26
28
|
- lib/pythonism.rb
|
27
29
|
- lib/pythonism/classes.rb
|
28
30
|
- lib/pythonism/classes/array.rb
|
@@ -34,8 +36,8 @@ files:
|
|
34
36
|
- lib/pythonism/classes/object.rb
|
35
37
|
- lib/pythonism/classes/string.rb
|
36
38
|
- lib/pythonism/classes/true_class.rb
|
37
|
-
- lib/pythonism/
|
38
|
-
- lib/pythonism/
|
39
|
+
- lib/pythonism/functions.rb
|
40
|
+
- lib/pythonism/functions/builtin.rb
|
39
41
|
- lib/pythonism/pythonize.rb
|
40
42
|
- lib/pythonism/pythonize/basic.rb
|
41
43
|
- lib/pythonism/pythonize/numeric.rb
|
data/lib/pythonism/methods.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'pythonism/methods/kernel'
|
@@ -1,55 +0,0 @@
|
|
1
|
-
# Define Kernel methods
|
2
|
-
module ::Kernel
|
3
|
-
# @return [Object]
|
4
|
-
def this
|
5
|
-
self
|
6
|
-
end
|
7
|
-
|
8
|
-
# @param [Object] obj
|
9
|
-
# @return [Boolean]
|
10
|
-
def bool (obj)
|
11
|
-
obj.__nonzero__
|
12
|
-
end
|
13
|
-
|
14
|
-
# @param [Object] obj
|
15
|
-
# @return [Class]
|
16
|
-
def type (obj)
|
17
|
-
obj.__class__
|
18
|
-
end
|
19
|
-
|
20
|
-
# @param [Object] obj
|
21
|
-
# @return [Array]
|
22
|
-
def list (obj)
|
23
|
-
obj.to_a
|
24
|
-
end
|
25
|
-
|
26
|
-
# @param [Object] obj
|
27
|
-
# @return [Fixnum,Bignum]
|
28
|
-
def int (obj)
|
29
|
-
obj.__int__
|
30
|
-
end
|
31
|
-
|
32
|
-
# @param [Object] obj
|
33
|
-
# @return [Fixnum,Bignum]
|
34
|
-
def long (obj)
|
35
|
-
obj.__long__
|
36
|
-
end
|
37
|
-
|
38
|
-
# @param [Object] obj
|
39
|
-
# @return [Float]
|
40
|
-
def float (obj)
|
41
|
-
obj.__float__
|
42
|
-
end
|
43
|
-
|
44
|
-
# @param [Object] obj
|
45
|
-
# @return [String]
|
46
|
-
def str (obj)
|
47
|
-
obj.__str__
|
48
|
-
end
|
49
|
-
|
50
|
-
# @param [Object] obj
|
51
|
-
# @return [String]
|
52
|
-
def oct (obj)
|
53
|
-
obj.__oct__
|
54
|
-
end
|
55
|
-
end
|