-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compute can load (Sourcery refactored) #14
base: compute_can_load
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,27 +15,27 @@ | |
|
||
|
||
class Can: | ||
def convert_string(string: str) -> str: | ||
if not isinstance(string, str): | ||
def convert_string(self) -> str: | ||
if not isinstance(self, str): | ||
raise TypeError("`string` needs to be an integer type!") | ||
|
||
string = string.upper() | ||
string = string.replace(' ', '_') | ||
string = normalize('NFKD', string) | ||
string = string.encode('ASCII', 'ignore') | ||
string = string.decode('ASCII') | ||
string = re.sub('[^A-Z0-9_]+', '', string) | ||
self = self.upper() | ||
self = self.replace(' ', '_') | ||
self = normalize('NFKD', self) | ||
self = self.encode('ASCII', 'ignore') | ||
self = self.decode('ASCII') | ||
self = re.sub('[^A-Z0-9_]+', '', self) | ||
|
||
return string | ||
return self | ||
|
||
def validate_byte(byte: int) -> bool: | ||
if (byte < 0) | (byte > 8): | ||
def validate_byte(self) -> bool: | ||
if (self < 0) | (self > 8): | ||
Comment on lines
-31
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
print("Byte number MUST be between 0 and 7 to be described.") | ||
return False | ||
return True | ||
|
||
def validate_bit(bit: int) -> bool: | ||
if (bit < 0) | (bit > 8): | ||
def validate_bit(self) -> bool: | ||
if (self < 0) | (self > 8): | ||
Comment on lines
-37
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
print("Bit number MUST be between 0 and 7 to be described.") | ||
return False | ||
return True | ||
|
@@ -188,10 +188,7 @@ def get(self) -> dict: | |
} | ||
|
||
def get_total_load(self, bitrate): | ||
load = 0 | ||
for topic in self.topics: | ||
load += topic.get_load(bitrate) | ||
return load | ||
return sum(topic.get_load(bitrate) for topic in self.topics) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
def __str__(self) -> str: | ||
return json.dumps(self.get(), indent=4) | ||
|
@@ -253,7 +250,7 @@ def export_c_library(self, filename: str = "can_ids.h"): | |
|
||
def export_csv(self, filename: str): | ||
import pandas as pd | ||
|
||
Comment on lines
-256
to
+253
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found the following improvement in Function |
||
modules = [] | ||
signature = [] | ||
ids = [] | ||
|
@@ -284,15 +281,15 @@ def export_csv(self, filename: str): | |
"frame_length": frame_length, | ||
"description": description, | ||
}) | ||
|
||
df.to_csv(filename) | ||
|
||
|
||
def get_can_load_by_topic(self): | ||
load = {} | ||
for module in self.modules: | ||
for topic in module["topics"]: | ||
if not topic["id"] in load.keys(): | ||
if topic["id"] not in load: | ||
Comment on lines
-295
to
+292
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
load[topic["id"]] = [] | ||
load[topic["id"]].append(self.get_topic_load(topic)) | ||
return load | ||
|
@@ -323,19 +320,14 @@ def plot_load(self): | |
ids = {} | ||
for module in self.modules: | ||
for topic in module["topics"]: | ||
if not topic["id"] in ids.keys(): | ||
if topic["id"] not in ids: | ||
ids[topic["id"]] = 0 | ||
ids[topic["id"]] += self.get_topic_load(topic) | ||
|
||
id = list(ids.keys()) | ||
id.sort() | ||
|
||
load = [] | ||
for i in id: | ||
load.append(ids[i]) | ||
|
||
axes[0][0].bar(range(0, len(id)), load, align='center', color='royalblue') | ||
axes[0][0].set_xticks(range(0, len(id))) | ||
id = sorted(ids.keys()) | ||
load = [ids[i] for i in id] | ||
axes[0][0].bar(range(len(id)), load, align='center', color='royalblue') | ||
axes[0][0].set_xticks(range(len(id))) | ||
Comment on lines
-326
to
+330
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
axes[0][0].set_xticklabels(list(id), fontsize = 9, rotation = 90) | ||
axes[0][0].set_title("Load x Id") | ||
axes[0][0].set_ylabel("Load [%]") | ||
|
@@ -356,14 +348,14 @@ def plot_load(self): | |
modules = {} | ||
for module in self.modules: | ||
for topic in module["topics"]: | ||
if not module["name"] in modules.keys(): | ||
if module["name"] not in modules: | ||
modules[module["name"]] = 0 | ||
modules[module["name"]] += self.get_topic_load(topic) | ||
load = list(modules.values()) | ||
modules = list(modules.keys()) | ||
axes[1][0].set_title("Load x Module") | ||
axes[1][0].bar(range(0, len(modules)), load) | ||
axes[1][0].set_xticks(range(0,len(modules))) | ||
axes[1][0].bar(range(len(modules)), load) | ||
axes[1][0].set_xticks(range(len(modules))) | ||
axes[1][0].set_xticklabels(modules, fontsize = 9, rotation=90) | ||
axes[1][0].set_ylabel("Load [%]") | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,7 @@ | |
can.export_c_library() | ||
can.export_csv("can_ids.csv") | ||
loads = can.get_can_load_by_topic() | ||
ids = list(loads.keys()) | ||
ids.sort() | ||
ids = sorted(loads.keys()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
for id in ids: | ||
print("Id: ", id, "load:", loads[id]) | ||
print("Total load:", round(can.get_can_load(),3), "%") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
Can.convert_string
refactored with the following changes:self
(instance-method-first-arg-name
)