Giter Club home page Giter Club logo

quantify's People

Contributors

cblock avatar floppy avatar georgepalmer avatar spatchcock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

quantify's Issues

Investigate making units singletons

Since units have a fixed definition it might be better to have single, singleton instances of each which all quantities can point to, rather than cloning new instances of each every time. Need to investigate this

Add support for i18n

I'd love to have i18n support for unit labels. What about switching the label definitions in the config.rb to symbols that are only looked up if one explicitly calls unit.label?

Tweak performance of quantify lib

Hi Andrew,

currently I'm in the process of performance tweaking quantify_mongoid. That's when I noticed that Quantity#to_si and Unit#for operations are very slow. Tracking it down further (see https://github.com/cblock/quantify/tree/perf/perf) I found out that Dimensions#to_hash is both expensive in terms of cpu cycles and frequently called when invoking #to_si on a quantity.
Same goes for Module#escape_multi_word_units when invoking Unit#for on complex units.

My feeling is that, e.g. #to_hash could be momoized but when I started refactoring I also noticed that you allow reconfiguration of dimensions and units during runtime. This in turn makes it necessary to invalidate memoized values for #to_hash and other operations.

So my question is: Is it really necessary to support reconfiguration during runtime? I found that you use that in quantify_amee to unregister some units. But wouldn't it be better to load everything once during initialization (e.g. into Singletons) and then not change it until app shuts down?

Also: I'm happy to help out refactoring quantify lib into that direction but I'm not too sure where to start. Any suggestions are greatly appreciated.

Consider refactoring Dimensions class

From cblock

"Dimensions.rb mixes the concept of a single dimension with that of a set of dimensions. Breaking this down into a Dimension.rb and a Dimensions.rb would make both comprehension and testing easier for outsiders like me."

Default type of 'ton'

Quantify won't parse '2 tons'. Should it pick a default type of ton to use instead of failing? An option to do this, at least, would be good, to deal with fluffy human input.

Infinite loop when trying to convert N/kg to SI

This should yield a quantity with units m/s^2, or if you consider N to be "proper" SI, it should do nothing. Instead, it seems to throw Quantify into an infinite loop.

Repro:

(1.N/1.kg).to_si

Cancelling the base units also doesn't seem to be able to "separate" the component units of the Newton:

(1.N/1.kg).cancel_base_units! :kg
=> 1 N/kg

Error parsing cubic feet

[1] pry(main)> Quantity.parse('1 cubic foot')
=> [1.0 ]
[2] pry(main)> Quantity.parse('1 cubic foot')
NoMethodError: undefined method label' for nil:NilClass from /Users/james/.rvm/gems/ruby-1.8.7-p352@askamee/gems/activesupport-3.1.1/lib/active_support/whiny_nil.rb:48:inmethod_missing'

Apparently it's the same for metres.

Gem::Exception: ruby_code case not handled: Syck::PrivateType

I get this error when I push to Heroku:

   Gem::Exception: ruby_code case not handled: Syck::PrivateType
   An error occurred while installing quantify (3.2.1), and Bundler cannot
   continue.
   Make sure that `gem install quantify -v '3.2.1'` succeeds before bundling.

   Failed to install gems via Bundler.

   Heroku push rejected, failed to compile Ruby/rails app

The gem works fine locally and I don't have problems with other gems, any ideas why I get this message using quantify?

Prefixes too narrowly constrained

Presently, prefixes are constrained to be applied only to 'valid' units (SI -> SI, Non-SI -> Non-SI). SI prefixes cannot be applied to non-SI units.

This is a problem at least in the case of the Litre. The millilitre and centilitre are commonly used units and therefore need to be represented.

The probable solution to this is probably to relax the constraint on applying prefixes. This does, however throw up the problem of unit symbols being conflated with one another, for example kt = kilo-tonne or carat. Perhaps some form of precendence needs to be set (probably in the symbol regex used in Unit.for) so that these conflicts follow some rule of thumb.

Better handling nil values

Currently nil values for quantities are automatically coerced into a 0.0 Float representation. My feeling is that this is not quite right.

Consider a use case where a user enters quantities via html form and chooses not to assign a quantity. After saving the form the quantity would automagically be converted into a 0.0 value (unless some extra logic in controllers / models fixes that).

I'd like to propose Quantity.new(nil,nil).to_s => "nil 1" i.e. a valid quantity with value nil and unit unity.

...and I'm happy to send in an appropriate pull request if you feel that this is a reasonable change to the behavior of the API.

Cancelling of units after operations

The automatic canceling of units might be an area for improvements.

I consciously did not make all cancelable units cancel automatically as there are cases where we want to preserve the units. For example, and quantity of x kilograms per kilogram of y is technically dimensionless (kg/kg - they cancel) but we might want to retain the context of it being "some mass per mass of something else".

Quantify currently support two systems for handling this:

  1. All units have an #acts_as_equivalent_unit flag. If a compound unit is derived from an operation a check is made to see if the unit is equivalent to another non-compound unit. If so, and if this non-compound unit has the #acts_as_equivalent_unit flag set to true, then the compound unit is replaced by the non-compound unit. The flag is basically say that "this unit should override any compound representations of the same thing". By default, all base units are configured to do this as well as some important units like joule, newton, and watt. So kg * m^2 / s^2 reduces to 'joule' and kg * L / kg reduces to L. This is not quite the same as cancelling, but rather replacing with an equivalent unit. It can give the impression of cancelling in some cases.
  2. The quantity and compound unit classes have methods for explicitly cancelling units. These methods take the units to cancel by as arguments. This represent a manual cancelling of units. I saw this as the best approach - do not cancel by default, but enable cancelling when the user chooses to.

The second approach is useful, but requires an extra method call which seems a bit much at times. Perhaps a global switch for declaring that all operations should cancel units would provide the right compromise between ease and control?

lots of "instance variable @foobar not initialized"

Any suggestions for how to use this?

While it is appearing to work, I'm getting lots of these:

/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:126: warning: instance variable @use_symbol_denominator_syntax not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:126: warning: instance variable @use_symbol_denominator_syntax not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/dimensions.rb:227: warning: instance variable @physical_quantity not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:126: warning: instance variable @use_symbol_denominator_syntax not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:95: warning: instance variable @use_symbol_parentheses not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/unit.rb:131: warning: instance variable @use_superscript_characters not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized
/Users/asnyder/.gem/ruby/1.9.3/gems/quantify-3.2.1/lib/quantify/unit/base_unit.rb:271: warning: instance variable @scaling not initialized

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.