PyMongo check if update succeeded - pymongo

I'm trying to figure out how to check if an update succeeded. By default, does db.users.update(....) return a getLastError type? So can I do something like:
error = db.users.update(.....)
if error.n == 1:
....
How can I check to make sure that the update succeeded?

pymongo's vague docs say update's return value is:
A document (dict) describing the effect of the update or None if write acknowledgement is disabled.
(Not explicitly stated but) this means that the return value is lastError (unless you use safe=True).

Related

Enum wrong value

I'm new to solidity and I'm working through a course and I've set myself a project but I can't work out what I'm doing wrong as the data I'm logging is showing me the values are what's expected based on the enum positions.
I'm using compiler version ^0.6.6
I first set up my enum
enum VOTE_STATUS {CLOSED,OPEN}
VOTE_STATUS public voting_status = VOTE_STATUS.CLOSED;
I then have a voting function which I'm calling require in.
function addVote(uint _vote) public{
// Make sure voting is open
require(voting_status == VOTE_STATUS.CLOSED,"Voting hasn't opened yet");
}
In remix when I click the voting_status button on the left-hand side it returns the correct option, 0 = CLOSED, 1 = OPEN.
The issue is when I run the require statement above it seems to think the enum is always set to OPEN when the logging and solidity say differently.
I've confirmed this by setting the require statement to check for OPEN when the status is closed and this gives me the result I'm looking for
voting_status = VOTE_STATUS.CLOSED;
require(voting_status == VOTE_STATUS.OPEN,"Voting hasn't opened yet");
Any help with this would be massively appreciated
Thanks
I think that you may have a conceptual misunderstanding. A require statement works as follows:
require('condition', 'error msg');
What the require statement does is to check if the condition is met. If the condition is NOT met, it reverts the transaction and returns the error msg. So what you want to do is a require, which checks the voting_status to be OPEN, and if it is not, then it will throw the error "Voting hasn't opened yet".
So just to be clearer, your current require statement:
require(voting_status == VOTE_STATUS.CLOSED,"Voting hasn't opened yet");
Is actually checking if the voting_status is set to CLOSED. And if it is not, then it throws the msg error "Voting hasn't opened yet".
So maybe what you are looking for is:
require(voting_status == VOTE_STATUS.OPEN,"Voting hasn't opened yet");
Hope you find this information useful :)

GORM db.First print log automatically

I am using the latest GORM v2.
When I use db.First(&AoiArea{}), it triggers a record not found error output in the log if nothing is found.
if err := db.First(&AoiArea{}).Error; errors.Is(err, gorm.ErrRecordNotFound) {
// Do something
}
Automatic O/P from First:
2021/09/17 09:02:24 ←[31;1mE:/xyz-api/models/utils.go:47
←[35;1mrecord not found ←[0m←[33m[0.000ms] ←[34;1m[rows:0]←[0m SELECT * FROM "aoi_area" ORDER BY "aoi_area"."id" LIMIT 1
Is there any way to prevent this default log?
I see there is a similar question here https://gorm.io/docs/error_handling.html#comment-5083714457 in their documentation Q&A but no appropriate answer.
Yes, First will always print the error log.
If you want to avoid the ErrRecordNotFound error, you could use Find like db.Limit(1).Find(&user), the Find method accepts both struct and slice data
doc is here: https://gorm.io/docs/query.html
You can try this
db.Limit(1).Find(&AoiArea{})

How to allow command with one discord.py check?

I have a command in discord.py where I would like to have, for example, #has_permissions(administrator=True), but I want to be able to run the command with, say, a custom check that checks if it is a certain user, so that the user/role/whatever can run the command, regardless of whether or not they have the permission. So I guess my question is this: is there a way to allow a command if a user passes at least 1 check? Thank you in advance!
Yes you can write your own check as described in the docs here: https://discordpy.readthedocs.io/en/master/ext/commands/api.html?highlight=has%20permissions#discord.ext.commands.check
def check_if_it_is_me(ctx):
return ctx.message.author.id == 85309593344815104
#bot.command()
#commands.check(check_if_it_is_me)
async def only_for_me(ctx):
await ctx.send('I know you!')
I found an answer! After reviewing the docs, I found the #commands.check_any() decorator. You can input checks for it to check, and if any of the inputted checks will be passed, it adds that check.
Read more about this decorator in the documentation.

PHP error_log errors to MySQL

In a previous ticket i asked about logging PHP errors in MySQL which gives me:
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
// mysql connect etc here...
$sql = "INSERT INTO `error_log` SET
`number` = ".mysql_real_escape_string($errno).",
`string` = ".mysql_real_escape_string($errstr).",
`file` = ".mysql_real_escape_string($errfile).",
`line` = ".mysql_real_escape_string($errline);
mysql_query($sql);
// Don't execute PHP internal error handler
return true;
}
// set to the user defined error handler
$new_error_handler = set_error_handler("myErrorHandler");
I can make this work but only if it is triggerred like this:
trigger_error("message here");
However, I also want the error handler to be called for all errors such as syntax errors like:
echo "foo;
But these errors are just outputted to the screen, what am i doing wrong?
You can only handle runtime errors with a custom error handler. The echo "foo error in your example happens when parsing (i.e. reading in) the source. Since PHP can not fully parse the code, it can also not run your error handler on this error.
If You're forced to test if syntax is correct, You can use php_check_syntax function, with filename parameter PHP Manual php_check_syntax
php_check_syntax also provides second parameter, witch when used will be populated by the error string, as far as i remember
That's indeed terrible way of error logging
You don't need not a single advantage of a database. Would you make a database lookup for the certain line number? Or order your results by file name?
database is a subject of many errors itself.
You've been told already that it's impossible to catch a parse error at the program logic level, because a syntactically wrong program will never run.
Let's take your code as an example. It will raise a MySQL error (because of poorly formed query) which you will never see. As well as any other errors occurred. That's what I am talking about.

Forcing a TFS checkin of a file via C#

How can I specify that I ALWAYS want the local file to replace the server copy even if the TFS copy is newer?
if (pendingChanges.GetUpperBound(0)>-1)
ChangeSetNumber = workspace.CheckIn(pendingChanges, filename);
I can see from the intelisense that I can specify checkinoptions as a parameter of the CheckIn method, I just cannot find what I need to put in to have it always check in and ignore any conflict I might come up with.
Thanks in advance.
EDIT: I found a command TF RESOLVE "item" /auto:AcceptYours /recursive So I guess my revised question would be is there a programming equivalent to the /auto:AcceptYours switch?
NecroEDIT: process the conflicts before doing the checkin
Conflict[] conflicts = workspace.QueryConflicts(new string[] { TFSProject }, true);
foreach (Conflict conflict in conflicts)
{
conflict.Resolution = Resolution.AcceptTheirs;
workspace.ResolveConflict(conflict);
}
Checkins are atomic - either they all succeed or they all fail. If there are any conflicts that need to be resolved before the check in, the check in operation will throw an exception. (Documentation)
You have to evaluate checkin for conflicts and then resolve the CheckinConflicts by Workspace.ResolveConflict Method.
ResolveConflict expects CheckinConflict, and result of the EvaluateCheckin (which is CheckinEvaluationResult) includes CheckinConflicts.
This page may help.
Note: checkinoptions is not related with what you are asking.
Hope this helps.