Skip to content

Commit 963ef19

Browse files
authored
datetime.datetime.utcnow() is deprecated, replace with a central utility (#658)
Signed-off-by: Will Rieger <[email protected]>
1 parent ba23862 commit 963ef19

24 files changed

+136
-109
lines changed

csp/impl/pandas_accessor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from csp.impl.pandas_ext_type import TsDtype, is_csp_type
1212
from csp.impl.struct import define_nested_struct
1313
from csp.impl.wiring.edge import Edge
14+
from csp.utils.datetime import utc_now
1415

1516
T = TypeVar("T")
1617

@@ -369,7 +370,7 @@ def snap(self, timeout: timedelta = timedelta(seconds=10), starttime: datetime =
369370
:returns: A new Series of type corresponding to the underlying edge type (TsDtype.subtype),
370371
with the same index as the original series, containing the snapped value of each Edge.
371372
"""
372-
starttime = starttime or datetime.utcnow()
373+
starttime = starttime or utc_now()
373374
return self.run(starttime, timeout, True, tick_count=1, snap=True)
374375

375376
def show_graph(self):
@@ -546,7 +547,7 @@ def snap(self, timeout: timedelta = timedelta(seconds=10), starttime: datetime =
546547
a time in the future (to schedule a snap at, i.e. the next minute)
547548
:returns: A new DataFrame with the same indices where every Edge is replaced by it's snapped value.
548549
"""
549-
starttime = starttime or datetime.utcnow()
550+
starttime = starttime or utc_now()
550551
return self.run(starttime, timeout, True, tick_count=1, snap=True)
551552

552553
def sample(self, trigger: Union[timedelta, np.timedelta64, pd.Timedelta, ts[object]], inplace: bool = False):

csp/impl/pandas_perspective.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
perspective,
1717
perspective_type_map,
1818
)
19+
from csp.utils.datetime import utc_now
1920

2021
_ = csp.impl.pandas_accessor
2122

@@ -240,7 +241,7 @@ def run(self, starttime=None, endtime=timedelta(seconds=60), realtime=True, clea
240241
"""Run a graph that sends data to the table on the current thread.
241242
Normally, this is only useful for debugging.
242243
"""
243-
starttime = starttime or datetime.utcnow()
244+
starttime = starttime or utc_now()
244245
if clear:
245246
self.clear()
246247
csp.run(self.graph, starttime=starttime, endtime=endtime, realtime=realtime)
@@ -250,7 +251,7 @@ def start(self, starttime=None, endtime=timedelta(seconds=60), *, realtime=True,
250251
If clear=True, will clear any data from the data before writing to it.
251252
If auto_shutdown=True, will stop the engine if the table gets garbage collected.
252253
"""
253-
starttime = starttime or datetime.utcnow()
254+
starttime = starttime or utc_now()
254255
if clear:
255256
self.clear()
256257
self._runner = csp.run_on_thread(

csp/tests/adapters/test_kafka.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
RawBytesMessageMapper,
1515
RawTextMessageMapper,
1616
)
17+
from csp.utils.datetime import utc_now
1718

1819
from .kafka_utils import _precreate_topic
1920

@@ -117,14 +118,14 @@ def graph(count: int):
117118
csp.stop_engine(stop)
118119

119120
count = 5
120-
results = csp.run(graph, count, starttime=datetime.utcnow(), endtime=timedelta(seconds=30), realtime=True)
121+
results = csp.run(graph, count, starttime=utc_now(), endtime=timedelta(seconds=30), realtime=True)
121122
assert len(results["sub_data"]) >= 5
122123
print(results)
123124
for result in results["sub_data"]:
124125
assert result[1].mapped_partition >= 0
125126
assert result[1].mapped_offset >= 0
126127
assert result[1].mapped_live is not None
127-
assert result[1].mapped_timestamp < datetime.utcnow()
128+
assert result[1].mapped_timestamp < utc_now()
128129
assert results["sub_data"][-1][1].mapped_live
129130

130131
@pytest.mark.skipif(not os.environ.get("CSP_TEST_KAFKA"), reason="Skipping kafka adapter tests")
@@ -204,9 +205,7 @@ def graph(symbols: list, count: int):
204205

205206
symbols = ["AAPL", "MSFT"]
206207
count = 100
207-
results = csp.run(
208-
graph, symbols, count, starttime=datetime.utcnow(), endtime=timedelta(seconds=30), realtime=True
209-
)
208+
results = csp.run(graph, symbols, count, starttime=utc_now(), endtime=timedelta(seconds=30), realtime=True)
210209
for symbol in symbols:
211210
pub = results[f"pall_{symbol}"]
212211
sub = results[f"sall_{symbol}"]
@@ -231,7 +230,7 @@ def pub_graph():
231230
csp.stop_engine(stop)
232231
# csp.print('pub', struct)
233232

234-
csp.run(pub_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=30), realtime=True)
233+
csp.run(pub_graph, starttime=utc_now(), endtime=timedelta(seconds=30), realtime=True)
235234

236235
# grab start/end times
237236
def get_times_graph():
@@ -251,9 +250,7 @@ def get_times_graph():
251250
# csp.print('sub', data)
252251
# csp.print('status', kafkaadapter.status())
253252

254-
all_data = csp.run(get_times_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=30), realtime=True)[
255-
"data"
256-
]
253+
all_data = csp.run(get_times_graph, starttime=utc_now(), endtime=timedelta(seconds=30), realtime=True)["data"]
257254
min_time = all_data[0][1].dt
258255

259256
def get_data(start_offset, expected_count):
@@ -276,7 +273,7 @@ def get_data(start_offset, expected_count):
276273
get_data,
277274
KafkaStartOffset.EARLIEST,
278275
10,
279-
starttime=datetime.utcnow(),
276+
starttime=utc_now(),
280277
endtime=timedelta(seconds=30),
281278
realtime=True,
282279
)["data"]
@@ -288,7 +285,7 @@ def get_data(start_offset, expected_count):
288285
get_data,
289286
KafkaStartOffset.LATEST,
290287
1,
291-
starttime=datetime.utcnow(),
288+
starttime=utc_now(),
292289
endtime=timedelta(seconds=1),
293290
realtime=True,
294291
)["data"]
@@ -306,7 +303,7 @@ def get_data(start_offset, expected_count):
306303
stime = all_data[2][1].dt + timedelta(milliseconds=1)
307304
expected = [x for x in all_data if x[1].dt >= stime]
308305
res = csp.run(
309-
get_data, stime, len(expected), starttime=datetime.utcnow(), endtime=timedelta(seconds=30), realtime=True
306+
get_data, stime, len(expected), starttime=utc_now(), endtime=timedelta(seconds=30), realtime=True
310307
)["data"]
311308
assert len(res) == len(expected)
312309

@@ -376,9 +373,7 @@ def graph(symbols: list, count: int):
376373

377374
symbols = ["AAPL", "MSFT"]
378375
count = 10
379-
results = csp.run(
380-
graph, symbols, count, starttime=datetime.utcnow(), endtime=timedelta(seconds=30), realtime=True
381-
)
376+
results = csp.run(graph, symbols, count, starttime=utc_now(), endtime=timedelta(seconds=30), realtime=True)
382377
# print(results)
383378
for symbol in symbols:
384379
pub = results[f"pub_{symbol}"]
@@ -405,7 +400,7 @@ def graph_sub():
405400

406401
# With bug this would deadlock
407402
with pytest.raises(RuntimeError):
408-
csp.run(graph_sub, starttime=datetime.utcnow(), endtime=timedelta(seconds=2), realtime=True)
403+
csp.run(graph_sub, starttime=utc_now(), endtime=timedelta(seconds=2), realtime=True)
409404
kafkaadapter2 = KafkaAdapterManager(**kafkaadapterkwargs)
410405

411406
def graph_pub():
@@ -414,7 +409,7 @@ def graph_pub():
414409

415410
# With bug this would deadlock
416411
with pytest.raises(RuntimeError):
417-
csp.run(graph_pub, starttime=datetime.utcnow(), endtime=timedelta(seconds=2), realtime=True)
412+
csp.run(graph_pub, starttime=utc_now(), endtime=timedelta(seconds=2), realtime=True)
418413

419414
@pytest.mark.skipif(not os.environ.get("CSP_TEST_KAFKA"), reason="Skipping kafka adapter tests")
420415
def test_invalid_broker(self, kafkaadapterkwargs):
@@ -434,7 +429,7 @@ def graph_sub():
434429

435430
# With bug this would deadlock
436431
with pytest.raises(RuntimeError):
437-
csp.run(graph_sub, starttime=datetime.utcnow(), endtime=timedelta(seconds=2), realtime=True)
432+
csp.run(graph_sub, starttime=utc_now(), endtime=timedelta(seconds=2), realtime=True)
438433

439434
kafkaadapter2 = KafkaAdapterManager(**dict_with_broker)
440435

@@ -444,7 +439,7 @@ def graph_pub():
444439

445440
# With bug this would deadlock
446441
with pytest.raises(RuntimeError):
447-
csp.run(graph_pub, starttime=datetime.utcnow(), endtime=timedelta(seconds=2), realtime=True)
442+
csp.run(graph_pub, starttime=utc_now(), endtime=timedelta(seconds=2), realtime=True)
448443

449444
@pytest.mark.skipif(not os.environ.get("CSP_TEST_KAFKA"), reason="Skipping kafka adapter tests")
450445
def test_meta_field_map_tick_timestamp_from_field(self, kafkaadapterkwargs):
@@ -464,7 +459,7 @@ def graph_sub():
464459
)
465460

466461
with pytest.raises(ValueError):
467-
csp.run(graph_sub, starttime=datetime.utcnow(), endtime=timedelta(seconds=2), realtime=True)
462+
csp.run(graph_sub, starttime=utc_now(), endtime=timedelta(seconds=2), realtime=True)
468463

469464
@pytest.mark.skipif(not os.environ.get("CSP_TEST_KAFKA"), reason="Skipping kafka adapter tests")
470465
def test_conf_options(self):
@@ -499,7 +494,7 @@ def pub_graph():
499494
stop = csp.filter(stop, stop)
500495
csp.stop_engine(stop)
501496

502-
csp.run(pub_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=5), realtime=True)
497+
csp.run(pub_graph, starttime=utc_now(), endtime=timedelta(seconds=5), realtime=True)
503498

504499
def sub_graph():
505500
kafkaadapter = KafkaAdapterManager(broker=kafkabroker, start_offset=KafkaStartOffset.EARLIEST)
@@ -526,7 +521,7 @@ def sub_graph():
526521
stop = csp.and_(*stop_flags)
527522
csp.stop_engine(csp.filter(stop, stop))
528523

529-
res = csp.run(sub_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=5), realtime=True)
524+
res = csp.run(sub_graph, starttime=utc_now(), endtime=timedelta(seconds=5), realtime=True)
530525
burst = res["burst"]
531526
assert len(burst) == 1
532527
assert isinstance(burst[0][1], list)

csp/tests/adapters/test_parquet.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import csp
1515
from csp.adapters.output_adapters.parquet import ParquetOutputConfig
1616
from csp.adapters.parquet import ParquetReader, ParquetWriter
17+
from csp.utils.datetime import utc_now
1718

1819

1920
class PriceQuantity(csp.Struct):
@@ -855,7 +856,7 @@ def write_data(file_name: str, binary_arrow: bool) -> csp.ts[AllTypes]:
855856
b=True,
856857
i=123,
857858
d=123.456,
858-
dt=datetime.utcnow(),
859+
dt=utc_now(),
859860
dte=date.today(),
860861
t=time(1, 2, 3),
861862
s="hello hello",

csp/tests/adapters/test_perspective.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import date, datetime, timedelta
33

44
import csp
5+
from csp.utils.datetime import utc_now
56

67
try:
78
from csp.adapters.perspective import PerspectiveAdapter
@@ -35,4 +36,4 @@ class TestPerspectiveAdapter(unittest.TestCase):
3536
@unittest.skipIf(not HAS_PERSPECTIVE, "Test requires perspective")
3637
def test_adapter(self):
3738
output = {}
38-
csp.run(my_graph, output, starttime=datetime.utcnow(), endtime=timedelta(seconds=1))
39+
csp.run(my_graph, output, starttime=utc_now(), endtime=timedelta(seconds=1))

csp/tests/adapters/test_status.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from csp import ts
88
from csp.adapters.kafka import DateTimeType, JSONTextMessageMapper, KafkaStatusMessageType
99
from csp.adapters.status import Level
10+
from csp.utils.datetime import utc_now
1011

1112
from .kafka_utils import _precreate_topic
1213

@@ -43,7 +44,7 @@ def graph():
4344
csp.stop_engine(done_flag)
4445

4546
_precreate_topic(topic)
46-
results = csp.run(graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=10), realtime=True)
47+
results = csp.run(graph, starttime=utc_now(), endtime=timedelta(seconds=10), realtime=True)
4748
status = results["status"][0][1]
4849
assert status.status_code == KafkaStatusMessageType.MSG_RECV_ERROR
4950
assert status.level == Level.ERROR

csp/tests/impl/test_dateframe.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime, timedelta
33

44
import csp
5+
from csp.utils.datetime import utc_now
56

67

78
class TestCspDataFrame(unittest.TestCase):
@@ -137,7 +138,7 @@ def test_perspective(self):
137138
_ = df.to_perspective(starttime, endtime)
138139

139140
# realtime
140-
widget = df.to_perspective(datetime.utcnow(), endtime=timedelta(seconds=30), realtime=True)
141+
widget = df.to_perspective(utc_now(), endtime=timedelta(seconds=30), realtime=True)
141142
import time
142143

143144
time.sleep(1)

csp/tests/impl/test_enum.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import csp
1111
from csp import ts
12+
from csp.utils.datetime import utc_now
1213

1314

1415
class MyEnum(csp.Enum):
@@ -129,7 +130,7 @@ def graph():
129130
csp.add_graph_output("eout", eout)
130131
csp.add_graph_output("ein", ein)
131132

132-
result = csp.run(graph, starttime=datetime.utcnow())
133+
result = csp.run(graph, starttime=utc_now())
133134
self.assertEqual([v[1] for v in result["eout"]], [MyEnum(3), MyEnum(20), MyEnum(1)])
134135
self.assertEqual([v[1] for v in result["ein"]], [3, 20, 1])
135136

csp/tests/impl/test_genericpushadapter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from datetime import datetime, timedelta
55

66
import csp
7+
from csp.utils.datetime import utc_now
78

89

910
class Driver:
@@ -48,7 +49,7 @@ def graph():
4849

4950
res = csp.run(
5051
graph,
51-
starttime=datetime.utcnow(),
52+
starttime=utc_now(),
5253
endtime=timedelta(1),
5354
realtime=True,
5455
queue_wait_time=timedelta(seconds=0),

csp/tests/impl/test_outputadapter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from csp.impl.outputadapter import OutputAdapter
1414
from csp.impl.pushadapter import PushInputAdapter
1515
from csp.impl.wiring import py_output_adapter_def, py_push_adapter_def
16+
from csp.utils.datetime import utc_now
1617

1718

1819
class MyData(csp.Struct):
@@ -178,13 +179,13 @@ def my_graph_with_manager():
178179

179180
class TestPythonOutputAdapter(unittest.TestCase):
180181
def test_basic(self):
181-
csp.run(my_graph_basic, starttime=datetime.utcnow(), endtime=timedelta(seconds=5), realtime=False)
182+
csp.run(my_graph_basic, starttime=utc_now(), endtime=timedelta(seconds=5), realtime=False)
182183
self.assertEqual(
183184
output_buffer[0], '[{"a": 1, "b": 2, "c": 3}, {"a": 1, "b": 2, "c": 3}, {"a": 1, "b": 2, "c": 3}]'
184185
)
185186

186187
def test_with_manager(self):
187-
csp.run(my_graph_with_manager, starttime=datetime.utcnow(), endtime=timedelta(seconds=1), realtime=True)
188+
csp.run(my_graph_with_manager, starttime=utc_now(), endtime=timedelta(seconds=1), realtime=True)
188189

189190
# assert that the adapter manager put the right things in the right place,
190191
# e.g. data_1 and data_2 into data_1 output, and data_3 into data_3 output

0 commit comments

Comments
 (0)