3

My company is very strict on adhering to pydocstyle. I'm running up against the line limit trying to modify these values nested in a json file.

    Rem_Conf['RemediationConfigurations'][0]['Parameters']['AutomationAssumeRole']['StaticValue']['Values'][0] = rolearn
    Rem_Conf['RemediationConfigurations'][0]['Parameters']['GranteeId']['StaticValue']['Values'][0] = canid
    Rem_Conf['RemediationConfigurations'][0]['Parameters']['TargetBucket']['StaticValue']['Values'][0] = targetbucket

How I can compress this to meet the 79 character pydocstyle limit?

4 Answers 4

3

One way is to replace the inline keys with short named variables

cfg='RemediationConfigurations'
params='Parameters'
role='AutomationAssumeRole'
sval='StaticValue'
val='Values'
guid='GranteeId'
tbuk='TargetBucket'
Rem_Conf[cfg][0][params][role][sval][val][0] = rolearn
Rem_Conf[cfg][0][params][guid][sval][val][0] = canid
Rem_Conf[cfg][0][params][tbuk][sval][val][0] = targetbucket

Here the longest line is 60 chars and it makes the pattern of values very clear to a reader

Sign up to request clarification or add additional context in comments.

Comments

2

You can wrap the lines in parentheses, like below. Of course, feel free to play around with the formatting. This is what the black formatter outputs.

(
    Rem_Conf["RemediationConfigurations"][0]["Parameters"]["AutomationAssumeRole"][
        "StaticValue"
    ]["Values"][0]
) = rolearn
(
    Rem_Conf["RemediationConfigurations"][0]["Parameters"]["GranteeId"]["StaticValue"][
        "Values"
    ][0]
) = canid
(
    Rem_Conf["RemediationConfigurations"][0]["Parameters"]["TargetBucket"][
        "StaticValue"
    ]["Values"][0]
) = targetbucket

IMHO, this would be a readable format:

(Rem_Conf
    ["RemediationConfigurations"]
    [0]
    ["Parameters"]
    ["AutomationAssumeRole"]
    ["StaticValue"]
    ["Values"]
    [0]
) = rolearn

This is what the black python formatter outputs on your original code:

Rem_Conf["RemediationConfigurations"][0]["Parameters"]["AutomationAssumeRole"][
    "StaticValue"
]["Values"][0] = rolearn
Rem_Conf["RemediationConfigurations"][0]["Parameters"]["GranteeId"]["StaticValue"][
    "Values"
][0] = canid
Rem_Conf["RemediationConfigurations"][0]["Parameters"]["TargetBucket"]["StaticValue"][
    "Values"
][0] = targetbucket

Comments

2

Other answers are even better, just wanted to add that at the very least you could use backslashes like that:

Rem_Conf['RemediationConfigurations'][0]['Parameters'] \
            ['AutomationAssumeRole']['StaticValue']['Values'][0] = rolearn
Rem_Conf['RemediationConfigurations'][0]['Parameters'] \
            ['GranteeId']['StaticValue']['Values'][0] = canid
Rem_Conf['RemediationConfigurations'][0]['Parameters'] \
            ['TargetBucket']['StaticValue']['Values'][0] = targetbucket

Python documentation states the following:

2.1.5. Explicit line joining

Two or more physical lines may be joined into logical lines using backslash characters (\), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character.

Comments

1

I would simply use an intermediate value:

Parameters = Rem_Conf['RemediationConfigurations'][0]['Parameters']
Parameters['AutomationAssumeRole']['StaticValue']['Values'][0] = rolearn
Parameters['GranteeId']['StaticValue']['Values'][0] = canid
Parameters['TargetBucket']['StaticValue']['Values'][0] = targetbucket

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.