Community
    • Login

    Unit Conversion Plugin

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    18 Posts 3 Posters 15.3k Views
    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.
    • Jared HowardJ
      Jared Howard
      last edited by

      Hello,

      I am new to plugin development and wanted to ask if anyone has developed a metric to imperial or imperial to metric plugin for use with notepad++?

      I need to convert mass amounts of data and would like to be able to alt + shift select a group of values and activate the plugin to convert.

      If nobody has developed this, any tips or tricks is appreciated.

      Thanks

      Claudia FrankC 1 Reply Last reply Reply Quote 0
      • dailD
        dail
        last edited by

        None exist that I’m aware of. This sounds like a fairly straight forward plugin. Your best bet is to install the Python Script plugin and you can write your plugin in Python. It would probably end up being < 50 lines of code. You could also use C/C++ or C# or something but the work involved in that for what you are doing probably isn’t worth it.

        1 Reply Last reply Reply Quote 0
        • Claudia FrankC
          Claudia Frank @Jared Howard
          last edited by

          Hello Jared-Howard,

          if you are looking for a python solution it might look like this

          editor.beginUndoAction()
          
          def conv_mile_kilometers(miles):  
              km = int(miles) / 1.6
              return km
          
          for i in range(editor.getSelections()):
              start = editor.getSelectionNStart(i)
              end = editor.getSelectionNEnd(i)
              word = editor.getTextRange(start,end)
              editor.setTarget(start, end)
              editor.replaceTarget('{0}'.format(conv_mile_kilometers(word)))    
              
          editor.endUndoAction()
          

          Just be aware that you select numbers only as I didn’t do any error checking.
          What needs to be done first is described here.
          Cheers
          Claudia

          1 Reply Last reply Reply Quote 1
          • Jared HowardJ
            Jared Howard
            last edited by

            Thank you both, I will play around with it and see if I can get it to work.

            1 Reply Last reply Reply Quote 0
            • Jared HowardJ
              Jared Howard
              last edited by

              I was able to get it to work, however is there a command so that the selection will include decimal values?

              For example when I run the script it will convert “25” but not “25.4”

              Thanks again

              1 Reply Last reply Reply Quote 0
              • Claudia FrankC
                Claudia Frank
                last edited by

                just change

                km = int(miles) / 1.6
                

                to

                km = float(miles) / 1.6
                

                Cheers
                Claudia

                1 Reply Last reply Reply Quote 0
                • Jared HowardJ
                  Jared Howard
                  last edited by

                  Excellent, has been working nicely so thank you for the help!

                  One last thing, is there a way to get the script to work when selecting multiple values when they are separated by a comma?

                  Example

                  25.4, 50.8,

                  1 Reply Last reply Reply Quote 0
                  • Claudia FrankC
                    Claudia Frank
                    last edited by

                    Yes, splitting a line is easy but depends on what is assumed as input.
                    Let’s say you have a line like

                    25.4, 50.8, ...
                    

                    or lines like

                    25.4, 50.8, ...
                    27.4, 54.8, ...
                    

                    or

                    25,4, 50,8, ...
                    

                    from scripts point of view either one could break the logic, even in the way how you select the values.
                    So question is what is/are your preferred way(s)? Depending on that we should be able to find a solution.

                    Cheers
                    Claudia

                    1 Reply Last reply Reply Quote 0
                    • Jared HowardJ
                      Jared Howard
                      last edited by

                      Lines are typically separated by a space, however can vary as to if they have a decimal point or not. They will most likely always be a variation of the below.

                      2.99, 10,
                      3.98, 9,

                      0.25, 0.39,
                      0.25, 0.39,

                      1 Reply Last reply Reply Quote 0
                      • Claudia FrankC
                        Claudia Frank
                        last edited by

                        Can I safely assume that you always use ALT+SHIFT to select the values?
                        This is critical because selecting with ALT+SHIFT and arrow right etc. results (taking your example) in

                        Selection:0 Values:2.99, 10,
                        Selection:1 Values:3.98, 9,
                        Selection:2 Values:
                        Selection:3 Values:0.25, 0.39,
                        Selection:4 Values:0.25, 0.39,

                        whereas using CTRL+ALT would result in

                        Selection:0 Values:2.99, 10,
                        3.98, 9,

                        0.25, 0.39,
                        0.25, 0.39,

                        As you see, the first would report 5 selections, each line as one selection where as
                        the second reports one selection for all 5 lines.

                        If using ALT+SHIFT we need to check if line has valid values (empty lines issue),
                        then split by comma, do conversion for each value and rebuild string.
                        Should we go this way?

                        Cheers
                        Claudia

                        1 Reply Last reply Reply Quote 0
                        • Jared HowardJ
                          Jared Howard
                          last edited by

                          Correct, I always use ALT+SHIFT for selection. Your proposal sounds like the correct way to proceed.

                          Thank you again for your assistance. I am more versed in Pascal and am not very fluent yet with Python.

                          1 Reply Last reply Reply Quote 0
                          • Claudia FrankC
                            Claudia Frank
                            last edited by Claudia Frank

                            Hi,

                            I did some tests(empty line, just a comma, double comma) and I guess this should do the trick

                            editor.beginUndoAction()
                            
                            def conv_mile_kilometers(miles):  
                                list_of_values = miles.split(',')
                                km = ''
                                for value in list_of_values:
                                    if len(value) > 0 and value.isspace != True:
                                        km += str(float(value) / 1.6) + ','
                                return km
                            
                            for i in range(editor.getSelections()):
                                start = editor.getSelectionNStart(i)
                                end = editor.getSelectionNEnd(i)
                                word = editor.getTextRange(start,end)
                                if len(word) > 0 and word.isspace != True:
                                    editor.setTarget(start, end)
                                    editor.replaceTarget('{0}'.format(conv_mile_kilometers(word)))    
                                
                            editor.endUndoAction() 
                            

                            Cheers
                            Claudia

                            1 Reply Last reply Reply Quote 0
                            • Claudia FrankC
                              Claudia Frank
                              last edited by

                              Correction:-(

                              Unfortunattely I cannot edit my post so

                              both isspace items need to be changed to isspace()
                              because they are functions.

                              Full code

                              editor.beginUndoAction()
                              
                              def conv_mile_kilometers(miles):  
                                  list_of_values = miles.split(',')
                                  km = ''
                                  for value in list_of_values:
                                      if len(value) > 0 and value.isspace() != True:
                                          km += str(float(value) / 1.6) + ','
                                  return km
                              
                              for i in range(editor.getSelections()):
                                  start = editor.getSelectionNStart(i)
                                  end = editor.getSelectionNEnd(i)
                                  word = editor.getTextRange(start,end)
                                  if len(word) > 0 and word.isspace() != True:
                                      editor.setTarget(start, end)
                                      editor.replaceTarget('{0}'.format(conv_mile_kilometers(word)))    
                                  
                              editor.endUndoAction() 
                              

                              Cheers
                              Claudia

                              1 Reply Last reply Reply Quote 0
                              • Jared HowardJ
                                Jared Howard
                                last edited by

                                Excellent it works great, thank you!

                                The last question I have regarding this is how do I format the number of decimals that the values get returned as when converted?

                                1 Reply Last reply Reply Quote 0
                                • Claudia FrankC
                                  Claudia Frank
                                  last edited by

                                  Hello Jared,

                                  I’m sorry I don’t understand your question could you gimme an example
                                  of what you want to do?

                                  Thanks
                                  Claudia

                                  1 Reply Last reply Reply Quote 0
                                  • Jared HowardJ
                                    Jared Howard
                                    last edited by

                                    Sorry I see my wording was not clear.

                                    Once I convert say millimeters to inches is there a way to control the number of decimal places?

                                    1.33 inches converted to metric is = 33.782

                                    I want the value to only display 2 decimal places, 33.78. How is that accomplished?

                                    Thanks again!

                                    1 Reply Last reply Reply Quote 0
                                    • Claudia FrankC
                                      Claudia Frank
                                      last edited by

                                      Hello,
                                      this can be done using format’s strength.
                                      Within the try block we need to change the following line

                                      km += str(float(value) / 1.6) + ','
                                      

                                      to

                                      km += '{0:.2f},'.format(float(value) / 1.6)
                                      

                                      Short explanation, the placeholder {0} receives the result from the
                                      calculation float(value) / 1.6.
                                      By specifying the colon after the zero, the format function gets informed
                                      that the result needs to be reformatted.
                                      The dot after the colon defines precision formatting,
                                      the number 2 specifies only 2 decimal places and the letter f is to inform that it is
                                      a float which needs to be formatted.

                                      But we can go one step further, if you are interested in keep the number of
                                      decimal places of the input and the output value the same then
                                      we can replace the km += … line with the following two lines.

                                      decimal_places = str(value)[::-1].find('.')
                                      km += '{0:.{1}f},'.format((float(value) / 1.6),decimal_places)
                                      

                                      as you see, the 2 is now replaced with another placeholder{1} which
                                      get the value of decimal_places variable, which itself gets the value
                                      from a stringified value where we search for the dot from right to left (that is what the -1 stands for).

                                      Cheers
                                      Claudia

                                      1 Reply Last reply Reply Quote 0
                                      • Jared HowardJ
                                        Jared Howard
                                        last edited by

                                        Outstanding!

                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post
                                        The Community of users of the Notepad++ text editor.
                                        Powered by NodeBB | Contributors