I have the following string: ['', '+VZWRSRP: 64,6300,-101.70', '', 'OK', '']
Where I try to put everything after the second comma into the variable PCI, everything after the 3rd comma into variable earfcn and everything after the 4th comman into variable RSRP.
As start I wanted to test it with RSRP and the following regex command:
cellinfo = ['', '+VZWRSRP: 64,6300,-101.70', '', 'OK', '']
rsrp = re.search('-(.+?)\'', cellinfo)
But somehow I can't get it working.
What's a good solution to achieve this?
I missed that the question is specified for micropython, I haven't worked with that, this answer works in normal python
import re
input_string = ", '+VZWRSRP: 64,6300,-101.70', '', 'OK', "
m = re.search(',.*?:(.*?),(.*?),(.*?),.*?,', input_string)
PCL = m.group(1)
earfcn = m.group(2)
RSRP = m.group(3)
returns:
PCL = 64
earfcn = 6300
RSRP = -101.70'
If you want the output to consist only out of values that could be translated to integers or floats:
part = ".*?(-*\d+\.*\d*).*?"
m = re.search(',.*?:{},{},{},.*?,'.format(part,part,part), input_string)
Will do the trick.
If your string is '+VZWRSRP: 64,6300,-101.70', use
part = ".*?(-*\d+\.*\d*).*?"
m = re.search('.*?:{},{},{}'.format(part,part,part), input_string)
Related
Hi I am trying to create string which will have words and its position as it appear in the input string. I am able to do it in python using below code -
from collections import defaultdict
import re
s = 'Create a string with position from a string a'
wp = defaultdict(list)
for n, k in enumerate(s.split()):
wp[k].append(n+1)
raw_output = re.search('{(.*)}', str(wp)).group(1).replace('[','').replace(']','')
final_output = re.sub("(\d), '", r"\1 '", raw_output)
And output is
"'Create': 1 'a': 2, 7, 9 'string': 3, 8 'with': 4 'position': 5 'from': 6"
How can I do the same in pyspark?
Pyspark has few additional concepts you might need to revisit, using RDD apis is the best
for your problem statement.
Here is a code snippet that should work for you.
def positional_encoder(sentence):
words=sentence.split(" ")
indexes=list(range(0,len(words)))
return list(zip(words,indexes))
data_rdd = sc.parallelize(["Create a string with position from a string a"])
words_index=data_rdd.map(lambda sentence: positional_encoder(sentence))
## Just for debugging:
words_index.collect() ## Remove this after debugging
how to read broken numbers on two lines in matlab?
I am generating some results in text files that are being broken into two lines. Example:
text x = 1.
2345 text
What would a code look like to read the value x = 1.2345?
Suppose the value of x = 1.2345 is in file named name.txt.
When it doesn't break the values I'm looking for:
text x = 1.2345 text
I use the following (working) code:
buffer = fileread('name.txt') ;
search = 'x = ' ;
local = strfind(buffer, search);
xvalue = sscanf(buffer(local(1,1)+numel(search):end), '%f', 1);
You can remove line breaks (and other "white space", if needed) before parsing the string:
>> str = sprintf('text x = 1.\n2345 text')
str =
'text x = 1.
2345 text'
>> str = regexprep(str, '\n', '')
str =
'text x = 1.2345 text'
Im trying to follow an exercise on calculating the maximum drawdown and maximum drawdown duration of a market market neutral vs a long-only trading strategy.
I followed the code to the T and has worked perfectly up until now, and I seem to be getting a ValueError Exception. What code do I need to change for my code to work?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from MaxDD_Function import calculateMaxDD
# CALCUALTING MAXDD AND CREATING THE FUNCTION.
def calculateMaxDD(cumret):
highwatermark = np.zeros(cumret.shape)
drawdown = np.zeros(cumret.shape)
drawdownduration = np.zeros(cumret.shape)
for t in np.arange(1, cumret.shape[0]):
highwatermark[t] = (np.maximum(highwatermark[t -1]), cumret[t])
drawdown[t] = ((1+ cumret[t] )/(1 + highwatermark[t]) - 1)
if drawdown[t] == 0:
drawdownduration[t] == 0
else:
drawdownduration[t] = drawdownduration[t -1] + 1
maxDD, i = np.min(drawdown, np.argmin(drawdown)) # drawdown < 0 always
maxDDD = np.max(drawdownduration)
return (maxDD, maxDDD, i)
# First part of example. Read the csv data and calculate.
#The first dataframe/set for my strategy
df = pd.read_csv('IGE_daily.csv')
#print (df.head())
df.sort_values(by= 'Date', inplace = True)
dailyret = df.loc[:, 'Adj Close'].pct_change()
excessRet = ((dailyret - 0.04)/252)
sharpeRatio = ((np.sqrt(252)*np.mean(excessRet))/np.std(excessRet))
print (sharpeRatio)
#Second part of example
#This is the second dataframe/set for my strategy.
df2 = pd.read_csv('SPY.csv')
#The new data frame, with both datasets.
df = pd.merge (df, df2, on = 'Date', suffixes = ('_IGE', '_SPY'))
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace = True)
df.sort_index(inplace = True)
dailyret = df [['Adj Close_IGE', 'Adj Close_SPY' ]].pct_change() # Daily
Returns
dailyret.rename(columns = {"Adj Close_IGE": "IGE", "Adj Close_SPY": "SPY"
}, inplace = True)
netRet = (dailyret['IGE'] - dailyret['SPY'])/2
sharpeRatio = np.sqrt(252) * np.mean(netRet)/np.std(netRet)
print (sharpeRatio)
cumret = np.cumprod(1 + netRet) - 1 #Cumalative return
#print (plt.plot(cumret))
#print (plt.show()) # Remember to always run plt.show to see the plot in
terminal.
maxDrawdown, maxDrawdownDuration, startDrawdownDay =
calculateMaxDD(cumret.values)
maxDrawdown = calculateMaxDD(cumret.values)
print (maxDrawdown)
Here are the results I got from my above mentioned code:
Ivies-MacBook-Pro:Quant_Trading Ivieidahosa$ python Ex3_4.py
-46.10531783058014
0.7743286831426566
Traceback (most recent call last):
File "Ex3_4.py", line 76, in <module>
maxDrawdown = calculateMaxDD(cumret.values)
File "Ex3_4.py", line 15, in calculateMaxDD
highwatermark[t] = (np.maximum(highwatermark[t -1]), cumret[t])
ValueError: invalid number of arguments
I expected the output on themaxDrawdown to be -0.09529268047208683,maxDrawdwnduration to be 497 andstartDrawdownday to be 1223.
Q: What code do I need to change for my code to work?
Your code uses a call to a numpy function having a defined a minimum-call-signature as: np.maximum( <array_like_A>, <array_like_B> )
This simply fails to meet the expected behaviour once only one of the expected pair of values was delivered in the reported line ( see the closing parenthesis ), or a scalar or any other, non-array-like type of object(s) were attempted to be delivered into the call-signature:
highwatermark[t] = ( np.maximum( highwatermark[t-1] ), cumret[t] )
where a tuple was attempted to get constructed on the right hand side of the value-assignment (well, actually an object-reference gets assigned in python, sure, but was trying to remain short here to tell that fast for an easy reading ), the first item of which was expected to get assigned to a returned value from a call to the above documented np.maximum(...) function. And Hic Sunt Leones ...
May like to start further bug-tracing with a cross-check-ing of the state of objects and the call-signature:
try:
for t in np.arange( 1, cumret.shape[0] ):
print( "The shape of <highwatermark[t-1]>-object was: ",
highwatermark[t-1].shape, " for t == ", t
)
except:
print( "The <highwatermark[t-1]>-object was not a numpy array",
" for t == ", t
)
finally:
print( np.maximum.__doc__ )
I have a list of lists roots_3 = [['4', '5', '10'], ['11', '12', '13'], ['0', '17', '26'], ['1', '10', '15'], ['4', '19', '26'], ['11', '14', '22'],.... ]
and I want to turn all the values inside in integer so I wrote a loop
for i in range(len(roots_3)):
for j in range(len(roots_3[i])):
roots_3[i][j]= np.int(roots_3[i][j])
and this is the error I get :
ValueError: invalid literal for int() with base 10: '\x00\x00\x00...
Note that roots_3 is obtained from several text files like this:
for file in files_3:
with open ("/Users/stordd/Desktop/StageI2M/Leiden/k3/{}".format(file), 'r+',encoding="utf8", errors='ignore') as f:
lines = f.readlines()
z = []
for line in lines:
value = line.split()
num_lines = len(f.readlines())
z.append(value[1])
roots_3.append(z)
f.close()
I got the text files from a python script running a C program :
start = time.time()
cmd = ["/Users/stordd/Desktop/StageI2M/C/forestenostre/grezza_foresta", "-w","/Users/stordd/Desktop/StageI2M/Leiden/text_file/USA.txt", "-m", "3", "-e", "-0"]
ntrial = input("How many trials? ")
for i in range(int(ntrial)):
# Open/Create the output file
outFile = open("/Users/stordd/Desktop/StageI2M/Leiden/k3/{}.txt".format(i), 'ab')
result = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out = result.stdout.read()
outFile.write(out)
outFile.close()
time.sleep(1)
end = time.time()
print(end - start)
and the text files output are very simple, they look like this :
radice 11
radice 14
radice 25
I have a weird problem... I can't make legend: Lets say I have:
fraction1Str = strcat('F1 = ', sprintf('%g',round(fraction1*100)/100), '+/-',sprintf('%g',round(fraction1STD*100)/100));
fraction2Str = strcat('F2 = ', sprintf('%g',round(fraction2*100)/100), '+/-',sprintf('%g',round(fraction2STD*100)/100));
fraction3Str = strcat('F3 = ', sprintf('%g',round(fraction3*100)/100), '+/-',sprintf('%g',round(fraction3STD*100)/100));
fractionStr = [fraction1Str; fraction2Str; fraction3Str];
...
hleg = legend(fractionStr);
That is giving me error: CAT arguments dimensions are not consistent.
All strings are the same format, even number of chars... Interestingly if I change to one of i.e.
fractionStr = [fraction3Str; fraction3Str; fraction3Str];
fractionStr = [fraction2Str; fraction2Str; fraction2Str];
fractionStr = [fraction1Str; fraction1Str; fraction1Str];
its working, but no with these three different strings... Any ideas?