-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmany_formatting_errors.py
More file actions
147 lines (121 loc) · 7.24 KB
/
Copy pathmany_formatting_errors.py
File metadata and controls
147 lines (121 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os,sys,json,datetime,math,random;import requests;from collections import defaultdict,OrderedDict
from typing import List,Dict,Optional,Union,Tuple,Any;import numpy as np;import pandas as pd
# This is a poorly formatted Python file with many style violations
class UnformattedExampleClass( object ):
def __init__(self,name,age=None,email=None,phone=None,address=None,city=None,state=None,zip_code=None):
self.name=name;self.age=age;self.email=email;self.phone=phone
self.address=address;self.city=city;self.state=state;self.zip_code=zip_code
self.data={"name":name,"age":age,"email":email}
def get_info(self ):
return f"Name: {self.name}, Age: {self.age}"
def update_data(self,**kwargs):
for key,value in kwargs.items():
if hasattr(self,key):setattr(self,key,value)
self.data.update(kwargs)
def process_data(data_list,filter_func=None,transform_func=None,sort_key=None,reverse=False):
if not data_list:return[]
if filter_func:data_list=[item for item in data_list if filter_func(item)]
if transform_func:data_list=[transform_func(item)for item in data_list]
if sort_key:data_list=sorted(data_list,key=sort_key,reverse=reverse)
return data_list
def calculate_statistics(numbers):
if not numbers:return None
mean=sum(numbers)/len(numbers); median=sorted(numbers)[len(numbers)//2]
variance=sum((x-mean)**2 for x in numbers)/len(numbers);std_dev=math.sqrt(variance)
return {"mean":mean,"median":median,"variance":variance,"std_dev":std_dev,"min":min(numbers),"max":max(numbers)}
def complex_nested_function(x,y,z):
def inner_function_1(a,b):
def deeply_nested(c,d):
return c*d+a*b
return deeply_nested(a+1,b-1)+deeply_nested(a-1,b+1)
def inner_function_2 (a,b,c):
result=[]
for i in range(a):
for j in range(b):
for k in range(c):
if i*j*k>0:result.append(i*j*k)
elif i+j+k==0:result.append(-1)
else :result.append(0)
return result
return inner_function_1(x,y)+sum(inner_function_2(x,y,z))
# Long lines and poor dictionary formatting
user_data={"users":[{"id":1,"name":"John Doe","email":"john@example.com","preferences":{"theme":"dark","notifications":True,"language":"en"},"metadata":{"created_at":"2023-01-01","last_login":"2024-01-01","login_count":150}},{"id":2,"name":"Jane Smith","email":"jane@example.com","preferences":{"theme":"light","notifications":False,"language":"es"},"metadata":{"created_at":"2023-02-15","last_login":"2024-01-15","login_count":89}}]}
# Poor list formatting and string concatenation
long_list_of_items=['item_1','item_2','item_3','item_4','item_5','item_6','item_7','item_8','item_9','item_10','item_11','item_12','item_13','item_14','item_15','item_16','item_17','item_18','item_19','item_20']
def generate_report(data,include_stats=True,include_charts=False,format_type='json',output_file=None):
if not data:raise ValueError("Data cannot be empty")
report={'timestamp':datetime.datetime.now().isoformat(),'data_count':len(data),'summary':{}}
# Bad formatting in loops and conditionals
for i,item in enumerate(data):
if isinstance(item,dict):
for key,value in item.items():
if key not in report['summary']:report['summary'][key]=[]
report['summary'][key].append(value)
elif isinstance(item,(int,float)):
if 'numbers' not in report['summary']:report['summary']['numbers']=[]
report['summary']['numbers'].append(item)
else:
if 'other' not in report['summary']:report['summary']['other']=[]
report['summary']['other'].append(str(item))
if include_stats and 'numbers' in report['summary']:
numbers=report['summary']['numbers']
report['statistics']=calculate_statistics(numbers)
# Long conditional chain with poor formatting
if format_type=='json':result=json.dumps(report,indent=None,separators=(',',':'))
elif format_type=='pretty_json':result=json.dumps(report,indent=2)
elif format_type=='string':result=str(report)
else:result=report
if output_file:
with open(output_file,'w')as f:f.write(result if isinstance(result,str)else json.dumps(result))
return result
class DataProcessor ( UnformattedExampleClass ) :
def __init__(self,data_source,config=None,debug=False):
super().__init__("DataProcessor")
self.data_source=data_source;self.config=config or{};self.debug=debug
self.processed_data=[];self.errors=[];self.warnings=[]
def load_data ( self ) :
try:
if isinstance(self.data_source,str):
if self.data_source.endswith('.json'):
with open(self.data_source,'r')as f:data=json.load(f)
elif self.data_source.endswith('.csv'):data=pd.read_csv(self.data_source).to_dict('records')
else:raise ValueError(f"Unsupported file type: {self.data_source}")
elif isinstance(self.data_source,list):data=self.data_source
else:data=[self.data_source]
return data
except Exception as e:
self.errors.append(str(e));return[]
def validate_data(self,data):
valid_items=[];invalid_items=[]
for item in data:
if isinstance(item,dict)and'id'in item and'name'in item:valid_items.append(item)
else:invalid_items.append(item)
if invalid_items:self.warnings.append(f"Found {len(invalid_items)} invalid items")
return valid_items
def process(self):
data=self.load_data()
if not data:return{"success":False,"error":"No data loaded"}
validated_data=self.validate_data(data)
processed_result=process_data(validated_data,
filter_func=lambda x:x.get('active',True),
transform_func=lambda x:{**x,'processed_at':datetime.datetime.now().isoformat()},
sort_key=lambda x:x.get('name',''))
self.processed_data=processed_result
return{"success":True,"count":len(processed_result),"data":processed_result}
if __name__=="__main__":
sample_data=[{"id":1,"name":"Alice","active":True},{"id":2,"name":"Bob","active":False},{"id":3,"name":"Charlie","active":True}]
processor=DataProcessor(sample_data,config={"debug":True})
result=processor.process()
if result["success"]:
print(f"Successfully processed {result['count']} items")
for item in result["data"][:3]:print(f"- {item['name']} (ID: {item['id']})")
else:print(f"Processing failed: {result.get('error','Unknown error')}")
# Generate report with poor formatting
report=generate_report(sample_data,include_stats=True,format_type='pretty_json')
print("Generated report:",report[:100]+"..."if len(report)>100 else report)
# Complex calculation with poor spacing
numbers=[random.randint(1,100)for _ in range(50)]
stats=calculate_statistics(numbers)
complex_result=complex_nested_function(5,3,2)
print(f"Statistics: mean={stats['mean']:.2f}, std_dev={stats['std_dev']:.2f}")
print(f"Complex calculation result: {complex_result}")