You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There a a handful of ways to interface Fortran and Python:
22
22
@@ -75,7 +75,7 @@ The problem at hand is an automatic gain control function function, expressed in
75
75
end
76
76
77
77
f2py:
78
-
-------
78
+
-----
79
79
80
80
f2py is a command line utility that comes with numpy. You can build a default simple wrapper with the f2py command::
81
81
@@ -108,7 +108,7 @@ So it can be called like so::
108
108
where `signal` and `filtered` are 1-d arrays of float32 values, both of the same length.
109
109
110
110
Giving f2py extra information:
111
-
...............................
111
+
..............................
112
112
113
113
f2py can build an interface to a fortran subroutine, but it can't do it all that well without some extra information. For instnce, note from the docstring that the argument `ampagc` is listed as an input argument, when it is really intended to be used for output.
Copy file name to clipboardExpand all lines: Sources/source/where_to_put_your_code.rst
+28-14Lines changed: 28 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
Where to put your custom code?
3
3
******************************
4
4
5
+
(You can find this page at: http://bit.ly/JustPackage)
6
+
5
7
A suggestion for how to manage your personal library of python functions you might use for scripting, data analysis, etc.
6
8
7
9
TL; DR
@@ -14,13 +16,19 @@ Make a "package" out of it so you can manage it in one place, and use it in othe
14
16
Introduction
15
17
------------
16
18
17
-
Many folks find they have a collection of little scripts and utilities that they want to be able to use and re-use for various projects. It is really NOT a good idea to simply copy and paste these around for use with each project -- you will regret that!
19
+
Many scientists and engineers that do a little coding find they have a collection of little scripts and utilities that they want to be able to use and re-use for various projects. It is really NOT a good idea to simply copy and paste these around for use with each project -- you will regret that!
18
20
19
21
It is also not a good idea to use the ``PYTHONPATH`` environment variable to set up a directory in which to dump stuff. (Google a bit if you want to know why).
20
22
21
-
The best way to do this with Python is to use the python package mechanism.
23
+
The best way to do this with Python is to use the Python package mechanism.
24
+
25
+
A Python "package" is a collection of modules and scripts -- we usually think of these as something carefully developed for a particular purpose and distributed to a wide audience for re-use -- the packages you can install with pip.
26
+
27
+
Indeed that is the case, but the "collection of modules and scripts" part can be used for your own code that no one else is ever going to touch, and the overhead is small if you use it only this way.
28
+
29
+
The challenge is that most of the documentation about python packaging is focused on creating a package of a library that you want to distribute to the community. In that case, it's very important to have full and proper meta data, tests, documentation, etc. As a result, the packaging documentation makes the whole process seem complicated and cumbersome.
22
30
23
-
A Python "package" is a collection of modules and scripts -- we usually think of these as something carefully developed for particular purpose and distributed to a wide audience for re-use -- the packages you can install with pip. Indeed that is the case, but the "collection of modules and scripts" part can be used for your own code that no one else is ever going to touch, and the overhead is small if you use it only this way.
31
+
But making a simple package for your own use can be very simple, and very useful.
24
32
25
33
Step by step:
26
34
-------------
@@ -33,18 +41,18 @@ Step by step:
33
41
34
42
4) In the outer dir, put in a file (we'll fill it in later) called ``setup.py``.
35
43
36
-
So you shoudl have::
44
+
So you should have::
37
45
38
46
my_code
39
47
my_code
40
48
__init__.py
41
49
setup.py
42
50
43
-
The inner my_code dir is now a python "package" -- any directory with a __init__.py file is a package. But how to use it?
51
+
The inner my_code dir is now a python "package" -- any directory with a ``__init__.py`` file is a package. But how to use it?
44
52
45
-
The setup.py file is where you specify for python how this package is setup. You can do a lot in there, and if you ever want to share your code with anyone else, you should follow:
53
+
The ``setup.py`` file is where you specify for python how this package is setup. You can do a lot in there, and if you ever want to share your code with anyone else, you should follow:
But for now, we are going to make it as *simple* as possible::
50
58
@@ -56,6 +64,7 @@ But for now, we are going to make it as *simple* as possible::
56
64
57
65
That's it -- really! There is a lot you can do here to support multiple packages, scripts, etc, but this is enough to get you started.
58
66
67
+
59
68
Putting in your code
60
69
--------------------
61
70
@@ -80,15 +89,19 @@ Create a file for your code, and put it in the inner my_code dir:
80
89
81
90
OK -- now you have a (useless) package with some code in it - how to use it?
82
91
83
-
To use this package, you need to "install" it into the python environment that you want to use. Some of us have a single python install -- maybe Anaconda's root environment, or the python.org python installer, or.... Some of us use virtualenv or conda environments. In any case, get yourself into that environment with a command line in the outer my_code dir, and type::
92
+
To use this package, you need to "install" it into the python environment that you want to use. Some of us have a single python install -- maybe Anaconda's root environment, or the python.org python installer, or ...
93
+
94
+
Some of us use virtualenv, or pipienv, or conda environments. In any case, get yourself into that environment at a command line and put yourself (``cd`` in Terminal, DOS box, etc...) in the outer my_code dir (where the setup.py is), and type::
95
+
96
+
pip install -e .
84
97
85
-
python setup.py develop
98
+
``pip install`` installs a package. ``-e`` means "do an editable install", and the dot (``.``) tells pip to install the package in the current directory. ``pip`` will look for a ``setup.py`` file in the current working dir. An editable install is like install, but instead of copying the code into the python environment, it adds it to the Python search path (only that particular environment's Python) so you can import it, but it will always be importing the current version of the files if you change things.
86
99
87
-
"develop" is like install, but instead of copying the code into the python environment, it adds it to the python search path (only that particular python instances search path!) so you can import it, but it will always be importing the current version of the files if you change things.
100
+
This means you can be actively maintaining your shared code, and other projects that use it will always get the latest version.
88
101
89
-
Now you can fire up python (or ipython, or a Jupyter notebook, or write code in a script, or...) and do::
102
+
Now you can fire up Python (or iPython, or a Jupyter notebook, or write code in a script, or...) and do:
90
103
91
-
from my_code import some_code
104
+
.. code-block:: ipython
92
105
93
106
In [2]: from my_code import some_code
94
107
@@ -108,12 +121,13 @@ If you have only a little bit of code, you can do all this with a single module,
108
121
109
122
If you have more than a few modules, it would probably make sense to keep them in separate packages, organized by functionality.
110
123
111
-
This is only the very simplest way to do it what you really SHOULD do is be more formal about the process:
124
+
This is only the very simplest way to do it. What you really SHOULD do is be more formal about the process:
112
125
- Do some versioning of the package
113
126
- Keep it in source code version control system (like git, etc)
127
+
- add tests of your code...
114
128
115
129
and others...
116
130
117
-
Look up "Software Carpentry" for many more ideas how better to manage your software for Science.
131
+
Look up "Software Carpentry" for many more ideas about how better to manage your Software for Science.
0 commit comments