Tech Bites

Optuna - Hyperparameter Optimization

Optuna - Hyperparameter Optimization

Optuna is a Python library for automated parameter optimization using adaptive search instead of manual tuning.

It shines whenever:

  • You have multiple knobs to tune.
  • The system returns one final numeric score.

This pattern shows up everywhere: machine learning metrics, simulation outcomes, and trading strategy ROI.

What Is a Hyperparameter?

A hyperparameter is a setting you choose before running an evaluation.

Examples:

  • A model’s learning rate or tree depth.
  • A trading rule’s lookback window or threshold.
  • A simulator’s step size or penalty weight.

Unlike learned parameters (like model weights), hyperparameters are not fit directly by gradient descent. You set them, run the system, and observe a final score.

Walrus Operator

Walrus Operator

The walrus operator (:=) in Python, introduced in Python 3.8, is also known as the assignment expression. It returns the value as well as assigning it. This can reduce many lines of code and greatly simplifies the language.

Example

# Without Walrus
numbers = [1,2,3]

numbers_length = len(numbers)
numbers_sum = sum(numbers)

numbers_description = {
    "length": numbers_length
    "sum": numbers_sum
}

# With Walrus - see how we save the variables and return the value
numbers = [1,2,3]

numbers_description = {
    "length": numbers_length := len(numbers)
    "sum": numbers_sum := sum(numbers)
}

Another Example

# Example code to read lines from a file and process non-empty lines
with open('example.txt', 'r') as file:
    while (line := file.readline().strip()):
        print(f"Processing line: {line}")

Managing Hatch Dependencies in VS Code

Managing Hatch Dependencies in vs Code

I recently started using hatch for python projects, and even though hatch will manage and install dependencies for you (just modify the pyproject.toml file), I noticed the import statements could not find the dependencies. This has to do with how Python and Hatch create virtual environments.

To fix this:

CMD + Shift + P
Python: Select Interpreter
Find the Python Environment that matches the Hatch environment.

Git Hooks

Git Hooks

Git hooks are scripts that Git automatically executes before or after specific events, such as committing changes or pushing to a repository. They allow you to customize and automate tasks related to these events, such as enforcing code style rules, running tests, or sending notifications. Hooks are useful for maintaining code quality and ensuring consistent workflows across a team. They can be set up in the .git/hooks directory of your repository and include both client-side and server-side hooks.

Hatch - a great python project management tool

Hatch - A Great Python Project Management Tool

Hatch is a python packaging tool. Useful for building python projects. Out of the box it supports testing, building, managing dependencies, and linting/formatting support.

https://wwww.hatch.pypa.io/latest.intro

New Project

hatch new “Hatch Demo”

Adding to Existing Project

hatch new –init

Build a project

hatch build

Test a project with coverage

hatch test –cover

Running static analysis

hatch fmt

Open a shell in the project to run scripts

hatch shell

Create a python virtual env

python3 -m venv /tmp/hatch_demo/

Recursion

Recursion

Definition of Recursion:

Recursion is a programming technique where a function calls itself in order to solve smaller instances of the same problem until it reaches a base case that does not require further recursion.

Example in Python:

def factorial(n):
    """Calculate the factorial of a number using recursion."""
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

# Example usage:
result = factorial(5)
print(f"The factorial of 5 is: {result}")

In this example, the factorial function calculates the factorial of a number n recursively. It calls itself with n - 1 until n reaches 0 (base case).

How To Search (And Replace) Quickly Across Directories

How to Search (And Replace) Quickly Across Directories

To find things quickly - use the silver searcher.

brew install the_silver_searcher

To find things:

ag 'the thing to find'

To see the count:

ag 'the thing to find' --count

To see the list of files only and search on a literal string:

ag 'the thing to find' -Q -l

To then replace the occurrences of the found use:

ag 'thething' -Q -l|xargs sed -i 's/thething/bar/g'

Safely retrieve properties using Supplier and Optional in Java

Safely Retrieve Properties Using Supplier and Optional in Java

The supplier allows you to pass something but not retrieve it right away. One use case is retrieving a property an object that could throw a null pointer exception. By not retrieving right away, but instead using a supplier, the supplier can be later executed inside a try/catch block and handle any exceptions thrown.

public void example() {
    // Without supplier - could throw a nullpointer exception on any getter
    try {
        String str = someObject.getLevel1().getLevel2().getLevel3().getString();
        someOtherObject.appendValue(str);
    catch (Exception e) {
        // Exception throw - do nothing
        // This code is ugly!!!
    }

    // With supplier
    Optional<String> result = resolve() => someObject.getLevel1().getLevel2().getLevel3().getString());
    // Now we can do something if the property is present or skip if it doesn't exist
    // Much cleaner code
    result.ifPresent(someOtherObject::appendValue);
}


// This resolve method can be reused
protected <T> Optional<T> resolve(Supplier<T> supplier) {
    try {
        T result = resolver.get();
        return Optional.ofNullable(result);
    } catch (NullPointerException) {
        return Optional.empty();
    }
}

Function Composition in Java

Function Composition in Java

An incredible tool in Java is the ability to create functions and compose them.

One common use case is to extract a variable from an object and then transform it.

In the object oriented paradigm, you would have a method that extracts and another that transforms. Then intermediate variables would be used to convert from the initial object to the final transformed result.

With function composition this is greatly simplified. The separate steps can be defined as functions and then composed together to create the chain of events that need to occur. Then, the final composed function can be used instead of manually calling each method. See the examples below.

Eclipse Gotcha: Refactor->Move not showing up in Git

Eclipse Gotcha: Refactor->Move Not Showing Up in Git

In eclipse, if you refactor->move a file from one project to another, it may not show up in your eclipse git staging as deleted from the original project.

To Avoid: Copy the file to a different project and then delete from the original.

To Fix: Use the command line to delete the file (it will no longer show up in eclipse, but the command line will display it) and then use git from then command line to push the change.

Set Interval Timer

Set Interval Timer

Be careful when using setInterval as a timer.

let timeInSeconds = 0;
let interval = setInterval(() => {
    someFunction();
    timeInSeconds++;
}, 1000);

While it appears your time increments every second, it will actually increment one second (1000 milliseconds) plus the time the someFuntion() takes to run. This timer will eventually be out of sync.

A better approach is to create a point in time when the timer starts and then subtract the difference anytime the timeInSeconds is updated. In this scenerio, you can set the interval to any milliseconds amount and get an accurate time everytime the timeInSeconds variable is updated.

React Reducer Runs Twice

React Reducer Runs Twice

If using the react hook useReducer(reducer, state) and you notice it runs twice after dispatching an action - this is actually by design for development mode.

The reducer dispatches an action with a given state and returns a new state. It should never mutate the previous state.

If you follow this rule, you’ll never notice the reducer running twice. However if you are mutating the state and returning the new state based off that, you’ll make the mutation twice. So if you are adding something to a list in the state, you’ll see it added twice. This feature of react development keeps our functions pure. Pure functions are much easier to test.

Maintaining List Order in Jpa

Maintaining List Order in Jpa

If you want to maintain the order of a list of objects retrieve from the database using hibernate/jpa, use the @OrderColumn annotation. An additional column will be created in the database (if ddl is set to update) that will keep track of the order/position of an item in a list.

CAVEAT: If you change the position of an item in a list OR delete an item besides the last one, it will cause a multi update across the list to update all other positions. Be aware of this intensive operation for large lists.

Postgres: Useful PSQL Commands

Postgres: Useful PSQL Commands

Connect to a database

psql -d dbname -U password

Create a database

CREATE DATABASE mydb;

Create a user

create user myuser with encrypted password 'mypasswd';

Grant Privileges

grant all privileges on database mydb to myuser;

To list databases

\l

To list users

\du

List all tables

\dt

Describe tables

\d

List schemas

\dn

Switch database

\c dbName

Run psql commands from file \i fileName

Check version

SELECT VERSION();

Quit

\q

SQL Injection

SQL Injection

SQL injection occurs when SQL code can be injected into API input. In this injection attack, valid input has SQL commands concatened with SQL execution commands. When the SQL code is executed, the commands are run. In this process data can be mutated and returned to the attacker.

Mitigation Tips:

  • Use prepared statements instead of concatenated SQL statements. This seperates inputs from the command.
  • Restrict the user account that the SQL command is executed with to only allowable actions.
  • Always sanitize input on the server.
  • Never trust the client input to have sanitized input, it can be exploited by an attacker.
  • Use mature libraries for data sanitization, there are too many variants to look for to write custom sanitation code.