How can I use multi-line matching in a bump2version config file? - multiline

I want to use bump2version for a file that looks like this (it's a rust Cargo.toml):
[package]
name = "my_super_package"
version = "0.1.34"
...
[dependencies]
my_other_super_package = { path = "../yadayadayada", version = "0.1.34", registry = "crates-haha" }
...
In the .bumpversion.cfg file, I cannot just use
[bumpversion:file:Cargo.toml]
parse = qv\((?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)
because that would accidentally also change the unrelated version of my_other_super_package that coincidentally has the same version number.
The bump2version docs say that search and replace can handle multi-line specs, so I tried
[bumpversion:file:Cargo.toml]
search = name = "my_super_package"\nversion = "{current_version}"
replace = name = "my_super_package"\nversion = "{new_version}"
but the newlines didn't seem to be matched. I also tried
[bumpversion:file:Cargo.toml]
parse = qv(^version = \((?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+))
but the "^version = " part seems to be ignored.
Help?

wandered same question, and found the answer.
File for bumping Cargo.lock and Cargo.toml (so git tree would be clean after VScode autobumping Cargo.lock file), the trick is to use tab symbols (maybe spaces will work as well, didn't test):
[bumpversion:file:Cargo.lock]
search = name = "my-project"
version = "{current_version}"
replace = name = "my-project"
version = "{new_version}"
so after bumping you get in Cargo.toml:
[package]
name = "my-project"
version = "1.2.2"

Related

I'm having trouble with sending a form using POST to retrieve data in R

I'm having trouble collecting doctors from https://www.uchealth.org/providers/. I've found out it's a POST method but with httr I can't seem to create the form. Here's what I have
url = url = 'https://www.uchealth.org/providers/'
formA = list(title = 'Search', onClick = 'swapForms();', doctor-area-of-care-top = 'Cancer Care')
formB = list(Search = 'swapForms();', doctor-area-of-care = 'Cancer Care')
get = POST(url, body = formB, encode = 'form')
I'm fairly certain formB is the correct one. However, I can't test it since I yield an error when trying to make the list. I believe it is because you can't use "-" characters when naming although I could be wrong on that. Could somebody help please?
I am unable to comment properly but try this to create an list. Code below worked for me.
library(httr)
url = 'https://www.uchealth.org/providers/'
formB = list(Search = 'swapForms();', `doctor-area-of-care` = 'Cancer Care')
get = POST(url, body = formB, encode = 'form')
When you are creating names with spaces or some other special character you have to put it into the operator above.

How to get corresponding file in another folder in Octave/MATLAB?

In one of my folders (say Folder01) there are files like "IGN_A.txt", "IGN_B.txt", "IGN_C.txt".........
In another folder (say Folder02) there are files like "sim_IGN_A_M01.txt", "sim_IGN_A_M02.txt", "sim_IGN_A_M03.txt" for the corresponding file "IGN_A.txt" in Folder01.
Similarly, "sim_IGN_B_M01.txt", "sim_IGN_B_M02.txt", "sim_IGN_B_M03.txt" for corresponding file "IGN_B.txt" in Folder01.
How can I get the corresponding files from those Folders.
For example, I want to get "IGN_A.txt" along with "sim_IGN_A_M01.txt", "sim_IGN_A_M02.txt", "sim_IGN_A_M03.txt".
Here. I added my code which can only get "IGN_A.txt" along with "sim_IGN_A.txt".
Folder01 = 'Home/A1';
Folder02 = 'Home/A2';
%Going Throuh all the Folder01 files
Allfiles_Folder01 = dir(fullfile(Folder01, '*IGN*.txt'));
for k = 1:length(Allfiles_Folder01)
fullFileName = fullfile(Folder01, Allfiles_Folder01(k).name);
READ_Folder01=dlmread(fullFileName,'',2,0);
fullFileName_Sim = fullfile(Folder02, strcat('sim_',Allfiles_Folder01(k).name))
READ_Folder02=dlmread(fullFileName_Sim,'',1,0);
end
If the naming convention is consistent as provided by you, this would be my suggestion:
% Get all filenames from Folder01 in cell array.
Allfiles_Folder01 = dir(fullfile(Folder01, '*IGN*.txt'));
Allfiles_Folder01 = {Allfiles_Folder01.name}
% Iterate all filenames from Folder01.
for k = 1:numel(Allfiles_Folder01)
% Cut file extension from current filename.
filename = Allfiles_Folder01{k};
filename = filename(1:end-4);
% Get all filenames from Folder02 with specific search string in cell array.
Allfiles_Folder02 = dir(fullfile(Folder02, strcat('*', filename, '*.txt')));
Allfiles_Folder02 = {Allfiles_Folder02.name}
% Do stuff with filenames from Folder02 corresponding to filename from Folder01.
% ...
% ...
end

Writing items from function to separate text files?

I'm running some web scraping, and now have a list of 911 links saved in the following (I included 5 to demonstrate how they're stored):
every_link = ['http://www.millercenter.org/president/obama/speeches/speech-4427', 'http://www.millercenter.org/president/obama/speeches/speech-4425', 'http://www.millercenter.org/president/obama/speeches/speech-4424', 'http://www.millercenter.org/president/obama/speeches/speech-4423', 'http://www.millercenter.org/president/obama/speeches/speech-4453']
These URLs link to presidential speeches over time. I want to store each individual speech (so, 911 unique speeches) in different text files, or be able to group by president. I'm trying to pass the following function on to these links:
def processURL(l):
open_url = urllib2.urlopen(l).read()
item_soup = BeautifulSoup(open_url)
item_div = item_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})
item_str = item_div.text.lower()
item_str_processed = punctuation.sub('',item_str)
item_str_processed_final = item_str_processed.replace('—',' ')
for l in every_link:
processURL(l)
So, I would want to save to unique text files words from the all the processed speeches. This might look like the following, with obama_44xx representing individual text files:
obama_4427 = "blah blah blah"
obama_4425 = "blah blah blah"
obama_4424 = "blah blah blah"
...
I'm trying the following:
for l in every_link:
processURL(l)
obama.write(processURL(l))
But that's not working...
Is there another way I should go about this?
Okay, so you have a couple of issues. First of all, your processURL function doesn't actually return anything, so when you try to write the return value of the function, it's going to be None. Maybe try something like this:
def processURL(link):
open_url = urllib2.urlopen(link).read()
item_soup = BeautifulSoup(open_url)
item_div = item_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})
item_str = item_div.text.lower()
item_str_processed = punctuation.sub('',item_str)
item_str_processed_final = item_str_processed.replace('—',' ')
splitlink = link.split("/")
president = splitlink[4]
speech_num = splitlink[-1].split("-")[1]
filename = "{0}_{1}".format(president, speech_num)
return filename, item_str_processed_final # returning a tuple
for link in every_link:
filename, content = processURL(link) # yay tuple unpacking
with open(filename, 'w') as f:
f.write(content)
This will write each file to a filename that looks like president_number. So for example, it will write Obama's speech with id number 4427 to a file called obama_4427. Lemme know if that works!
You have to call the processURL function and have it return the text you want written. After that, you simply have to add the writing to disk code within the loop. Something like this:
def processURL(l):
open_url = urllib2.urlopen(l).read()
item_soup = BeautifulSoup(open_url)
item_div = item_soup.find('div',{'id': 'transcript'},{'class': 'displaytext'})
item_str = item_div.text.lower()
#item_str_processed = punctuation.sub('',item_str)
#item_str_processed_final = item_str_processed.replace('—',' ')
return item_str
for l in every_link:
speech_text = processURL(l).encode('utf-8').decode('ascii', 'ignore')
speech_num = l.split("-")[1]
with open("obama_"+speech_num+".txt", 'w') as f:
f.write(speech_text)
The .encode('utf-8').decode('ascii', 'ignore') is purely for dealing with non-ascii characters in the text. Ideally you would handle them in a different way, but that depends on your needs (see Python: Convert Unicode to ASCII without errors).
Btw, the 2nd link in your list is 404. You should make sure your script can handle that.

How to go about creating XLS file (Matlab/HTML/JS)?

I have been using MATLAB to ask plenty of questions via questdlg and then saving all the answers as data = {'Name of variable1', 'Name of variable2', etc}. I am on a MAC so xlswrite does not work. I have used xlwrite and it works when I hit run in MATLAB. I have compiled this via the Application compiler and included the xlwrite file. When running the file, the questions are asked, but the file is not created. This is a little odd as it works and creates the file in MATLAB directly.
I switched to dlmcell and this works in MATLAB but not when run externally. As this needs to run on Windows and MAC I would have to use dlmcell? (testing on mac, then distributing on mac and windows).
What I am trying to find out, is why would it work in MATLAB but not when compiled? If this is a big problem, then I thought HTML would be a good choice. Can I write data to .txt/.xls via HTML?
prompt = {'Enter serial number'};
dlg_title = 'Serial Number';
num_lines = 1;
defaultans = {'15'};
Serial_Number = inputdlg(prompt,dlg_title,num_lines,defaultans);
SN = char(Serial_Number) %ANSWER%
prompt = {'Enter your name'};
dlg_title = 'Your name';
num_lines = 1;
defaultans = {''};
Name = inputdlg(prompt,dlg_title,num_lines,defaultans);
User = char(Name) %ANSWER%
%%PREPERATION%%
LicensePI = 'Which license is installed?'; %QUESTION%
set(0,'DefaultImageVisible','off') %Disables icon
LicensePIAns = questdlg('Which license is installed?', 'Preperation', 'Basic', 'Medium','Advanced'); %ANSWER%
LicenseGen = 'Which type of temporary license was preferred?'; %QUESTION%
LicensGenAns = questdlg('Which type of temporary license was preferred?', 'Basic', 'Medium', 'Advanced'); %ANSWER%
%%PREPERATION%%
filename = 'Test.xls';
data = {SN, User, LicensePI, LicensePIAns, LicenseGen, LicensGenAns}
dlmcell('sat.txt',data);

Using MATLAB to parse HTML for URL in anchors, help fast

I'm on a strict time limit and I really need a regex to parse this type of anchor (they're all in this format)
20120620_0512_c2_102..>
for the URL
20120620_0512_c2_1024.jpg
I know its not a full URL, it's relative, please help
Here's my code so far
year = datestr(now,'yyyy');
timestamp = datestr(now,'yyyymmdd');
html = urlread(['http://sohowww.nascom.nasa.gov//data/REPROCESSING/Completed/' year '/c2/' timestamp '/']);
links = regexprep(html, '<a href=.*?>', '');
Try the following:
url = 'http://sohowww.nascom.nasa.gov/data/REPROCESSING/Completed/2012/c2/20120620/';
html = urlread(url);
t = regexp(html, '<a href="([^"]*\.jpg)">', 'tokens');
t = [t{:}]'
The resulting cell array (truncated):
t =
'20120620_0512_c2_1024.jpg'
'20120620_0512_c2_512.jpg'
...
'20120620_2200_c2_1024.jpg'
'20120620_2200_c2_512.jpg'
I think this is what you are looking for:
htmlLink = '20120620_0512_c2_102..>';
link = regexprep(htmlLink, '(.*)', '$2');
link =
20120620_0512_c2_1024.jpg
regexprep works also for cell arrays of strings, so this works too:
htmlLinksCellArray = { '20120620_0512_c2_102..>', '20120620_0512_c2_102..>', '20120620_0512_c2_102..>' };
linksCellArray = regexprep(htmlLinksCellArray, '(.*)', '$2')
linksCellArray =
'20120620_0512_c2_1024.jpg' '20120620_0512_c2_1025.jpg' '20120620_0512_c2_1026.jpg'