Note that there are some explanatory texts on larger screens.

plurals
  1. POcreate new ctype python
    text
    copied!<p>below classes are in pure python, but the are a little slow on heavily dense calculation and lead to about 1 minute to do about 5 million action.</p> <p><strong>the question</strong>:</p> <ol> <li>Is there any way to write them in C and import them to python for more speed?</li> <li>how?</li> </ol> <p>the usage is something like this:</p> <pre class="lang-py prettyprint-override"><code>_cm=Unit(...) # Unit _m =Unit(...) # Unit a= 1*_cm # UnitValue b= 2*_m # UnitValue _m2 = _m**2 # Unit b*a == 0.02 * _m2 # UnitValue a*b == 200 * _cm2 # UnitValue # _cm2 is auto created unit based on Unit.__new__ b-a = 0.99*_m # UnitValue a-b = -99*_cm # UnitValue # and so on... </code></pre> <p><strong>NOTE:</strong> the classes are bigger that what included here...</p> <pre class="lang-py prettyprint-override"><code>class Unit(object): def __new__(self, *args, **kwargs): # check if this unit not created before and if ues return before created-one. # useful for cmp between units. return created_or_new_unit_instance def __div__(self, other): return self * (other ** -1) # self / other def __rdiv__(self, other): return other * (self ** -1) # other / self def __rmul__(self, other): return self * other # other * self def __mul__(self, other): # self * other if isinstance(other, SUPPORTED_NUMBERS): return UnitValue(other, self) elif isinstance(other, UnitValue): # FIXME DOUBLICATED! return (other.value * (other.unit * self)) elif isinstance(other, Unit): # multipling two units self * other # calculate the other exchange factor againest self in `v` if v == 1.0: # the bases may differ from self.bases or other.bases # so just create a new Unit and let __new__ handle doublication. return Unit(bases) else: return v * Unit(bases) return NotImplemented def __pow__(self, other, modulo=None): # @UnusedVariable # create new powered unit. return new Unit </code></pre> <p>and the other Class:</p> <pre class="lang-py prettyprint-override"><code>class UnitValue(object): def __init__(self, value, unit): self.value = value self.unit = unit def __add__(self, other): # self + other if isinstance(other, UnitValue): o = self.unit.get_unitvalue_in_this_unit(other) # other is UnitValue v = self.value + o.value return UnitValue(v, self.unit) if other == 0: return UnitValue(self.value, self.unit) return NotImplemented def __mul__(self, other): # self * other if isinstance(other, SUPPORTED_NUMBERS): return UnitValue(other * self.value, self.unit) elif isinstance(other, UnitValue): return (self.value * other.value) * (self.unit * other.unit) return NotImplemented def __pow__(self, other, modulo=None): v = self.value ** other u = self.unit ** other if modulo: v = v % modulo return UnitValue(v, u) def __cmp__(self, other): if isinstance(other, UnitValue): vo = self.unit.get_unitvalue_in_this_unit(other).value else: vo = other if vo is None: vo = 0 diff = self.value - vo epsi = 1e-10 if abs(diff) &lt; epsi: return 0 elif diff &gt; 0: return 1 elif diff &lt; 0: return -1 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload