Reputation: 8498
I'm attempting to do straight string matches from text files. Sometimes a match means a string contains multiple target strings. Currently I have code that looks like
interesting_matches = [
"sys/bootdisk.py",
" engine stalled for ",
" changed to stalled)",
"DSR failure",
"Detected IDI failure",
"idi_shallow_verify_failure",
"Malformed block history",
"Out of order sequence message on",
"Port reset timeout of",
"gmp_info",
"test_thread",
" panic @ time ",
": *** FAILED ASSERTION",
"filesystem full",]
for match in interesting_matches:
# Iterate through simple matches.
if match in line:
processed_line_data = self._process_line(
match,
line,
line_datetime,
line_num,
current_version)
if "kern_sig" in line and "pid" in line:
processed_line_data = self._process_line(
("kern_sig", "pid"),
line,
line_datetime,
line_num,
current_version)
if "vfs_export" in line and "ignoring" in line:
processed_line_data = self._process_line(
("vfs_export", "ignoring"),
line,
line_datetime,
line_num,
current_version)
if "job_d" in line and
"State transition from state " in line and
" took longer than " in line:
processed_line_data = self._process_line(
(
"job_d",
"state transition from state",
" took longer than "),
line,
line_datetime,
line_num,
current_version)
if processed_line_data is not None:
return_list.append(processed_line_data)
What I would love to do is something similar to
interesting_matches = [
"sys/bootdisk.py",
" engine stalled for ",
" changed to stalled)",
"DSR failure",
"Detected IDI failure",
"idi_shallow_verify_failure",
"Malformed block history",
"Out of order sequence message on",
"Port reset timeout of",
"gmp_info",
"test_thread",
" panic @ time ",
": *** FAILED ASSERTION",
"filesystem full",
("kern_sig", "pid"),
("vfs_export", "ignoring"),
("job_d", "State transition from state", " took longer than "),]
for matches in interesting_matches
if any(match in line for match in matches):
processed_line_data = self._process_line(
match,
line,
line_datetime,
line_num,
current_version)
But the mixing of tuples and strings causes a value error stating that you can't compare strings and tuples.
How can I write a single comparison if I want to have single and multiple strings to check for?
EDIT:
Here's working code based on Sean's answer
interesting_matches = [
("sys/bootdisk.py",),
(" engine stalled for ",),
(" changed to stalled)",),
("DSR failure",),
("Detected IDI failure",),
("idi_shallow_verify_failure",),
("Malformed block history",),
("Out of order sequence message on",),
("Port reset timeout of",),
("gmp_info",),
("test_thread",),
(" panic @ time ",),
(": *** FAILED ASSERTION",),
("filesystem full",),
("kern_sig", "pid"),
("vfs_export", "ignoring"),
("job_d", "State transition from state", " took longer than "),]
for matches in interesting_matches:
if all(match in "test_thread" for match in matches):
print(matches)
Upvotes: 1
Views: 53
Reputation: 916
Very doable. Try this: For the single-substring signatures, wrap them in a tuple anyway. Now your list is homogeneous and your problem becomes a lot simpler. For each signature tuple, check to see if all substrings in the signature are also in the line.
Upvotes: 1