Skip to content

Instantly share code, notes, and snippets.

@Infinitusvoid
Infinitusvoid / option_exec.py
Last active August 30, 2023 21:24
Python : How to load and import code form file ?
def execute_file_content(file_path):
if file_path:
with open(file_path, 'r') as file:
code = compile( file.read(), file_path, 'exec')
exec(code, globals())
print("loaded :", file_path)
execute_file_content(r"C:\Users\x\my_module.py")
#some_func()
@Infinitusvoid
Infinitusvoid / main.cpp
Last active August 30, 2023 21:30
C++20 : How to execute python from Cpp ?
#include <iostream>
#include <cstdlib>
int executePythonFromCpp(const std::string& file_path_to_python, const std::string& file_path_to_script)
{
std::string command = file_path_to_python + " " + file_path_to_script;
int result = system(command.c_str());
// Check the result of the system call
if (result == 0) {
@Infinitusvoid
Infinitusvoid / main.cpp
Last active March 4, 2023 21:41
C++ : random glm::vec3, float
#include <cstdlib> // for rand and RAND_MAX
#include <glm/glm.hpp>
glm::vec3 generate_random_position(const glm::vec3& min_value, const glm::vec3& max_value)
{
glm::vec3 result;
result.x = min_value.x + static_cast<float>(rand()) / static_cast<float>(RAND_MAX) * (max_value.x - min_value.x);
result.y = min_value.y + static_cast<float>(rand()) / static_cast<float>(RAND_MAX) * (max_value.y - min_value.y);
result.z = min_value.z + static_cast<float>(rand()) / static_cast<float>(RAND_MAX) * (max_value.z - min_value.z);
return result;
@Infinitusvoid
Infinitusvoid / main.cpp
Last active August 30, 2023 21:25
C++ : How to generate a random int between min max ?
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
int generate_random_int(int min_value, int max_value)
{
srand(time(nullptr)); // seed the random number generator with the current time
int range = max_value - min_value + 1;
int random_value = rand() % range + min_value; // generate a random integer in the specified range
return random_value;
}
@Infinitusvoid
Infinitusvoid / main.cpp
Created February 15, 2023 22:21
C++ : How to write WAV audio file?
#include <iostream>
#include <fstream>
#include <cmath>
int write_wav_sound()
{
const int SAMPLE_RATE = 44100;
const double PI = 3.14159265359;
const double FREQUENCY = 440.0;
const double DURATION = 2.0;
@Infinitusvoid
Infinitusvoid / app.js
Last active February 15, 2023 09:42
Javascript : How to transform epoch time into date?
//To convert this timestamp to a human-readable date and time, you can use JavaScript's Date object. Here's an example:
//The output of this code will be a string representation of the date and time in the local time zone of the computer running the code.
const timestamp = 1676456193458;
const date = new Date(timestamp);
console.log(date);
@Infinitusvoid
Infinitusvoid / main.cpp
Last active May 14, 2024 14:32
C++ : Timing
#include <iostream>
#include <chrono>
#include <thread>
struct Timer
{
std::chrono::time_point<std::chrono::steady_clock> start, end;
std::chrono::duration<float> duration;
Timer()
@Infinitusvoid
Infinitusvoid / main.cpp
Created January 15, 2023 22:30
C++ : How to track memory allocations in code?
#include <iostream>
#include <memory>
// we override the new operator globally
// By overring the new operator we ara saying don't use the new operator that is in the standard library
struct AllocationMetrics
{
uint32_t total_allocated = 0;
uint32_t total_freed = 0;
@Infinitusvoid
Infinitusvoid / main.cpp
Created January 15, 2023 21:11
C++ : Implicit conversion example, explicit keyword
#include <iostream>
namespace Without_explicit
{
struct Entity
{
int a;
int b;
@Infinitusvoid
Infinitusvoid / main.cpp
Created January 15, 2023 20:53
C++ : Pure virtual functions ( Interfaces )
#include <iostream>
#include <string>
struct IDescription
{
virtual std::string get_description() = 0;
};
struct Gallery : public IDescription