Skip to content

Instantly share code, notes, and snippets.

View VehpuS's full-sized avatar
🦔

Moshe Jonathan Gordon Radian VehpuS

🦔
View GitHub Profile
// set location of docx text content file
$xmlFile = $targetDir."/word/document.xml";
$reader = new XMLReader;
$reader->open($xmlFile);
// set up variables for formatting
$text = ''; $formatting['bold'] = 'closed'; $formatting['italic'] = 'closed'; $formatting['underline'] = 'closed'; $formatting['header'] = 0;
// loop through docx xml dom
while ($reader->read()){
@VehpuS
VehpuS / touchmove-mouseover-simulation.md
Last active February 20, 2024 02:12
Simulating Javascript mouseover events on touch screens using touchmove attributes and document.elementFromPoint

I made an HTML port of game of life made with a table of checkboxes based on Pluralsight's Play by Play: HTML, CSS, and JavaScript with Lea Verou (http://www.pluralsight.com/courses/play-by-play-lea-verou). I was able to create a cool mousedown interface that allowed the "player" to draw on the board by dragging the mouse while it was down, and wanted to do the same for touch devices.

Unfortunately, touch events only hold references to the DOM element where they began (i.e. where "touchstart" fired) and do not recognize the DOM elements that exist under them (a detailed explanation for this can be found here: http://stackoverflow.com/questions/4550427/prefered-alternative-to-onmouseover-for-touch).

Instead, touch events maintain several lists of Touch objects - which represent interactions with the touch screen. Among these objects' properties are clientX and clientY, which indicate the X and Y coordinates of the events relative to the viewport (go to http://www.javascriptkit.com/javatutors/touchevents.shtm

@VehpuS
VehpuS / lazy_dict.py
Last active April 21, 2024 11:08
An implementation for a dictionary with values that are evaluated only on their first access (or when accessing their values using .values).
import inspect
import types
class lazy_dict(dict):
'''
@summary:
An object that allows to store in a key the return results of functions when they are accessed for the first
time (AKA lazy evaluation).
You may use this storage mechanism by passing functions or lambdas without parameters.
@VehpuS
VehpuS / extendable_proxy_class.py
Created August 31, 2016 14:53
Extendable Proxy Class - a class that behaves like another class, but can be extended with attributes / methods as needed.
# Proxy class created by "tomer filiba" at http://code.activestate.com/recipes/496741-object-proxying/
class Proxy(object):
__slots__ = ["_obj", "__weakref__"]
def __init__(self, obj):
object.__setattr__(self, "_obj", obj)
#
# proxying (special cases)
#
def __getattribute__(self, name):
return getattr(object.__getattribute__(self, "_obj"), name)
import requests # pip install requests
from functools import reduce
import json
from pprint import pprint, pformat
def create_mlnx_os_session(switch_ip, user_name, password, https=True, verify=False):
'''
@summary:
Using the requests library create a session with a MLNX-OS running machine, that can serve both web and JSON requests
@param switch_ip:
@VehpuS
VehpuS / chromeExtensionFacebookSend.js
Created November 28, 2017 02:11
A function that allows a chrome extension to create a Facebook send message window with a pre-filled message, logging that message to the extension's console.
const sendMessage = (recipientID, link, message) => {
const url = ("http://www.facebook.com/dialog/send?" +
"app_id=" + appId +
"&link=" + encodeURI(link) +
"&to=" + recipientID +
"&display=popup" +
"&redirect_uri=" + encodeURI(link));
chrome.tabs.create({ url }, (tab) => {
const msg_val = 'document.querySelector("#feedform_user_message").value';
chrome.tabs.executeScript(tab.id, {
@VehpuS
VehpuS / dns_server.py
Created December 11, 2017 16:38
Based on https://github.com/samuelcolvin/dnserver - a DNS server in python with convenient parameters, and lots of documentation - for use and learning.
#!/usr/local/bin/python3
# -*- python -*-
'''
Owner : Moshe J. Gordon Radian
Description : DNS utilities using dnslib.
Based on: https://github.com/samuelcolvin/dnserver
Some resources about DNS:
- http://www.slashroot.in/what-dns-zone-file-complete-tutorial-zone-file-and-its-contents
- https://computer.howstuffworks.com/dns.htm
@VehpuS
VehpuS / pasteArea.js
Last active October 4, 2019 12:50
Create an html element that allows pasting images to a webpage. Fiddle running the example code: https://jsfiddle.net/9unkzh0q/
document.getElementById('pasteArea').onpaste = function (event) {
console.log(event);
// use event.originalEvent.clipboard for newer chrome versions
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
JSON.stringify(items)
// find pasted image among pasted items
var blob = null;
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") === 0) {
blob = items[i].getAsFile();
def partition (count, total, steps=1, minimum=0, maximum=None, sum_total=True):
'''
@summary:
Given a number "total" and number of parts to split it to "count", return a count-length tuple made of numbers that sum up to "total".
Steps defines in what increments to increase each "bin", starting with 0 or minimum sized bins to bins of size total or maximum.
If sum total is True - only partitions summing to total will return, otherwise, partitions with lower sums will be returned as well.
@param count:
How many bins to split total into.
@param total:
The total size to split into bins.
@VehpuS
VehpuS / bit_index_string.c
Last active May 10, 2018 07:44
OutstandingSmartArtificialintelligence created by VehpuS - https://repl.it/@VehpuS/OutstandingSmartArtificialintelligence
#include <stdio.h>
char txt[] = {
[(0 << 0) ... ((1 << 0) - 1)] = '0',
[(1 << 0) ... ((1 << 1) - 1)] = '1',
[(1 << 1) ... ((1 << 2) - 1)] = '2',
[(1 << 2) ... ((1 << 3) - 1)] = '3',
[(1 << 3) ... ((1 << 4) - 1)] = '4',
[(1 << 4) ... ((1 << 5) - 1)] = '5',
'\0'