User Tools

Site Tools


python:python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python:python [2023/10/28 00:07] dblumepython:python [2024/02/27 15:43] (current) – [Linux script that takes either stdin or files] dblume
Line 23: Line 23:
  
 Would be good to experiment with [[http://pingswept.org/2009/01/24/least-squares-polynomial-fitting-in-python/|least-squares polynomial fitting in Python]]. Would be good to experiment with [[http://pingswept.org/2009/01/24/least-squares-polynomial-fitting-in-python/|least-squares polynomial fitting in Python]].
 +
 ===== Data Analysis ===== ===== Data Analysis =====
  
Line 79: Line 80:
 ===== Linux script that takes either stdin or files ===== ===== Linux script that takes either stdin or files =====
  
 +Newer way, **use [[https://docs.python.org/3/library/fileinput.html|module fileinput]]**. Older way:
 <code python> <code python>
 if __name__=='__main__': if __name__=='__main__':
Line 87: Line 89:
             if not line:             if not line:
                 break                 break
-            my_process_line( line.rstrip() )+            my_process_line(line.rstrip())
     else:     else:
         # Process lines of the files specified.         # Process lines of the files specified.
         for fname in sys.argv[1:]:         for fname in sys.argv[1:]:
-            if not os.path.exists( fname ): +            if not os.path.exists(fname): 
-                treat_argument_as_literal( fname )+                treat_argument_as_literal(fname)
                 continue                 continue
-            with open( fname, 'r' ) as f:+            with open(fname, 'r') as f:
                 while 1:                 while 1:
                     line = f.readline()                     line = f.readline()
                     if not line:                     if not line:
                         break                         break
-                    my_process_line( line.rstrip() )+                    my_process_line(line.rstrip())
 </code> </code>
  
Line 148: Line 150:
          
 if __name__ == '__main__': if __name__ == '__main__':
-    cProfile.run( "my_function()" )+    cProfile.run("my_function()")
 </code> </code>
  
Line 162: Line 164:
  
 # Split input data by row and then on spaces # Split input data by row and then on spaces
-rows = [ line.strip().split(' ') for line in data.split('\n') ]+rows = [line.strip().split(' ') for line in data.split('\n')]
  
 # Reorganize data by columns # Reorganize data by columns
Line 168: Line 170:
  
 # Compute column widths by taking maximum length of values per column # Compute column widths by taking maximum length of values per column
-col_widths = [ max(len(value) for value in col) for col in cols ]+col_widths = [max(len(value) for value in col) for col in cols]
  
 # Create a suitable format string # Create a suitable format string
-format = ' '.join(['%%%ds' % width for width in col_widths ])+format = ' '.join(['%%%ds' % width for width in col_widths])
  
 # Print each row using the computed format # Print each row using the computed format
Line 202: Line 204:
         self.__m_x = x         self.__m_x = x
  
-    x = property( getx, setx )+    x = property(getx, setx)
  
 class B: class B:
     """ Old; very small, was for for multitudes of objects. """     """ Old; very small, was for for multitudes of objects. """
-    __slots__ = [ "__m_x" ]+    __slots__ = ["__m_x"]
     def __init__(self):     def __init__(self):
         self.__m_x = 0         self.__m_x = 0
Line 217: Line 219:
         self.__m_x = x         self.__m_x = x
  
-    x = property( getx, setx )        +    x = property(getx, setx)        
  
 class C(object): class C(object):
-    """ New, reccommended. """+    """ New, recommended. """
     def __init__(self):     def __init__(self):
         self.__x = 0         self.__x = 0
Line 231: Line 233:
         self.__x = x         self.__x = x
  
-    x = property( getx, setx )+    x = property(getx, setx)
 </code> </code>
  
python/python.1698476854.txt.gz · Last modified: 2023/10/28 00:07 by dblume