Skip to content

Commit a385bd6

Browse files
committed
More cleanup of C++ examples, cleanup some leftover utcnow
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
1 parent 104e1ad commit a385bd6

13 files changed

Lines changed: 71 additions & 74 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Here is a very simple example of a small `csp` program to calculate a [bid-ask s
2323
```python
2424
import csp
2525
from csp import ts
26-
from datetime import datetime
26+
from csp.utils.datetime import utc_now
2727

2828

2929
@csp.node
@@ -44,7 +44,7 @@ def my_graph():
4444

4545

4646
if __name__ == '__main__':
47-
csp.run(my_graph, starttime=datetime.utcnow())
47+
csp.run(my_graph, starttime=utc_now())
4848
```
4949

5050
Running this, our output should look like (with some slight variations for current time):

cpp/csp/core/Platform.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@
1717
#define DLL_LOCAL
1818

1919
#ifdef CSPTYPESIMPL_EXPORTS
20-
#define CSPTYPESIMPL_EXPORT __declspec(dllexport)
20+
#define CSPTYPESIMPL_EXPORT __declspec( dllexport )
2121
#else
22-
#define CSPTYPESIMPL_EXPORT __declspec(dllimport)
22+
#define CSPTYPESIMPL_EXPORT __declspec( dllimport )
2323
#endif
2424

2525
#ifdef CSPIMPL_EXPORTS
26-
#define CSPIMPL_EXPORT __declspec(dllexport)
26+
#define CSPIMPL_EXPORT __declspec( dllexport )
2727
#else
28-
#define CSPIMPL_EXPORT __declspec(dllimport)
28+
#define CSPIMPL_EXPORT __declspec( dllimport )
2929
#endif
3030

3131
// C API export macro - used for ABI-stable C functions
3232
// On Windows: export from cspimpl.dll
3333
// On Unix: ensure symbols have default visibility
3434
#ifdef CSPIMPL_EXPORTS
35-
#define CSP_C_API_EXPORT __declspec(dllexport)
35+
#define CSP_C_API_EXPORT __declspec( dllexport )
3636
#else
37-
#define CSP_C_API_EXPORT __declspec(dllimport)
37+
#define CSP_C_API_EXPORT __declspec( dllimport )
3838
#endif
3939

40-
#define START_PACKED __pragma( pack(push, 1) )
41-
#define END_PACKED __pragma( pack(pop))
40+
#define START_PACKED __pragma( pack( push, 1 ) )
41+
#define END_PACKED __pragma( pack( pop ) )
4242

43-
#define NO_INLINE __declspec(noinline)
43+
#define NO_INLINE __declspec( noinline )
4444

4545
inline tm * localtime_r( const time_t * timep, tm * result )
4646
{
@@ -103,14 +103,14 @@ inline uint8_t ffs(uint64_t n)
103103
#define CSPTYPESIMPL_EXPORT
104104

105105
// C API export macro - ensure symbols have default visibility for external use
106-
#define CSP_C_API_EXPORT __attribute__((visibility("default")))
106+
#define CSP_C_API_EXPORT __attribute__( ( visibility( "default" ) ) )
107107

108-
#define DLL_LOCAL __attribute__ ((visibility ("hidden")))
108+
#define DLL_LOCAL __attribute__ ( ( visibility( "hidden" ) ) )
109109

110110
#define START_PACKED
111-
#define END_PACKED __attribute__((packed))
111+
#define END_PACKED __attribute__( ( packed ) )
112112

113-
#define NO_INLINE __attribute__ ((noinline))
113+
#define NO_INLINE __attribute__ ( ( noinline ) )
114114

115115
inline constexpr uint8_t clz(uint32_t n) { return __builtin_clz(n); }
116116
inline constexpr uint8_t clz(uint64_t n) { return __builtin_clzl(n); }

docs/wiki/get-started/More-with-CSP.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ We also turn off memoization for the node by passing `memoize=False`. This means
1515
```python
1616
import csp
1717
from csp import ts
18+
1819
from datetime import timedelta
1920
import numpy as np
2021

@@ -38,9 +39,10 @@ def poisson_counter(rate: float) -> ts[int]:
3839
We can run the node using `csp.run` as follows:
3940

4041
```python
41-
from datetime import datetime
42+
from datetime import timedelta
43+
from csp.utils.datetime import utc_now
4244

43-
res = csp.run(poisson_counter, rate=2.0, starttime=datetime.utcnow(), endtime=timedelta(seconds=10), realtime=False)
45+
res = csp.run(poisson_counter, rate=2.0, starttime=utc_now(), endtime=timedelta(seconds=10), realtime=False)
4446
print(f'Final count: {res[0][-1][1]}')
4547
```
4648

docs/wiki/how-tos/Write-C-API-Adapters.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,8 +769,11 @@ _managed_output_def = output_adapter_def(
769769
#### Use Managed Adapters in Your Graph
770770

771771
```python
772+
from datetime import timedelta
773+
772774
import csp
773-
from datetime import datetime, timedelta
775+
from csp.utils.datetime import utc_now
776+
774777
from my_adapter import MyAdapterManager
775778

776779
@csp.graph
@@ -782,22 +785,25 @@ def my_graph():
782785
data = mgr.subscribe(int, interval_ms=100)
783786
mgr.publish(data)
784787

785-
csp.run(my_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=10))
788+
csp.run(my_graph, starttime=utc_now(), endtime=timedelta(seconds=10))
786789
```
787790

788791
#### Use in Your Graph
789792

790793
```python
794+
from datetime import timedelta
795+
791796
import csp
792-
from datetime import datetime, timedelta
797+
from csp.utils.datetime import utc_now
798+
793799
from my_adapter import my_input, LogAdapter
794800

795801
@csp.graph
796802
def my_graph():
797803
data = my_input(int, interval_ms=100)
798804
LogAdapter(data, prefix="[MyApp] ")
799805

800-
csp.run(my_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=10))
806+
csp.run(my_graph, starttime=utc_now(), endtime=timedelta(seconds=10))
801807
```
802808

803809
## See Also

examples/05_cpp/3_cpp_adapter/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ After building, you can use the adapter in Python:
102102

103103
```python
104104
import csp
105-
from datetime import datetime, timedelta
105+
from csp.utils.datetime import utc_now
106+
107+
from datetime import timedelta
106108

107109
from counteradapter import CounterAdapterManager
108110

@@ -121,8 +123,7 @@ def my_graph():
121123
mgr.publish(data)
122124

123125
# Run for 2 seconds in realtime mode
124-
csp.run(my_graph, starttime=datetime.utcnow(),
125-
endtime=datetime.utcnow() + timedelta(seconds=2), realtime=True)
126+
csp.run(my_graph, starttime=utc_now(), endtime=timedelta(seconds=2), realtime=True)
126127
```
127128

128129
Or run the example directly:

examples/05_cpp/3_cpp_adapter/counteradapter/__main__.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,12 @@
1515
PYTHONPATH=build:$PYTHONPATH python example.py
1616
"""
1717

18-
import sys
19-
from datetime import datetime, timedelta
18+
from datetime import timedelta
2019

2120
import csp
21+
from counteradapter import CounterAdapterManager
2222
from csp import ts
23-
24-
25-
# For development/testing, add the build directory to path
26-
# In production, you would install the module properly
27-
def setup_path():
28-
"""Add the build directory to the Python path if needed."""
29-
import os
30-
31-
build_dir = os.path.join(os.path.dirname(__file__), "build")
32-
if os.path.exists(build_dir) and build_dir not in sys.path:
33-
sys.path.insert(0, build_dir)
34-
35-
36-
setup_path()
37-
38-
# Import the adapter after setting up the path
39-
try:
40-
from counteradapter import CounterAdapterManager
41-
except ImportError:
42-
print("Error: Could not import counteradapter.")
43-
print("Make sure you have built the C++ extension module.")
44-
print("See the build instructions in README.md")
45-
sys.exit(1)
23+
from csp.utils.datetime import utc_now
4624

4725

4826
@csp.node
@@ -84,7 +62,7 @@ def main():
8462
print("=" * 40)
8563

8664
# Run the graph for 2 seconds
87-
start = datetime.utcnow()
65+
start = utc_now()
8866
end = start + timedelta(seconds=2)
8967

9068
result = csp.run(counter_graph, starttime=start, endtime=end, realtime=True)

examples/05_cpp/3_cpp_adapter/counteradapter/test_counteradapter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
from datetime import datetime, timedelta
1+
from datetime import timedelta
22

33
import csp
4+
from csp.utils.datetime import utc_now
45

56
from .__main__ import counter_graph
67

78

89
def test_counteradapter():
9-
start = datetime.utcnow()
10+
start = utc_now()
1011
end = start + timedelta(seconds=2)
1112

1213
result = csp.run(counter_graph, starttime=start, endtime=end, realtime=True)

examples/05_cpp/4_c_api_adapter/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ my_output_adapter = output_adapter_def(
148148
## Usage from Python
149149

150150
```python
151+
from datetime import timedelta
152+
151153
import csp
154+
from csp.utils.datetime import utc_now
155+
152156
from exampleadapter import example_input, example_output
153157

154158
@csp.graph
@@ -159,7 +163,7 @@ def my_graph():
159163
# Output to stdout with a prefix
160164
example_output(data, prefix="[MyApp] ")
161165

162-
csp.run(my_graph, starttime=datetime.utcnow(), endtime=timedelta(seconds=5))
166+
csp.run(my_graph, starttime=utc_now(), endtime=timedelta(seconds=5))
163167
```
164168

165169
## API Headers

examples/07_end_to_end/earthquake.ipynb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@
279279
"import csp\n",
280280
"from csp.impl.pushpulladapter import PushPullInputAdapter\n",
281281
"from csp.impl.wiring import py_pushpull_adapter_def\n",
282+
"from csp.utils.datetime import utc_now\n",
282283
"\n",
283284
"\n",
284285
"# We use a csp.Struct to store the earthquake event data\n",
@@ -328,7 +329,7 @@
328329
" self.push_tick(False, event_data.time, event_data)\n",
329330
"\n",
330331
" print(\"-------------------------------------------------------------------\")\n",
331-
" print(f\"{datetime.utcnow()}: Historical replay complete, pulled {len(catalog)} events\")\n",
332+
" print(f\"{utc_now()}: Historical replay complete, pulled {len(catalog)} events\")\n",
332333
" print(\"-------------------------------------------------------------------\")\n",
333334
" self.flag_replay_complete()\n",
334335
"\n",
@@ -349,7 +350,7 @@
349350
" break\n",
350351
"\n",
351352
" print(\"-------------------------------------------------------------------\")\n",
352-
" print(f\"{datetime.utcnow()}: Refreshing earthquake live feed with {len(new_events)} events\")\n",
353+
" print(f\"{utc_now()}: Refreshing earthquake live feed with {len(new_events)} events\")\n",
353354
" print(\"-------------------------------------------------------------------\")\n",
354355
"\n",
355356
" for event in reversed(new_events):\n",
@@ -400,8 +401,8 @@
400401
" print(\"End of graph building\")\n",
401402
"\n",
402403
"\n",
403-
"start = datetime.utcnow() - timedelta(hours=24)\n",
404-
"end = datetime.utcnow() + timedelta(minutes=10)\n",
404+
"start = utc_now() - timedelta(hours=24)\n",
405+
"end = utc_now() + timedelta(minutes=10)\n",
405406
"csp.run(earthquake_graph, starttime=start, endtime=end, realtime=True)\n",
406407
"print(\"Done.\")"
407408
]

examples/07_end_to_end/mta.ipynb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@
395395
},
396396
{
397397
"cell_type": "code",
398-
"execution_count": 8,
398+
"execution_count": null,
399399
"id": "f808ff5b-2735-4b58-bd29-054dabdc6620",
400400
"metadata": {},
401401
"outputs": [
@@ -656,6 +656,7 @@
656656
"import csp\n",
657657
"from csp.impl.pushadapter import PushInputAdapter\n",
658658
"from csp.impl.wiring import py_push_adapter_def\n",
659+
"from csp.utils.datetime import utc_now\n",
659660
"\n",
660661
"\n",
661662
"class Event(csp.Struct):\n",
@@ -691,7 +692,7 @@
691692
"\n",
692693
" while self._running:\n",
693694
" print(\"----------------------------------------------\")\n",
694-
" print(f\"{datetime.utcnow()}: refreshing MTA feed\")\n",
695+
" print(f\"{utc_now()}: refreshing MTA feed\")\n",
695696
" print(\"----------------------------------------------\")\n",
696697
" print(\" Station | Line | Direction | Arrival time\")\n",
697698
" feed.refresh()\n",
@@ -738,7 +739,7 @@
738739
" print(\"End of graph building\")\n",
739740
"\n",
740741
"\n",
741-
"start = datetime.utcnow()\n",
742+
"start = utc_now()\n",
742743
"end = start + timedelta(minutes=3)\n",
743744
"csp.run(mta_graph, starttime=start, realtime=True, endtime=end)\n",
744745
"print(\"Done.\")"

0 commit comments

Comments
 (0)