pythonism 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +30 -0
- data/Rakefile +1 -0
- data/lib/pythonism.rb +7 -0
- data/lib/pythonism/classes.rb +5 -0
- data/lib/pythonism/classes/array.rb +17 -0
- data/lib/pythonism/classes/boolean.rb +2 -0
- data/lib/pythonism/classes/class.rb +12 -0
- data/lib/pythonism/classes/false_class.rb +16 -0
- data/lib/pythonism/classes/fixnum.rb +13 -0
- data/lib/pythonism/classes/nil_class.rb +16 -0
- data/lib/pythonism/classes/object.rb +4 -0
- data/lib/pythonism/classes/string.rb +21 -0
- data/lib/pythonism/classes/true_class.rb +13 -0
- data/lib/pythonism/methods.rb +1 -0
- data/lib/pythonism/methods/kernel.rb +55 -0
- data/lib/pythonism/pythonize.rb +10 -0
- data/lib/pythonism/pythonize/basic.rb +69 -0
- data/lib/pythonism/pythonize/numeric.rb +65 -0
- data/lib/pythonism/pythonize/statement.rb +33 -0
- data/lib/pythonism/version.rb +3 -0
- data/pythonism.gemspec +28 -0
- data/spec/pythonism/classes/array_spec.rb +14 -0
- data/spec/pythonism/classes/fixnum_spec.rb +14 -0
- data/spec/pythonism/classes/string_spec.rb +14 -0
- data/spec/pythonism_spec.rb +6 -0
- data/spec/spec_helper.rb +6 -0
- metadata +84 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 USAMI Kenta
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
Pythonism gem
|
2
|
+
=============
|
3
|
+
|
4
|
+
Ruby for my dear Pythonistas...
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'pythonism'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install pythonism
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
TODO: Write usage instructions here
|
23
|
+
|
24
|
+
## Contributing
|
25
|
+
|
26
|
+
1. Fork it
|
27
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
28
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
29
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
30
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/pythonism.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Pythonized +String+ class
|
2
|
+
class String
|
3
|
+
include Pythonism::Pythonize::Basic
|
4
|
+
|
5
|
+
# @return [Boolean]
|
6
|
+
def __nonzero__
|
7
|
+
self.size != 0
|
8
|
+
end
|
9
|
+
|
10
|
+
# @return [Array]
|
11
|
+
def to_a
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [String]
|
16
|
+
def self.to_s; 'list'; end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Alias of +false+ object
|
2
|
+
False = false
|
3
|
+
|
4
|
+
# Pythonized +FalseClass+ class
|
5
|
+
class FalseClass
|
6
|
+
include Pythonism::Pythonize::Basic
|
7
|
+
|
8
|
+
# @return [String]
|
9
|
+
def inspect; 'False'; end
|
10
|
+
|
11
|
+
# @return [FalseClass]
|
12
|
+
def __nonzero__; false; end
|
13
|
+
|
14
|
+
# @return [String]
|
15
|
+
def self.to_s; 'bool'; end
|
16
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Alias of +nil+ object
|
2
|
+
None = nil
|
3
|
+
|
4
|
+
# Pythonized +NilClass+ class
|
5
|
+
class NilClass
|
6
|
+
include Pythonism::Pythonize::Basic
|
7
|
+
|
8
|
+
# @return [String]
|
9
|
+
def inspect; 'False'; end
|
10
|
+
|
11
|
+
# @return [FalseClass]
|
12
|
+
def __nonzero__; false; end
|
13
|
+
|
14
|
+
# @return [String]
|
15
|
+
def self.to_s; 'bool'; end
|
16
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Pythonized +String+ class
|
2
|
+
class String
|
3
|
+
include Pythonism::Pythonize::Basic
|
4
|
+
include Pythonism::Pythonize::Numeric
|
5
|
+
|
6
|
+
alias startswith start_with?
|
7
|
+
alias endswith end_with?
|
8
|
+
|
9
|
+
# @return [Boolean]
|
10
|
+
def __nonzero__
|
11
|
+
self.size != 0
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [Array]
|
15
|
+
def to_a
|
16
|
+
self.each_char.to_a
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [String]
|
20
|
+
def self.to_s; 'str'; end
|
21
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'pythonism/methods/kernel'
|
@@ -0,0 +1,55 @@
|
|
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
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Pythonism::Pythonize
|
2
|
+
# Provide Basic methods
|
3
|
+
module Basic
|
4
|
+
# @return [Object]
|
5
|
+
def __new__ (object)
|
6
|
+
raise TypeError
|
7
|
+
end
|
8
|
+
|
9
|
+
# @return [String]
|
10
|
+
def __str__
|
11
|
+
self.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [String]
|
15
|
+
def __repr__
|
16
|
+
self.inspect
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [Array]
|
20
|
+
def __list__
|
21
|
+
self.to_a
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Class]
|
25
|
+
def __class__
|
26
|
+
self.send(:class)
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Boolean]
|
30
|
+
def __lt__ (other)
|
31
|
+
self < other
|
32
|
+
end
|
33
|
+
|
34
|
+
# @return [Boolean]
|
35
|
+
def __le__ (other)
|
36
|
+
self <= other
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Boolean]
|
40
|
+
def __eq__ (other)
|
41
|
+
self == other
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [Boolean]
|
45
|
+
def __gt__ (other)
|
46
|
+
self > other
|
47
|
+
end
|
48
|
+
|
49
|
+
# @return [Boolean]
|
50
|
+
def __ge__ (other)
|
51
|
+
self >= other
|
52
|
+
end
|
53
|
+
|
54
|
+
# @return [Fixnum]
|
55
|
+
def __cmp__ (other)
|
56
|
+
self <=> other
|
57
|
+
end
|
58
|
+
|
59
|
+
# @return [Boolean]
|
60
|
+
def __ne__ (other)
|
61
|
+
not self.__eq__(other)
|
62
|
+
end
|
63
|
+
|
64
|
+
# @return [Boolean]
|
65
|
+
def __nonzero__
|
66
|
+
true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Pythonism::Pythonize
|
2
|
+
# Provide Numeric methods
|
3
|
+
module Numeric
|
4
|
+
|
5
|
+
# @return [Fixnum,Bignum]
|
6
|
+
def __int__
|
7
|
+
self.to_i
|
8
|
+
end
|
9
|
+
|
10
|
+
# @return [Fixnum,Bignum]
|
11
|
+
def __long__
|
12
|
+
self.to_i
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [Float]
|
16
|
+
def __float__
|
17
|
+
self.to_f
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [String]
|
21
|
+
def __oct__
|
22
|
+
'%o' % self.__int__
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [String]
|
26
|
+
def __hex__
|
27
|
+
'%x' % self.__int__
|
28
|
+
end
|
29
|
+
|
30
|
+
# @return [Object]
|
31
|
+
def __add__ (other)
|
32
|
+
self + other
|
33
|
+
end
|
34
|
+
|
35
|
+
# @return [Object]
|
36
|
+
def __sub__ (other)
|
37
|
+
self - other
|
38
|
+
end
|
39
|
+
|
40
|
+
# @return [Object]
|
41
|
+
def __mul__ (other)
|
42
|
+
self * other
|
43
|
+
end
|
44
|
+
|
45
|
+
# @return [Object]
|
46
|
+
def __mod__(other)
|
47
|
+
self % other
|
48
|
+
end
|
49
|
+
|
50
|
+
# @return [Object]
|
51
|
+
def __and__(other)
|
52
|
+
self & other
|
53
|
+
end
|
54
|
+
|
55
|
+
# @return [Object]
|
56
|
+
def __xor__(other)
|
57
|
+
self ^ other
|
58
|
+
end
|
59
|
+
|
60
|
+
# @return [Object]
|
61
|
+
def __or__(other)
|
62
|
+
self | other
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Pythonism::Pythonize::Statement
|
2
|
+
def import (obj)
|
3
|
+
case obj.inspect
|
4
|
+
when 'main'
|
5
|
+
puts <<EOT
|
6
|
+
The Zen of Python, by Tim Peters
|
7
|
+
|
8
|
+
Beautiful is better than ugly.
|
9
|
+
Explicit is better than implicit.
|
10
|
+
Simple is better than complex.
|
11
|
+
Complex is better than complicated.
|
12
|
+
Flat is better than nested.
|
13
|
+
Sparse is better than dense.
|
14
|
+
Readability counts.
|
15
|
+
Special cases aren't special enough to break the rules.
|
16
|
+
Although practicality beats purity.
|
17
|
+
Errors should never pass silently.
|
18
|
+
Unless explicitly silenced.
|
19
|
+
In the face of ambiguity, refuse the temptation to guess.
|
20
|
+
There should be one-- and preferably only one --obvious way to do it.
|
21
|
+
Although that way may not be obvious at first unless you're Dutch.
|
22
|
+
Now is better than never.
|
23
|
+
Although never is often better than *right* now.
|
24
|
+
If the implementation is hard to explain, it's a bad idea.
|
25
|
+
If the implementation is easy to explain, it may be a good idea.
|
26
|
+
Namespaces are one honking great idea -- let's do more of those!
|
27
|
+
EOT
|
28
|
+
nil
|
29
|
+
else
|
30
|
+
require obj.to_s
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/pythonism.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pythonism/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "pythonism"
|
8
|
+
gem.version = Pythonism::VERSION
|
9
|
+
gem.authors = ["USAMI Kenta"]
|
10
|
+
gem.email = ["tadsan@zonu.me"]
|
11
|
+
gem.description = "This gem was made for friendship with Pythonistas."
|
12
|
+
gem.summary = <<EOT
|
13
|
+
Pythonism gem provides Python-like interface and functions.
|
14
|
+
|
15
|
+
We are convinced that you should come to like Ruby if you do not like Ruby.
|
16
|
+
We are convinced that you should come to like Python if you do not like Python.
|
17
|
+
|
18
|
+
But, we have not a plan of perfect python emulation in the future either.
|
19
|
+
|
20
|
+
I say honestly, but this is a joke gem.
|
21
|
+
EOT
|
22
|
+
gem.homepage = "http://dt.zonu.me/"
|
23
|
+
|
24
|
+
gem.files = `git ls-files`.split($/)
|
25
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
26
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
27
|
+
gem.require_paths = %w(lib)
|
28
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Array do
|
4
|
+
context '' do
|
5
|
+
let(:ary){ [1, 2, 3] }
|
6
|
+
it{ expect( bool(ary) ).to be_true }
|
7
|
+
it{ expect( list(ary) ).to eq ary }
|
8
|
+
end
|
9
|
+
context 'empty' do
|
10
|
+
let(:ary){ [] }
|
11
|
+
it{ expect( bool(ary) ).to be_false }
|
12
|
+
it{ expect( list(ary) ).to eq ary }
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe Fixnum do
|
4
|
+
context 'non-zero number' do
|
5
|
+
let(:num){ 1 }
|
6
|
+
it{ expect( bool(num) ).to be_true }
|
7
|
+
it{ expect( int(num) ).to eq num }
|
8
|
+
end
|
9
|
+
context 'zero' do
|
10
|
+
let(:num){ 0 }
|
11
|
+
it{ expect( bool(num) ).to be_false }
|
12
|
+
it{ expect( int(num) ).to eq num }
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe String do
|
4
|
+
context 'string' do
|
5
|
+
let(:str){ 'str' }
|
6
|
+
it{ expect( bool(str) ).to be_true }
|
7
|
+
it{ expect( list(str) ).to eq str.each_char.to_a }
|
8
|
+
end
|
9
|
+
context 'empty string' do
|
10
|
+
let(:str){ '' }
|
11
|
+
it{ expect( bool(str) ).to be_false }
|
12
|
+
it{ expect( list(str) ).to eq str.each_char.to_a }
|
13
|
+
end
|
14
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pythonism
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- USAMI Kenta
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-14 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: This gem was made for friendship with Pythonistas.
|
15
|
+
email:
|
16
|
+
- tadsan@zonu.me
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/pythonism.rb
|
27
|
+
- lib/pythonism/classes.rb
|
28
|
+
- lib/pythonism/classes/array.rb
|
29
|
+
- lib/pythonism/classes/boolean.rb
|
30
|
+
- lib/pythonism/classes/class.rb
|
31
|
+
- lib/pythonism/classes/false_class.rb
|
32
|
+
- lib/pythonism/classes/fixnum.rb
|
33
|
+
- lib/pythonism/classes/nil_class.rb
|
34
|
+
- lib/pythonism/classes/object.rb
|
35
|
+
- lib/pythonism/classes/string.rb
|
36
|
+
- lib/pythonism/classes/true_class.rb
|
37
|
+
- lib/pythonism/methods.rb
|
38
|
+
- lib/pythonism/methods/kernel.rb
|
39
|
+
- lib/pythonism/pythonize.rb
|
40
|
+
- lib/pythonism/pythonize/basic.rb
|
41
|
+
- lib/pythonism/pythonize/numeric.rb
|
42
|
+
- lib/pythonism/pythonize/statement.rb
|
43
|
+
- lib/pythonism/version.rb
|
44
|
+
- pythonism.gemspec
|
45
|
+
- spec/pythonism/classes/array_spec.rb
|
46
|
+
- spec/pythonism/classes/fixnum_spec.rb
|
47
|
+
- spec/pythonism/classes/string_spec.rb
|
48
|
+
- spec/pythonism_spec.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
homepage: http://dt.zonu.me/
|
51
|
+
licenses: []
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.8.23
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Pythonism gem provides Python-like interface and functions. We are convinced
|
74
|
+
that you should come to like Ruby if you do not like Ruby. We are convinced that
|
75
|
+
you should come to like Python if you do not like Python. But, we have not a plan
|
76
|
+
of perfect python emulation in the future either. I say honestly, but this is a
|
77
|
+
joke gem.
|
78
|
+
test_files:
|
79
|
+
- spec/pythonism/classes/array_spec.rb
|
80
|
+
- spec/pythonism/classes/fixnum_spec.rb
|
81
|
+
- spec/pythonism/classes/string_spec.rb
|
82
|
+
- spec/pythonism_spec.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
has_rdoc:
|