Skip to content

Instantly share code, notes, and snippets.

@mmikhan
Last active June 4, 2023 21:20
Show Gist options
  • Save mmikhan/6189f88b00eacb7a69939134b68ebd45 to your computer and use it in GitHub Desktop.
Save mmikhan/6189f88b00eacb7a69939134b68ebd45 to your computer and use it in GitHub Desktop.
Workday Random Numbers Generator
#!/usr/bin/env python3
"""
Workday Random Numbers Generator
This script generates random floating-point numbers between 8 and 10,
rounded to 2 decimal places, only for workdays in a specified month and year.
Since the script usually runs from the first week of the following month, the
default month and year are set to the previous month and year. The script also
highlights weekends in red. The output is printed to the console and copied to
your clipboard.
Instructions to make the script executable on macOS:
1. Save your script, for example, as `workday_random_numbers.py`.
2. Open Terminal and navigate to the directory containing your script.
3. Make the script executable by running the following command in Terminal:
chmod +x workday_random_numbers.py
4. Add the script's directory to the `PATH` environment variable by adding the
following line to your `~/.bash_profile` or `~/.zshrc` file:
export PATH="$PATH:/path/to/your/script/directory"
Replace `/path/to/your/script/directory` with the actual path to the directory
containing your script.
5. Save the changes and restart Terminal, or run `source ~/.bash_profile` (bash)
or `source ~/.zshrc` (zsh) to apply the changes immediately.
6. Run the script from anywhere in the Terminal by typing its name:
workday_random_numbers.py
Note: Ensure the numpy, pandas, and pyperclip libraries are installed in your
Python environment.
"""
import numpy as np
import pandas as pd
import pyperclip
import datetime
def generate_workday_random_numbers(
year: int = pd.Timestamp.now().year,
month: int = pd.Timestamp.now().month,
start_number: float = 8,
end_number: float = 10,
) -> np.ndarray:
"""
Generate random numbers for each workday of the specified month.
Parameters
----------
year : int, optional
Year, by default pd.Timestamp.now().year
month : int, optional
Month, by default pd.Timestamp.now().month
start_number : float, optional
Start number, by default 8
end_number : float, optional
End number, by default 10
Returns
-------
np.ndarray
Array of random numbers for each workday of the specified month.
"""
# Generate date range for the specified month
start_date = pd.Timestamp(year, month, 1)
end_date = start_date + pd.offsets.MonthEnd(0)
date_range = pd.date_range(start_date, end_date)
# Generate random numbers or 0 depending on the day of the week
random_numbers = [
np.around(np.random.uniform(start_number, end_number), decimals=2)
if day.weekday() < 5
else 0
for day in date_range
]
# Combine dates and random numbers
date_random_numbers = list(zip(date_range, random_numbers))
output = ""
clipboard_output = ""
for date, number in date_random_numbers:
if date.weekday() >= 5:
# Highlight weekends using ANSI escape code for red color
output += f"\033[91m{date.date()}, {date.day_name()}: {number}\033[0m\n"
else:
output += f"{date.date()}: {number}\n"
clipboard_output += f"{number}\n"
return output, clipboard_output
if __name__ == "__main__":
today = datetime.date.today()
last_month = today.replace(day=1) - datetime.timedelta(days=1)
month = last_month.month
year = last_month.year
start_number = 8
end_number = 8.5
result, clipboard_result = generate_workday_random_numbers(
year, month, start_number, end_number
)
print(result)
# Copy only the numbers to the clipboard
pyperclip.copy(clipboard_result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment