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/where_to_put_your_code.rst.txt
+19-10Lines changed: 19 additions & 10 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,16 +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
23
The best way to do this with Python is to use the Python package mechanism.
22
24
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.
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.
24
26
25
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.
26
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.
30
+
31
+
But making a simple package for your own use can be very simple, and very useful.
27
32
28
33
Step by step:
29
34
-------------
@@ -47,7 +52,7 @@ The inner my_code dir is now a python "package" -- any directory with a ``__init
47
52
48
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::
53
58
@@ -84,15 +89,19 @@ Create a file for your code, and put it in the inner my_code dir:
84
89
85
90
OK -- now you have a (useless) package with some code in it - how to use it?
86
91
87
-
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 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, 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::
88
95
89
96
pip install -e .
90
97
91
-
``pip install`` installs a package. ``-e`` means "do an editable install", and the dot (``.``) means, install the package in the current directory. ``pip`` will look for a ``setup.py`` file in the current working dir. And 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.
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.
99
+
100
+
This means you can be actively maintaining your shared code, and other projects that use it will always get the latest version.
92
101
93
-
This means you can be activly maintaining your shared code, and other projects that use it will always get the latest version.
102
+
Now you can fire up Python (or iPython, or a Jupyter notebook, or write code in a script, or...) and do:
94
103
95
-
Now you can fire up Python (or iPython, or a Jupyter notebook, or write code in a script, or...) and do: ::
104
+
.. code-block:: ipython
96
105
97
106
In [2]: from my_code import some_code
98
107
@@ -112,13 +121,13 @@ If you have only a little bit of code, you can do all this with a single module,
112
121
113
122
If you have more than a few modules, it would probably make sense to keep them in separate packages, organized by functionality.
114
123
115
-
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:
116
125
- Do some versioning of the package
117
126
- Keep it in source code version control system (like git, etc)
118
-
- add tests of your code...
127
+
- add tests of your code...
119
128
120
129
and others...
121
130
122
-
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