Kwarg argument initialized as "NoneType"

Question:

I have an object class that takes sidewalk data and parses it for a transportation model.

At one point in the model, it uses a function of the sidewalk width and the pavement conditions to generate a score. This is relevant because the width and pavement condition score are handled exactly the same way throughout the process, but sidewalk_width initializes as an int, and sidewalk_score initializes as a NoneType.

Since it’s a NoneType, nothing that’s assigned to it seems to stick, so it keeps returning as None, even when I see a value should have been assigned.

The Sidewalk() class object is generated with the code below:

class Sidewalk(object):
    def __init__(self, **kwargs):
        self.sidewalk_width = kwargs.get("sidewalk_width")
        self.sidewalk_score = kwargs.get("sidewalk_condition_score")

The specific records are assigned with the block below:

def get_plts(path):
    with fiona.open(path, 'r', layer='plts') as src:
        driver = src.driver
        crs = src.crs
        schema = src.schema
        for f in src:
            data = f['properties']
            sidewalks = [Sidewalk(
                    sidewalk_width=data['sidewalk_width'],
                    sidewalk_score=data['sidewalk_condition_score'])]

And they ultimately get run through the function below:

    def _calculate_condition_score(self, sidewalk):
        width = sidewalk.sidewalk_width
        cond = sidewalk.sidewalk_score

Before they’re initialized, the script can read the numbers from the geopackage without any problems. Both the width and the condition score are both assigned as integers in the geopackage. If a street has no sidewalk, Width will report as "None" for that record only, then continue operating correctly with an actual integer value, where the Condition score is always "None" regardless.

Why is width coming across intact from start to finish, where the condition seems to be locked in as NoneType?

I’ve plugged print statements throughout the model and found that the problem is happening at or around the time of initialization. By the time the model is actually working with the data, the type issue has already taken effect and the numbers for the condition score will only report as "Null".

Asked By: armchairavenger

||

Answers:

In the data dictionary, the score is stored in the 'sidewalk_condition_score' key, but since you passed the kwarg as sidewalk_score=data['sidewalk_condition_score'], the score becomes stored in the "sidewalk_score" key. To access it, you want to write kwargs.get("sidewalk_score")

Answered By: SimonUnderwood
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.