Skip to content

Commit 588dea3

Browse files
authored
Merge pull request #662 from sbenthall/sb-helpers
Move file manipulation methods into helpers.py
2 parents ba6ac6c + 3657200 commit 588dea3

File tree

2 files changed

+170
-179
lines changed

2 files changed

+170
-179
lines changed

HARK/core.py

Lines changed: 0 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,182 +1144,3 @@ def updateDynamics(self):
11441144
for this_type in self.agents:
11451145
setattr(this_type, var_name, this_obj)
11461146
return dynamics
1147-
1148-
1149-
# ------------------------------------------------------------------------------
1150-
# Code to copy entire modules to a local directory
1151-
# ------------------------------------------------------------------------------
1152-
1153-
# Define a function to run the copying:
1154-
def copy_module(target_path, my_directory_full_path, my_module):
1155-
'''
1156-
Helper function for copy_module_to_local(). Provides the actual copy
1157-
functionality, with highly cautious safeguards against copying over
1158-
important things.
1159-
1160-
Parameters
1161-
----------
1162-
target_path : string
1163-
String, file path to target location
1164-
1165-
my_directory_full_path: string
1166-
String, full pathname to this file's directory
1167-
1168-
my_module : string
1169-
String, name of the module to copy
1170-
1171-
Returns
1172-
-------
1173-
none
1174-
'''
1175-
1176-
if target_path == 'q' or target_path == 'Q':
1177-
print("Goodbye!")
1178-
return
1179-
elif target_path == os.path.expanduser("~") or os.path.normpath(target_path) == os.path.expanduser("~"):
1180-
print("You have indicated that the target location is " + target_path +
1181-
" -- that is, you want to wipe out your home directory with the contents of " + my_module +
1182-
". My programming does not allow me to do that.\n\nGoodbye!")
1183-
return
1184-
elif os.path.exists(target_path):
1185-
print("There is already a file or directory at the location " + target_path +
1186-
". For safety reasons this code does not overwrite existing files.\n Please remove the file at "
1187-
+ target_path +
1188-
" and try again.")
1189-
return
1190-
else:
1191-
user_input = input("""You have indicated you want to copy module:\n """ + my_module
1192-
+ """\nto:\n """ + target_path + """\nIs that correct? Please indicate: y / [n]\n\n""")
1193-
if user_input == 'y' or user_input == 'Y':
1194-
# print("copy_tree(",my_directory_full_path,",", target_path,")")
1195-
copy_tree(my_directory_full_path, target_path)
1196-
else:
1197-
print("Goodbye!")
1198-
return
1199-
1200-
1201-
def print_helper():
1202-
1203-
my_directory_full_path = os.path.dirname(os.path.realpath(__file__))
1204-
1205-
print(my_directory_full_path)
1206-
1207-
1208-
def copy_module_to_local(full_module_name):
1209-
'''
1210-
This function contains simple code to copy a submodule to a location on
1211-
your hard drive, as specified by you. The purpose of this code is to provide
1212-
users with a simple way to access a *copy* of code that usually sits deep in
1213-
the Econ-ARK package structure, for purposes of tinkering and experimenting
1214-
directly. This is meant to be a simple way to explore HARK code. To interact
1215-
with the codebase under active development, please refer to the documentation
1216-
under github.com/econ-ark/HARK/
1217-
1218-
To execute, do the following on the Python command line:
1219-
1220-
from HARK.core import copy_module_to_local
1221-
copy_module_to_local("FULL-HARK-MODULE-NAME-HERE")
1222-
1223-
For example, if you want SolvingMicroDSOPs you would enter
1224-
1225-
from HARK.core import copy_module_to_local
1226-
copy_module_to_local("HARK.SolvingMicroDSOPs")
1227-
1228-
'''
1229-
1230-
# Find a default directory -- user home directory:
1231-
home_directory_RAW = os.path.expanduser("~")
1232-
# Thanks to https://stackoverflow.com/a/4028943
1233-
1234-
# Find the directory of the HARK.core module:
1235-
# my_directory_full_path = os.path.dirname(os.path.realpath(__file__))
1236-
hark_core_directory_full_path = os.path.dirname(os.path.realpath(__file__))
1237-
# From https://stackoverflow.com/a/5137509
1238-
# Important note from that answer:
1239-
# (Note that the incantation above won't work if you've already used os.chdir()
1240-
# to change your current working directory,
1241-
# since the value of the __file__ constant is relative to the current working directory and is not changed by an
1242-
# os.chdir() call.)
1243-
#
1244-
# NOTE: for this specific file that I am testing, the path should be:
1245-
# '/home/npalmer/anaconda3/envs/py3fresh/lib/python3.6/site-packages/HARK/SolvingMicroDSOPs/---example-file---
1246-
1247-
# Split out the name of the module. Break if proper format is not followed:
1248-
all_module_names_list = full_module_name.split('.') # Assume put in at correct format
1249-
if all_module_names_list[0] != "HARK":
1250-
print("\nWarning: the module name does not start with 'HARK'. Instead it is: '"
1251-
+ all_module_names_list[0]+"' --please format the full namespace of the module you want. \n"
1252-
"For example, 'HARK.SolvingMicroDSOPs'")
1253-
print("\nGoodbye!")
1254-
return
1255-
1256-
# Construct the pathname to the module to copy:
1257-
my_directory_full_path = hark_core_directory_full_path
1258-
for a_directory_name in all_module_names_list[1:]:
1259-
my_directory_full_path = os.path.join(my_directory_full_path, a_directory_name)
1260-
1261-
head_path, my_module = os.path.split(my_directory_full_path)
1262-
1263-
home_directory_with_module = os.path.join(home_directory_RAW, my_module)
1264-
1265-
print("\n\n\nmy_directory_full_path:", my_directory_full_path, '\n\n\n')
1266-
1267-
# Interact with the user:
1268-
# - Ask the user for the target place to copy the directory
1269-
# - Offer use "q/y/other" option
1270-
# - Check if there is something there already
1271-
# - If so, ask if should replace
1272-
# - If not, just copy there
1273-
# - Quit
1274-
1275-
target_path = input("""You have invoked the 'replicate' process for the current module:\n """ +
1276-
my_module + """\nThe default copy location is your home directory:\n """ +
1277-
home_directory_with_module + """\nPlease enter one of the three options in single quotes below, excluding the quotes:
1278-
1279-
'q' or return/enter to quit the process
1280-
'y' to accept the default home directory: """+home_directory_with_module+"""
1281-
'n' to specify your own pathname\n\n""")
1282-
1283-
if target_path == 'n' or target_path == 'N':
1284-
target_path = input("""Please enter the full pathname to your target directory location: """)
1285-
1286-
# Clean up:
1287-
target_path = os.path.expanduser(target_path)
1288-
target_path = os.path.expandvars(target_path)
1289-
target_path = os.path.normpath(target_path)
1290-
1291-
# Check to see if they included the module name; if not add it here:
1292-
temp_head, temp_tail = os.path.split(target_path)
1293-
if temp_tail != my_module:
1294-
target_path = os.path.join(target_path, my_module)
1295-
1296-
elif target_path == 'y' or target_path == 'Y':
1297-
# Just using the default path:
1298-
target_path = home_directory_with_module
1299-
else:
1300-
# Assume "quit"
1301-
return
1302-
1303-
if target_path != 'q' and target_path != 'Q' or target_path == '':
1304-
# Run the copy command:
1305-
copy_module(target_path, my_directory_full_path, my_module)
1306-
1307-
return
1308-
1309-
if target_path != 'q' and target_path != 'Q' or target_path == '':
1310-
# Run the copy command:
1311-
copy_module(target_path, my_directory_full_path, my_module)
1312-
1313-
return
1314-
1315-
1316-
def main():
1317-
print("Sorry, HARK.core doesn't actually do anything on its own.")
1318-
print("To see some examples of its frameworks in action, try running a model module.")
1319-
print("Several interesting model modules can be found in /ConsumptionSavingModel.")
1320-
print('For an extraordinarily simple model that demonstrates the "microeconomic" and')
1321-
print('"macroeconomic" frameworks, see /FashionVictim/FashionVictimModel.')
1322-
1323-
1324-
if __name__ == '__main__':
1325-
main()

HARK/helpers.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
"""
2+
Functions for manipulating the file system or environment.
3+
"""
4+
5+
# ------------------------------------------------------------------------------
6+
# Code to copy entire modules to a local directory
7+
# ------------------------------------------------------------------------------
8+
9+
# Define a function to run the copying:
10+
def copy_module(target_path, my_directory_full_path, my_module):
11+
'''
12+
Helper function for copy_module_to_local(). Provides the actual copy
13+
functionality, with highly cautious safeguards against copying over
14+
important things.
15+
16+
Parameters
17+
----------
18+
target_path : string
19+
String, file path to target location
20+
21+
my_directory_full_path: string
22+
String, full pathname to this file's directory
23+
24+
my_module : string
25+
String, name of the module to copy
26+
27+
Returns
28+
-------
29+
none
30+
'''
31+
32+
if target_path == 'q' or target_path == 'Q':
33+
print("Goodbye!")
34+
return
35+
elif target_path == os.path.expanduser("~") or os.path.normpath(target_path) == os.path.expanduser("~"):
36+
print("You have indicated that the target location is " + target_path +
37+
" -- that is, you want to wipe out your home directory with the contents of " + my_module +
38+
". My programming does not allow me to do that.\n\nGoodbye!")
39+
return
40+
elif os.path.exists(target_path):
41+
print("There is already a file or directory at the location " + target_path +
42+
". For safety reasons this code does not overwrite existing files.\n Please remove the file at "
43+
+ target_path +
44+
" and try again.")
45+
return
46+
else:
47+
user_input = input("""You have indicated you want to copy module:\n """ + my_module
48+
+ """\nto:\n """ + target_path + """\nIs that correct? Please indicate: y / [n]\n\n""")
49+
if user_input == 'y' or user_input == 'Y':
50+
# print("copy_tree(",my_directory_full_path,",", target_path,")")
51+
copy_tree(my_directory_full_path, target_path)
52+
else:
53+
print("Goodbye!")
54+
return
55+
56+
57+
def print_helper():
58+
59+
my_directory_full_path = os.path.dirname(os.path.realpath(__file__))
60+
61+
print(my_directory_full_path)
62+
63+
64+
def copy_module_to_local(full_module_name):
65+
'''
66+
This function contains simple code to copy a submodule to a location on
67+
your hard drive, as specified by you. The purpose of this code is to provide
68+
users with a simple way to access a *copy* of code that usually sits deep in
69+
the Econ-ARK package structure, for purposes of tinkering and experimenting
70+
directly. This is meant to be a simple way to explore HARK code. To interact
71+
with the codebase under active development, please refer to the documentation
72+
under github.com/econ-ark/HARK/
73+
74+
To execute, do the following on the Python command line:
75+
76+
from HARK.core import copy_module_to_local
77+
copy_module_to_local("FULL-HARK-MODULE-NAME-HERE")
78+
79+
For example, if you want SolvingMicroDSOPs you would enter
80+
81+
from HARK.core import copy_module_to_local
82+
copy_module_to_local("HARK.SolvingMicroDSOPs")
83+
84+
'''
85+
86+
# Find a default directory -- user home directory:
87+
home_directory_RAW = os.path.expanduser("~")
88+
# Thanks to https://stackoverflow.com/a/4028943
89+
90+
# Find the directory of the HARK.core module:
91+
# my_directory_full_path = os.path.dirname(os.path.realpath(__file__))
92+
hark_core_directory_full_path = os.path.dirname(os.path.realpath(__file__))
93+
# From https://stackoverflow.com/a/5137509
94+
# Important note from that answer:
95+
# (Note that the incantation above won't work if you've already used os.chdir()
96+
# to change your current working directory,
97+
# since the value of the __file__ constant is relative to the current working directory and is not changed by an
98+
# os.chdir() call.)
99+
#
100+
# NOTE: for this specific file that I am testing, the path should be:
101+
# '/home/npalmer/anaconda3/envs/py3fresh/lib/python3.6/site-packages/HARK/SolvingMicroDSOPs/---example-file---
102+
103+
# Split out the name of the module. Break if proper format is not followed:
104+
all_module_names_list = full_module_name.split('.') # Assume put in at correct format
105+
if all_module_names_list[0] != "HARK":
106+
print("\nWarning: the module name does not start with 'HARK'. Instead it is: '"
107+
+ all_module_names_list[0]+"' --please format the full namespace of the module you want. \n"
108+
"For example, 'HARK.SolvingMicroDSOPs'")
109+
print("\nGoodbye!")
110+
return
111+
112+
# Construct the pathname to the module to copy:
113+
my_directory_full_path = hark_core_directory_full_path
114+
for a_directory_name in all_module_names_list[1:]:
115+
my_directory_full_path = os.path.join(my_directory_full_path, a_directory_name)
116+
117+
head_path, my_module = os.path.split(my_directory_full_path)
118+
119+
home_directory_with_module = os.path.join(home_directory_RAW, my_module)
120+
121+
print("\n\n\nmy_directory_full_path:", my_directory_full_path, '\n\n\n')
122+
123+
# Interact with the user:
124+
# - Ask the user for the target place to copy the directory
125+
# - Offer use "q/y/other" option
126+
# - Check if there is something there already
127+
# - If so, ask if should replace
128+
# - If not, just copy there
129+
# - Quit
130+
131+
target_path = input("""You have invoked the 'replicate' process for the current module:\n """ +
132+
my_module + """\nThe default copy location is your home directory:\n """ +
133+
home_directory_with_module + """\nPlease enter one of the three options in single quotes below, excluding the quotes:
134+
135+
'q' or return/enter to quit the process
136+
'y' to accept the default home directory: """+home_directory_with_module+"""
137+
'n' to specify your own pathname\n\n""")
138+
139+
if target_path == 'n' or target_path == 'N':
140+
target_path = input("""Please enter the full pathname to your target directory location: """)
141+
142+
# Clean up:
143+
target_path = os.path.expanduser(target_path)
144+
target_path = os.path.expandvars(target_path)
145+
target_path = os.path.normpath(target_path)
146+
147+
# Check to see if they included the module name; if not add it here:
148+
temp_head, temp_tail = os.path.split(target_path)
149+
if temp_tail != my_module:
150+
target_path = os.path.join(target_path, my_module)
151+
152+
elif target_path == 'y' or target_path == 'Y':
153+
# Just using the default path:
154+
target_path = home_directory_with_module
155+
else:
156+
# Assume "quit"
157+
return
158+
159+
if target_path != 'q' and target_path != 'Q' or target_path == '':
160+
# Run the copy command:
161+
copy_module(target_path, my_directory_full_path, my_module)
162+
163+
return
164+
165+
if target_path != 'q' and target_path != 'Q' or target_path == '':
166+
# Run the copy command:
167+
copy_module(target_path, my_directory_full_path, my_module)
168+
169+
return
170+

0 commit comments

Comments
 (0)