Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_cell(address, value):
# Active Sheet
Range(address).value = value
cell = Range(address).value
assert_equal(cell, value)
# SheetName
Range('Sheet2', address).value = value
cell = Range('Sheet2', address).value
assert_equal(cell, value)
# SheetIndex
Range(3, address).value = value
cell = Range(3, address).value
assert_equal(cell, value)
def test_range_address():
""" Style: Range('A1:C3') """
address = 'C1:E3'
# Active Sheet
Range(address[:2]).value = data # assign to starting cell only
cells = Range(address).value
assert_equal(cells, data)
# Sheetname
Range('Sheet2', address).value = data
cells = Range('Sheet2', address).value
assert_equal(cells, data)
# Sheetindex
Range(3, address).value = data
cells = Range(3, address).value
assert_equal(cells, data)
def test_dataframe():
df_expected = DataFrame({'a': [1, 2, 3.3, np.nan], 'b': ['test1', 'test2', 'test3', None]})
Range('Sheet5', 'A1').value = df_expected
cells = Range('Sheet5', 'B1:C5').value
df_result = DataFrame(cells[1:], columns=cells[0])
assert_frame_equal(df_expected, df_result)
def test_horizontal():
Range('Sheet4', 'A20').value = data
cells = Range('Sheet4', 'A20').horizontal.value
assert_equal(cells, [data[0]])
# Preallocation
price = np.zeros((num_timesteps + 1, num_simulations))
percentiles = np.zeros((num_timesteps + 1, 3))
# Set initial values
price[0,:] = starting_price
percentiles[0,:] = starting_price
# Simulation at each time step
for t in range(1, num_timesteps + 1):
rand_nums = np.random.randn(num_simulations)
price[t,:] = price[t-1,:] * np.exp((mu - 0.5 * vol**2) * dt + vol * rand_nums * np.sqrt(dt))
percentiles[t, :] = np.percentile(price[t, :], perc_selection)
if animate:
Range((t+2, 16)).value = percentiles[t, :]
Range((t+2, 19)).value = price[t, 0] # Sample path
if sys.platform.startswith('win'):
Application(wb).screen_updating = True
if not animate:
Range('P2').value = percentiles
Range('S2').value = price[:, :1] # Sample path
def main():
wb = Workbook.caller()
# User Inputs
num_simulations = Range('E3').options(numbers=int).value
time = Range('E4').value
num_timesteps = Range('E5').options(numbers=int).value
dt = time/num_timesteps # Length of time period
vol = Range('E7').value
mu = np.log(1 + Range('E6').value) # Drift
starting_price = Range('E8').value
perc_selection = [5, 50, 95] # percentiles (hardcoded for now)
# Animation
animate = Range('E9').value.lower() == 'yes'
# Excel: clear output, write out initial values of percentiles/sample path and set chart source
# and x-axis values
Range('O2').table.clear_contents()
Range('P2').value = [starting_price, starting_price, starting_price, starting_price]
Chart('Chart 5').set_source_data(Range((1, 15),(num_timesteps + 2, 19)))
Range('O2').value = np.round(np.linspace(0, time, num_timesteps + 1).reshape(-1,1), 2)
# Preallocation
price = np.zeros((num_timesteps + 1, num_simulations))
percentiles = np.zeros((num_timesteps + 1, 3))
def refresh():
"""
Refreshes the tables in Excel given the input parameters.
"""
# Connect to the Workbook
wb = Book.caller()
# Read input
start_date = Range(sheet_dashboard, 'start_date').value
end_date = Range(sheet_dashboard, 'end_date').value
account_name = Range(sheet_dashboard, 'account').value
property_name = Range(sheet_dashboard, 'property').value
profile_name = Range(sheet_dashboard, 'view').value
max_results = Range(sheet_dashboard, 'max_rows').value
# Clear Content
Range(sheet_dashboard, 'behavior').clear_contents()
Range(sheet_dashboard, 'effective').clear_contents()
# Behavior table
behavior(start_date, end_date, account_name, property_name, profile_name, max_results)
This is a wrapper around fibonacci() to handle all the Excel stuff
"""
# Create a reference to the calling Excel Workbook
wb = Workbook.caller()
# Get the input from Excel and turn into integer
n = Range('B1').options(numbers=int).value
# Call the main function
seq = fibonacci(n)
# Clear output
Range('C1').vertical.clear_contents()
# Return the output to Excel in column orientation
Range('C1').options(transpose=True).value = seq
args[i] = conversion.read(Range(impl=xlplatform.Range(xl=arg)), None, arg_info['options'])
else:
args[i] = conversion.read(None, arg, arg_info['options'])
if this_workbook:
xlplatform.BOOK_CALLER = Dispatch(this_workbook)
if func_info['async_mode'] and func_info['async_mode'] == 'threading':
cache_key = get_cache_key(func, args, caller)
cached_value = cache.get(cache_key)
if cached_value is not None: # test against None as np arrays don't have a truth value
if not is_dynamic_array: # for dynamic arrays, the cache is cleared below
del cache[cache_key]
ret = cached_value
else:
# You can't pass pywin32 objects directly to threads
xw_caller = Range(impl=xlplatform.Range(xl=caller))
thread = AsyncThread(xw_caller.sheet.book.app.pid,
xw_caller.sheet.book.name,
xw_caller.sheet.name,
xw_caller.address,
func,
args,
cache_key,
is_dynamic_array)
thread.start()
return [["#N/A waiting..." * xw_caller.columns.count] * xw_caller.rows.count]
else:
if is_dynamic_array:
cache_key = get_cache_key(func, args, caller)
cached_value = cache.get(cache_key)
if cached_value is not None:
ret = cached_value
def xl_fibonacci():
"""
This is a wrapper around fibonacci() to handle all the Excel stuff
"""
# Create a reference to the calling Excel Workbook
wb = Workbook.caller()
# Get the input from Excel and turn into integer
n = Range('B1').options(numbers=int).value
# Call the main function
seq = fibonacci(n)
# Clear output
Range('C1').vertical.clear_contents()
# Return the output to Excel in column orientation
Range('C1').options(transpose=True).value = seq