Skip to content

Commit fd066c5

Browse files
Fix Python Scripts to Support Cache Control (#78)
* fix(scripts): Fixing the component comparsion logic in Vault * fix(scripts): Fixing the component comparsion logic in Vault * Revert "fix(scripts): Fixing the component comparsion logic in Vault" This reverts commit 32f68b1. * fix(scripts): Fixing the component comparsion logic in vault * fix(scripts): Adding the cache control logic to the Python SDK * fix(scripts): Adding the cache control logic to the Python SDK * fix(scripts): Removed the unnecessary comments in the scripts
1 parent 3d0ed79 commit fd066c5

22 files changed

+157
-66
lines changed

examples/afi_optimizer_example.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ def main():
6262
afi_optimizer_strategy, afi_optimizer_output_types)
6363
afi_optimization_parameters_root = AFIOptimizationParametersRoot(
6464
data=afi_optimization_parameters)
65-
65+
# uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
66+
# Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
67+
# cache_control = "max-stale=0"
6668
afi_optimizations_api = AFIOptimizerApi(api_client)
67-
69+
6870
post_and_optimize_response = afi_optimizations_api.post_and_optimize(
6971
afi_optimization_parameters_root=afi_optimization_parameters_root)
70-
72+
# comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
73+
# post_and_optimize_response = afi_optimizations_api.post_and_optimize(
74+
# afi_optimization_parameters_root=afi_optimization_parameters_root, cache_control=cache_control)
7175
if post_and_optimize_response[1] == 201:
7276
output_optimization_result(post_and_optimize_response[0]['data'])
7377
else:

examples/axp_optimizer_example.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def main():
8181
account=axp_optimizer_account,
8282
optimization=axp_optimizer_optimization
8383
)
84+
# uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
85+
# Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
86+
# cache_control = "max-stale=0"
8487
axp_optimization_parameters_root = AxiomaEquityOptimizationParametersRoot(
8588
data=axp_optimizer_parameters)
8689

@@ -89,7 +92,10 @@ def main():
8992
post_and_optimize_response = axp_optimizations_api.post_and_optimize(
9093
axioma_equity_optimization_parameters_root=axp_optimization_parameters_root
9194
)
92-
95+
# comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
96+
# post_and_optimize_response = axp_optimizations_api.post_and_optimize(
97+
# axioma_equity_optimization_parameters_root=axp_optimization_parameters_root, cache_control=cache_control
98+
# )
9399
if post_and_optimize_response[1] == 201:
94100
output_optimization_result(post_and_optimize_response[0]['data'])
95101
else:

examples/bpm_optimizer_example.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def main():
5252
# }
5353
# }
5454
# }
55+
# uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
56+
# Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
57+
# cache_control = "max-stale=0"
5558
bpm_optimizer_strategy = BPMOptimizerStrategy(
5659
id="CLIENT:/Aapi/BPMAPISIMPLE")
5760
bpm_optimizer_trades_list = OptimizerTradesList(
@@ -67,6 +70,9 @@ def main():
6770

6871
post_and_optimize_response = bpm_optimizations_api.post_and_optimize(
6972
bpm_optimization_parameters_root=bpm_optimization_parameters_root)
73+
# comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
74+
# post_and_optimize_response = bpm_optimizations_api.post_and_optimize(
75+
# bpm_optimization_parameters_root=bpm_optimization_parameters_root, cache_control=cache_control)
7076

7177
if post_and_optimize_response[1] == 201:
7278
output_optimization_result(post_and_optimize_response[0]['data'])

examples/fi_example.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ def main():
7070
discount_curve = "UST"
7171
)
7272

73+
# uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
74+
# Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
75+
# cache_control = "max-stale=0"
76+
7377
securities = [security1, security2]
7478

7579
jobSettings = FIJobSettings(as_of_date="20201201",
@@ -82,7 +86,9 @@ def main():
8286
fi_calculations_api = FICalculationsApi(api_client)
8387
run_calculation_response = fi_calculations_api.post_and_calculate(
8488
fi_calculation_parameters_root=fi_calculation_parameters_root)
85-
89+
# comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
90+
# run_calculation_response = fi_calculations_api.post_and_calculate(
91+
# fi_calculation_parameters_root=fi_calculation_parameters_root, cache_control=cache_control)
8692
if run_calculation_response[1] != 202 and run_calculation_response[1] != 201:
8793
print_error(run_calculation_response)
8894
sys.exit()

examples/fpo_optimizer_example.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ def main():
6363
# }
6464
# }
6565
# }
66+
# uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
67+
# Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
68+
# cache_control = "max-stale=0"
6669
fpo_optimizer_strategy = OptimizerStrategy(
6770
id="Client:/analytics_api/dbui_simple_strategy")
6871
fpo_pa_doc = PaDoc("CLIENT:/FPO/FPO_MASTER")
@@ -90,7 +93,10 @@ def main():
9093
post_and_optimize_response = fpo_optimizations_api.post_and_optimize(
9194
fpo_optimization_parameters_root=fpo_optimization_parameters_root
9295
)
93-
96+
# comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
97+
# post_and_optimize_response = fpo_optimizations_api.post_and_optimize(
98+
# fpo_optimization_parameters_root=fpo_optimization_parameters_root, cache_control=cache_control
99+
# )
94100
if post_and_optimize_response[1] == 201:
95101
output_optimization_result(post_and_optimize_response[0]['data'])
96102
else:

examples/pa_engine_multiple_unit_example.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
host = "https://api.factset.com"
2222
username = "<username-serial>"
23-
password = "<apikey>"
23+
password = "<apiKey>"
2424

2525

2626
def main():
@@ -45,18 +45,20 @@ def main():
4545
pa_document_name = "PA_DOCUMENTS:DEFAULT"
4646
pa_component_name = "Weights"
4747
pa_component_category = "Weights / Exposures"
48-
pa_benchmark_sp_50 = "BENCH:SP50"
49-
pa_benchmark_r_1000 = "BENCH:R.1000"
48+
portfolio = "BENCH:SP50"
49+
benchmark = "BENCH:R.1000"
5050
startdate = "20180101"
5151
enddate = "20181231"
5252
frequency = "Monthly"
53-
53+
# uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
54+
# Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
55+
# cache_control = "max-stale=0"
5456
get_components_response = components_api.get_pa_components(document=pa_document_name)
5557
component_id = [id for id in list(
5658
get_components_response[0].data.keys()) if get_components_response[0].data[id].name == pa_component_name and get_components_response[0].data[id].category == pa_component_category][0]
5759
print("PA Component Id: " + component_id)
58-
pa_accounts = [PAIdentifier(id=pa_benchmark_sp_50)]
59-
pa_benchmarks = [PAIdentifier(id=pa_benchmark_r_1000)]
60+
pa_accounts = [PAIdentifier(id=portfolio)]
61+
pa_benchmarks = [PAIdentifier(id=benchmark)]
6062
pa_dates = PADateParameters(
6163
startdate=startdate, enddate=enddate, frequency=frequency)
6264

@@ -72,7 +74,8 @@ def main():
7274

7375
post_and_calculate_response = pa_calculations_api.post_and_calculate(
7476
pa_calculation_parameters_root=pa_calculation_parameter_root)
75-
77+
# comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
78+
# post_and_calculate_response = pa_calculations_api.post_and_calculate(pa_calculation_parameters_root=pa_calculation_parameter_root, cache_control=cache_control)
7679
if post_and_calculate_response[1] == 202 or post_and_calculate_response[1] == 200:
7780
calculation_id = post_and_calculate_response[0].data.calculationid
7881
print("Calculation Id: " + calculation_id)

examples/pa_engine_single_unit_example.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
host = "https://api.factset.com"
2222
username = "<username-serial>"
23-
password = "<apikey>"
23+
password = "<apiKey>"
2424

2525
def main():
2626
config = Configuration()
@@ -44,18 +44,20 @@ def main():
4444
pa_document_name = "PA_DOCUMENTS:DEFAULT"
4545
pa_component_name = "Weights"
4646
pa_component_category = "Weights / Exposures"
47-
pa_benchmark_sp_50 = "BENCH:SP50"
48-
pa_benchmark_r_1000 = "BENCH:R.1000"
47+
portfolio = "BENCH:SP50"
48+
benchmark = "BENCH:R.1000"
4949
startdate = "20180101"
5050
enddate = "20181231"
5151
frequency = "Monthly"
52-
52+
# uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
53+
# Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
54+
# cache_control = "max-stale=0"
5355
get_components_response = components_api.get_pa_components(document=pa_document_name)
5456
component_id = [id for id in list(
5557
get_components_response[0].data.keys()) if get_components_response[0].data[id].name == pa_component_name and get_components_response[0].data[id].category == pa_component_category][0]
5658
print("PA Component Id: " + component_id)
57-
pa_accounts = [PAIdentifier(id=pa_benchmark_sp_50)]
58-
pa_benchmarks = [PAIdentifier(id=pa_benchmark_r_1000)]
59+
pa_accounts = [PAIdentifier(id=portfolio)]
60+
pa_benchmarks = [PAIdentifier(id=benchmark)]
5961
pa_dates = PADateParameters(
6062
startdate=startdate, enddate=enddate, frequency=frequency)
6163

@@ -69,7 +71,8 @@ def main():
6971

7072
post_and_calculate_response = pa_calculations_api.post_and_calculate(
7173
pa_calculation_parameters_root=pa_calculation_parameter_root)
72-
74+
# comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
75+
# post_and_calculate_response = pa_calculations_api.post_and_calculate(pa_calculation_parameters_root=pa_calculation_parameter_root, cache_control=cache_control)
7376
if post_and_calculate_response[1] == 201:
7477
output_calculation_result(post_and_calculate_response[0]['data'])
7578
elif post_and_calculate_response[1] == 200:

examples/pub_engine_multiple_unit_example.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def main():
3939
pub_account_id = "BENCH:SP50"
4040
startdate = "-1M"
4141
enddate = "0M"
42-
42+
# uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
43+
# Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
44+
# cache_control = "max-stale=0"
4345
pub_account_identifier = PubIdentifier(pub_account_id)
4446
pub_dates = PubDateParameters(enddate, startdate=startdate)
4547

@@ -54,7 +56,8 @@ def main():
5456
pub_calculations_api = PubCalculationsApi(api_client)
5557
post_and_calculate_response = pub_calculations_api.post_and_calculate(
5658
pub_calculation_parameters_root=pub_calculation_parameters_root)
57-
59+
# comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
60+
# post_and_calculate_response = pub_calculations_api.post_and_calculate(pub_calculation_parameters_root=pub_calculation_parameters_root, cache_control=cache_control)
5861
if post_and_calculate_response[1] == 202 or post_and_calculate_response[1] == 200:
5962
calculation_id = post_and_calculate_response[0].data.calculationid
6063
print("Calculation Id: " + calculation_id)

examples/pub_engine_single_unit_example.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def main():
3939
pub_account_id = "BENCH:SP50"
4040
startdate = "-1M"
4141
enddate = "0M"
42-
42+
# uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
43+
# Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
44+
# cache_control = "max-stale=0"
4345
pub_account_identifier = PubIdentifier(pub_account_id)
4446
pub_dates = PubDateParameters(enddate, startdate=startdate)
4547

@@ -52,8 +54,9 @@ def main():
5254

5355
pub_calculations_api = PubCalculationsApi(api_client)
5456
post_and_calculate_response = pub_calculations_api.post_and_calculate(
55-
pub_calculation_parameters_root=pub_calculation_parameters_root)
56-
57+
pub_calculation_parameters_root=pub_calculation_parameters_root)
58+
# comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
59+
# post_and_calculate_response = pub_calculations_api.post_and_calculate(pub_calculation_parameters_root=pub_calculation_parameters_root, cache_control=cache_control)
5760
if post_and_calculate_response[1] == 201:
5861
output_calculation_result(
5962
"single_unit", (post_and_calculate_response[0].read()))

examples/quant_engine_single_unit_feather_example_basic.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def main():
5353
expr="P_PRICE(#DATE,#DATE,#FREQ)", name="Price (fql)")
5454
fqlExpression2 = QuantFqlExpression(source="FqlExpression",
5555
expr="OS_TOP_HLDR_POS(3,#DATE,#DATE,M,,S,SEC)", name="Top 3 Pos (fql)")
56-
56+
# uncomment the below code line to setup cache control; max-stale=0 will be a fresh adhoc run and the max-stale value is in seconds.
57+
# Results are by default cached for 12 hours; Setting max-stale=300 will fetch a cached result which is 5 minutes older.
58+
# cache_control = "max-stale=0"
5759
quant_calculation_parameters = {"1": QuantCalculationParameters(
5860
universe=screeningExpressionUniverse,
5961
dates=fdsDate,
@@ -69,6 +71,9 @@ def main():
6971

7072
post_and_calculate_response = quant_calculations_api.post_and_calculate(
7173
quant_calculation_parameters_root=quant_calculation_parameter_root)
74+
# comment the above line and uncomment the below line to run the request with the cache_control header defined earlier
75+
# post_and_calculate_response = quant_calculations_api.post_and_calculate(
76+
# quant_calculation_parameters_root=quant_calculation_parameter_root, cache_control=cache_control)
7277

7378
if post_and_calculate_response[1] == 201:
7479
output_calculation_result('data', post_and_calculate_response[0])

0 commit comments

Comments
 (0)