Description of dictionary return values

Asked 2 years ago, Updated 2 years ago, 31 views

I don't understand the meaning of the return value statement below.

return {k:v for k, vin balance.items()
        if isinstance(v,str) and float(v)}

Based on the list description, I thought it was the same expression as the following description. Is it correct?

return{}
fork, vin balance.item():
    if(isinstance(v,str) and float(v)):
       .setdefault(k,v) 

Here's a big picture of the code:

def balance(self):
    endpoint=self.url+'/api/accounts/balance'
    return self._request(endpoint=endpoint)

@property
    def position (self):
    balance=self.balance()
    return {k:v fork, vin balance.items()
            if isinstance(v,str) and float(v)}

*The above code is #12 Python×Bitcoin Automatic Trading|Create a class to make the code easier to read! introduced the code.

python

2022-09-30 20:23

2 Answers

Based on the list description, I thought it was the same expression as the following description. Is it correct?

As you can see, .setdefault(k,v) is a syntax error. Even if you correct the syntax error, it is set to return{} first, so the Coincheck.position property always returns empty dictionary.

Below is the stub code for operation check.

 if__name__=='__main__':
  import json
  from print import print

  cc = Coincheck ('a', 'b')

  # Exchange API Documentation | Coincheck for Virtual Currency Exchange
  # # https://coincheck.com/ja/documents/exchange/api#account-balance
  cc.balance=lambda: json.loads(' ''
    {
      "success": true,
      "jpy": "0.8401",
      "btc": "7.75052654",
      "jpy_reserved": "3000.0",
      "btc_reserved": "3.5002",
      "jpy_lend_in_use": "0",
      "btc_lend_in_use": "0.3",
      "jpy_lent": "0",
      "btc_lent": "1.2",
      "jpy_debt": "0",
      "btc_debt": "0"
    }''')

  print('Original:')  
  print(cc.position)

  # replace Coincheck.position property
  def my position (self):
    balance=self.balance()
    dic = {}
    fork, vin balance.items():
      if isinstance(v,str):
        try:
          if float(v):dic[k]=v
        exceptionValueError:
          continue

    returndic
  
  Coincheck.position=property(myposition)

  print('Replaced:')  
  print(cc.position)

# execution result
Original:
{'btc': '7.75052654',
 'btc_lend_in_use': '0.3',
 'btc_lent': '1.2',
 'btc_reserved': '3.5002',
 'jpy': '0.8401',
 'jpy_reserved': '3000.0'}

Replaced:
{'btc': '7.75052654',
 'btc_lend_in_use': '0.3',
 'btc_lent': '1.2',
 'btc_reserved': '3.5002',
 'jpy': '0.8401',
 'jpy_reserved': '3000.0'}

The exception handling is because float() can cause ValueError.For example,

cc.balance=lambda:{
    'a': '1.0', 'b': '2.0', 'c': '3.0c', 'd': 4.0
  }

When executing the original code as the , the error occurs as follows:

Traceback (most recent call last):
  File "coin_check.py", line 74, in<module>
    print(f'Original: {cc.position}')
  File "coin_check.py", line61, in position
    return {k:v fork, vin balance.items()
  File "coin_check.py", line 62, in <dictcomp>
    if isinstance(v,str) and float(v)}
ValueError: could not convert string to float: '3.0c'

AP As far as the API document is concerned, ValueError does not appear, so the above exception handling may not be necessary.


2022-09-30 20:23

The expression after return is not a list-enclosed expression, but a "dictionary expression" as described in 5.5. Dictionary.

In addition, dictionary expressions can be used to create dictionaries from any key-value pair:

>>{x:x**2 for x in(2,4,6)}
{2: 4, 4: 16, 6: 36}

There is also something called "collective inclusion notation."These are like relatives in list inclusion, and instead of lists, you will create a dictionary (dict) or set (set).

If you did not use the inclusions and you also deployed if outside of for:

data={}
fork, vin balance.items():
    if isinstance(v, str) and float(v):
        data[k] = v
return data


2022-09-30 20:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.