optimize python iterations of entries, transactions; use exceptions more

This commit is contained in:
John Wiegley 2004-09-08 03:33:56 -04:00
parent 612e94ceaa
commit 842359474e
7 changed files with 93 additions and 87 deletions

View file

@ -392,7 +392,7 @@ OPT_BEGIN(set_price, "z:") {
if (std::strchr(optarg, '='))
config.price_settings.push_back(optarg);
else
std::cerr << "Error: Invalid price setting: " << optarg << std::endl;
throw error(std::string("Invalid price setting: ") + optarg);
} OPT_END(set_price);
OPT_BEGIN(account, "a:") {

View file

@ -1,5 +1,6 @@
#include "gnucash.h"
#include "ledger.h"
#include "error.h"
#include <iostream>
#include <sstream>
@ -218,21 +219,17 @@ static void dataHandler(void *userData, const char *s, int len)
case XACT_ACCOUNT: {
accounts_map::iterator i = accounts_by_id.find(std::string(s, len));
if (i == accounts_by_id.end()) {
std::cerr << "Could not find account " << std::string(s, len)
<< std::endl;
std::exit(1);
}
if (i == accounts_by_id.end())
throw error(std::string("Could not find account ") + std::string(s, len));
transaction_t * xact = curr_entry->transactions.back();
xact->account = (*i).second;
account_comm_map::iterator ac = account_comms.find(xact->account);
if (ac == account_comms.end()) {
std::cerr << "Could not find account " << *(xact->account)
<< std::endl;
std::exit(1);
}
if (ac == account_comms.end())
throw error(std::string("Could not find account ") +
std::string(*(xact->account)));
commodity_t * default_commodity = (*ac).second;
curr_quant.set_commodity(*default_commodity);
@ -300,13 +297,10 @@ unsigned int gnucash_parser_t::parse(std::istream& in,
while (! in.eof()) {
in.getline(buf, BUFSIZ - 1);
if (! XML_Parse(parser, buf, std::strlen(buf), in.eof())) {
std::cerr << XML_ErrorString(XML_GetErrorCode(parser))
<< " at line " << XML_GetCurrentLineNumber(parser)
<< std::endl;
return NULL;
}
if (! XML_Parse(parser, buf, std::strlen(buf), in.eof()))
throw parse_error(original_file ? *original_file : "<gnucash>",
XML_GetCurrentLineNumber(parser),
XML_ErrorString(XML_GetErrorCode(parser)));
}
XML_ParserFree(parser);

View file

@ -267,16 +267,11 @@ entry_t * journal_t::derive_entry(strings_list::iterator i,
entry_t * matching = NULL;
if (! parse_date((*i).c_str(), &added->date)) {
std::cerr << "Error: Bad entry date: " << *i << std::endl;
return false;
}
++i;
if (! parse_date((*i).c_str(), &added->date))
throw error("Bad date passed to 'entry'");
if (i == end) {
std::cerr << "Error: Too few arguments to 'entry'." << std::endl;
return false;
}
if (++i == end)
throw error("Too few arguments to 'entry'");
mask_t regexp(*i++);
@ -290,17 +285,12 @@ entry_t * journal_t::derive_entry(strings_list::iterator i,
added->payee = matching ? matching->payee : regexp.pattern;
if (i == end) {
std::cerr << "Error: Too few arguments to 'entry'." << std::endl;
return false;
}
if (i == end)
throw error("Too few arguments to 'entry'");
if ((*i)[0] == '-' || std::isdigit((*i)[0])) {
if (! matching) {
std::cerr << "Error: Missing account name for non-matching entry."
<< std::endl;
return false;
}
if (! matching)
throw error("Missing account name for non-matching entry");
transaction_t * m_xact, * xact, * first;
m_xact = matching->transactions.front();
@ -343,16 +333,12 @@ entry_t * journal_t::derive_entry(strings_list::iterator i,
if (! acct)
acct = find_account(acct_regex.pattern);
if (! acct) {
std::cerr << "Error: Could not find account name '"
<< acct_regex.pattern << "'." << std::endl;
return false;
}
if (! acct)
throw error(std::string("Could not find account name '") +
acct_regex.pattern + "'");
if (i == end) {
std::cerr << "Error: Too few arguments to 'entry'." << std::endl;
return false;
}
if (i == end)
throw error("Too few arguments to 'entry'");
amount_t amt(*i++);
transaction_t * xact = new transaction_t(acct, amt);
@ -365,12 +351,11 @@ entry_t * journal_t::derive_entry(strings_list::iterator i,
if (i != end && std::string(*i++) == "-from" && i != end) {
if (account_t * acct = find_account(*i++))
added->add_transaction(new transaction_t(acct));
} else {
if (! matching) {
std::cerr << "Error: Could not figure out the account to draw from."
<< std::endl;
std::exit(1);
}
}
else if (! matching) {
throw error("Could not figure out the account to draw from");
}
else {
transaction_t * xact
= new transaction_t(matching->transactions.back()->account);
added->add_transaction(xact);
@ -419,6 +404,10 @@ unsigned int transactions_len(entry_t& entry)
transaction_t& transactions_getitem(entry_t& entry, int i)
{
static int last_index = 0;
static entry_t * last_entry = NULL;
static transactions_list::iterator elem;
std::size_t len = entry.transactions.size();
if (abs(i) >= len) {
@ -426,11 +415,19 @@ transaction_t& transactions_getitem(entry_t& entry, int i)
throw_error_already_set();
}
if (&entry == last_entry && i == last_index + 1) {
last_index = i;
return **++elem;
}
int x = i < 0 ? len + i : i;
transactions_list::iterator elem = entry.transactions.begin();
elem = entry.transactions.begin();
while (--x >= 0)
elem++;
last_entry = &entry;
last_index = i;
return **elem;
}
@ -441,6 +438,10 @@ unsigned int entries_len(journal_t& journal)
entry_t& entries_getitem(journal_t& journal, int i)
{
static int last_index = 0;
static journal_t * last_journal = NULL;
static entries_list::iterator elem;
std::size_t len = journal.entries.size();
if (abs(i) >= len) {
@ -448,11 +449,19 @@ entry_t& entries_getitem(journal_t& journal, int i)
throw_error_already_set();
}
if (&journal == last_journal && i == last_index + 1) {
last_index = i;
return **++elem;
}
int x = i < 0 ? len + i : i;
entries_list::iterator elem = journal.entries.begin();
elem = journal.entries.begin();
while (--x >= 0)
elem++;
last_journal = &journal;
last_index = i;
return **elem;
}
@ -461,21 +470,33 @@ unsigned int accounts_len(account_t& account)
return account.accounts.size();
}
account_t& accounts_getitem(account_t& account, int index)
account_t& accounts_getitem(account_t& account, int i)
{
static int last_index = 0;
static account_t * last_account = NULL;
static accounts_map::iterator elem;
std::size_t len = account.accounts.size();
if (abs(index) >= len) {
if (abs(i) >= len) {
PyErr_SetString(PyExc_IndexError, "Index out of range");
throw_error_already_set();
}
int x = 0;
for (accounts_map::iterator i = account.accounts.begin();
i != account.accounts.end();
i++)
if (x++ == index)
return *((*i).second);
if (&account == last_account && i == last_index + 1) {
last_index = i;
return *(*++elem).second;
}
int x = i < 0 ? len + i : i;
elem = account.accounts.begin();
while (--x >= 0)
elem++;
last_account = &account;
last_index = i;
return *(*elem).second;
}
void export_journal()

View file

@ -290,11 +290,8 @@ int parse_and_report(int argc, char * argv[], char * envp[])
command = "e";
else if (command == "equity")
command = "E";
else {
std::ostringstream msg;
msg << "Unrecognized command '" << command << "'";
throw error(msg.str());
}
else
throw error(std::string("Unrecognized command '") + command + "'");
config.process_options(command, arg, args.end());

View file

@ -19,4 +19,5 @@ register_parser (parser)
journal = Journal ()
parse_journal_file (args[0], journal)
print journal[-1].payee
for entry in journal:
print entry.payee

View file

@ -576,11 +576,8 @@ unsigned int textual_parser_t::parse(std::istream& in,
time_commodity->flags |= COMMODITY_STYLE_NOMARKET;
}
if (errors > 0) {
std::ostringstream msg;
msg << "Errors parsing file '" << path << "'";
throw error(msg.str());
}
if (errors > 0)
throw error(std::string("Errors parsing file '") + path + "'");
return count;
}

View file

@ -642,11 +642,10 @@ value_expr_t * parse_logic_expr(std::istream& in)
}
default:
if (! in.eof()) {
std::ostringstream err;
err << "Unexpected character '" << c << "'";
throw value_expr_error(err.str());
}
if (! in.eof())
throw value_expr_error(std::string("Unexpected character '") +
c + "'");
break;
}
}
}
@ -687,22 +686,19 @@ value_expr_t * parse_value_expr(std::istream& in)
node->right = choices = new value_expr_t(value_expr_t::O_COL);
choices->left = parse_logic_expr(in);
c = peek_next_nonws(in);
if (c != ':') {
std::ostringstream err;
err << "Unexpected character '" << c << "'";
throw value_expr_error(err.str());
}
if (c != ':')
throw value_expr_error(std::string("Unexpected character '") +
c + "'");
in.get(c);
choices->right = parse_logic_expr(in);
break;
}
default:
if (! in.eof()) {
std::ostringstream err;
err << "Unexpected character '" << c << "'";
throw value_expr_error(err.str());
}
if (! in.eof())
throw value_expr_error(std::string("Unexpected character '") +
c + "'");
break;
}
c = peek_next_nonws(in);
}