Skip to content

Commit

Permalink
fix default not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
fgmacedo committed Oct 4, 2013
1 parent 41af8df commit 1e312ae
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 59 deletions.
7 changes: 4 additions & 3 deletions raspador/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,13 @@ class DateField(BaseField):
default_format_string = '%d/%m/%Y'
convertion_function = lambda self, date: datetime.date(date)

def __init__(self, search=None, formato=None, **kwargs):
self.formato = formato if formato else self.default_format_string
def __init__(self, search=None, format_string=None, **kwargs):
self.format_string = format_string \
if format_string else self.default_format_string
super(DateField, self).__init__(search=search, **kwargs)

def to_python(self, value):
date_value = datetime.strptime(value, self.formato)
date_value = datetime.strptime(value, self.format_string)
return self.convertion_function(date_value)


Expand Down
2 changes: 1 addition & 1 deletion raspador/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def finalize_item(self):
if hasattr(field, 'finalize') and \
isinstance(field.finalize, collections.Callable):
value = field.finalize()
if value is None:
if value is None and hasattr(field, 'default'):
value = field.default
if value is not None:
self.assign_value_into_item(name, value)
Expand Down
110 changes: 55 additions & 55 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ class TestBaseField(unittest.TestCase):

def test_should_retornar_valor_no_analizar(self):
s = "02/01/2013 10:21:51 COO:022734"
campo = BaseField(r'COO:(\d+)')
valor = campo.parse_block(s)
self.assertEqual(valor, '022734')
field = BaseField(r'COO:(\d+)')
value = field.parse_block(s)
self.assertEqual(value, '022734')

def test_should_retornar_none_sem_search(self):
s = "02/01/2013 10:21:51 COO:022734"
campo = BaseField()
valor = campo.parse_block(s)
self.assertEqual(valor, None)
field = BaseField()
value = field.parse_block(s)
self.assertEqual(value, None)

def test_should_aceitar_callback(self):
s = "02/01/2013 10:21:51 COO:022734"

def dobro(valor):
return int(valor) * 2
def dobro(value):
return int(value) * 2

campo = BaseField(r'COO:(\d+)', input_processor=dobro)
valor = campo.parse_block(s)
self.assertEqual(valor, 45468) # 45468 = 2 x 22734
field = BaseField(r'COO:(\d+)', input_processor=dobro)
value = field.parse_block(s)
self.assertEqual(value, 45468) # 45468 = 2 x 22734

def test_should_recusar_callback_invalido(self):
self.assertRaises(
Expand All @@ -39,103 +39,103 @@ def test_should_recusar_callback_invalido(self):

def test_should_utilizar_grupo_quando_informado(self):
s = "Contador de Reduções Z: 1246"
campo = BaseField(r'Contador de Reduç(ão|ões) Z:\s*(\d+)', groups=1,
field = BaseField(r'Contador de Reduç(ão|ões) Z:\s*(\d+)', groups=1,
input_processor=int)
valor = campo.parse_block(s)
self.assertEqual(valor, 1246)
value = field.parse_block(s)
self.assertEqual(value, 1246)


class TestIntegerField(unittest.TestCase):
def test_should_obter_valor(self):
s = "02/01/2013 10:21:51 COO:022734"
campo = IntegerField(r'COO:(\d+)')
valor = campo.parse_block(s)
self.assertEqual(valor, 22734)
field = IntegerField(r'COO:(\d+)')
value = field.parse_block(s)
self.assertEqual(value, 22734)


class TestFloatField(unittest.TestCase):
def test_should_obter_valor(self):
s = "VENDA BRUTA DIÁRIA: 793.00"
campo = FloatField(r'VENDA BRUTA DIÁRIA:\s+(\d+\.\d+)')
valor = campo.parse_block(s)
self.assertEqual(valor, 793.0)
field = FloatField(r'VENDA BRUTA DIÁRIA:\s+(\d+\.\d+)')
value = field.parse_block(s)
self.assertEqual(value, 793.0)

def test_should_obter_valor_com_separador_de_milhar(self):
s = "VENDA BRUTA DIÁRIA: 10,036.70"
campo = FloatField(r'VENDA BRUTA DIÁRIA:\s+([\d,]+.\d+)')
valor = campo.parse_block(s)
self.assertEqual(valor, 10036.7)
field = FloatField(r'VENDA BRUTA DIÁRIA:\s+([\d,]+.\d+)')
value = field.parse_block(s)
self.assertEqual(value, 10036.7)


class TestBRFloatField(unittest.TestCase):
def test_should_obter_valor(self):
s = "VENDA BRUTA DIÁRIA: 793,00"
campo = BRFloatField(r'VENDA BRUTA DIÁRIA:\s+(\d+,\d+)')
valor = campo.parse_block(s)
self.assertEqual(valor, 793.0)
field = BRFloatField(r'VENDA BRUTA DIÁRIA:\s+(\d+,\d+)')
value = field.parse_block(s)
self.assertEqual(value, 793.0)

def test_should_obter_valor_com_separador_de_milhar(self):
s = "VENDA BRUTA DIÁRIA: 10.036,70"
campo = BRFloatField(r'VENDA BRUTA DIÁRIA:\s+([\d.]+,\d+)')
valor = campo.parse_block(s)
self.assertEqual(valor, 10036.7)
field = BRFloatField(r'VENDA BRUTA DIÁRIA:\s+([\d.]+,\d+)')
value = field.parse_block(s)
self.assertEqual(value, 10036.7)


class TestStringField(unittest.TestCase):
def test_should_obter_valor(self):
s = "1 Dinheiro 0,00"
campo = StringField(r'\d+\s+(\w[^\d]+)')
valor = campo.parse_block(s)
self.assertEqual(valor, 'Dinheiro')
field = StringField(r'\d+\s+(\w[^\d]+)')
value = field.parse_block(s)
self.assertEqual(value, 'Dinheiro')


class TestBooleanField(unittest.TestCase):
s = " CANCELAMENTO "

def test_should_obter_valor_verdadeiro_se_bater_e_capturar(self):
campo = BooleanField(r'^\s+(CANCELAMENTO)\s+$')
valor = campo.parse_block(self.s)
self.assertEqual(valor, True)
field = BooleanField(r'^\s+(CANCELAMENTO)\s+$')
value = field.parse_block(self.s)
self.assertEqual(value, True)

def test_should_retornar_falso_ao_finalizar_quando_regex_nao_bate(self):
campo = BooleanField(r'^\s+HAH\s+$')
valor = campo.parse_block(self.s)
self.assertEqual(valor, None)
valor = campo.default
self.assertEqual(valor, False)
field = BooleanField(r'^\s+HAH\s+$')
value = field.parse_block(self.s)
self.assertEqual(value, None)
value = field.default
self.assertEqual(value, False)


class TestDateField(unittest.TestCase):
def test_should_obter_valor(self):
s = "02/01/2013 10:21:51 COO:022734"
campo = DateField(r'^(\d+/\d+/\d+)')
valor = campo.parse_block(s)
field = DateField(r'^(\d+/\d+/\d+)')
value = field.parse_block(s)
data_esperada = date(2013, 1, 2)
self.assertEqual(valor, data_esperada)
self.assertEqual(value, data_esperada)

def test_should_obter_respeitando_formato(self):
def test_should_obter_respeitando_format_string(self):
s = "2013-01-02T10:21:51 COO:022734"
campo = DateField(r'^(\d+-\d+-\d+)', formato='%Y-%m-%d')
valor = campo.parse_block(s)
field = DateField(r'^(\d+-\d+-\d+)', format_string='%Y-%m-%d')
value = field.parse_block(s)
data_esperada = date(2013, 1, 2)
self.assertEqual(valor, data_esperada)
self.assertEqual(value, data_esperada)


class TestDateTimeField(unittest.TestCase):
def test_should_obter_valor(self):
s = "02/01/2013 10:21:51 COO:022734"
campo = DateTimeField(r'^(\d+/\d+/\d+ \d+:\d+:\d+)')
valor = campo.parse_block(s)
field = DateTimeField(r'^(\d+/\d+/\d+ \d+:\d+:\d+)')
value = field.parse_block(s)
data_esperada = datetime(2013, 1, 2, 10, 21, 51)
self.assertEqual(valor, data_esperada)
self.assertEqual(value, data_esperada)

def test_should_obter_respeitando_formato(self):
def test_should_obter_respeitando_format_string(self):
s = "2013-01-02T10:21:51 COO:022734"
campo = DateTimeField(r'^(\d+-\d+-\d+T\d+:\d+:\d+)',
formato='%Y-%m-%dT%H:%M:%S')
valor = campo.parse_block(s)
field = DateTimeField(r'^(\d+-\d+-\d+T\d+:\d+:\d+)',
format_string='%Y-%m-%dT%H:%M:%S')
value = field.parse_block(s)
data_esperada = datetime(2013, 1, 2, 10, 21, 51)
self.assertEqual(valor, data_esperada)
self.assertEqual(value, data_esperada)

if __name__ == '__main__':
import logging
Expand Down

0 comments on commit 1e312ae

Please sign in to comment.