Random snippets of all sorts of code, mixed with a selection of help and advice.
How to control zigbee devices directly from code?
29 November 2025 @ 9:53 am
I want to create smart home automation system like home assistant. I know about zigbee2MQTT, but I think it would be better to control everything directly. How to do this, I know Assembly, C, Java, Python
During red–black tree insertion fix-up, when (if ever) does the black-height of nodes change?
29 November 2025 @ 9:39 am
Here’s a cleaned-up Stack Overflow version you can copy–paste and tweak:
I’m studying red–black trees (CLRS style) and I’m confused about how black-height behaves during RB-INSERT-FIXUP.
Definitions I’m using (same as CLRS):
Leaves are the sentinel nil nodes, which are black.
For a node x, the black-height bh(x) is the number of black nodes on any simple path from x down to a leaf, excluding x itself.
All paths from x to its leaves contain the same number of black nodes, so bh(x) is well defined.
Consider the usual insertion Case 1 (parent is a left child and the uncle is red). Before the fix-up, the situation is:
C (black)
/ \
A D
/ \
z ...
where:
How to avoid per-process memory duplication when using rapidgzip with multiprocessing in Python?
29 November 2025 @ 9:39 am
I have a Python file:
from concurrent.futures import ProcessPoolExecutor
import tarfile, rapidgzip
def processNdjson(ndjsonName):
with rapidgzip.open(inTarDir) as myZip:
myZip.import_index(rapidgzipDir)
with tarfile.open(fileobj=myZip, mode="r:*") as f:
member = f.getmember(ndjsonName)
dataFile = f.extractfile(member)
for oneLine in dataFile:
# process oneLine here
if __name__ == "__main__":
rapidgzipDir = ...
inTarDir = ...
rapidgzipDir = ...
nCore = 5
ndjsonNames = ["name1.ndjson", "name2.ndjson"]
with ProcessPoolExecutor(nCore) as pool:
results = pool.map(worker, ndjsonNames)
Above,
inTarDir is the directory to a .tar.gz file that contains multiple .ndjson files.
rapidgzipDir is the pre-index file to be used by
Counting Robot Container Stacking Operations in C++
29 November 2025 @ 9:26 am
I’m trying to solve a problem where a robot stacks containers in a warehouse, and I want to calculate the number of operations it performs. The rules are:
There are N containers arranged in a single row.
Each container has a size Ai (1 ≤ Ai ≤ M).
The robot can pick a container and place it inside a larger container to its right.
A container that already contains another cannot be used again.
I need to count the total number of robot operations (nestings).
Input:
First line: two integers M (max container size) and N (number of containers).
Second line: N integers A1, A2, ..., AN representing the container sizes in order.
Output:
One integer: the number of robot operations performed.
Scope Tailwind to certain element
29 November 2025 @ 6:29 am
New to Tailwind and I am asking this as Tailwind is defined as a framework. Not sure if it's feasible but can we scope tailwind to particular div?
Putting this in a bit detail as you can understand where I stand.
I have got this div with custom css-
<div id="contend">
<div id='contendcustomcss">
<!--- contend here owned by another team. Works perfectly. Cannot touch --->
</div>
<div>
Now my team is late to the party
<div id="contend">
<div id='contendcustomcss">
<!--- contend here owned by another team. Works perfectyly. Cannot touch --->
</div>
<div id='contendtailwindcss">
<!--- contend owned by my team. Active work happening here --->
</div>
<div>
Is there any way to tell Tailwind, not to go anywhere inside "contendcustomcss"
How to modify mulitple columns applying if else to multiple pandas dataframe columns
29 November 2025 @ 3:19 am
I have a dataFrame with columns Age, Salary and others, if I used:
df['Age'] = df['Age'].apply(lambda x : x+100 if x>30 else 0)
Then I can modify the Age column with the if else condition. Also, if I used:
df[['Age', 'Salary']] = df[['Age', 'Salary']].apply(lambda x : x+100)
Then, I can apply the lambda equation to each column independently. But as soon as I use an if else condition on both columns as:
df[['Age', 'Salary']] = df[['Age', 'Salary']].apply(lambda x : x+100 if x>30 else 0)
Then I get the following error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
So, how can I modify the Age, Salary and n columns applying the same if else or other lambda condition to each column independently?
I know two possible solution are:
To use a for to call each column:
cols = ['Age', 'Salary'] for i i
AudioSystem,isAudioLineSupported returns false
29 November 2025 @ 2:34 am
I am trying to use some Java audio code that worked back in 2019. I am now using the Eclipse IDE with JavaSE-21 running on Windows 11. The following code is not working:
AudioFormat af = makeAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, af);
if (!AudioSystem.isLineSupported(info)) {
System.out.println("Unsupported audio format");
System.out.println(info);
}
The println outputs this information:
Unsupported audio format interface
TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, big-endian
I have tried mono/stereo and big-endian/little-endian: they all fail.
After looking at the Oracle documentation I tried this code:
if (AudioSystem.isLineSupported(Port.Info.MICROPHONE)) {
It also fails with Port.Info.LINE_IN.
Audio input definitely works with Audacity, and with t
How do I use Socket.io with Fastay? I can’t access the server instance
28 November 2025 @ 8:32 pm
I’m trying to run Fastay.js as a separate backend, and I really need Socket.io because my app depends on real-time features. But I’m stuck: Fastay doesn’t give me access to the underlying HTTP server, and Socket.io needs that to start.
Fastay boots up fine, but I have no way to grab the server instance to pass into Socket.io.Does anyone know how to fix this? Is there ANY way to access the internal server?
Really need to get this working ASAP. Thanks!
// src/index.ts
import { createApp } from '@syntay/fastay';
import { Server as SocketServer } from 'socket.io';
void (async () => {
const app await createApp({
apiDir: './src/api',
baseRoute: '/',
port: 3000
});
const io = new SocketServer(app); // breaks because "server" doesn't exist
io.on('connection', socket => {
console.log('User connected:', socket.id);
});
})();
Define runtype with npm lib "runtypes" that shall check a recursive typescript type
28 November 2025 @ 5:34 pm
I would like to define a runtype that is equivalent to the following static typescript type:
export type Tourney2 = {
type: 'flat';
name: string;
scoreType: TScoreType;
matches: string;
} | {
type: 'tree';
name: string;
scoreType: TScoreType;
subTourneys: Tourney2[];
}
I have tried some variants with Lazy, but they do not work and throw the following error when I try to call guard().
My last trial was:
import * as rt from 'runtypes';
export const ScoreType =
rt.Object({
type: rt.Literal('welove'),
min: rt.Number,
max: rt.Number,
divisor: rt.Number,
})
export type TScoreType = rt.Static<typeof ScoreType>;
export const Tourney: rt.Runtype<Tourney2> = rt.Union(
rt.Object({
type: rt.Literal('flat'),
name: rt.String,
scoreType: ScoreType,
matches: rt.String,
}),
rt.Object({
type: rt.Literal('tree'),
Cant open more than 8000 tcp connections with Java
28 November 2025 @ 2:27 pm
I am trying to open to open 10K+ connections to a server, but my app stops at 8K connections when looking at the server logs. When using two remote servers it will struggles when reaching between 8K and 9K connections. Making me belief the problem is on the client side.
I tried the following settings on the client:
Setting the ephemeral ports:net.ipv4.ip_local_port_range = 32768 60999
Setting the ulimit to 65535
But still my Java application can't open more than 8K outgoing connections.
Some experimenting showed that:
Doing a little less than 8K in rapid succession is fine.
Doing 8.1K requests causes a 30 delay before it continues with only little bursts.
During the tests the machine never runs out of CPU or memory. During the delay it goes back to running at an idle 1-2%.
No error or exception is being thrown.
The code: