Please let me know how the code of if described in python is handled.

Asked 2 years ago, Updated 2 years ago, 18 views

I don't understand the meaning of the ninth line code (the third line code in the method _request) of the CC class described in Python.Could you please let me know?

Where applicable:

body=json.dumps(params) if params else'"

What is the decision process for if params else'?

Also, if you want to use this _request method, please let me know how the results will change with the values of the param and method arguments.

code:

class CC(object):
    def_init__(self, access_key, secret_key):
        self.access_key=access_key
        self.secret_key=secret_key
        self.url='https://CC.com'

    def_request(self, endpoint, param=None, method='GET'):
        nonce = str(int(time.time()))
        body=json.dumps(params) if params else'"

        message=nonce+endpoint+body
        signature = hmac.new(self.secret_key.encode(),
                             message.encode(),
                             hashlib.sha256).hexdigest()

        headers = {
            'ACCESS-KEY': self.access_key,
            'ACCESS-NONCE': nonce,
            'ACCESS-SIGNATURE': signature,
            'Content-Type': 'application/json'
        }

        if method=='GET':
            r=requests.get(endpoint,headers=headers,params=params)
        else:
            r=requests.post(endpoint,headers=headers,data=body)
        return r.json()

python

2022-09-30 14:40

1 Answers

How is the if params else' process going?

This article will be helpful.
Explain the Python trinomial operator thoroughly! Describe the if statement (else/elif) in one line!

Determine that params is specified (that is, not None) and not an empty string, where json.dumps(params) is substituted for body and is not specified (that is, None).

When you use this_request method, please tell me how the values of the parameters and method arguments change the results.

The argument params appears to be used to create and specify the signature of the header after processing the first question above.

The argument method specifies the calling method, where GET is called, and POST is called except GET.

If the argument method is GET, the argument param is still specified in params of requests.get().

And if the argument method is not GET, the body is specified in data of requests.post().

"The ""results"" are probably json data that will be notified as a result of the call, but ""how they change"" depends on the content of the site method parameters and the results, so the information in this question will not answer."

NoneDetermination Comments:

The question did not include good or bad descriptions, so I wrote what would work.I haven't actually checked the operation.
However, it is a reference article that @metropolis commented on, and the content of the article deals with money (virtual currency), so I think the author of the article is checking the operation.

However, as the questioner is concerned, it is not a good way to write.
Please refer to articles like this.
To make a None decision in Python and process the variable in if minutes if it is not None

If you apply the above, you should do the following.

body=json.dumps(params) if params is not None else'"

By the way, if the empty string ' is specified instead of None, if params and if params is not None will change their behavior.
If params is an empty string when params is an empty string, the empty string on the else side is substituted, and if params is not None, the empty string is called ' to the parameter.

Maybe that's how I made the reference article.
I have corrected the first answer.

Comment on the method argument:

Is it correct to understand that POST is called if "GET" is passed to the method argument?

Was it a mistake when writing comments?

If "GET" is passed, GET is called.

While "GET" and 'GET' may appear to be different, Python represents the string GET.

Or lowercase "get" is different from GET, so POST is called

Similarly, if you specify a character/string unrelated to the HTTP method as the method argument, POST is called.


2022-09-30 14:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.