Skip to content

Commit

Permalink
Added codeFieldAdd1
Browse files Browse the repository at this point in the history
  • Loading branch information
nitzanguberman committed Jul 11, 2017
1 parent e36849b commit 97f4d5c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 83 deletions.
2 changes: 1 addition & 1 deletion upload/muni/__init__.py
Expand Up @@ -22,6 +22,7 @@
from schema import SchemaMuni

# Might need a FIXME: if we override one of the keys...
# TODO: Can we lose this and create it dynamically?
munis_loaders = {AshdodMuni.MUNI:AshdodMuni,
BeerMuni.MUNI: BeerMuni,
GushMuni.MUNI: GushMuni,
Expand All @@ -42,5 +43,4 @@
HaifaMuni.MUNI: HaifaMuni,
HolonMuni.MUNI: HolonMuni,
RamatGanMuni.MUNI: RamatGanMuni,
TelAvivMuni.MUNI: TelAvivMuni,
}
104 changes: 55 additions & 49 deletions upload/muni/fields.py
@@ -1,49 +1,55 @@
import re

class AbstractField(object):
def __init__(self, line):
self.value = line
def process(self):
raise NotImplementedError("process is not implemented")

def is_valid(self):
return True

def error(self):
return ""


class AmountField(AbstractField):
name = 'amount'
def __init__(self, amount):
#TODO: this is ugly, will be removed. Dash Yaniv.
self.value = amount
amount = amount.replace(',','')
amount = amount.split('.')[0]
self.amount = amount.strip()

def is_valid(self):
return None != re.match("^[-+]?[0-9]*$", self.amount)

def process(self):
if not self.is_valid():
raise Exception("Amount Field is not valied (%s)" %(self.value, ))
if self.amount == '-' or self.amount == '':
return 0

return abs(int(self.amount))



class CodeField(AbstractField):
name = 'code'
def process(self):
return self.value.replace("-", '').replace(' ','')
def is_valid(self):
return '' != self.process()

class DescriptionField(AbstractField):
name = 'name'
def process(self):
return self.value

import re

class AbstractField(object):
def __init__(self, line):
self.value = line
def process(self):
raise NotImplementedError("process is not implemented")

def is_valid(self):
return True

def error(self):
return ""


class AmountField(AbstractField):
name = 'amount'
def __init__(self, amount):
#TODO: this is ugly, will be removed. Dash Yaniv.
self.value = amount
amount = amount.replace(',','')
amount = amount.split('.')[0]
self.amount = amount.strip()

def is_valid(self):
return None != re.match("^[-+]?[0-9]*$", self.amount)

def process(self):
if not self.is_valid():
raise Exception("Amount Field is not valid (%s)" %(self.value, ))
if self.amount == '-' or self.amount == '':
return 0
return abs(int(self.amount))



class CodeField(AbstractField):
name = 'code'
def process(self):
return self.value.replace("-", '').replace(' ','')
def is_valid(self):
return '' != self.process()

class CodeFieldAdd1(CodeField):
def process(self):
code = super(CodeFieldAdd1, self).process()
if len(code) > 0:
return '1'+code
else: return ''

class DescriptionField(AbstractField):
name = 'name'
def process(self):
return self.value

38 changes: 19 additions & 19 deletions upload/muni/hertzelia.py
@@ -1,19 +1,19 @@
from muni import AbstractMuni
from fields import AmountField, CodeField, DescriptionField

class HertzeliaMuni(AbstractMuni):
MUNI = 'hertzelia'
def __init__(self,**kwargs):
super(HertzeliaMuni, self).__init__(**kwargs)
self.data_fields.add_value({0: CodeField,
5: DescriptionField,
8: AmountField},2014)
self.start_in_row.add_value(4,year=2014)
self.data_fields.add_value({0: CodeField,
5: DescriptionField,
6: AmountField},2015)
self.start_in_row.add_value(4,year=2015)
self.data_fields.add_value({0: CodeField,
5: DescriptionField,
6: AmountField},2016)
self.start_in_row.add_value(2,year=2016)
from muni import AbstractMuni
from fields import AmountField, CodeField, CodeFieldAdd1, DescriptionField

class HertzeliaMuni(AbstractMuni):
MUNI = 'hertzelia'
def __init__(self,**kwargs):
super(HertzeliaMuni, self).__init__(**kwargs)
self.data_fields.add_value({0: CodeFieldAdd1,
5: DescriptionField,
8: AmountField}, 2014)
self.start_in_row.add_value(4, year=2014)
self.data_fields.add_value({0: CodeField,
5: DescriptionField,
6: AmountField}, 2015)
self.start_in_row.add_value(4, year=2015)
self.data_fields.add_value({0: CodeField,
5: DescriptionField,
6: AmountField}, 2016)
self.start_in_row.add_value(2, year=2016)
10 changes: 5 additions & 5 deletions upload/muni/muni.py
Expand Up @@ -18,15 +18,15 @@ class Attr(object):
def __init__(self, default_value=None):
self.default_value = default_value
self.years_value = {}

def add_value(self,value,year=None):
if year is not None:
self.years_value[year]=value
self.years_value[year] = value
else:
self.default_value = value


def __call__(self,year, *args, **kwargs):
if year in self.years_value:
def __call__(self, year = None, *args, **kwargs):
if year is not None and year in self.years_value:
return self.years_value[year]
else:
return self.default_value
Expand All @@ -52,7 +52,7 @@ def __init__(self, print_data=False, clean=False):
self.data_fields.add_value(self.years[year],year)


def handle_sheet(self, year, filename):
def handle_sheet(self, year, filename, add_first_digit = None):
print 'handling file: %s' %(filename,)
dataset = get_raw_budget(self.MUNI, year, clean = self.clean)
if dataset.count()>0 and not self.clean:
Expand Down
1 change: 0 additions & 1 deletion upload/muni/muni_info.py
Expand Up @@ -9,7 +9,6 @@
'kfar_shmaryahu': {'heb_name': u'כפר שמריהו'},
'kfar_saba': {'heb_name': u'כפר סבא'},
'qiryat_bialik': {'heb_name': u'קרית ביאליק'},
'rehovot': {'heb_name': u'רחובות'},
'rishon_letzion': {'heb_name': u'ראשון לציון'},
'tel_aviv': {'heb_name': u'תל אביב'},
'netanya': {'heb_name': u'נתניה'},
Expand Down
16 changes: 8 additions & 8 deletions visualization/main.py
Expand Up @@ -30,8 +30,15 @@ def _parse_csv(self, filename):

def handle(self, *args, **options):
print "bla for the win"
# get the db, exit if exists
dataset = get_scheme()
if dataset.count() > 0:
if options['clean']:
dataset.delete_many({})
else:
print 'Schema is already uploaded. Exiting.'
return
reader = self._parse_csv(SCHEME_FILENAME)

# Create all the nodes in a linear way.
# We Create dictionary in the following format
# { node_code : [node_object, node_parent_code] }
Expand All @@ -58,13 +65,6 @@ def handle(self, *args, **options):
nodes[parent][0].add_child(node)

# Upload the Tree to the DB.
dataset = get_scheme()
if dataset.count() > 0:
if options['clean']:
dataset.delete_many({})
else:
print 'Schema is already uploaded. Exiting.'
return
dataset.insert(root.to_dict())
dataset.close()

Expand Down

0 comments on commit 97f4d5c

Please sign in to comment.