Skip to content

Commit

Permalink
Minor linting.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed Jan 23, 2016
1 parent 6d0553a commit 121b214
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
42 changes: 30 additions & 12 deletions csvkit/convert/fixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,21 @@ def fixed2csv(f, schema, output=None, **kwargs):

class FixedWidthReader(six.Iterator):
"""
Given a fixed-width file and a schema file, produce an analog to a csv reader that yields a row
of strings for each line in the fixed-width file, preceded with a row of headers as provided in the schema. (This might be problematic if fixed-width-files ever have header rows also, but I haven't seen that.)
The schema_file should be in CSV format with a header row which has columns 'column', 'start', and 'length'. (Other columns will be ignored.) Values in the 'start' column are assumed to be "zero-based" unless the first value is "1" in which case all values are assumed to be "one-based."
Given a fixed-width file and a schema file, produce an analog to a csv
reader that yields a row of strings for each line in the fixed-width file,
preceded with a row of headers as provided in the schema. (This might be
problematic if fixed-width-files ever have header rows also, but I haven't
seen that.)
The schema_file should be in CSV format with a header row which has columns
'column', 'start', and 'length'. (Other columns will be ignored.) Values
in the 'start' column are assumed to be "zero-based" unless the first value
is "1" in which case all values are assumed to be "one-based."
"""
def __init__(self, f, schema, encoding=None):
if encoding is not None:
f = iterdecode(f, encoding)

self.file = f
self.parser = FixedWidthRowParser(schema)
self.header = True
Expand All @@ -73,7 +80,10 @@ def __next__(self):

class FixedWidthRowParser(object):
"""
Instantiated with a schema, able to return a sequence of trimmed strings representing fields given a fixed-length line. Flexible about where the columns are, as long as they are headed with the literal names 'column', 'start', and 'length'.
Instantiated with a schema, able to return a sequence of trimmed strings
representing fields given a fixed-length line. Flexible about where the
columns are, as long as they are headed with the literal names 'column',
'start', and 'length'.
"""
def __init__(self, schema):
self.fields = [] # A list of FixedWidthFields
Expand All @@ -97,7 +107,10 @@ def parse(self, line):


def parse_dict(self, line):
"""Convenience method returns a dict. Equivalent to dict(zip(self.headers,self.parse(line)))."""
"""
Convenience method returns a dict. Equivalent to
``dict(zip(self.headers,self.parse(line)))``.
"""
return dict(zip(self.headers,self.parse(line)))

@property
Expand All @@ -106,7 +119,10 @@ def headers(self):

class SchemaDecoder(object):
"""
Extracts column, start, and length columns from schema rows. Once instantiated, each time the instance is called with a row, a (column,start,length) tuple will be returned based on values in that row and the constructor kwargs.
Extracts column, start, and length columns from schema rows. Once
instantiated, each time the instance is called with a row, a
``(column,start,length)`` tuple will be returned based on values in that
row and the constructor kwargs.
"""
REQUIRED_COLUMNS = [('column', None), ('start', int), ('length', int)]

Expand All @@ -115,7 +131,7 @@ class SchemaDecoder(object):
column = None
one_based = None

def __init__(self, header, **kwargs):
def __init__(self, header):
"""
Constructs a schema row decoder.
"""
Expand All @@ -130,10 +146,12 @@ def __init__(self, header, **kwargs):

def __call__(self, row):
"""
Return a tuple (column, start, length) based on this instance's parameters.
If the first time this is called, the row's 'start' value is 1, then all 'start'
values including the first will be one less than in the actual input data, to adjust for
one-based specifications. Values for 'start' and 'length' will be cast to integers.
Return a tuple (column, start, length) based on this instance's
parameters. If the first time this is called, the row's 'start'
value is 1, then all 'start' values including the first will be one
less than in the actual input data, to adjust for one-based
specifications. Values for 'start' and 'length' will be cast to
integers.
"""
if self.one_based is None:
self.one_based = (int(row[self.start]) == 1)
Expand Down
2 changes: 0 additions & 2 deletions csvkit/convert/js.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from ordereddict import OrderedDict
import simplejson as json

import itertools

import agate
import six

Expand Down

0 comments on commit 121b214

Please sign in to comment.