Skip to content

Instantly share code, notes, and snippets.

View victory-sokolov's full-sized avatar
🎯
Focusing

Viktor Sokolov victory-sokolov

🎯
Focusing
View GitHub Profile
@victory-sokolov
victory-sokolov / unionWithExclusion.ts
Last active September 9, 2021 09:50
Union two objects and exclude false values
/**
* Union two objects and exclude false values when merging same keys
* @param left
* @param right
* @returns New combined object
*/
export function unionWithExclusion(left: object, right: object): object {
return [left, right].reduce((prev: any, current) => {
if(current) {
Object.entries(current).map(([key, value]) => {
@victory-sokolov
victory-sokolov / dumptDataToFile.js
Created February 16, 2021 09:39
Download object data to a file
dataToFile(content, fileName, contentType) {
const a = document.createElement("a");
const file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
dataToFile(JSON.stringify(data), 'results.json', 'application/json');
@victory-sokolov
victory-sokolov / cameraJS
Last active December 8, 2020 10:16
Get camera feed
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1, maximum-scale=1">
<title>Document</title>
</head>
<body>
<div class="display-cover">
@victory-sokolov
victory-sokolov / CameraAccess.js
Created November 5, 2020 09:14
React Native WebView frontal camera access
import React from 'react';
import {
StyleSheet,
StatusBar,
PermissionsAndroid,
} from 'react-native';
import {
Header,
Colors,
@victory-sokolov
victory-sokolov / object-to-dict.py
Last active November 23, 2023 14:38
Convert SQLAlchemy object to Python dictionary
users = User.query.filter_by(id=id).first()
users = []
for user in users:
dictret = dict(receipt.__dict__)
dictret.pop('_sa_instance_state', None)
users.append(dictret)
@victory-sokolov
victory-sokolov / ClassMethods.py
Last active February 23, 2020 14:05
Get list of methods from class
# Returns list of methods of class: PersonClass
method_list = [func for func in dir(PersonClass) if callable(getattr(PersonClass, func)) and not func.startswith("__")]
# Execute methods
for method in method_list:
execute = getattr(class_name, method)
execute()
@victory-sokolov
victory-sokolov / split_list_n_parts.py
Created January 6, 2020 12:41
Split list into n parts
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
lst = [1,3,5,6,7,9,5,4,2,3,5,6,7,4,3]
res = list(chunks(lst, 3))
print(res) # [[1, 3, 5], [6, 7, 9], [5, 4, 2], [3, 5, 6], [7, 4, 3]]
@victory-sokolov
victory-sokolov / io-json.java
Created December 31, 2019 09:28
Read/Write JSON
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public static JSONObject readJSON(String file) throws IOException, ParseException {
JSONParser parser = new JSONParser();
FileReader config = new FileReader(file);
Object obj = parser.parse(config);
return (JSONObject) obj;
@victory-sokolov
victory-sokolov / excel-to-pdf.py
Created December 31, 2019 09:26
Converts xlsx,csv to PDF file
# Note Microsoft Excel must be installed
from openpyxl import Workbook, load_workbook
from win32com.client import Dispatch
path = os.path.dirname(os.path.abspath(__file__))
o = Dispatch("Excel.Application")
o.Visible = False
o.DisplayAlerts = False
@victory-sokolov
victory-sokolov / regex_patterns.js
Last active November 23, 2023 14:38
JS Regex Patterns
/* Common Regular Expressions */
// Match domain (com,net,ru)
let url = "https://www.youtube.com/watch?v=vEPOU_fKmR8";
url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
let site = matches && matches[1];