Skip to content
Snippets Groups Projects
Commit 355e2ca0 authored by Caty Jeanne's avatar Caty Jeanne
Browse files

hey

parent 2ba40be7
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
%% Cell type:code id: tags:
``` python
import sqlite3
# Connect to the database
conn = sqlite3.connect('company.db')
# Create a cursor object
cursor = conn.cursor()
print("Connected to company.db")
```
%% Output
Connected to company.db
%% Cell type:code id: tags:
``` python
import pandas as pd
from datetime import datetime, timedelta
chrismas_day = datetime(2024, 12, 25)
cursor.execute("SELECT NUMERO_EMPLOYE, NOM, PRENOM, CS_PRODUCTION,CS_JOURNEE, DATE_DEBUT FROM EMPLOYE")
employees = cursor.fetchall()
# Create a list to store the data
prime_totale = []
prime_partielle=[]
def generate_mondays(start_date):
"""
Génère une liste de lundis à partir d'une date donnée jusqu'à un an après.
:param start_date: Date de début au format 'YYYY-MM-DD'
:return: Liste de lundis au format 'YYYY-MM-DD'
"""
# Convertir la date de départ en objet datetime
start = datetime.strptime(start_date, '%Y-%m-%d')
end_date = start + timedelta(days=365)
# Trouver le premier lundi (si la date donnée n'est pas un lundi)
if start.weekday() != 0: # Si ce n'est pas un lundi
start += timedelta(days=(7 - start.weekday())) # Aller au prochain lundi
# Générer les lundis
mondays = []
current_date = start
while current_date <= end_date:
mondays.append(current_date.strftime('%Y-%m-%d'))
current_date += timedelta(weeks=1) # Passer au lundi suivant
return mondays
mondays=generate_mondays('2024-12-04')
```
%% Output
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_3000\983263414.py in <module>
5
6 chrismas_day = datetime(2024, 12, 25)
----> 7 cursor.execute("SELECT NUMERO_EMPLOYE, NOM, PRENOM, CS_PRODUCTION,CS_JOURNEE, DATE_DEBUT FROM EMPLOYE")
8 employees = cursor.fetchall()
9 # Create a list to store the data
ProgrammingError: Cannot operate on a closed database.
%% Cell type:code id: tags:
``` python
# Calculate the prime de noel for each employee
for employee in employees:
NUMERO_EMPLOYE, NOM, PRENOM,CS_PRODUCTION,CS_JOURNEE, DATE_DEBUT = employee
start_date = datetime.strptime(DATE_DEBUT, '%Y-%m-%d')
delta = chrismas_day - start_date
for
if delta.days >= 365:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_noel= salaire_base*4.5
prime_totale.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_noel= salaire_base*4.5
prime_totale.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
else:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_noel= max(0, salaire_base * delta.days / 365 )
prime_partielle.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_noel=max(0, salaire_base*100 * delta.days / 365 )
prime_partielle.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, prime_noel))
# Create the dataframe
prime_totale = pd.DataFrame(prime_totale, columns=['NUMERO_EMPLOYE','NOM', 'PRENOM' ,'DATE_DEBUT', 'PRIME_NOEL'])
prime_partielle = pd.DataFrame(prime_partielle, columns=['NUMERO_EMPLOYE','NOM', 'PRENOM' , 'DATE_DEBUT', 'PRIME_NOEL'])
prime_totale.head()
```
%% Cell type:code id: tags:
``` python
import sqlite3
# Connexion à la base de données
conn = sqlite3.connect('company.db')
cursor = conn.cursor()
# Création de la table LUNDIS si elle n'existe pas déjà
cursor.execute("""
CREATE TABLE IF NOT EXISTS LUNDIS (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date_lundi TEXT UNIQUE
)
""")
# SQL avec WITH RECURSIVE pour générer les lundis
recursive_sql = """
WITH RECURSIVE lundis(date_lundi) AS (
SELECT DATE('2024-12-02') -- Premier lundi
UNION ALL
SELECT DATE(date_lundi, '+7 days')
FROM lundis
WHERE date_lundi < '2026-12-02' -- Limite à 1 an
)
INSERT INTO LUNDIS (DATE_LUNDI)
SELECT date_lundi
FROM lundis
WHERE date_lundi NOT IN (SELECT DATE_LUNDI FROM LUNDIS); -- Évite les doublons
"""
# Exécution de la requête
cursor.executescript(recursive_sql)
# Vérification des résultats
cursor.execute("SELECT * FROM LUNDIS ORDER BY DATE_LUNDI;")
lundis = cursor.fetchall()
# Affichage des lundis insérés
for lundi in lundis:
print(lundi)
# Sauvegarde des changements
conn.commit()
# Fermeture de la connexion
conn.close()
```
%% Cell type:code id: tags:
``` python
import sqlite3
import pandas as pd
from datetime import datetime, timedelta
# Fonction pour générer les lundis
def generate_mondays(start_date):
"""
Génère une liste de lundis à partir d'une date donnée jusqu'à un an après.
:param start_date: Date de début au format 'YYYY-MM-DD'
:return: Liste de lundis au format 'YYYY-MM-DD'
"""
start = datetime.strptime(start_date, '%Y-%m-%d')
end_date = start + timedelta(days=365)
# Ajuster au premier lundi
if start.weekday() != 0:
start += timedelta(days=(7 - start.weekday()))
mondays = []
current_date = start
while current_date <= end_date:
mondays.append(current_date.strftime('%Y-%m-%d'))
current_date += timedelta(weeks=1)
return mondays
# Utilisation de SQLite
try:
# Connexion à la base de données
conn = sqlite3.connect('company.db')
cursor = conn.cursor()
print("Connected to company.db")
# Génération des lundis
mondays = generate_mondays(datetime.now().strftime('%Y-%m-%d'))
# Exemple d'opérations avec les employés
chrismas_day = datetime(2024, 12, 25)
cursor.execute("SELECT NUMERO_EMPLOYE, NOM, PRENOM, CS_PRODUCTION, CS_JOURNEE, DATE_DEBUT FROM EMPLOYE")
employees = cursor.fetchall()
prime_totale = []
prime_partielle = []
Bonus=[]
for employee in employees:
NUMERO_EMPLOYE, NOM, PRENOM, CS_PRODUCTION, CS_JOURNEE, DATE_DEBUT = employee
start_date = datetime.strptime(DATE_DEBUT, '%Y-%m-%d')
for LUNDI in mondays:
lundi = datetime.strptime(LUNDI, '%Y-%m-%d')
delta = lundi - start_date
delta_days = delta.days
delta_noel =lundi - chrismas_day
delta_noel_days=delta_noel.days
prime_noel=0
prime_anniversaire=0
if 0 <= delta_noel_days%365<= 6:
if delta.days >= 365:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_noel= salaire_base*4.5
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_noel= salaire_base*4.5
else:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_noel= max(0, salaire_base * delta.days / 365 )
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_noel=max(0, salaire_base*100 * delta.days / 365 )
if 0 <= abs(delta_days%365)<= 6:
if CS_JOURNEE is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_HORAIRE WHERE CS_HORAIRE = {CS_JOURNEE}")
salaire_base=cursor.fetchone()[0]
prime_anniversaire= salaire_base
if CS_PRODUCTION is not None:
cursor.execute(f"SELECT SALAIRE_SEMAINE_BASE FROM GRILLE_SALAIRE_PROD WHERE CS_PRODUCTION = {CS_PRODUCTION}")
salaire_base=cursor.fetchone()[0]
prime_anniversaire= salaire_base
Bonus.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, LUNDI, prime_noel+prime_anniversaire))
Bonus.append((NUMERO_EMPLOYE, NOM, PRENOM, DATE_DEBUT, (datetime.datetime.strptime(LUNDI, "%Y-%m-%d") - datetime.timedelta(days=7), prime_noel+prime_anniversaire))
# Sauvegarde des lundis dans une table de la base
cursor.executemany("INSERT OR IGNORE INTO LUNDIS (date_lundi) VALUES (?)", [(monday,) for monday in mondays])
conn.commit()
print("Mondays added to the database.")
# cursor.executemany("INSERT OR IGNORE INTO LUNDIS (date_lundi) VALUES (?)", [(monday,) for monday in mondays])
# conn.commit()
# pri<nt("Mondays added to the database.")
except sqlite3.Error as e:
print(f"SQLite error: {e}")
# except sqlite3.Error as e:
# print(f"SQLite error: {e}")
```
%% Output
Connected to company.db
Mondays added to the database.
File "C:\Users\jeann\AppData\Local\Temp\ipykernel_3000\817864059.py", line 103
^
SyntaxError: unexpected EOF while parsing
%% Cell type:code id: tags:
``` python
Bonus = pd.DataFrame(Bonus, columns=['NUMERO_EMPLOYE','NOM', 'PRENOM' ,'DATE_DEBUT', 'LUNDI','PRIMES'])
Bonus.head()
```
%% Output
NUMERO_EMPLOYE NOM PRENOM DATE_DEBUT LUNDI PRIMES
0 1 Dubois Michel 2020-11-13 2024-12-09 0.0
1 1 Dubois Michel 2020-11-13 2024-12-16 0.0
2 1 Dubois Michel 2020-11-13 2024-12-23 0.0
3 1 Dubois Michel 2020-11-13 2024-12-30 2160.0
4 1 Dubois Michel 2020-11-13 2025-01-06 0.0
%% Cell type:code id: tags:
``` python
datetime.now().strftime('%Y-%m-%d')
```
%% Output
'2024-12-04'
%% Cell type:code id: tags:
``` python
```
......
......@@ -2684,7 +2684,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
......@@ -2698,9 +2698,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.0"
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment