Skip to content

Commit 8e0e607

Browse files
committed
Change to using file instead of filename
This is more consistent with how the rest of Python handles specifying files. You can specify an absolute or relative path.
1 parent 73ffc04 commit 8e0e607

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

cqc/pythonLib.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -2495,7 +2495,7 @@ def getTime(self, block=True):
24952495
class CQCToFile(CQCHandler):
24962496
"""Handler to be used when writing the CQC commands to a file."""
24972497

2498-
def __init__(self, filename='CQC_File', pend_messages=False,
2498+
def __init__(self, file='CQC_File', pend_messages=False,
24992499
overwrite=False, binary=True):
25002500

25012501
# Call init of CQCHandler
@@ -2507,28 +2507,26 @@ def __init__(self, filename='CQC_File', pend_messages=False,
25072507

25082508
self.binary = binary
25092509

2510-
# Set path of file to write to
2511-
script_dir = sys.path[0]
2512-
self.filename = os.path.join(script_dir, filename)
2510+
self.file = file
25132511

25142512
# Check if file exists
25152513
if overwrite:
25162514
# Remove file if we can overwrite
25172515
try:
2518-
os.remove(self.filename)
2516+
os.remove(self.file)
25192517
except FileNotFoundError:
25202518
pass
25212519
else:
2522-
if not os.path.isfile(self.filename):
2520+
if not os.path.isfile(self.file):
25232521
pass
25242522
else:
25252523
# Append number to filename if can't overwrite
25262524
num = 0
25272525
while True:
2528-
if os.path.isfile(self.filename + str(num)):
2526+
if os.path.isfile(self.file + str(num)):
25292527
num += 1
25302528
else:
2531-
self.filename = self.filename + str(num)
2529+
self.file = self.file + str(num)
25322530
break
25332531

25342532
# Don't want notify when writing to file
@@ -2542,10 +2540,10 @@ def commit(self, msg):
25422540
"""
25432541

25442542
if self.binary is True:
2545-
with open(self.filename, 'ab') as f:
2543+
with open(self.file, 'ab') as f:
25462544
f.write(msg)
25472545
else:
2548-
with open(self.filename, 'a') as f:
2546+
with open(self.file, 'a') as f:
25492547
f.write(str(msg) + '\n')
25502548

25512549
def new_qubitID(self):

tests/test_CQCToFile.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# filename = os.path.join(str(tmpdir), 'CQC_File')
66
#
7-
# with CQCToFile(filename=filename) as cqc:
7+
# with CQCToFile(file=filename) as cqc:
88

99
from cqc.pythonLib import CQCToFile, qubit
1010
import os
@@ -15,15 +15,15 @@ def test_name(tmpdir):
1515

1616
filename = os.path.join(str(tmpdir), 'CQC_File')
1717

18-
with CQCToFile(filename=filename) as cqc:
18+
with CQCToFile(file=filename) as cqc:
1919
assert cqc.name == 'CQCToFile'
2020

2121

2222
def test_sendSimple(tmpdir):
2323

2424
filename = os.path.join(str(tmpdir), 'CQC_File')
2525

26-
with CQCToFile(filename=filename, binary=False) as cqc:
26+
with CQCToFile(file=filename, binary=False) as cqc:
2727
cqc.sendSimple(CQC_TP_HELLO)
2828

2929
with open(filename) as f:
@@ -36,7 +36,7 @@ def test_createqubit(tmpdir):
3636

3737
filename = os.path.join(str(tmpdir), 'CQC_File')
3838

39-
with CQCToFile(filename=filename, binary=False) as cqc:
39+
with CQCToFile(file=filename, binary=False) as cqc:
4040

4141
q = qubit(cqc)
4242
q.H()
@@ -54,7 +54,7 @@ def test_releasequbit(tmpdir):
5454

5555
filename = os.path.join(str(tmpdir), 'CQC_File')
5656

57-
with CQCToFile(filename=filename, binary=False) as cqc:
57+
with CQCToFile(file=filename, binary=False) as cqc:
5858

5959
q = qubit(cqc)
6060
q.H()
@@ -74,7 +74,7 @@ def test_Hgate(tmpdir):
7474

7575
filename = os.path.join(str(tmpdir), 'CQC_File')
7676

77-
with CQCToFile(filename=filename, binary=False) as cqc:
77+
with CQCToFile(file=filename, binary=False) as cqc:
7878

7979
q = qubit(cqc)
8080
q.H()
@@ -93,7 +93,7 @@ def test_some_combinations(tmpdir):
9393

9494
filename = os.path.join(str(tmpdir), 'CQC_File')
9595

96-
with CQCToFile(filename=filename) as cqc:
96+
with CQCToFile(file=filename) as cqc:
9797

9898
q = cqc.createEPR("Alice")
9999
q.H()
@@ -112,7 +112,7 @@ def test_flushing(tmpdir):
112112

113113
filename = os.path.join(str(tmpdir), 'CQC_File')
114114

115-
with CQCToFile(filename=filename, pend_messages=True) as cqc:
115+
with CQCToFile(file=filename, pend_messages=True) as cqc:
116116

117117
assert not cqc._pending_headers
118118

@@ -132,7 +132,7 @@ def test_qubitIDs(tmpdir):
132132

133133
filename = os.path.join(str(tmpdir), 'CQC_File')
134134

135-
with CQCToFile(filename=filename) as cqc:
135+
with CQCToFile(file=filename) as cqc:
136136

137137
a = qubit(cqc)
138138
a.X()
@@ -150,7 +150,7 @@ def test_measurement(tmpdir):
150150

151151
filename = os.path.join(str(tmpdir), 'CQC_File')
152152

153-
with CQCToFile(filename=filename) as cqc:
153+
with CQCToFile(file=filename) as cqc:
154154

155155
q = qubit(cqc)
156156
a = q.measure()
@@ -161,7 +161,7 @@ def test_flush_on_exit(tmpdir):
161161

162162
filename = os.path.join(str(tmpdir), 'CQC_File')
163163

164-
with CQCToFile(filename=filename, pend_messages=True, binary=False) as cqc:
164+
with CQCToFile(file=filename, pend_messages=True, binary=False) as cqc:
165165

166166
q = qubit(cqc)
167167
q.H()

0 commit comments

Comments
 (0)