muflax 0.1.34

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/README ADDED
@@ -0,0 +1,39 @@
1
+ Convenience library that pulls in all standard libraries and hacks I use in quick scripts.
2
+
3
+ [ TODO
4
+
5
+ - shortcut for "@var = var" initializations; I think Rails ships it even?
6
+ - .to_i!, .max!
7
+ - a few more extensions to range_math, notably "pass method call to numbers"
8
+
9
+ ]
10
+ [ Memoization
11
+
12
+ Should work like this:
13
+
14
+ - get file and body of the method to be memoized (via method_source gem)
15
+ - hash that as the method signature
16
+ - take arguments, hash them (via Ruby) (you shouldn't pass fancy arguments, only trivially hashable ones, like strings or symbols, not arrays or some custom object shit)
17
+ - save/retrieve result in/from hash (as simple pickle)
18
+ - initialize hash from database, "checkpoint.cache" and save results
19
+
20
+ - annotate all methods that can be memoized? memoize everything by default? not sure yet, but opt-in is probably the most predictable behavior
21
+
22
+ This enables checkpoint-esque behavior: any method that hasn't changed (same signature) and takes the same arguments just loads the result from a big hash instead of re-generating it every time, but any actual change will be taken into account.
23
+
24
+ The database can simply be regularly cleaned / deleted, or just discard old signatures (when initializing, look up method name -> test signature -> different? discard cache for this method)
25
+
26
+ Like:
27
+
28
+ ~~~
29
+ require "muflax/checkpoint"
30
+
31
+ +Checkpoint
32
+ def method args
33
+ ...
34
+ end
35
+
36
+ method(42)
37
+ ~~~
38
+
39
+ ]
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ desc "open a pry session preloaded with this library"
2
+ task :pry do
3
+ sh "pry -Ilib -rmuflax"
4
+ end
5
+
6
+ desc "build a gem from the gemspec"
7
+ task :build do
8
+ sh "mkdir -p pkg"
9
+ sh "gem build muflax.gemspec"
10
+ sh "mv muflax-*.gem pkg/"
11
+ end
12
+
13
+ desc "clean pkg"
14
+ task :clean do
15
+ sh "rm -f pkg/*"
16
+ end
17
+
18
+ desc "install a gem"
19
+ task :install => [:clean, :build] do
20
+ sh "gem install -l --no-format-executable pkg/muflax-*.gem"
21
+ end
22
+
23
+ task :default => :install
data/lib/muflax.rb ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ # Copyright muflax <mail@muflax.com>, 2013
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ # ruby libs
7
+ require "csv"
8
+ require "date"
9
+ require "fileutils"
10
+ require "set"
11
+ require "yaml"
12
+ require "zlib"
13
+
14
+ # external libs
15
+ require "awesome_print"
16
+ require "highline/import"
17
+ require "range_math"
18
+ require "trollop"
19
+
20
+ # rails
21
+ require "active_support"
22
+ require "active_support/all"
23
+
24
+ # avoid rails deprecation warning
25
+ I18n.enforce_available_locales = false
26
+
27
+ def require_local_libs path, location=__FILE__
28
+ Dir["#{File.join(File.dirname(location), path)}/*.rb"].each do |lib|
29
+ require lib
30
+ end
31
+ end
32
+
33
+ # gem libs
34
+ require_local_libs "muflax"
@@ -0,0 +1,59 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Copyright muflax <mail@muflax.com>, 2013
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ class Array
7
+ def align str=" ", alignment: :left
8
+ lines = []
9
+ columns = 0
10
+
11
+ # split all lines
12
+ self.each do |line|
13
+ line = line.split(str, -1)
14
+ lines << line
15
+ columns = [columns, line.size - 1].max
16
+ end
17
+
18
+ just_function = case alignment
19
+ when :left ; :ljust
20
+ when :right ; :rjust
21
+ when :center ; :center
22
+ else
23
+ raise "invalid alignment: #{alignment}"
24
+ end
25
+
26
+ # justify all columns
27
+ (0..columns).each do |column|
28
+ length = lines.map{|line| line[column]}.length_of_longest
29
+
30
+ lines.each do |line|
31
+ elem = line[column]
32
+ unless elem.nil?
33
+ # how much the element is internally longer than it appears
34
+ elem_diff = elem.to_s.length - elem.str_length
35
+
36
+ line[column] = elem.send(just_function, length + elem_diff)
37
+ end
38
+ end
39
+ end
40
+
41
+ # join lines back together
42
+ lines.map{|line| line.join(str)}
43
+ end
44
+
45
+ def align! str=" ", alignment: :left
46
+ self.replace(self.align(str, alignment: alignment))
47
+ end
48
+
49
+ def triangle
50
+ return to_enum(:triangle) unless block_given?
51
+
52
+ self.each.with_index do |a, ai|
53
+ self.each.with_index do |b, bi|
54
+ next if bi < ai
55
+ yield [a, b]
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Copyright muflax <mail@muflax.com>, 2013
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ # This is very stupid and shouldn't be used in real code, which is why I will use it in real code.
7
+
8
+ def bp
9
+ require "debug_inspector"
10
+ require "pry"
11
+ RubyVM::DebugInspector.open do |inspector|
12
+ eval("binding.pry", inspector.frame_binding(2))
13
+ end
14
+ end
15
+
16
+ def bpe
17
+ require "debug_inspector"
18
+ require "pry"
19
+ RubyVM::DebugInspector.open do |inspector|
20
+ eval("binding.pry; exit", inspector.frame_binding(2))
21
+ end
22
+ end
23
+
24
+ def v *variables
25
+ require "debug_inspector"
26
+ RubyVM::DebugInspector.open do |inspector|
27
+ variables.each do |variable|
28
+ eval("print '#{variable}: '; ap #{variable}", inspector.frame_binding(2))
29
+ end
30
+ end
31
+ end
data/lib/muflax/dir.rb ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Copyright muflax <mail@muflax.com>, 2014
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ class Dir
7
+ class << self
8
+ alias_method :"_[]", :"[]"
9
+
10
+ # make glob sort by default
11
+ def [] *args
12
+
13
+ self.send(:"_[]", *(args.map{|a| File.expand_path(a)})).sort
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Copyright muflax <mail@muflax.com>, 2013
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ module Enumerable
7
+ # find shortest element
8
+ def shortest
9
+ self.min_by{|s| s.str_length}
10
+ end
11
+
12
+ # find longest element
13
+ def longest
14
+ self.max_by{|s| s.str_length}
15
+ end
16
+
17
+ def length_of_longest
18
+ self.longest.str_length
19
+ end
20
+
21
+ def length_of_shortest
22
+ self.shortest.str_length
23
+ end
24
+
25
+ def average
26
+ self.sum.to_f / self.size.to_f
27
+ end
28
+ alias :mean :average
29
+ alias :avg :average
30
+
31
+ def geometric_mean
32
+ self.reduce(:*).to_f ** (1.0 / self.size.to_f)
33
+ end
34
+
35
+ def median
36
+ self.percentile 0.5
37
+ end
38
+
39
+ def median_by &block
40
+ self.percentile_by 0.5, &block
41
+ end
42
+
43
+ def percentile percent
44
+ self.sort[(self.size * percent).round]
45
+ end
46
+
47
+ def percentile_by percent, &block
48
+ self.sort_by(&block)[(self.size * percent).round]
49
+ end
50
+
51
+ def triangle
52
+ self.to_a.triangle
53
+ end
54
+
55
+ def histogram &block
56
+ histo = vivaHash 0
57
+ self.each do |el|
58
+ histo[block.call(el)] += 1
59
+ end
60
+
61
+ histo
62
+ end
63
+
64
+ def count_by &block
65
+ self.histogram(&block).sort_by(&:second).to_h
66
+ end
67
+ end
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Copyright muflax <mail@muflax.com>, 2013
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ class File
7
+ def self.save name, as: :text, &blk
8
+ name = File.expand_path(name)
9
+
10
+ # make sure directory exists
11
+ dir = File.dirname(name)
12
+ FileUtils.mkdir_p dir if not Dir.exists? dir
13
+
14
+ # now open it
15
+ case as
16
+ when :anki
17
+ f = CSV.open(name, "w", :col_sep => "\t", &blk)
18
+ when :text
19
+ f = File.open(name, "w", &blk)
20
+ else
21
+ raise "unsupported file format: #{as}"
22
+ end
23
+
24
+ f
25
+ end
26
+
27
+ def self.load name
28
+ name = File.expand_path(name)
29
+
30
+ file = case name
31
+ when /\.gz$/
32
+ Zlib::GzipReader.open(name)
33
+ else
34
+ File.open(name)
35
+ end
36
+
37
+ file
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Copyright muflax <mail@muflax.com>, 2013
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ # simple auto-vivifying hash
7
+ # TODO support nested hashes
8
+ def vivaHash default=[]
9
+ hash = Hash.new do |h, k|
10
+ h[k] = default.duplicable? ? default.dup : default
11
+ end
12
+
13
+ hash
14
+ end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Copyright muflax <mail@muflax.com>, 2014
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ module Kernel
7
+ def hr
8
+ width, height = HighLine::SystemExtensions.terminal_size
9
+ linewidth = [(width || 0) - 1, 1].max
10
+
11
+ puts "-" * (linewidth)
12
+ end
13
+
14
+ def zsh command
15
+ system "zsh", "-l", "-c", command
16
+ end
17
+
18
+ def fish command
19
+ system "fish", "-l", "-c", command
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Copyright muflax <mail@muflax.com>, 2013
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ def require_local lib_name, location=__FILE__
7
+ file = File.symlink?(location) ? File.readlink(location) : location
8
+ lib = File.join File.dirname(file), "/../lib/#{lib_name}.rb"
9
+
10
+ if File.exists? lib
11
+ # using local version
12
+ require lib
13
+ else
14
+ require lib_name
15
+ end
16
+ end
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Copyright muflax <mail@muflax.com>, 2013
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ # make it possible to track data in attributes
7
+ class Class
8
+ alias_method :attr_reader_without_tracking, :attr_reader
9
+ def attr_reader(*names)
10
+ attr_readers.concat(names)
11
+ attr_reader_without_tracking(*names)
12
+ end
13
+
14
+ def attr_readers
15
+ @attr_readers ||= [ ]
16
+ end
17
+
18
+ alias_method :attr_writer_without_tracking, :attr_writer
19
+ def attr_writer(*names)
20
+ attr_writers.concat(names)
21
+ attr_writer_without_tracking(*names)
22
+ end
23
+
24
+ def attr_writers
25
+ @attr_writers ||= [ ]
26
+ end
27
+
28
+ alias_method :attr_accessor_without_tracking, :attr_accessor
29
+ def attr_accessor(*names)
30
+ attr_readers.concat(names)
31
+ attr_writers.concat(names)
32
+ attr_accessor_without_tracking(*names)
33
+ end
34
+ end
35
+
36
+ class Object
37
+ def differences_with(other)
38
+ # get list of all attributes
39
+ attrs = (self.class.attr_readers + self.class.attr_writers).uniq.sort
40
+
41
+ # pick all attributes that they disagree about
42
+ attrs.reject do |attr|
43
+ self.respond_to? attr and other.respond_to? attr and self.send(attr) == other.send(attr)
44
+ end
45
+ end
46
+
47
+ # consistent string length
48
+ def str_length
49
+ HighLine.uncolor(self.to_s).length
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- encoding: utf-8 -*-
3
+ # Copyright muflax <mail@muflax.com>, 2013
4
+ # License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>
5
+
6
+ class String
7
+ def mgsub replacements={}
8
+ str = self.dup
9
+
10
+ str.mgsub! replacements
11
+
12
+ str
13
+ end
14
+ alias :gsub_all :mgsub
15
+
16
+ def mgsub! replacements={}
17
+ replacements.each do |a, b|
18
+ self.gsub! a, b
19
+ end
20
+
21
+ self
22
+ end
23
+ alias :gsub_all! :mgsub!
24
+ end
25
+