Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awesome snippets #60

Open
Proteusiq opened this issue Jul 6, 2024 · 1 comment
Open

Awesome snippets #60

Proteusiq opened this issue Jul 6, 2024 · 1 comment
Assignees

Comments

@Proteusiq
Copy link
Owner

from typing import Callable

def dictionary_filter(*, blob:dict, callback:Callable) -> dict:
    """
    callable is a function with key, value arguments
    example: `dictionary_filter(blob=ex, callback=lambda key,value: key in ["hello",])`
    """
    
    result = {}
    for key, value in blob.items():
        if callback(key, value):
            result[key] = value
    return result

ex = {"hello": 1,
 "world": 2,
 "name": 4
}

dictionary_filter(blob=ex, callback=lambda key,value: value > 1)
@Proteusiq Proteusiq self-assigned this Jul 6, 2024
@Proteusiq
Copy link
Owner Author

SQL

WITH player_performance AS (
    SELECT 
        "match_date",
        "position",
        "player_id",
        "predicted_score",
        "game_id",
        recorded_at AS recorded_time,
        TO_TIMESTAMP(CONCAT("match_date", "match_time"), 'YYYY-MM-DDHH24:MI:SS') AS prediction_time
    FROM game_data
    JOIN score_predictions ON (game_data.id = "game_id")
    WHERE 
        "target_metric" = 'player_id' 
), filtered_performance AS (
    SELECT 
        "match_date",
        "game_id",
        "position",
        "player_id",
        "predicted_score",
        recorded_time,
        MAX(prediction_time) AS prediction_time
    FROM player_performance
    WHERE recorded_time > prediction_time
    GROUP BY "match_date", "position", "player_id", "predicted_score", "game_id", recorded_time
)
SELECT 
    "match_date",
    COUNT("game_id") FILTER (WHERE "position" = 0 AND "player_id" = "predicted_score") AS correct_predictions,
    COUNT("game_id") FILTER (WHERE "player_id" = "predicted_score") AS top_predictions,
    COUNT("game_id") FILTER (WHERE "position" = 0) AS total_predictions,
    CAST(COUNT("game_id") FILTER (WHERE "position" = 0 AND "player_id" = "predicted_score") AS FLOAT) / COUNT("game_id") FILTER (WHERE "position" = 0) AS accuracy,
    CAST(COUNT("game_id") FILTER (WHERE "player_id" = "predicted_score") AS FLOAT) / COUNT("game_id") FILTER (WHERE "position" = 0) AS top_accuracy
FROM filtered_performance
GROUP BY "match_date"
ORDER BY "match_date" DESC;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant