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 [2014/05/23 00:59] – [Python] dblumepython:python [2024/02/27 15:43] (current) – [Linux script that takes either stdin or files] dblume
Line 1: Line 1:
-====== Python ======+====== General Python Notes ======
  
 Man, there's too much to do and note.  Logging some stuff to investigate later... Man, there's too much to do and note.  Logging some stuff to investigate later...
Line 9: Line 9:
   * Of course, there's [[:xampp|getting Python running under Xampp]].   * Of course, there's [[:xampp|getting Python running under Xampp]].
   * There's [[http://www.infoworld.com/d/application-development/pillars-python-six-python-web-frameworks-compared-169442|A comparison of CubicWeb, Django, Pyramid, Web.py, Web2py, and Zope 2]]   * There's [[http://www.infoworld.com/d/application-development/pillars-python-six-python-web-frameworks-compared-169442|A comparison of CubicWeb, Django, Pyramid, Web.py, Web2py, and Zope 2]]
-  * Look into the microframework [[http://flask.pocoo.org/|Flask]]. Here's a [[http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world|tutorial on making a blog with it]].+  * Look into the microframework [[http://flask.pocoo.org/|Flask]]. Here's a [[http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world|tutorial on making a blog with it]]. Consider flask with gunicorn and nginx.
   * [[https://dataset.readthedocs.org/en/latest/|dataset]], an abstraction layer above relational databases.   * [[https://dataset.readthedocs.org/en/latest/|dataset]], an abstraction layer above relational databases.
   * [[http://docopt.org/|docopt]] for a stupidly easy and correct command-line interface description language.   * [[http://docopt.org/|docopt]] for a stupidly easy and correct command-line interface description language.
   * [[http://stevenloria.com/python-best-practice-patterns-by-vladimir-keleshev-notes/|Best practices]] by Vladimir Keleshev.   * [[http://stevenloria.com/python-best-practice-patterns-by-vladimir-keleshev-notes/|Best practices]] by Vladimir Keleshev.
-  * [[https://twitter.com/brandon_rhodes/status/449938490516860928|You can ask a new-style class for its .__subclasses__()]] It uses weak references. Look at [[https://gist.github.com/etrepum/9862280|this interesting example with a child class inside a method]]. +  * [[https://twitter.com/brandon_rhodes/status/449938490516860928|You can ask a new-style class for its .__subclasses__()]] It uses weak references. Look at [[https://gist.github.com/etrepum/9862280|this interesting example with a child class inside a method]]. 
 +  * A Power Point deck by Alex Martelli describing [[http://www.aleax.it/yt_pydi.pdf|Dependency Injection vs. Template Method (override base method) vs. Monkey Patching]]. 
 +  * [[http://stackoverflow.com/questions/101268/hidden-features-of-python|Hidden Features of Python]] at StackOverflow 
 +  * [[http://chriskiehl.com/article/parallelism-in-one-line|Parallelism in one line]]
  
 Would be nice to create a binary search in text files in Python.  Maybe based on an answer deep in [[http://stackoverflow.com/questions/744256/reading-huge-file-in-python|Reading Huge File in Python]]. Would be nice to create a binary search in text files in Python.  Maybe based on an answer deep in [[http://stackoverflow.com/questions/744256/reading-huge-file-in-python|Reading Huge File in Python]].
Line 20: 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 76: 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 84: 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 145: Line 150:
          
 if __name__ == '__main__': if __name__ == '__main__':
-    cProfile.run( "my_function()" )+    cProfile.run("my_function()")
 </code> </code>
  
Line 159: 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 165: 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 199: 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 214: 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 228: Line 233:
         self.__x = x         self.__x = x
  
-    x = property( getx, setx )+    x = property(getx, setx)
 </code> </code>
  
Line 247: Line 252:
         fcntl.flock(f, fcntl.LOCK_UN)         fcntl.flock(f, fcntl.LOCK_UN)
 </code> </code>
 +
 +====== Various Approaches to threaded URL Requests ======
 +
 +  * [[https://stackoverflow.com/questions/2632520/what-is-the-fastest-way-to-send-100-000-http-requests-in-python|Use Queue and threading's Thread]]
 +  * [[https://www.shanelynn.ie/using-python-threading-for-multiple-results-queue/|Use threading and store results in a pre-allocated list]]. Then use Queue for lots of URLs.
 +  * [[https://dev.to/rhymes/how-to-make-python-code-concurrent-with-3-lines-of-code-2fpe|Use concurrent.futures' ThreadPoolExecutor and map()]].
 +  * Or, use the doc's [[https://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor-example|ThreadPoolExecutor Example]].
 +  * And, as mentioned in Parallelism in One Line, [[https://chriskiehl.com/article/parallelism-in-one-line|multiprocessing.dummy's Pool and its own map()]].
  
 ====== Fibonacci Generator with Itertools ====== ====== Fibonacci Generator with Itertools ======
Line 268: Line 281:
 #    for i in range( 5 ): #    for i in range( 5 ):
 #        print i, fib( i ) #        print i, fib( i )
 +</code>
 +
 +====== Parse C++ Code with nm and graphviz's dot ======
 +
 +<code bash>
 +sudo apt-get install graphviz
 +</code>
 +
 +<code python>
 +    # Read `nm` output
 +    output = subprocess.check_output(['nm', '--demangle', entry.path]).decode('utf-8')
 +    for line in output.split('\n'):
 +        if 'Singleton' in line:
 +
 +    # Make a .dot file
 +    with open(filename + '.dot', 'w') as file:
 +        file.write('digraph {\n')
 +        for f, destinations in self.graph.items():
 +            for d in destinations:
 +                file.write(f'  "{f}" -> "{d}"\n')
 +
 +
 +def create_png_image(filename):
 +    with open(filename + '.png', 'wb') as fout:
 +        p1 = subprocess.run(['dot', filename + '.dot', '-Tpng'], stdout=fout)
 +
 </code> </code>
  
python/python.1400831943.txt.gz · Last modified: 2023/04/12 20:44 (external edit)