Exemplo n.º 1
0
def get_files(directory):
    for name, filename in lms_files.iteritems():

        if name not in [STREETS, NON_URBAN_INTERSECTION, ACCIDENTS]:
            continue

        files = filter(lambda path: filename.lower() in path.lower(), os.listdir(directory))
        amount = len(files)
        if amount == 0:
            raise ValueError("file not found in directory: " + filename)
        if amount > 1:
            raise ValueError("there are too many matches: " + filename)

        csv = CsvReader(os.path.join(directory, files[0]))

        if name == STREETS:
            streets_map = {}
            for settlement in itertools.groupby(csv, lambda street: street.get(field_names.settlement, "OTHER")):
                key, val = tuple(settlement)

                streets_map[key] = [{field_names.street_sign: x[field_names.street_sign],
                                     field_names.street_name: x[field_names.street_name]} for x in val if
                                    field_names.street_name in x and field_names.street_sign in x]
            csv.close()
            yield name, streets_map
        elif name == NON_URBAN_INTERSECTION:
            roads = {(x[field_names.road1], x[field_names.road2]): x[field_names.junction_name] for x in csv if
                     field_names.road1 in x and field_names.road2 in x}
            csv.close()
            yield ROADS, roads
        elif name == ACCIDENTS:
            yield name, csv
Exemplo n.º 2
0
def get_files(directory):
    for name, filename in lms_files.iteritems():

        if name not in [STREETS, NON_URBAN_INTERSECTION, ACCIDENTS]:
            continue

        files = filter(lambda path: filename.lower() in path.lower(),
                       os.listdir(directory))
        amount = len(files)
        if amount == 0:
            raise ValueError(
                "file doesn't exist directory, cannot parse it; directory: {0};filename: {1}"
                .format(directory, filename))
        if amount > 1:
            raise ValueError(
                "there are too many files in the directory, cannot parse!;directory: {0};filename: {1}"
                .format(directory, filename))

        csv = CsvReader(os.path.join(directory, files[0]))

        if name == STREETS:
            streets_map = {}
            for settlement in itertools.groupby(
                    csv, lambda street: street.get(field_names.settlement,
                                                   "OTHER")):
                key, val = tuple(settlement)

                streets_map[key] = [{
                    field_names.street_sign:
                    x[field_names.street_sign],
                    field_names.street_name:
                    x[field_names.street_name]
                } for x in val if field_names.street_name in x
                                    and field_names.street_sign in x]
            csv.close()
            yield name, streets_map
        elif name == NON_URBAN_INTERSECTION:
            roads = {(x[field_names.road1], x[field_names.road2]):
                     x[field_names.junction_name]
                     for x in csv
                     if field_names.road1 in x and field_names.road2 in x}
            csv.close()
            yield ROADS, roads
        elif name == ACCIDENTS:
            yield name, csv
Exemplo n.º 3
0
def get_files(directory):
    for name, filename in lms_files.iteritems():

        if name not in (STREETS, NON_URBAN_INTERSECTION, ACCIDENTS, INVOLVED,
                        VEHICLES):
            continue

        files = filter(lambda path: filename.lower() in path.lower(),
                       os.listdir(directory))
        amount = len(files)
        if amount == 0:
            raise ValueError("Not found: '%s'" % filename)
        if amount > 1:
            raise ValueError("Ambiguous: '%s'" % filename)

        csv = CsvReader(os.path.join(directory, files[0]))

        if name == STREETS:
            streets_map = {}
            for settlement in itertools.groupby(
                    csv, lambda street: street.get(field_names.settlement,
                                                   "OTHER")):
                key, val = tuple(settlement)

                streets_map[key] = [{
                    field_names.street_sign:
                    x[field_names.street_sign],
                    field_names.street_name:
                    x[field_names.street_name]
                } for x in val if field_names.street_name in x
                                    and field_names.street_sign in x]
            csv.close()
            yield name, streets_map
        elif name == NON_URBAN_INTERSECTION:
            roads = {(x[field_names.road1], x[field_names.road2], x["KM"]):
                     x[field_names.junction_name]
                     for x in csv
                     if field_names.road1 in x and field_names.road2 in x}
            csv.close()
            yield ROADS, roads
        elif name in (ACCIDENTS, INVOLVED, VEHICLES):
            yield name, csv