Skip to content

Commit 246a7c2

Browse files
committed
Fixed linting errors.
1 parent f12fcca commit 246a7c2

File tree

4 files changed

+55
-91
lines changed

4 files changed

+55
-91
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[flake8]
22
count = True
33
exclude = docs, .git, .idea, __pycache__
4-
ignore = E203 W291 W293 E743 E265 W605
4+
ignore = E203 W291 W293 E743 E265 W605 W503
55
max-line-length = 120
66
statistics = True

cqc/MessageHandler.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
CQC_CMD_ROT_X,
3939
CQC_CMD_ROT_Y,
4040
CQC_CMD_ROT_Z,
41-
CQC_TP_HELLO,
42-
CQC_TP_COMMAND,
43-
CQC_TP_FACTORY,
44-
CQC_TP_GET_TIME,
4541
CQC_CMD_I,
4642
CQC_CMD_X,
4743
CQC_CMD_Y,
@@ -67,7 +63,6 @@
6763
CQC_ERR_UNSUPP,
6864
CQC_ERR_UNKNOWN,
6965
CQC_ERR_GENERAL,
70-
CQCSequenceHeader,
7166
CQCFactoryHeader,
7267
CQCType,
7368
CQCTypeHeader,
@@ -106,6 +101,7 @@ def has_extra(cmd):
106101

107102
return False
108103

104+
109105
def is_error_message(message: bytes):
110106

111107
# Only CQCHeaders can be error messages, so if the length does not correspond it is not an error message
@@ -130,8 +126,6 @@ def is_error_message(message: bytes):
130126
else:
131127
return False
132128

133-
134-
135129

136130
def print_error(error):
137131
logging.error("Uncaught twisted error found: {}".format(error))
@@ -191,7 +185,6 @@ def __init__(self, factory):
191185
# Query/assign like this: self.references[app_id][ref_id]
192186
self.references = {}
193187

194-
195188
@inlineCallbacks
196189
def handle_cqc_message(self, header, message, transport=None):
197190
"""
@@ -390,17 +383,18 @@ def handle_factory(self, header, data):
390383

391384
return succ and should_notify
392385

393-
394386
@inlineCallbacks
395387
def handle_mix(self, header: CQCHeader, data: bytes):
396388
"""
397-
Handler for messages of TP_MIX. Notice that header is the CQC Header, and data is the complete body, excluding the CQC Header.
389+
Handler for messages of TP_MIX. Notice that header is the CQC Header,
390+
and data is the complete body, excluding the CQC Header.
398391
"""
399392
# Strategy for handling TP_MIX:
400393
# The first bit of data will be a CQCType header. We extract this header.
401394
# We extract from this first CQCType header the type of the following instructions, and we invoke the
402395
# corresponding handler from self.messageHandlers. This handler expects as parameter "header" a CQCHeader.
403-
# Therefore, we construct the CQCHeader that corresponds to the CQCType header (remember that the CQCType header is just a reduced CQCHeader),
396+
# Therefore, we construct the CQCHeader that corresponds to the CQCType header
397+
# (remember that the CQCType header is just a reduced CQCHeader),
404398
# and input that constructed CQCHeader as "header" parameter.
405399
# After this handler returns, we repeat until the end of the program.
406400

@@ -423,7 +417,6 @@ def handle_mix(self, header: CQCHeader, data: bytes):
423417
if type_header.type == CQCType.IF:
424418
current_position += result
425419

426-
427420
# A TP_MIX should return the first error if there is an error message present, and otherwise return one TP_DONE
428421
# We use the next function to retrieve the first error message from the list.
429422
# Notice the [:] syntax. This ensures the underlying list is updated, and not just the variable.
@@ -445,15 +438,17 @@ def handle_conditional(self, header: CQCHeader, data: bytes):
445438
# Strategy for handling TP_IF:
446439
# We extract the CQCIFHeader from the data. We then extract all necessary variables from the header.
447440
# We then evaluate the conditional. If the conditional evaluates to FALSE, then we return the bodylength of
448-
# the IF. The mix handler will then skip this bodylength. If the conditional evaluates to True, then we return 0.
441+
# the IF. The mix handler will then skip this bodylength.
442+
# If the conditional evaluates to True, then we return 0.
449443

450444
if_header = CQCIFHeader(data[:CQCIFHeader.HDR_LENGTH])
451445

452446
try:
453447
first_operand_value = self.references[header.app_id][if_header.first_operand]
454448
except KeyError:
455449
self.return_messages.append(
456-
self.create_return_message(header.app_id, CQC_ERR_GENERAL, cqc_version=header.version))
450+
self.create_return_message(header.app_id, CQC_ERR_GENERAL, cqc_version=header.version)
451+
)
457452

458453
if if_header.type_of_second_operand is CQCIFHeader.TYPE_VALUE:
459454
second_operand_value = if_header.second_operand
@@ -469,7 +464,6 @@ def handle_conditional(self, header: CQCHeader, data: bytes):
469464
else:
470465
return if_header.length
471466

472-
473467
@abstractmethod
474468
def handle_hello(self, header, data):
475469
pass

cqc/cqcHeader.py

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030

3131
# This import allows to use type hints for classes that have not been defined yet.
32-
# See https://stackoverflow.com/questions/33533148/how-do-i-specify-that-the-return-type-of-a-method-is-the-same-as-the-class-itsel
32+
# See https://stackoverflow.com/questions/33533148/
3333
# This import must be the very first import in this file, otherwise an error is raised
3434
from __future__ import annotations
3535

@@ -134,8 +134,8 @@ class CQCType(IntEnum):
134134
GET_TIME = 8 # Get creation time of qubit
135135
INF_TIME = 9 # Return timinig information
136136
NEW_OK = 10 # Created a new qubit
137-
MIX = 11 # Indicate that the CQC program will contain multiple header types
138-
IF = 12 # Announce a CQC IF header
137+
MIX = 11 # Indicate that the CQC program will contain multiple header types
138+
IF = 12 # Announce a CQC IF header
139139

140140
ERR_GENERAL = 20 # General purpose error (no details
141141
ERR_NOQUBIT = 21 # No more qubits available
@@ -146,16 +146,16 @@ class CQCType(IntEnum):
146146

147147

148148
class CQCLogicalOperator(IntEnum):
149-
EQ = 0 # Equal
150-
NEQ = 1 # Not equal
149+
EQ = 0 # Equal
150+
NEQ = 1 # Not equal
151151

152152
@staticmethod
153-
def opposite_of(operator: 'CQCLogicalOperator'): # String literal type hint because it is a forward reference
154-
opposites = {
155-
CQCLogicalOperator.EQ: CQCLogicalOperator.NEQ,
156-
CQCLogicalOperator.NEQ: CQCLogicalOperator.EQ
157-
}
158-
return opposites[operator]
153+
def opposite_of(operator: CQCLogicalOperator):
154+
opposites = {
155+
CQCLogicalOperator.EQ: CQCLogicalOperator.NEQ,
156+
CQCLogicalOperator.NEQ: CQCLogicalOperator.EQ
157+
}
158+
return opposites[operator]
159159

160160
@staticmethod
161161
def is_true(first_operand: int, operator: CQCLogicalOperator, second_operand: int):
@@ -165,6 +165,7 @@ def is_true(first_operand: int, operator: CQCLogicalOperator, second_operand: in
165165
}
166166
return comparison_method[operator](second_operand)
167167

168+
168169
class Header(metaclass=abc.ABCMeta):
169170
"""
170171
Abstact class for headers.
@@ -342,22 +343,19 @@ class CQCTypeHeader(Header):
342343
PACKAGING_FORMAT = "!BI"
343344
HDR_LENGTH = struct.calcsize(PACKAGING_FORMAT)
344345

345-
346-
def _setVals(self, tp: CQCType=0, length: int=0) -> None:
346+
def _setVals(self, tp: CQCType = 0, length: int = 0) -> None:
347347
"""
348348
Set using given values.
349349
"""
350350
self.type = tp
351351
self.length = length
352352

353-
354353
def _pack(self) -> bytes:
355354
"""
356355
Pack data into packet format. For defnitions see cLib/cgc.h
357356
"""
358357
return struct.pack(self.PACKAGING_FORMAT, self.type, self.length)
359358

360-
361359
def _unpack(self, headerBytes) -> None:
362360
"""
363361
Unpack packet data.
@@ -366,24 +364,21 @@ def _unpack(self, headerBytes) -> None:
366364
self.type = unpacked[0]
367365
self.length = unpacked[1]
368366

369-
370-
371367
def _printable(self) -> str:
372368
"""
373369
Produce a printable string for information purposes.
374370
"""
375371
return "CQC Type header. Type=" + str(self.type) + " | Length=" + str(self.length)
376372

377-
378373
def make_equivalent_CQCHeader(self, version: int, app_id: int) -> CQCHeader:
379374
"""
380-
Produce a CQC Header that is equivalent to this CQCTypeHeader. This method does not make any modifications to self.
375+
Produce a CQC Header that is equivalent to this CQCTypeHeader.
376+
This method does not make any modifications to self.
381377
"""
382378
cqc_header = CQCHeader()
383379
cqc_header.setVals(version, self.type, app_id, self.length)
384380
return cqc_header
385-
386-
381+
387382

388383
class CQCIFHeader(Header):
389384
"""
@@ -396,12 +391,14 @@ class CQCIFHeader(Header):
396391
TYPE_VALUE = 0
397392
TYPE_REF_ID = 1
398393

399-
def _setVals(self,
400-
first_operand: int=0,
401-
operator: CQCLogicalOperator=0,
402-
type_of_second_operand: int=0,
403-
second_operand: int=0,
404-
length: int=0) -> None:
394+
def _setVals(
395+
self,
396+
first_operand: int = 0,
397+
operator: CQCLogicalOperator = 0,
398+
type_of_second_operand: int = 0,
399+
second_operand: int = 0,
400+
length: int = 0
401+
) -> None:
405402
"""
406403
Set the fields of this header.
407404
first_operand must be a reference id.
@@ -415,7 +412,6 @@ def _setVals(self,
415412
self.second_operand = second_operand
416413
self.length = length
417414

418-
419415
def _pack(self) -> bytes:
420416
"""
421417
Pack data into packet format. For defnitions see cLib/cgc.h
@@ -428,8 +424,7 @@ def _pack(self) -> bytes:
428424
self.type_of_second_operand,
429425
self.second_operand,
430426
self.length
431-
)
432-
427+
)
433428

434429
def _unpack(self, headerBytes) -> None:
435430
"""
@@ -443,7 +438,6 @@ def _unpack(self, headerBytes) -> None:
443438
self.second_operand = unpacked[3]
444439
self.length = unpacked[4]
445440

446-
447441
def _printable(self) -> str:
448442
"""
449443
Produce a printable string for information purposes.
@@ -455,11 +449,12 @@ def _printable(self) -> str:
455449
operand_type = "Value"
456450

457451
# parenthesis to concatenate the string over multiple lines
458-
return ("CQC IF header. RefID=" + str(self.first_operand)
459-
+ " | Operator=" + str(self.operator)
460-
+ " | " + operand_type + "=" + str(self.second_operand)
461-
+ " | Second_operand_type=" + operand_type
462-
+ " | Body_length=" + str(self.length)
452+
return (
453+
"CQC IF header. RefID=" + str(self.first_operand)
454+
+ " | Operator=" + str(self.operator)
455+
+ " | " + operand_type + "=" + str(self.second_operand)
456+
+ " | Second_operand_type=" + operand_type
457+
+ " | Body_length=" + str(self.length)
463458
)
464459

465460

@@ -471,7 +466,6 @@ class CQCCmdHeader(Header):
471466
PACKAGING_FORMAT = "!HBB"
472467
HDR_LENGTH = struct.calcsize(PACKAGING_FORMAT)
473468

474-
475469
def _setVals(self, qubit_id=0, instr=0, notify=False, block=False, action=False):
476470
"""
477471
Set using given values.
@@ -540,23 +534,20 @@ class CQCAssignHeader(Header):
540534

541535
PACKAGING_FORMAT = "!I"
542536
HDR_LENGTH = struct.calcsize(PACKAGING_FORMAT)
543-
544537

545-
def _setVals(self, ref_id: int=0) -> None:
538+
def _setVals(self, ref_id: int = 0) -> None:
546539
"""
547540
Set using given values.
548541
"""
549542
self.ref_id = ref_id
550543

551-
552544
def _pack(self) -> bytes:
553545
"""
554546
Pack data into packet format. For defnitions see cLib/cgc.h
555547
"""
556548

557549
return struct.pack(self.PACKAGING_FORMAT, self.ref_id)
558550

559-
560551
def _unpack(self, headerBytes) -> None:
561552
"""
562553
Unpack packet data. For definitions see cLib/cqc.h
@@ -565,7 +556,6 @@ def _unpack(self, headerBytes) -> None:
565556

566557
self.ref_id = unpacked[0]
567558

568-
569559
def _printable(self) -> str:
570560
"""
571561
Produce a printable string for information purposes.
@@ -574,9 +564,6 @@ def _printable(self) -> str:
574564
return "CQC Assign sub header. RefID=" + str(self.ref_id)
575565

576566

577-
578-
579-
580567
class CQCXtraHeader(Header):
581568
"""
582569
Optional addtional cmd header information. Only relevant for certain commands.
@@ -782,7 +769,7 @@ class CQCCommunicationHeader(Header):
782769

783770
PACKAGING_FORMAT = "!HHL"
784771
PACKAGING_FORMAT_V1 = "!HLH"
785-
HDR_LENGTH = struct.calcsize(PACKAGING_FORMAT) # Both versions have the same size
772+
HDR_LENGTH = struct.calcsize(PACKAGING_FORMAT) # Both versions have the same size
786773

787774
def __init__(self, headerBytes=None, cqc_version=CQC_VERSION):
788775
"""

0 commit comments

Comments
 (0)