ensembl-hive-python3  2.7.0
AddTogether.py
Go to the documentation of this file.
1 
2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 import eHive
18 
19 import time
20 
22  """Runnable that adds up all the partial-multiplications from PartMultiply"""
23 
24  def param_defaults(self):
25  return {
26  'take_time' : 0,
27  'partial_product' : {}
28  }
29 
30 
31  def fetch_input(self):
32  a_multiplier = self.param_required('a_multiplier')
33  partial_product = self.param('partial_product')
34  print(partial_product)
35 
36  partial_product['1'] = str(a_multiplier)
37  partial_product['0'] = '0'
38 
39 
40  def run(self):
41  b_multiplier = self.param_required('b_multiplier')
42  partial_product = self.param('partial_product')
43  self.param('result', add_together(b_multiplier, partial_product))
44  time.sleep( self.param('take_time') )
45 
46  def write_output(self):
47  self.dataflow( { 'result': self.param('result') }, 1)
48 
49 
50 
51 def add_together(b_multiplier, partial_product):
52 
53  b_multiplier = str(b_multiplier)
54  accu = [0] * (1 + len(b_multiplier) + len(partial_product['1']))
55 
56  for (i,b_digit) in enumerate(reversed(b_multiplier)):
57  product = str(partial_product[b_digit])
58  for (j,p_digit) in enumerate(reversed(product)):
59  accu[i+j] += int(p_digit)
60 
61  carry = 0
62  for i in range(len(accu)):
63  val = carry + accu[i]
64  accu[i] = val % 10
65  carry = val // 10
66 
67  return ''.join(str(_) for _ in reversed(accu)).lstrip('0')
68 
69 1;
eHive.process.BaseRunnable
This is the counterpart of GuestProcess.
Definition: process.py:60
eHive.process.BaseRunnable.dataflow
def dataflow(self, output_ids, branch_name_or_code=1)
Dataflows the output_id(s) on a given branch (default 1).
Definition: process.py:229
eHive.examples.LongMult.AddTogether.AddTogether
Runnable that adds up all the partial-multiplications from PartMultiply.
Definition: AddTogether.py:22
eHive.process.BaseRunnable.param_required
def param_required(self, param_name)
Returns the value of the parameter "param_name" or raises an exception if anything wrong happens or t...
Definition: process.py:253
eHive.examples.LongMult.AddTogether.AddTogether.param_defaults
def param_defaults(self)
Returns the defaults parameters for this runnable.
Definition: AddTogether.py:24
eHive.examples.LongMult.AddTogether.AddTogether.run
def run(self)
Definition: AddTogether.py:40
eHive.examples.LongMult.AddTogether.AddTogether.fetch_input
def fetch_input(self)
Definition: AddTogether.py:31
eHive.examples.LongMult.AddTogether.add_together
def add_together(b_multiplier, partial_product)
Definition: AddTogether.py:51
eHive.examples.LongMult.AddTogether.AddTogether.write_output
def write_output(self)
Definition: AddTogether.py:46
eHive.process.BaseRunnable.param
def param(self, param_name, *args)
When called as a setter: sets the value of the parameter "param_name".
Definition: process.py:266