Python: Difference between revisions

From Bitpost wiki
No edit summary
No edit summary
Line 35: Line 35:
     $ pip freeze > requirements.txt
     $ pip freeze > requirements.txt
     # The best of both worlds.
     # The best of both worlds.
=== Creating modules that can be imported OR run directly ===
Set up your module like this:
  #!/bin/env python
  from __future__ import print_function
  import argparse
  import sys
  def main(argv):
  parser = argparse.ArgumentParser(description='Run a performance test')
  parser.add_argument('--server', dest='serveraddr', type=str, action='store', help='address of server',
  required=True)
  parser.add_argument('--client', dest='clientaddr', type=str, action='store', help='address of client',
  required=True)
  parser.add_argument('--port', dest='port', type=int, action='store', help='port',
  required=True)
  parser.add_argument('--count', dest='count', type=int, action='store', default=100,
  help='number of objects')
  args = parser.parse_args(argv)
    # code it up
    # use dest values, eg: args.serveraddr
  return 0
  # Only run main if we called this directly, so we can use this as an import elsewhere
  if __name__ == '__main__':
  main(sys.argv)
Run it from inside other python code like this:
#!/bin/env python
import performance_test
performance_test.main(['--server=10.122.82.241','--client=10.122.83.57','--port=5000']);

Revision as of 17:01, 5 March 2018

Installation on Ubuntu

Virtual environments are great. Best to use them right out of the gate, as Ubuntu apparently has hacked up their version.

sudo apt update
sudo apt install virtualenv
virtualenv my_python
source my_pyton/bin/activate
pip install --upgrade pip

Here are the things I had to install to get my work env going:

sudo apt install python-pip libxml2-dev libxslt1-dev

And that never got me there. More lessons to learn...

Configure pycharm to use a virtualenv

File > Settings > Project: ate > Project Interpreter > click Gear in top-right > Add Local

Select the python bin eg:

/home/m/Envs/ate/bin/python2

Dependency Control

Great suggestion for python dependency control:

   use two dependency files, one general, and one for current freeze version numbers
   node.js has a similar approach with its new lock files (good stuff i think)
   gentoo just fucking gets it right (nearly) every time, somehow
   ---
   # requirements-to-freeze.txt (typical mostly-unconstrained Method #1) is used to specify your top-level dependencies, and any explicit versions you need to specify.
   # requirements.txt (fully-versioned Method #2) contains the output of $ pip freeze after $ pip install requirements-to-freeze.txt has been run.
   $ cd project-repo
   $ pip install -r requirements-to-freeze.txt --upgrade
   Installing collected packages: six, enum34, ipaddress, ...
   $ pip freeze > requirements.txt
   # The best of both worlds.

Creating modules that can be imported OR run directly

Set up your module like this:

 #!/bin/env python
 from __future__ import print_function
 import argparse
 import sys

 def main(argv):
 	parser = argparse.ArgumentParser(description='Run a performance test')
 	parser.add_argument('--server', dest='serveraddr', type=str, action='store', help='address of server',
 						required=True)
 	parser.add_argument('--client', dest='clientaddr', type=str, action='store', help='address of client',
 						required=True)
 	parser.add_argument('--port', dest='port', type=int, action='store', help='port',
 						required=True)
 	parser.add_argument('--count', dest='count', type=int, action='store', default=100,
 						help='number of objects')

 	args = parser.parse_args(argv)

   # code it up
   # use dest values, eg: args.serveraddr

 	return 0

 # Only run main if we called this directly, so we can use this as an import elsewhere
 if __name__ == '__main__':
 	main(sys.argv)

Run it from inside other python code like this:

#!/bin/env python
import performance_test
performance_test.main(['--server=10.122.82.241','--client=10.122.83.57','--port=5000']);