Skip to content

Instantly share code, notes, and snippets.

Avatar
🛠️
WiP

Sergey G. Brester sebres

🛠️
WiP
View GitHub Profile
@sebres
sebres / bot-bridge-nicks.lua
Last active Nov 5, 2020
bot-bridge-nicks.lua -- HexChat plug-in to wrap IRC bots to real user nicknames
View bot-bridge-nicks.lua
hexchat.register("bot-bridge-nicks", "0.1", "Rewrites messages of bots to display bridge users as IRC users")
-- ijchain, ischain are bridges:
botspattern = {"i.*chain", "^<(.-)> (.*)$", "^(.-) (.*)$"}
local function fullname(user, nick)
return user:gsub("%s+", "-").."!"..user:gsub("%s+", "_").."@behind.the.bridge/"..nick
end
local function on_message(args)
@sebres
sebres / cm.tcl
Created Oct 9, 2020
cm.tcl -- rich (data mining & science) console mode example (shorten, wrap or suppress an output return value in tcl)
View cm.tcl
proc cm args {
array set in {-out auto}
array set in $args
set scr ""
puts -nonewline "%cm% "; flush stdout
while {[gets stdin line] >= 0 && ($line ni {/cm /exit} || $scr ne "")} {
append scr $line "\n"
if {![info complete $scr]} {
puts -nonewline " ... "; flush stdout
continue
@sebres
sebres / poparse.tcl
Created Oct 7, 2020
poparse.tcl -- PO files (Portable Object) (GNU gettext format) parser
View poparse.tcl
#**************************************************************************
# parse
# Internally used proc to parse PO files (Portable Object) (GNU gettext
# format).
# Parameters:
# f_ --
#**************************************************************************
proc parse { f_ } {
## TODO [SB]: file should be read partially, possible with other RE (non greedy ".*?" - could be slow by big files) :
set text_ [read $f_]
@sebres
sebres / lpop.tcl
Created May 11, 2020
lpop -- definition for lpop command (compatible to TIP 523 implementation)
View lpop.tcl
if {[namespace which -command ::lpop] eq {}} {
proc ::lpop {listvar {index end} args} {
upvar $listvar l
if {![info exists l]} {
return -code error "can't read \"$listvar\": no such variable"
}
if {![llength $args]} {
if {$index eq "end"} {
if {![llength $l]} {
return -code error -errorcode {TCL VALUE INDEX OUTOFRANGE} "index \"$index\" out of range"
@sebres
sebres / test-concurrent-ipset.tcl
Last active Apr 3, 2020
test-concurrent-ipset.tcl -- test script concurrently flooding IPs in ipset(s)
View test-concurrent-ipset.tcl
#!/usr/bin/env tclsh
package require Thread
array set in {-max-threads 4 -single-set 0 -max-ips 255 -iterations 3 -debug 0}
array set in $::argv
set commoncode {
if {$in(-single-set)} {
proc thipset {n} {return "f2b-test"}
@sebres
sebres / threads-communication.tcl
Created Mar 3, 2020
threads-communication.tcl -- simplest example how workers can communicate with master
View threads-communication.tcl
# task params for all threads
tsv::array set params {one 1 two 2}
# worker task:
set th_task {
if {[catch {
# do some work (here sum of params):
set res [expr {[tsv::get params one] + [tsv::get params two]}]
# provide result to master:
thread::send -async $master [list lappend th_res $res]
@sebres
sebres / collatz-conjecture-iter.py
Last active Jan 8, 2020
collatz-conjecture-iter.py -- simplest Collatz conjecture iterator in python
View collatz-conjecture-iter.py
# collatz-conjecture-iter.py -- simplest Collatz conjecture iterator in python
# Author: Serg G. Brester aka sebres
import math
f = lambda x: 3*x+1 if x & 1 else x/2
def fcci(x):
"""single collatz conjecture iteration starting by given x
"""
@sebres
sebres / range.tcl
Created Oct 23, 2019
(experimental) range -- create a list generating a sequence of numbers or strings
View range.tcl
if {[namespace which -command ::tcl::mathfunc::strincr] eq ""} {
proc ::tcl::mathfunc::strincr {v {increment}} {
string trimleft [binary format I* [expr {"0b[binary scan $v B* v; set v]" + $increment}]] \x00
}
}
proc range args {
set op "<"; set step 1
switch -exact -- [llength $args] \
1 { lassign $args to; set from [expr {$to*0}] } \
2 { lassign $args from to;
@sebres
sebres / ldeclare.tcl
Created Oct 17, 2019
ldeclare -- multi-line list declaration with simple substitution (and comments)
View ldeclare.tcl
# -------------------------------------------------------------------------------
# ldeclare -- multi-line list declaration with simple substitution (and comments)
# -------------------------------------------------------------------------------
proc ldeclare l {
uplevel "list [string map [list \n "\\\n"] \
[regsub -all -line {^\s*\#[^\n]*} $l {}]
]"
}
# -------------------------------------------------------------------------------
@sebres
sebres / multi-thread-eval-tcl-vs-python.tcl
Created Sep 25, 2019
Multi-threaded evaluation Tcl vs. Python (with GIL)
View multi-thread-eval-tcl-vs-python.tcl
#
# IMPORTANT:
# note to get correct results you should warm up Tcl and Python,
# so better execute it 3 times before you take results for
# the comparison
## ============================================================
## Tcl
## ============================================================
You can’t perform that action at this time.