Skip to content

Commit fc2a46a

Browse files
first version of script to build a simple pacakage
1 parent 9ecddfd commit fc2a46a

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

Sources/code/make_my_package.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
A simple script that builds a as_simple_as_possible package
5+
to use for your own code.
6+
"""
7+
import sys, os
8+
9+
USAGE = """
10+
python make_my_package.py your_package_name
11+
12+
Running the script will create a minimal package
13+
you can use to manage your personal code
14+
"""
15+
16+
setup_template = """#!/usr/bin/env python
17+
from setuptools import setup
18+
19+
setup(name='{package_name}',
20+
packages=['{package_name}'],
21+
)
22+
23+
"""
24+
25+
test_code = '''#!/usr/bin/env python
26+
27+
"""
28+
Just an example, but this could be a collection of utility functions, etc.
29+
30+
Here would be documentation of what's in this file
31+
32+
In this case, just one function to make sure it works.
33+
"""
34+
35+
def test_fun():
36+
print("yup -- this worked!!")
37+
38+
'''
39+
40+
if __name__ == "__main__":
41+
42+
43+
try:
44+
package_name = sys.argv[1]
45+
except IndexError:
46+
print("You need to provide a name for your package")
47+
print(USAGE)
48+
sys.exit(1)
49+
50+
this_dir = os.curdir
51+
os.mkdir(package_name)
52+
os.chdir(package_name)
53+
with open("setup.py", 'w') as setupfile:
54+
setupfile.write(setup_template.format(package_name=package_name))
55+
os.mkdir(package_name)
56+
os.chdir(package_name)
57+
with open("__init__.py", 'w') as initfile:
58+
initfile.write("#!/usr/bin/env python\n\n"
59+
"__version__ = '0.0.0'\n"
60+
)
61+
62+
with open("test_code.py", 'w') as initfile:
63+
initfile.write(test_code)
64+
65+
os.chdir("../..")
66+
67+
68+
69+
70+

Sources/source/where_to_put_your_code.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ Where to put your custom code?
66

77
A suggestion for how to manage your personal library of python functions you might use for scripting, data analysis, etc.
88

9+
910
TL; DR
1011
======
1112

1213
If you have a collection of your own code you want access to for various projects:
1314

1415
Make a "package" out of it so you can manage it in one place, and use it in other places.
1516

17+
You DO NOT NEED TO PUT IT ON PYPI !
18+
19+
1620
Introduction
1721
------------
1822

@@ -35,7 +39,7 @@ Step by step:
3539

3640
1) Create a directory in your user (or home, or ... ) dir for your code. Let's call it "my_code".
3741

38-
2) This is going to seem odd, but create another dir with the same name inside that -- this is where the actual code goes. (it's a convention to name the top dir the same thing as the "package" name, but it doesn't matter. But the inner name does -- that is the name of your package.
42+
2) This is going to seem odd, but create another dir with the same name inside that -- this is where the actual code goes. (it's a convention to name the top directory the same thing as the "package" name, but you don't have to follow that convention. But the inner name does -- that is the name of your package.
3943

4044
3) In that dir, put in an empty, for now, file called ``__init__.py``.
4145

0 commit comments

Comments
 (0)