From 861bd8db11f29cb98d01675c8471e8d068ee07df Mon Sep 17 00:00:00 2001 From: Karen Zeng Date: Thu, 13 Apr 2023 15:42:15 -0400 Subject: [PATCH] update --- test2.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test3.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ test4.py | 17 +++++++++++++++++ test5.py | 8 ++++++++ 4 files changed, 134 insertions(+) create mode 100644 test2.py create mode 100644 test3.py create mode 100644 test4.py create mode 100644 test5.py diff --git a/test2.py b/test2.py new file mode 100644 index 0000000..563be77 --- /dev/null +++ b/test2.py @@ -0,0 +1,58 @@ +import time +'''import matplotlib.pyplot as plt''' + +# Durations to measure in seconds +durations = [0.01, 0.1, 1.0] + +# Number of repetitions +num_repetitions = 100 + +# Lists to store jitter for each duration +jitter_10ms = [] +jitter_100ms = [] +jitter_1s = [] + +# Function to measure jitter +def measure_jitter(duration): + start_time = time.perf_counter_ns() + time.sleep(duration) + end_time = time.perf_counter_ns() + return end_time - start_time + +# Measure jitter for each duration and repeat for num_repetitions times +for duration in durations: + for _ in range(num_repetitions): + jitter = measure_jitter(duration) + if duration == 0.01: + jitter_10ms.append(jitter) + elif duration == 0.1: + jitter_100ms.append(jitter) + elif duration == 1.0: + jitter_1s.append(jitter) +''' +# Create histograms for each duration's jitter +plt.hist(jitter_10ms, bins=10, color='r', alpha=0.5, label='10ms') +plt.hist(jitter_100ms, bins=10, color='g', alpha=0.5, label='100ms') +plt.hist(jitter_1s, bins=10, color='b', alpha=0.5, label='1s') +plt.xlabel('Jitter (ns)') +plt.ylabel('Frequency') +plt.title('Jitter of time.sleep() for Different Durations') +plt.legend() +plt.show() +''' + +import random +import secrets + +# Create a dictionary with 1 million integer keys +dict = {} + +for i in range(1000000): + # Generate a random 64-byte value as bytes + random_value = secrets.token_bytes(64) + # Convert the bytes value to hexadecimal representation + hex_value = random_value.hex() + # Add the key-value pair to the dictionary + dict[i] = hex_value + +print("Dictionary created with 1 million integer keys.") \ No newline at end of file diff --git a/test3.py b/test3.py new file mode 100644 index 0000000..1aed487 --- /dev/null +++ b/test3.py @@ -0,0 +1,51 @@ + +class Vault: + def __init__(self): + self._treasure = "gold" + self._known_good_copy = self.__dict__.copy() + + def __getattribute__(self, name): + if name == "treasure": + if self.__dict__ != self._known_good_copy: + print("Attribute deleted!") + return self._treasure + else: + return object.__getattribute__(self, name) + + def __setattr__(self, name, value): + if name != "_treasure" and name != "_known_good_copy": + print("Attribute deletion detected!") + self._known_good_copy = self.__dict__.copy() + object.__setattr__(self, name, value) +v = Vault() +Vault.treasure = " haha monkeypatched" +print(v.treasure) + +''' +class Vault: + def __init__(self): + self._treasure = "gold" + self._known_good_copy = self.__dict__.copy() + + def __get__(self, instance, owner): + if instance is None: + return self + else: + if instance.__dict__ != self._known_good_copy: + print("Attribute deleted!") + return self._treasure + + def __set__(self, instance, value): + print("Attribute deletion detected!") + self._known_good_copy = instance.__dict__.copy() + + def __delete__(self, instance): + print("Attribute deletion detected!") + self._known_good_copy = instance.__dict__.copy() + + def __set_name__(self, owner, name): + self.name = name +v = Vault() +Vault._treasure = " haha monkeypatched" +print(v._treasure) +''' \ No newline at end of file diff --git a/test4.py b/test4.py new file mode 100644 index 0000000..e65fddf --- /dev/null +++ b/test4.py @@ -0,0 +1,17 @@ +import asyncio + +# Define a coroutine function +async def greet(name): + print(f"Hello, {name}!") + await asyncio.sleep(1) + print(f"Goodbye, {name}!") + +# Call the coroutine function +coroutine_obj = greet("Karen") + +# coroutine_obj is a coroutine object, not the actual execution of the coroutine + +# Use an event loop to run the coroutine +loop = asyncio.get_event_loop() +loop.run_until_complete(coroutine_obj) +loop.close() \ No newline at end of file diff --git a/test5.py b/test5.py new file mode 100644 index 0000000..73bbc4c --- /dev/null +++ b/test5.py @@ -0,0 +1,8 @@ +def think(): + x=1 + def think2(): + nonlocal x + x=2 + think2() + return x +print(think())