Styleguide/Scripts: Difference between revisions

Content deleted Content added
Cbarr (talk | contribs)
Created page with "=="Form-countryspecific-*" creating script== <syntaxhighlight lang="python"> import wikipedia wiki = wikipedia site = wiki.getSite('en', 'wikidonate') basetitle = 'Template..."
 
Cbarr (talk | contribs)
 
(5 intermediate revisions by the same user not shown)
Line 1:
==Introduction==
=="Form-countryspecific-*" creating script==
=="Form-countryspecific-*" creating code==
<syntaxhighlight lang="python">
import wikipedia
Line 703 ⟶ 704:
finally:
wikipedia.stopme()
 
</syntaxhighlight>
 
=="Form-countryspecific-*" Mining code==
<syntaxhighlight lang="python">
import wikipedia
import datetime
wiki = wikipedia
site = wiki.getSite('en', 'wikidonate')
 
basetitle = 'Template:2012FR/Form-countryspecific-control'
editsummary = 'Updating localized landing page via python script.'
overwrite = True # overwrite a page if it already exists? True/False (must be capitalised)
paymentProcessing={
'':['GC'],
'/USD':['GC'],
'/EUR':['GC'],
'/XX':['GC'],
'/AE':['GC'],
'/AN':['GC'],
'/AR':['GC'],
'/PG':['GC'],
'/KI':['GC'],
'/NR':['GC'],
'/VU':['GC'],
'/AU':['GC'],
'/BD':['GC'],
'/BG':['GC'],
'/BH':['GC'],
'/BM':['GC'],
'/BO':['GC'],
'/BR':['GC'],
'/BZ':['GC'],
'/CA':['GC'],
'/LI':['GC'],
'/CH':['GC'],
'/CL':['GC'],
'/CN':['GC'],
'/CO':['GC'],
'/CR':['GC'],
'/CZ':['GC'],
'/DK':['GC'],
'/DO':['GC'],
'/DZ':['GC'],
'/EG':['GC'],
'/MW':['GC'],
'/SD':['GC'],
'/GB':['GC'],
'/GT':['GC'],
'/HK':['GC'],
'/HN':['GC'],
'/HR':['GC'],
'/HU':['GC'],
'/ID':['GC'],
'/IL':['GC'],
'/NP':['GC'],
'/IN':['GC'],
'/JM':['GC'],
'/JO':['GC'],
'/JP':['GC'],
'/KE':['GC'],
'/KR':['GC'],
'/KW':['GC'],
'/KZ':['GC'],
'/LB':['GC'],
'/LK':['GC'],
'/LT':['GC'],
'/LV':['GC'],
'/MA':['GC'],
'/MK':['GC'],
'/MO':['GC'],
'/MU':['GC'],
'/MX':['GC'],
'/MY':['GC'],
'/NI':['GC'],
'/NO':['GC'],
'/NZ':['GC'],
'/OM':['GC'],
'/PA':['GC'],
'/PE':['GC'],
'/PH':['GC'],
'/PK':['GC'],
'/PL':['GC'],
'/PY':['GC'],
'/QA':['GC'],
'/RO':['GC'],
'/TM':['GC'],
'/UZ':['GC'],
'/RU':['GC'],
'/SA':['GC'],
'/SE':['GC'],
'/SG':['GC'],
'/SV':['GC'],
'/TH':['GC'],
'/TN':['GC'],
'/TR':['GC'],
'/TT':['GC'],
'/TW':['GC'],
'/UA':['GC'],
'/UY':['GC'],
'/VN':['GC'],
'/DM':['GC'],
'/ZA':['GC'],
'/PR':['GC'],
'/AF':['GC'],
'/AS':['GC'],
'/AO':['GC'],
'/AG':['GC'],
'/BS':['GC'],
'/BB':['GC'],
'/BW':['GC'],
'/BN':['GC'],
'/BI':['GC'],
'/KH':['GC'],
'/KM':['GC'],
'/CG':['GC'],
'/CK':['GC'],
'/DJ':['GC'],
'/TP':['GC'],
'/ER':['GC'],
'/ET':['GC'],
'/FJ':['GC'],
'/GM':['GC'],
'/GE':['GC'],
'/GD':['GC'],
'/GQ':['GC'],
'/GU':['GC'],
'/GW':['GC'],
'/IS':['GC'],
'/IQ':['GC'],
'/KP':['GC'],
'/LR':['GC'],
'/MV':['GC'],
'/MH':['GC'],
'/MR':['GC'],
'/FM':['GC'],
'/MD':['GC'],
'/MZ':['GC'],
'/NA':['GC'],
'/NG':['GC'],
'/MP':['GC'],
'/PW':['GC'],
'/PS':['GC'],
'/KN':['GC'],
'/LC':['GC'],
'/SB':['GC'],
'/VC':['GC'],
'/TZ':['GC'],
'/TO':['GC'],
'/VE':['GC'],
'/VI':['GC'],
'/YE':['GC'],
'/ZM':['GC'],
'/ZW':['GC'],
'/US':['GC'],
'/EC':['GC'],
'/RS':['GC'],
'/AL':['GC'],
'/AD':['GC'],
'/CY':['GC'],
'/LU':['GC'],
'/MT':['GC'],
'/GF':['GC'],
'/AM':['GC'],
'/AW':['GC'],
'/AZ':['GC'],
'/BY':['GC'],
'/BJ':['GC'],
'/BA':['GC'],
'/BF':['GC'],
'/CM':['GC'],
'/CV':['GC'],
'/CF':['GC'],
'/TD':['GC'],
'/CI':['GC'],
'/FO':['GC'],
'/GA':['GC'],
'/GL':['GC'],
'/LY':['GC'],
'/MG':['GC'],
'/ML':['GC'],
'/MC':['GC'],
'/ME':['GC'],
'/NE':['GC'],
'/RE':['GC'],
'/SM':['GC'],
'/SN':['GC'],
'/SC':['GC'],
'/SR':['GC'],
'/TG':['GC'],
'/VA':['GC'],
'/DE':['GC'],
'/FR':['GC'],
'/NL':['GC'],
'/AT':['GC'],
'/IT':['GC'],
'/ES':['GC'],
'/BE':['GC'],
'/FI':['GC'],
'/IE':['GC'],
'/GR':['GC'],
'/PT':['GC'],
'/SK':['GC'],
'/SI':['GC'],
'/EE':['GC']
}
 
def main():
now = datetime.datetime.now()
fout = open("control-" + now.strftime("%Y-%m-%d@%H:%M:%S") + ".txt", "w")
 
values = ''
for country, amounts in paymentProcessing.iteritems():
oldpage = wiki.Page(site, basetitle + country)
oldcontent = oldpage.get()
lines = oldcontent.split('\n')
 
j=0
values = values + '\'' + country + '\':['
for i, x in enumerate(lines):
if lines[i].find('| donate-amount-') != -1:
values = values + (lines[i].strip('| donate-amount-'))[4:] + ','
j=j+1
values = values + '],\n'
values = values.replace(',]',']')
values = values[:-2]
fout.write(values)
 
if __name__ == '__main__':
try:
main()
finally:
wikipedia.stopme()
</syntaxhighlight>
 
==Google currency conversion mining code==
<syntaxhighlight lang="cpp">
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
 
using namespace std;
 
long calculateDigits(double d)
{
long result = 0;
long temp = d;
long place = 1;
 
while(temp/place)
{
place *= 10;
result++;
}
return(result);
}
 
int main()
{
string website = "http://www.google.com/finance/";
string file = "converter?a=1&from=USD&to=";
string currency;
 
ofstream out;
ifstream currencyFile;
 
out.open("Google/ratesGoogle.txt");
currencyFile.open("currencyCodesAll.txt");
 
while(currencyFile >> currency)
{
string command = "wget '" + website + file + currency
+ "' && grep 'currency_converter_result' '"
+ file + currency + "' > 'resultGoogle.grep'";
 
system(command.c_str());
ifstream in("resultGoogle.grep");
string result;
 
getline(in,result,'\n');
 
in.close();
 
system("rm resultGoogle.grep");
 
result.erase(0,58);
 
bool remove = false;
for(long i = 0 ; i < result.size() ; i++)
{
if(result[i] == ' ')
remove = true;
if(remove)
result[i] = ' ';
}
 
if(result != "")
out << currency << " " << result << endl;
}
system("rm conv*");
out.close();
currencyFile.close();
 
vector<string> currencyList;
vector<double> rateList;
 
double rate;
 
ifstream in("Google/ratesGoogle.txt");
 
while(in >> currency && in >> rate)
{
rate = ceil(rate*100)/100.0;
currencyList.push_back(currency);
rateList.push_back(rate);
}
 
in.close();
 
unsigned options[16] = {1,2,3,5,10,15,20,25,30,35,50,75,100,150,200,250};
 
vector <vector <double> > currencyOptions;
currencyOptions.resize(rateList.size());
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
{
currencyOptions[i].resize(16);
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
currencyOptions[i][j] = rateList[i]*options[j];
}
}
 
// ===================== raw table ==============================
out.open("Google/currencyOptionsRawGoogle.csv");
 
out << "USD, rate,";
for(long j=0 ; j < 16 ; j++)
{
out << options[j] << ", ";
}
out << endl;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << currencyOptions[i][j] << ", ";
}
out << endl;
}
 
out.close();
 
// ================== rounded to nearest dollar ====================
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
for(long j=0 ; j < currencyOptions[i].size() ; j++)
currencyOptions[i][j] = ceil(currencyOptions[i][j]);
 
out.open("Google/currencyOptionsRoundedGoogle.csv");
 
out << "USD, rate,";
for(long j=0 ; j < 16 ; j++)
{
out << options[j] << ", ";
}
out << endl;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << ", ";
}
out << endl;
}
 
out.close();
 
// =================== zero bottom 1% digits =========================
double temp;
long digits;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
temp = currencyOptions[i][j];
digits = calculateDigits(temp);
 
if(digits < 3)
{}
else
{
temp /= (long)(pow(10,digits-3));
temp = ceil(temp);
temp *= (long)(pow(10,digits-3));
currencyOptions[i][j] = (long)temp;
}
}
 
out.open("Google/currencyOptionsZeroBottomGoogle.csv");
 
out << "USD, rate,";
for(long j=0 ; j < 16 ; j++)
{
out << options[j] << ", ";
}
out << endl;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << ", ";
}
out << endl;
}
 
out.close();
 
// =================== zero bottom 1% digits 5-ify =========================
 
long ones;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
temp = currencyOptions[i][j];
digits = calculateDigits(temp);
 
if(digits == 1)
{
//temp /= 5;
//temp = round(temp);
//temp *= 5;
//currencyOptions[i][j] = temp;
}
else
{
if(digits > 3)
temp /= (long)(pow(10,digits-3));
 
ones = (long)(temp)%10;
temp = temp - ones;
if(ones > 0 and ones <= 5)
{
temp += 5;
}
else if(ones > 6 and ones <= 9)
{
temp += 10;
}
if(digits > 3)
temp *= (long)(pow(10,digits-3));
 
currencyOptions[i][j] = (long)temp;
}
}
 
out.open("Google/currencyOptions5ifyGoogle.csv");
 
out << "USD, rate,";
for(long j=0 ; j < 16 ; j++)
{
out << options[j] << ", ";
}
out << endl;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << ", ";
}
out << endl;
}
 
out.close();
 
return 0;
}
 
</syntaxhighlight>
 
==Bloomberg currency conversion mining code==
<syntaxhighlight lang="cpp">
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
 
using namespace std;
 
long calculateDigits(double d)
{
long result = 0;
long temp = d;
long place = 1;
 
while(temp/place)
{
place *= 10;
result++;
}
return(result);
}
 
int main()
{
string website = "http://www.bloomberg.com/personal-finance/calculators/currency-converter/";
string currency;
 
ofstream out;
 
out.open("Bloomberg/ratesBloomberg.txt");
 
string command = "wget '" + website
+ "' && grep \":CUR\'\" 'index.html' > 'resultBloomberg.grep'";
system(command.c_str());
 
ifstream in("resultBloomberg.grep");
string result;
 
while(getline(in,result,'\n'))
{
 
string tempString;
 
result.erase(0,23);
tempString = result.substr(0,3);
result.erase(0,12);
result.erase(result.end()-1,result.end());
 
if(result != "")
out << tempString << " " << result << endl;
}
 
in.close();
system("rm resultBloomberg.grep");
system("rm inde*");
out.close();
 
vector<string> currencyList;
vector<double> rateList;
 
double rate;
 
in.open("Bloomberg/ratesBloomberg.txt");
 
while(in >> currency && in >> rate)
{
rate = ceil(rate*100)/100.0;
currencyList.push_back(currency);
rateList.push_back(rate);
}
 
in.close();
 
unsigned options[17] = {1,2,3,5,10,15,20,25,30,35,50,75,100,150,200,250,29500000};
 
vector <vector <double> > currencyOptions;
currencyOptions.resize(rateList.size());
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
{
currencyOptions[i].resize(17);
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
currencyOptions[i][j] = rateList[i]*options[j];
}
}
 
// ===================== raw table ==============================
out.open("Bloomberg/currencyOptionsRawBloomberg.csv");
 
out << "USD, rate,";
for(long j=0 ; j < 17 ; j++)
{
out << options[j] << ", ";
}
out << endl;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << currencyOptions[i][j] << ", ";
}
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << " ";
}
out << endl;
}
 
out.close();
 
// ================== rounded to nearest dollar ====================
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
for(long j=0 ; j < currencyOptions[i].size() ; j++)
currencyOptions[i][j] = ceil(currencyOptions[i][j]);
 
out.open("Bloomberg/currencyOptionsRoundedBloomberg.csv");
 
out << "USD, rate,";
for(long j=0 ; j < 17 ; j++)
{
out << options[j] << ", ";
}
out << endl;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << ", ";
}
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << " ";
}
out << endl;
}
 
out.close();
 
// =================== zero bottom 1% digits =========================
double temp;
long digits;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
temp = currencyOptions[i][j];
digits = calculateDigits(temp);
 
if(digits < 4)
{}
else
{
temp /= (long)(pow(10,digits-3));
temp = ceil(temp);
temp *= (long)(pow(10,digits-3));
currencyOptions[i][j] = (long)temp;
}
}
 
out.open("Bloomberg/currencyOptionsZeroBottomBloomberg.csv");
 
out << "USD, rate,";
for(long j=0 ; j < 17 ; j++)
{
out << options[j] << ", ";
}
out << endl;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << ", ";
}
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << " ";
}
out << endl;
}
 
out.close();
 
// =================== zero bottom 1% digits 5-ify =========================
long ones;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
temp = currencyOptions[i][j];
digits = calculateDigits(temp);
 
if(digits == 1)
{
//temp /= 5;
//temp = round(temp);
//temp *= 5;
//currencyOptions[i][j] = temp;
}
else
{
if(digits > 3)
temp /= (long)(pow(10,digits-3));
 
ones = (long)(temp)%10;
temp = temp - ones;
if(ones > 0 and ones <= 5)
{
temp += 5;
}
else if(ones >= 6 and ones <= 9)
{
temp += 10;
}
if(digits > 3)
temp *= (long)(pow(10,digits-3));
 
currencyOptions[i][j] = (long)temp;
}
}
 
out.open("Bloomberg/currencyOptions5ifyBloomberg.csv");
 
out << "USD, rate,";
for(long j=0 ; j < 17 ; j++)
{
out << options[j] << ", ";
}
out << endl;
 
for(long i = 0 ; i < currencyOptions.size() ; i++)
{
out << currencyList[i] << ", " << rateList[i] << ", ";
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << ", ";
}
 
for(long j=0 ; j < currencyOptions[i].size() ; j++)
{
out << (long)currencyOptions[i][j] << " ";
}
out << ", ";
 
out << "\"" << (long)currencyOptions[i][3] << " "
<< (long)currencyOptions[i][4] << " "
<< (long)currencyOptions[i][6] << " "
<< (long)currencyOptions[i][9] << " "
<< (long)currencyOptions[i][10] << " "
<< (long)currencyOptions[i][12] << " "
<< (long)currencyOptions[i][15] << "\"" << endl;
}
 
out.close();
 
return 0;
}
 
</syntaxhighlight>