2

I'm porting a piece of Ruby to Python. I've ported the majority of the code except the following. I can't really figure out what it does.

the variable rest would be a string representing a phone number. Its only really the

country_handler = splitter_mapping[i][presumed_cc]

Which I find confusing. I realise I could probably use kwargs in python but I'm not really sure.

Any ideas?

module Phony

  class CountryCodes

    attr_reader :splitter_mapping

    def split_cc rest
      presumed_cc = ''
      1.upto(3) do |i|
        presumed_cc << rest.slice!(0..0)
        country_handler = splitter_mapping[i][presumed_cc]
        return [country_handler, presumed_cc, rest] if country_handler
      end
      # This line is never reached as CCs are in prefix code.
    end
2
  • 1
    It looks like you start with a string and it's trying to find a prefix of that string (of length 1 to 3) that existings in splitter mapping (which appears to be an array of hashes of country codes to some sort of values, arranged by the length of the country code). No idea why though Commented Nov 21, 2012 at 13:15
  • Sorry missed the splitter_mapping definition the source file can be found here - github.com/floere/phony/blob/master/lib/phony/country_codes.rb Commented Nov 21, 2012 at 13:26

1 Answer 1

1

Well, from the code and Frederick Cheung's comment, I think a translation would go something like this:

def slit_cc(rest):
    for i in range(1, 4): # country_code size
        presumed_cc = rest[0:i]
        country_handler = splitter_mapping[i].get(presumed_cc):
        if country_handler:
            return [country_handler, presumed_cc, rest]

    return None # shouldn't happen

splitter_mapping appears to be a dictionary of dictionaries. The first level keys are country_code sizes (string length), and the second level keys are country codes. The values seem to be a country's telephone dialing prefix.

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

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.