惯用python中的Ruby代码

时间:2015-11-16 05:31:08

标签: python ruby

我正在练习python,我无法将我的Ruby代码转换为惯用的python。

我可以查询一张卡片,但我不明白如何在一行(或尽可能简洁),查询,过滤并从查询中返回一张卡片。我的代码变成了一大堆try / except语句,所以我猜我错过了什么。

RUBY VERSION

  #fetch the customer 
  customer = Stripe::Customer.retrieve(self.stripe_id)
  #Retrieve the card fingerprint using the stripe_card_token  
  card_fingerprint = Stripe::Token.retrieve(token_id).try(:card).try(:fingerprint) 
  # check whether a card with that fingerprint already exists
  default_card = customer.sources.all(:object => "card").select{|card| card.fingerprint ==  card_fingerprint}.last if card_fingerprint 
  #create new card if do not already exists
  default_card = customer.sources.create(:source => token_id) unless default_card 
  #set the default card of the customer to be this card, as this is the last card provided by User and probably he want this card to be used for further transactions
  customer.default_card = default_card.id 
  # save the customer
  customer.save

PYTHON VERSION

    # fetch the customer
    customer = stripe.Customer.retrieve(self.stripe_id)
    # Blank default card, which will equal either an existing card or a new card
    default_card
    # Retrieve the card fingerprint using the stripe_card_token
    card_fingerprint = stripe.Token.retrieve(token_id)
    if card_fingerprint:
        # Return all customer cards
        default_cards = customer.sources.all(object='card')
        for card in default_cards
            if card.fingerprint == card_fingerprint:
                # Set default card to the matching card
                default_card = card
    # If not card found, create a new one
    if !default_card:
        customer.sources.create(source=token_id)
    # Set this card as default, regardless if it was new or exising
    customer.default_source = default_card.id

https://stripe.com/docs/api/python#retrieve_card

1 个答案:

答案 0 :(得分:0)

这是我提出的最好的!我避免使用我在ruby中使用的单行if语句,这是由Python样式指南提供的,但是对于我来自Ruby的看起来很奇怪

PYTHON VERSION

    # Fetch the customer
    customer = stripe.Customer.retrieve(self.stripe_id)
    # Retrieve the card fingerprint using the stripe_card_token
    card_fingerprint = stripe.Token.retrieve(token_id)
    if card_fingerprint:
        cards = [x for x in customer.sources.all(object='card') if x.fingerprint == card_fingerprint]
    # If a card or multiple cards return with said fingerprint, return the last one and assign default_card to this card
    if cards:
        default_card = cards.pop()
    # If no cards matched, create a new default card
    else:
        default_card = customer.sources.create(source=token_id)
    # Set this card as default, regardless if it was new or already existed
    customer.default_source = default_card.id
    customer.save()
    return customer.sources.all(object='card')

我也可以用try替换if else,除掉局部变量cards但不确定这是否是惯用的

        try:
            default_card = [x for x in customer.sources.all(object='card') if x.fingerprint == card_fingerprint].pop()
        except IndexError e:
            default_card = customer.sources.create(source=token_id)