مراجعة مكتبة `time` في Python بشكل سهل وممتع وتفاعلي


مراجعة مكتبة time في بايثون (مع بعض الطرافة! :smile:)

هل تحتاج إلى تأخير عملية تنفيذ :hourglass_flowing_sand:، أو حساب الوقت الذي تستغرقه قطعة من الشيفرة :stopwatch:، أو حتى معرفة الوقت الحالي :watch:؟ مكتبة time هي الحل الأمثل في بايثون! دعونا نستعرض بعض الدوال المهمة في هذه المكتبة مع قليل من الفكاهة :nerd_face:.

1. time.sleep(seconds) :zzz:

تريد أن تمنح بايثون قيلولة؟ هذه الدالة ستؤجل تنفيذ الكود لعدد من الثواني التي تحددها. مثالية لأخذ قسط من الراحة خلال البرمجة! :sleeping:

import time

print("أريد أخذ قيلولة لمدة 3 ثواني... 😴")
time.sleep(3)
print("استيقظت! وقت لإكمال البرمجة. 🧑‍💻")

2. time.time() :clock3:

تريد معرفة الوقت الحالي (بالأرقام الكبيرة التي تُحتسب من بداية العصر الحجري للحاسوب)؟ هذه الدالة تعطيك الوقت الحالي بالثواني منذ 1 يناير 1970 (المعروف بـ “Epoch Time”). :date:

current_time = time.time()
print(f"الوقت الحالي بالثواني منذ 1970: {current_time} 🕰️")

3. time.ctime(seconds) :scroll:

هذه الدالة تحول لك الوقت بالثواني إلى صيغة قابلة للقراءة. تريد أن تعرف متى حدث شيء مهم؟ استخدم هذه الدالة! :face_with_monocle:

print("الوقت الحالي بالتنسيق الجميل: ", time.ctime()) 📅

4. time.localtime(seconds) :timer_clock:

إذا كنت تريد تفصيلًا أدق للوقت، مثل معرفة السنة، الشهر، اليوم، إلخ، فهذه الدالة تعطيك التفاصيل الكاملة في صيغة هيكل زمني. :spiral_calendar:

local_time = time.localtime()
print(f"الوقت بالتفصيل: {local_time.tm_hour}:{local_time.tm_min}:{local_time.tm_sec} 🕰️")

5. time.strftime(format, time_object) :art:

هذه الدالة رائعة إذا كنت تريد تخصيص شكل عرض الوقت حسب رغبتك. لديك مطلق الحرية في تحديد التنسيق الذي تريده. :hammer_and_wrench:

formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f"التنسيق المفضل لدي للوقت: {formatted_time} 🎨")

6. time.perf_counter() :zap:

هل تحتاج إلى حساب الوقت المستغرق لتنفيذ قطعة من الشيفرة بدقة عالية؟ time.perf_counter() هي الدالة المثالية لذلك. :stopwatch:

start_time = time.perf_counter()
# كود تريد قياس الوقت المستغرق لتنفيذه
time.sleep(2)
end_time = time.perf_counter()
print(f"الوقت المستغرق: {end_time - start_time:.4f} ثواني ⚡")

ختامًا :tada::

مكتبة time في بايثون هي أداة قوية للتحكم في الوقت وتنظيمه. سواء كنت تريد قضاء وقت معين في انتظار شيء ما، أو ترغب في حساب الوقت المستغرق لتنفيذ مهمة، time ستساعدك. لذا، لا تستهين بالوقت، واستخدم time بحكمة! :hourglass_flowing_sand:

(ملاحظة: تأكد من ألا تقضي الكثير من الوقت في استخدام time.sleep، البرمجة تنتظر! :laughing:)


Reviewing the time Module in Python (With a Touch of Humor! :smile:)

Do you need to delay execution :hourglass_flowing_sand:, measure how long a piece of code takes to run :stopwatch:, or just want to know the current time :watch:? The time module in Python is here to help! Let’s dive into some key functions of this module with a sprinkle of humor :nerd_face:.

1. time.sleep(seconds) :zzz:

Want to give Python a little nap? This function pauses the execution of your code for a specified number of seconds. Perfect for when you need a break during coding! :sleeping:

import time

print("Taking a quick 3-second nap... 😴")
time.sleep(3)
print("I'm back! Time to keep coding. 🧑‍💻")

2. time.time() :clock3:

Need to know the current time (in big numbers counted from the dawn of computing)? This function gives you the current time in seconds since January 1, 1970 (known as “Epoch Time”). :date:

current_time = time.time()
print(f"The current time in seconds since 1970: {current_time} 🕰️")

3. time.ctime(seconds) :scroll:

This function converts the time in seconds into a readable format. Want to know when something important happened? Use this function! :face_with_monocle:

print("Current time in a nice format: ", time.ctime()) 📅

4. time.localtime(seconds) :timer_clock:

If you need more detailed information about the time, such as the year, month, day, etc., this function gives you the full breakdown in a time structure. :spiral_calendar:

local_time = time.localtime()
print(f"Detailed time: {local_time.tm_hour}:{local_time.tm_min}:{local_time.tm_sec} 🕰️")

5. time.strftime(format, time_object) :art:

This function is fantastic if you want to format the time exactly how you like. You have full control over how the time is displayed. :hammer_and_wrench:

formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print(f"My favorite time format: {formatted_time} 🎨")

6. time.perf_counter() :zap:

Need to measure the precise time it takes to run a piece of code? time.perf_counter() is the perfect tool for that. :stopwatch:

start_time = time.perf_counter()
# Code you want to measure the execution time for
time.sleep(2)
end_time = time.perf_counter()
print(f"Elapsed time: {end_time - start_time:.4f} seconds ⚡")

Wrapping Up :tada::

The time module in Python is a powerful tool for controlling and managing time. Whether you need to pause for a bit or measure the duration of a task, time has got you covered. So, don’t underestimate time—use time wisely! :hourglass_flowing_sand:

(P.S. Don’t spend too much time using time.sleep, the code won’t write itself! :laughing:)


إعجاب واحد (1)