Skip to content

Instantly share code, notes, and snippets.

def validate_argument_dict(dict_arg, dict_expected):
for key, value in dict_arg.items():
if key not in dict_expected:
raise ValueError(f'Wrong key {key}')
type_expected = type(dict_expected[key])
if not isinstance(value, type_expected):
raise ValueError(f'Wrong value type for the {key}. {type_expected} is expected.')
return dict_arg
PD_EXPECTED = {
@lapshinmr
lapshinmr / where.py
Created August 28, 2020 09:05
Mongoengine $where
def create_user_other_parcels(self, parcel_dashboard: ParcelDashboard, user: PortalUser):
# expression is needed to normalize phones for all parcels and fine parcels with the same phone
expression = """
function () {
const userPhone = '<user_phone>';
let phone = this.receiver_phone;
if (phone === undefined || phone === null) {
return false;
};
phone = phone.replace(/[+() -]/, '');
for (let ion in FORMULAS[this.recipeSelected].ions) {
this.$watch(`solute.${ion}`, function (newValue, oldValue) {
console.log(newValue, oldValue)
}, { deep: true })
}
@lapshinmr
lapshinmr / color_picker.vue
Last active May 28, 2021 05:24
Template for the color picker based on the radio button and browser variables.
<template>
<div>
<div v-for="(color, colorName, index) in colorThemes"
>
<input
type="radio"
name="theme"
:id="'color' + index"
:value="colorName"
v-model="colorTheme"
@lapshinmr
lapshinmr / timezone.py
Created June 22, 2018 10:45
Create datetime object with one timezone and convert to another.
d = datetime.now(tz=pytz.timezone('Asia/Seoul'))
d.astimezone(pytz.timezone('Europe/Samara'))
@lapshinmr
lapshinmr / parser.py
Created June 15, 2018 09:59
element inner html parsing
title = title_el.text or ''
for part in title_el:
title += part.text
title += part.tail or ''
title2 = etree.tostring(title_el, method='text', encoding='utf-8').decode('utf-8')
@lapshinmr
lapshinmr / pymongo.py
Created March 1, 2018 09:36
Find index in documents list by id
def get_word_idx(word_id):
return WordsChart.objects.aggregate(
{
"$project": {
"matchedIndex": {
"$indexOfArray": [
"$list", word_id
]
}
}
return send_file(BytesIO(open(z.filename, 'rb').read()),
attachment_filename=curdir_basename + '.zip',
as_attachment=True)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Draggable - Handles</title>
<style>
ul {
width: 300px;
}
switchFromParToInput: function(id) {
var p_element = $('#' + id).find('.count');
var input_element = $('<input>', {
type: 'text',
class: $(p_element).attr('class'),
value: $(p_element).text()
}
);
p_element.replaceWith($(input_element));
var value = input_element.val();