'int' object is not subscriptable (function calling error) - python

I'm attempting to break a vigenere cipher without knowing the key and I'm struggling to figure out my error since the caesar_break function seems to work perfectly fine on its own.
This is the code for the function i'm attempting to call:
def caesar_break( cipher, frequencies ): # help!
alpha = ["A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" , "M" ,
"N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" , "Y" , "Z" ]
dist_ls = []
for key in alpha:
poss_plaintxt = caesar_dec( cipher, key )
counts = letter_counts( poss_plaintxt )
observed = normalize( counts )
dist = distance( observed, frequencies )
dist_ls.append( (dist , poss_plaintxt , key) )
can = dist_ls[ 0 ][ 0 ]
can_t = 0
for t in dist_ls:
if t[ 0 ] < can:
can = t[ 0 ]
can_t = t
return [ can_t[ 2 ], can_t[ 1 ] ]
This is what I have so far for my current function, I am not completely done with it, but I just need to figure out this error in order to move forward:
def vig_break( c, maxlen, frequencies ):
from i in range( 1, maxlen ):
break_ls = list(vig_break_for_length( c, i, frequencies ))
print( break_ls ) #this print is unneeded, i just like to test my code as i go
For specificity this is the error code:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ciphers.py", line 310, in vig_break
break_ls = list(vig_break_for_length( c, i, frequencies ))
File "ciphers.py", line 288, in vig_break_for_length
split_break = caesar_break( ciphertext, frequencies )
File "ciphers.py", line 192, in caesar_break
return [ can_t[ 2 ], can_t[ 1 ] ]
TypeError: 'int' object is not subscriptable

Related

Converting PL/SQL procedures to Pyspark

BEGIN
open v_refcur for
SELECT A.LGCY_LNDR_NO
, A.LGCY_LNDR_BR_NO
, A.LNDR_NM
, B.ADDR_LINE1_TXT
, B.ADDR_LINE2_TXT
, B.CITY_NM
, B.ST_CD
, B.POSTAL_CD
, C.FAX_NO
FROM LNDR_CUST_XREF A
LEFT OUTER JOIN LNDR_CUST_ADDR B
ON A.LNDR_ID = B.LNDR_ID
AND B.ADDR_TYP_CD = 'MAIL'
LEFT OUTER JOIN LNDR_CUST_ADDR C
ON A.LNDR_ID = C.LNDR_ID
AND C.ADDR_TYP_CD = 'SITE'
WHERE A.LGCY_LNDR_NO = LNDR_NO
AND A.LGCY_LNDR_BR_NO = BRN_NO
AND A.TA_CUST_FLG = 'Y';
SQL_CD := SWV_SQLCODE;
END;
What will be the line by line conversion of this above code? I dont have the databases in-hand, so what would be the most appropriate gist of the PL/SQL code in Pyspark?
this statement can be re-written something like below -
df = (df_LNDR_CUST_XREF.alias('A')
.df_LNDR_CUST_ADDR.alias('B'), ((A.LNDR_ID == B.LNDR_ID) & (B.ADDR_TYP_CD == 'MAIL'), "left")
.df_LNDR_CUST_ADDR.alias('C'), ((A.LNDR_ID == C.LNDR_ID) & (C.ADDR_TYP_CD == 'SITE'), "left")
.where ((A.LGCY_LNDR_NO == LNDR_NO) & (A.LGCY_LNDR_BR_NO == BRN_NO) & (A.TA_CUST_FLG == 'Y'))
.select (F.col("A.LGCY_LNDR_NO")
, F.col("A.LGCY_LNDR_BR_NO")
, F.col("A.LNDR_NM")
, F.col("B.ADDR_LINE1_TXT")
, F.col("B.ADDR_LINE2_TXT")
, F.col("B.CITY_NM")
, F.col("B.ST_CD")
, F.col("B.POSTAL_CD")
, F.col("C.FAX_NO"))
)
I havent tested it though.

Differential evolution in SciPy

I am trying to use differential_evolution from SciPy. I have three matrices: x, y and P - all of size (14,6). I have to use the following formula:
z= np.log10(g)+ np.log10(c)*np.log10(P)
to find the value of c (real number from 0 to 2) which minimize:
numpy.median(z**2)
this expression. What I try is this (I provide the data for the convenience):
import numpy as np
from scipy.optimize import differential_evolution
def func(c, args):
z = args[0] + np.log10(c)*np.log10(args[1])
return np.median(z**2)
if __name__ == '__main__':
bounds = [(0, 2)]
x = np.array([[126581.94951205, 97601.85624482, 59659.00330833,
27646.48551627, 9202.50377458, 4840.25789068],
[213571.84886437, 148750.52154776, 85979.81139937,
38757.37831212, 11775.99906427, 4619.32027948],
[195684.50299021, 131818.78542437, 74376.55189913,
32793.21715377, 10288.70838873, 4042.58093119],
[177598.13865746, 120942.50439911, 68866.09898276,
30819.5354775 , 10588.08746517, 5011.71808947],
[126433.18311483, 85863.57788065, 48923.64502157,
21828.60950911, 7907.37639781, 4410.61819399],
[103431.88029629, 67452.94418262, 37608.36861047,
16456.97701443, 6027.98704858, 3550.06927169],
[100689.06813945, 64380.21348052, 34764.02910376,
14849.85472635, 5607.19256065, 3605.5709208 ],
[ 96509.22946744, 63832.74512518, 36041.69174706,
15802.87650901, 6473.33232805, 4664.07058733],
[113078.63455882, 73227.02362359, 40861.09037499,
17385.89127848, 7074.98444924, 5136.84232454],
[121241.93118924, 78537.13681709, 44257.97654994,
18584.94999742, 7733.39219718, 5869.49536788],
[115948.06368262, 73995.07204278, 41536.21315507,
16851.59724901, 6736.25125909, 4851.5738275 ],
[115024.20359423, 72108.15245783, 40341.98473413,
15900.55422399, 6243.63777265, 4411.24859372],
[108754.83802899, 66210.25952459, 36485.42905112,
14577.73925124, 5553.23702141, 3736.5217322 ],
[ 95340.59125024, 58458.97552915, 32364.19705748,
13236.30114676, 4929.04023171, 3202.21731277]])
y = y=np.array([[118166.08 , 95784.692 , 68134.878 , 37119.959 , 17924.157 ,
7445.3083],
[ 99265.027 , 70679.135 , 43297.559 , 19822.017 , 8527.8497,
3404.7113],
[ 80227.797 , 50972.879 , 26648.604 , 11190.488 , 4836.6514,
2249.9172],
[ 68510.582 , 39288.19 , 19938.938 , 9312.6881, 4907.6661,
2681.2709],
[ 65193.15 , 36610.107 , 18612.181 , 9211.144 , 5416.1685,
3372.1282],
[ 67188.918 , 37227.699 , 20132.92 , 11663.275 , 7315.3472,
4648.1669],
[ 64802.06 , 38885.622 , 22008.537 , 13100.638 , 8043.0185,
5049.2097],
[ 68104.867 , 41212.89 , 23247.898 , 14134.707 , 8805.2547,
5526.1014],
[ 74180.595 , 41268.904 , 22868.016 , 13841.437 , 8660.1413,
5401.245 ],
[ 78920.685 , 42743.389 , 23932.305 , 13910.089 , 8439.3342,
5141.7051],
[ 91329.012 , 45733.772 , 25430.818 , 14144.185 , 8273.7953,
5016.5839],
[ 92217.594 , 44984.3 , 23353.596 , 13467.631 , 8099.728 ,
4948.26 ],
[ 94508.441 , 48114.879 , 24735.311 , 13358.097 , 7821.8587,
4806.7923],
[108211.73 , 53987.095 , 25872.772 , 13189.61 , 7552.7164,
4497.2611]])
P=10000*np.array([[0.6011,0.6011,0.6011,0.6011,0.6011,0.6011],
[0.9007,0.9007,0.9007,0.9007,0.9007,0.9007],
[1.1968,1.1968,1.1968,1.1968,1.1968,1.1968],
[1.4178,1.4178,1.4178,1.4178,1.4178,1.4178],
[1.5015,1.5015,1.5015,1.5015,1.5015,1.5015],
[1.439,1.439,1.439,1.439,1.439,1.439],
[1.2721,1.2721,1.2721,1.2721,1.2721,1.2721],
[1.0616,1.0616,1.0616,1.0616,1.0616,1.0616],
[0.8543,0.8543,0.8543,0.8543,0.8543,0.8543],
[0.6723,0.6723,0.6723,0.6723,0.6723,0.6723],
[0.5204,0.5204,0.5204,0.5204,0.5204,0.5204],
[0.3963,0.3963,0.3963,0.3963,0.3963,0.3963],
[0.2990,0.2990,0.2990,0.2990,0.2990,0.2990],
[0.2211,0.2211,0.2211,0.2211,0.2211,0.2211]])
g=np.log10(y) - np.log10(x)
args = (g,P)
result = differential_evolution(func, bounds, args=args)
print(func(bounds, args))
I get this error: TypeError: func() takes exactly 2 arguments (3 given) . Is there any way to fix this?
def func(c, g, P):
z = g + np.log10(c)*np.log10(P)
return np.median(z**2)
if __name__ == '__main__':
# Your arrays go here
g = np.log10(y) - np.log10(x)
args = (g, P)
result = differential_evolution(func, bounds, args=(g, P))
# will print the value of c and value of the optimized function
print (result.x, result.fun)

python 3.x need help spliting 1 text file into 4 dictionaries

Can anyone help me with an issue i am having? i am trying to wright some code that will let a user select specific files called diarys then compare it to there current skill levels, each file has a easy, medium, hard, and elite section that i want to put in separate dictionary's, i can get it to print the correct info from the whole file but i cant figure out how to split it up into 4 separate dictionaries
# shows all osrs diarys
def diary_selection():
diary_options = {
0 : 'ardougne', 1 : 'desert', 2 : 'falador', 3 : 'fremennik', 4 : 'kandarin',
5 : 'lumbridge', 6 : 'morytania', 7 : 'varrock', 8 : 'western', 9 : 'wilderness'
}
print(diary_options)
# if not a correct number gives prompt to retry
while True:
try:
d_sel = int(input ("\nType in the number next to the diary: "))
diary_select = d_sel
if not (0 < diary_select > 9):
print('option is valid')
break
else:
print(" invalid option.")
diary_selection()
except ValueError:
print(" invalid option, numbers only, please try again.")
# gets the name of the diary acording to what number was pressed
current_diary = diary_options.get(diary_select)
#creats a filename for spicific diary
diary_file_name = str((current_diary + "_diary.txt"))
print (diary_file_name,"\n")
#searches for file name in folder ./diary_requirements
f = open("./diary_requirements/"+diary_file_name,"r")
file_contents = f.read()
return file_contents
the files i am trying to manipulate are structured inside a txt file in this format with easy,medium,hard,elite levels.
easy_levels = {
"Attack" : 0
, "Defense" : 0
, "Strength" : 0
, "Hitpoints": 0
, "Range" : 30
, "Prayer" : 0
, "Magic" : 0
, "Cooking" : 0
, "Woodcutting" : 0
, "Fletching" : 20
, "Fishing" : 0
, "Firemaking" : 0
, "Crafting" : 0
, "Smithing" : 0
, "Mining" : 15
, "Herblore" : 0
, "Agility" : 0
, "Thieving" : 0
, "Slayer" : 0
, "Farming" : 0
, "Runecrafting" : 0
, "Hunting": 9
, "Construction" : 0
,
}
medium_levels = {
"Attack" : 0
, "Defense" : 0
, "Strength" : 0
, "Hitpoints": 0
, "Range" : 30
, "Prayer" : 0
, "Magic" : 0
, "Cooking" : 42
, "Woodcutting" : 35
, "Fletching" : 5
, "Fishing" : 46
, "Firemaking" : 35
, "Crafting" : 0
, "Smithing" : 0
, "Mining" : 40
, "Herblore" : 0
, "Agility" : 37
, "Thieving" : 0
, "Slayer" : 0
, "Farming" : 0
, "Runecrafting" : 0
, "Hunting": 31
, "Construction" : 0
,
}
I am guessing the only part you struggle is how to fill out four dictionaries given a file with the structure you described.
If you are sure that those files will not be altered by anything/anyone other than you, and you are okay with using unsafe & dirty code, you can just do:
exec(file_contents)
What this does is, since the content of your file structure is already valid python, it executes it in the scope it's been called. So after executing it, you have access to variables easy_levels, medium_levels, hard_levels, elite_levels in the scope you called it. Please note that this assumes you have those variables correctly defined in any diary you try to access, if the variables defined per diary may change, you should use a safer method (or an ugly quick hack accessing locals()).
Try this:
import os
import imp
from pprint import pprint
# shows all osrs diarys
def diary_selection():
diary_options = {
0 : 'ardougne', 1 : 'desert', 2 : 'falador', 3 : 'fremennik', 4 : 'kandarin',
5 : 'lumbridge', 6 : 'morytania', 7 : 'varrock', 8 : 'western', 9 : 'wilderness'
}
print(diary_options)
# if not a correct number gives prompt to retry
while True:
try:
d_sel = int(input ("\nType in the number next to the diary: "))
diary_select = d_sel
if diary_select in diary_options:
print('option is valid')
break
else:
print(" invalid option.")
#diary_selection()
except ValueError:
print(" invalid option, numbers only, please try again.")
# gets the name of the diary acording to what number was pressed
current_diary = diary_options.get(diary_select)
#creats a filename for spicific diary
diary_file_name = str((current_diary + "_diary.txt"))
print (diary_file_name,"\n")
#searches for file name in folder ./diary_requirements
#f = open("./diary_requirements/"+diary_file_name,"r")
#file_contents = f.read()
#return file_contents
foo = imp.load_source('userInfo', os.getcwd() + '/diary_requirements/' + diary_file_name)
print('{}\nEasy levels\n{}'.format('-'*40, '-'*40))
pprint(foo.easy_levels)
print('{}\nMediyum levels\n{}'.format('-'*40, '-'*40))
pprint(foo.medium_levels)
diary_selection()
Output (python ):
{0: 'ardougne', 1: 'desert', 2: 'falador', 3: 'fremennik', 4: 'kandarin', 5: 'lumbridge', 6: 'morytania', 7: 'varrock', 8: 'western', 9: 'wilderness'}
Type in the number next to the diary: 6
option is valid
morytania_diary.txt
----------------------------------------
Easy levels
----------------------------------------
{'Agility': 0,
'Attack': 0,
'Construction': 0,
'Cooking': 0,
'Crafting': 0,
'Defense': 0,
'Farming': 0,
'Firemaking': 0,
'Fishing': 0,
'Fletching': 20,
'Herblore': 0,
'Hitpoints': 0,
'Hunting': 9,
'Magic': 0,
'Mining': 15,
'Prayer': 0,
'Range': 30,
'Runecrafting': 0,
'Slayer': 0,
'Smithing': 0,
'Strength': 0,
'Thieving': 0,
'Woodcutting': 0}
----------------------------------------
Mediyum levels
----------------------------------------
{'Agility': 37,
'Attack': 0,
'Construction': 0,
'Cooking': 42,
'Crafting': 0,
'Defense': 0,
'Farming': 0,
'Firemaking': 35,
'Fishing': 46,
'Fletching': 5,
'Herblore': 0,
'Hitpoints': 0,
'Hunting': 31,
'Magic': 0,
'Mining': 40,
'Prayer': 0,
'Range': 30,
'Runecrafting': 0,
'Slayer': 0,
'Smithing': 0,
'Strength': 0,
'Thieving': 0,
'Woodcutting': 35}

Can JModelica print results directly to file?

I am running the following JModelica script:
#!/usr/local/jmodelica/bin/jm_python.sh
import pyjmi
op = pyjmi.transfer_optimization_problem("BatchReactor", "model.mop")
opt_opts = op.optimize_options()
opt_opts['n_e'] = 40 # Number of elements
opt_opts['IPOPT_options']['tol'] = 1e-10
opt_opts['IPOPT_options']['print_level'] = 8
opt_opts['IPOPT_options']['output_file'] = '/z/out'
res = op.optimize(options=opt_opts)
I had hoped that the results (e.g. time, x1, x2, &c.) would be printed to the file /z/out. But the file only contains IPOPT verbose debugging/status info.
Is there a way to print the information that would be stored in res directly to a file? Either by somehow writing res itself or, preferably, having IPOPT/JModelica write the results without having to go through Python?
There is a way to print the information directly to a file. The following accomplishes this. Note that result_file_name is the key to making this happen.
#!/usr/local/jmodelica/bin/jm_python.sh
import pyjmi
op = pyjmi.transfer_optimization_problem("BatchReactor", "model.mop")
opt_opts = op.optimize_options()
opt_opts['n_e'] = 40 # Number of elements
opt_opts['result_file_name'] = '/z/out'
opt_opts['IPOPT_options']['tol'] = 1e-10
opt_opts['IPOPT_options']['print_level'] = 0
res = op.optimize(options=opt_opts)
Unfortunately, the contents of the file are somewhat mysterious.
You may find that using result_file_name per another answer here results in an output file which is difficult to understand.
The following produces a nicer format:
import StringIO
import numpy as np
def PrintResToFile(filename,result):
def StripMX(x):
return str(x).replace('MX(','').replace(')','')
varstr = '#Variable Name={name: <10}, Unit={unit: <7}, Val={val: <10}, Col={col:< 5}, Comment="{comment}"\n'
with open(filename,'w') as fout:
#Print all variables at the top of the file, along with relevant information
#about them.
for var in result.model.getAllVariables():
if not result.is_variable(var.getName()):
val = result.initial(var.getName())
col = -1
else:
val = "Varies"
col = result.get_column(var.getName())
unit = StripMX(var.getUnit())
if not unit:
unit = "X"
fout.write(varstr.format(
name = var.getName(),
unit = unit,
val = val,
col = col,
comment = StripMX(var.getAttribute('comment'))
))
#Ensure that time variable is printed
fout.write(varstr.format(
name = 'time',
unit = 's',
val = 'Varies',
col = 0,
comment = 'None'
))
#The data matrix contains only time-varying variables. So fetch all of
#these, couple them in tuples with their column number, sort by column
#number, and then extract the name of the variable again. This results in a
#list of variable names which are guaranteed to be in the same order as the
#data matrix.
vkeys_in_order = map(lambda x: x[1], sorted([(result.get_column(x),x) for x in result.keys() if result.is_variable(x)]))
for vk in vkeys_in_order:
fout.write("{0:>13},".format(vk))
fout.write("\n")
sio = StringIO.StringIO()
np.savetxt(sio, result.data_matrix, delimiter=',', fmt='%13.5f')
fout.write(sio.getvalue())
which looks like this:
#Variable Name=S0 , Unit=kg , Val=2.0 , Col=-1 , Comment="Solid Mass"
#Variable Name=F0 , Unit=kg , Val=0.0 , Col=-1 , Comment="Fluid Mass"
#Variable Name=a , Unit=Hz , Val=0.2 , Col=-1 , Comment="None"
#Variable Name=b , Unit=kg/s , Val=1.0 , Col=-1 , Comment="None"
#Variable Name=f , Unit=kg/s , Val=0.05 , Col=-1 , Comment="None"
#Variable Name=h , Unit=1/g , Val=0.05 , Col=-1 , Comment="None"
#Variable Name=der(F) , Unit=X , Val=Varies , Col= 1 , Comment="None"
#Variable Name=F , Unit=kg , Val=Varies , Col= 3 , Comment="None"
#Variable Name=der(S) , Unit=X , Val=Varies , Col= 2 , Comment="None"
#Variable Name=S , Unit=kg , Val=Varies , Col= 4 , Comment="None"
#Variable Name=u , Unit=X , Val=Varies , Col= 5 , Comment="None"
#Variable Name=startTime , Unit=X , Val=0.0 , Col=-1 , Comment="None"
#Variable Name=finalTime , Unit=X , Val=100.0 , Col=-1 , Comment="None"
#Variable Name=time , Unit=s , Val=Varies , Col= 0 , Comment="None"
time, der(F), der(S), F, S, u,
0.00000, 0.97097, -0.97097, 0.00000, 2.00000, 0.97097
0.38763, 1.07704, -1.05814, 0.38519, 1.61698, 1.00000
1.61237, 0.88350, -0.80485, 1.70714, 0.35885, 0.65862
2.50000, 0.00000, 0.09688, 2.14545, 0.00000, 0.00000
2.88763, 0.09842, -0.00000, 2.18330, 0.00000, 0.06851
4.11237, 0.10342, 0.00000, 2.30688, 0.00000, 0.07077
5.00000, 0.10716, 0.00000, 2.40033, 0.00000, 0.07240
5.38763, 0.10882, -0.00000, 2.44219, 0.00000, 0.07311
6.61237, 0.11421, 0.00000, 2.57875, 0.00000, 0.07535

Array only filling last value

I'm having a problem with my array only filling the last space with the calculated value. My code is below:
c_volume = 4.45e-5
c_flow_rate = 1.67e-6
acr = c_flow_rate/c_volume
t1 = [3600.0,18000.0, 36000.0]
air_conc_t1 = [6.42404968e+02, 2.74977722e+02, 1.45282562e+02]
t2 = [7200.0, 21600.0, 39600.0]
air_conc_t2 = [4.53346985e+02, 2.41359268e+02, 1.28038071e+02]
===============================================================
n_calc = np.zeros((len(t1),1), dtype='f')
def n(t1, air_conc_t1, t2, air_conc_t2):
return (1/(t2-t1))*(np.log(air_conc_t1/air_conc_t2))
for i in range(len(t1)):
n_calc[i] = n(t1[i], air_conc_t1[i], t2[i], air_conc_t2[i])
===============================================================
calc_f1 = np.zeros((len(t1),1), dtype='f')
calc_f2 = np.zeros((len(t1),1), dtype='f')
calc_N = np.zeros((len(t1),1), dtype='f')
def f1(acr, n_calc):
return (acr+n_calc)/n_calc
calc_f1[i] = f1(acr, n_calc[i])
def f2(acr, n_calc):
return (acr-n_calc)/n_calc
calc_f2[i] = f2(acr, n_calc[i])
def N(R, calc_root, m_thickness, calc_f1, calc_f2):
return (2*R*np.tan(calc_root*m_thickness))/(calc_root*m_thickness*
(calc_f1+calc_f2*calc_root*m_thickness*((1/np.tan(calc_root\
*m_thickness))+np.tan(calc_root*m_thickness))))
for i in xrange(len(t1)):
calc_N[i] = N(R, calc_root[i], m_thickness, calc_f1[i], calc_f2[i])
print calc_f1
print calc_f2
print calc_N
I'm getting the following printed:
[[ 0. ]
[ 0. ]
[ 1070.23657227]]
[[ 0. ]
[ 0. ]
[ 1068.2364502]]
[[ inf]
[ inf]
[ 3.55326119e-06]]
I'm not sure why the first two values of the array are not being filled, but the last one is. I've calculated them by hand and get values.
I'm very new to programming so any help would be appreciated.
Thanks,
Shane
your calls to f1 and f2 are not in a loop. Note that everything at the same level of indentation is included in the loop.
try this instead:
for i in xrange(len(t1)):
calc_f1[i] = f1(acr, n_calc[i])
calc_f2[i] = f2(acr, n_calc[i])
calc_N[i] = N(R, calc_root[i], m_thickness, calc_f1[i], calc_f2[i])
print calc_f1
print calc_f2
print calc_N
Lines
calc_f1[i] = f1(acr, n_calc[i])
calc_f2[i] = f2(acr, n_calc[i])
are outside any for loop (maybe it is only problem with indentions).

Categories

Resources