• Nov 18, 2022 •AustinLeath
0 likes • 6 views
# Python Program to calculate the square root num = float(input('Enter a number: ')) num_sqrt = num ** 0.5 print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
• Oct 10, 2025 •AustinLeath
0 likes • 2 views
#Original def output_json_log_data_to_file(filename, record_dictionary_list): with open(filename, 'w') as outputFile: for record in record_dictionary_list: json.dump(record, outputFile) outputFile.write('\n') #Atomic def output_json_log_data_to_file(filename, record_dictionary_list): # Use atomic file operations to prevent race conditions with readers # Write to temporary file first, then atomically rename to target file tmp_filename = filename + '.tmp' with open(tmp_filename, 'w') as outputFile: for record in record_dictionary_list: json.dump(record, outputFile) outputFile.write('\n') # Atomic rename - this prevents readers from seeing partial writes shutil.move(tmp_filename, filename)
• Nov 19, 2022 •CodeCatch
0 likes • 0 views
from collections import defaultdict def combine_values(*dicts): res = defaultdict(list) for d in dicts: for key in d: res[key].append(d[key]) return dict(res) d1 = {'a': 1, 'b': 'foo', 'c': 400} d2 = {'a': 3, 'b': 200, 'd': 400} combine_values(d1, d2) # {'a': [1, 3], 'b': ['foo', 200], 'c': [400], 'd': [400]}
0 likes • 4 views
def clamp_number(num, a, b): return max(min(num, max(a, b)), min(a, b)) clamp_number(2, 3, 5) # 3 clamp_number(1, -1, -5) # -1
0 likes • 3 views
from math import pi def rads_to_degrees(rad): return (rad * 180.0) / pi rads_to_degrees(pi / 2) # 90.0
• Jun 1, 2023 •CodeCatch
bytes_data = b'Hello, World!' string_data = bytes_data.decode('utf-8') print("String:", string_data)