omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Adding up measurements

    Editorial
    3
    8
    47062
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Jozh
      Jozh last edited by

      I often make a list of measurements in feet and inch. Then have to pull my calculator to add them all up. Is there a way to build the workflow to do this for me?

      Input:
      5' 6"
      3' 8"

      Output:
      9' 2"

      Discussion started with RV: Forums New Discussion workflow

      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by

        Here's a fairly simple regex-based Python script/workflow:

        → Add up Measurements

        For simplicity's sake, it only supports integers, but extending it to handle floating-point values wouldn't be very difficult (wasn't sure if you'd actually need it).

        1 Reply Last reply Reply Quote 0
        • Jozh
          Jozh last edited by

          Thank you that's almost exactly what I was looking for.

          1 Reply Last reply Reply Quote 0
          • Jozh
            Jozh last edited by

            What would it take to add fractions to the script and what would it have to be 8.5" or 8 1/2".
            Thanks

            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by

              feet_and_inches() can be expressed in just a few lines of code for floating point values. I will have to think a bit more to deal with fractions.

              # coding: utf-8
              
              def feet_and_inches(s='''5.5' 6.5" 3.6' 8.6"'''):
                  inches = sum(float(x.replace("'", '')) * 12 if "'" in x
                          else float(x.replace('"', '')) for x in s.split())
                  return '{:g}\' {:g}"'.format(inches // 12, inches % 12)
                      
              if __name__ == '__main__':
                  test_data = '''5' 6"
                                 5' 6" 3' 8"
                                 5' 6.5" 3' 8.5"
                                 5.5' 6" 3.5' 8"
                                 5.5' 6.5" 3.5' 8.5"
                                 5.5' 6.5" 3.5' 8.6"
                                 5.5' 6.5" 3.6' 8.6"'''.splitlines()
                  print('=' * 20)
                  for line in test_data:
                      print('{:>19} ==> {}'.format(line.strip(), feet_and_inches(line)))
              
              1 Reply Last reply Reply Quote 0
              • ccc
                ccc last edited by

                I believe that this version deals with all the cases but I would not be shocked if there were problems...

                
                #!/usr/bin/env python
                # coding: utf-8
                
                from __future__ import (absolute_import, division, print_function, unicode_literals)
                
                def fractions_to_floats(s):
                    frac_part = ''
                    output = []
                    for x in reversed(s.split()):  # processing terms from right to left
                        a, _, b = x.partition('/')
                        if b:
                            frac_part = '{}{}'.format(int(a) / int(b.strip('\'"')), b[-1])
                        else:
                            if x[-1] in '\'"':
                                output += [x, frac_part]
                            else:
                                output.append(x + frac_part.lstrip('0'))
                            frac_part = ''
                    output.append(frac_part)
                    return ' '.join(output)
                
                def feet_and_inches(s='''5.5' 6.5" 3.6' 8.6"'''):
                    if '/' in s:
                        s = fractions_to_floats(s)
                    assert '/' not in s, 'fractions_to_floats() failed: ' + s
                    inches = sum(float(x.replace("'", '')) * 12 if "'" in x
                            else float(x.replace('"', '')) for x in s.split())
                    return '{:g}\' {:g}"'.format(inches // 12, inches % 12)
                
                if __name__ == '__main__':
                    test_data = '''
                        0
                        1
                        6"
                        13
                        14"
                        5'
                        4' 1' "4
                        4' 5" 1'
                        5' 6"
                        5' 6" 1"
                        5' 6" 3' 8"
                        5' 6.5" 3' 8.5"
                        5.5' 6" 3.5' 8"
                        5.5' 6.5" 3.5' 8.5"
                        5.5' 6.5" 3.5' 8.6"
                        5.5' 6.5" 3.6' 8.6"
                        15 1/64"
                        15 1/32"
                        15 1/16"
                        15 1/8"
                        15 1/4"
                        15 1/2"
                        0' 1" 2' 3" 4' 5" 6 7/8"
                        15 1/16'
                        15 1/8'
                        15 1/4'
                        15 1/2'
                        16' 2 1/16"
                        16' 2 1/8"
                        16' 2 1/4"
                        16' 2 1/2"
                        16 1/2' 2 1/16"
                        16 1/2' 2 1/8"
                        16 1/2' 2 1/4"
                        16 1/2' 2 1/2"
                        16' 1' 2 1/16"
                        16' 1' 2 1/8"
                        16' 1' 2 1/4"
                        16' 1' 2 1/2"
                        16.5' 1' 2 1/16"
                        16.5' 1' 2 1/8"
                        16.5' 1' 2 1/4"
                        16.5' 1' 2 1/2"'''.splitlines()
                    print('=' * 20)
                    for line in test_data:
                        print('{:>24} ==> {}'.format(line.strip(), feet_and_inches(line)))
                
                1 Reply Last reply Reply Quote 0
                • Jozh
                  Jozh last edited by

                  Thank you for your time. How would you add that first code to this code?

                  import editor
                  import re
                  
                  def main():
                  	text = editor.get_selected_text()
                  	feet = sum(int(x) for x in re.findall(r"(\d+)'", text))
                  	inches = sum(int(x) for x in re.findall(r'(\d+)"', text))
                  	if not (feet or inches):
                  		return
                  	feet += (inches / 12)
                  	inches %= 12
                  	replacement = '%i\' %i"' % (feet, inches)
                  	start, end = editor.get_selection()
                  	editor.replace_text(start, end, replacement)
                  
                  main()
                  
                  1 Reply Last reply Reply Quote 0
                  • ccc
                    ccc last edited by

                    from __future__ import division
                    import editor
                    
                    # <put the fractions_to_floats() and feet_and_inches() code here>
                    
                    def main():
                        text = editor.get_selected_text()
                        if text:
                            start, end = editor.get_selection()
                            editor.replace_text(start, end, feet_and_inches(text))
                    
                    main()
                    
                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors