Skip to content

Commit 2dfd15e

Browse files
authored
Merge pull request #1712 from borglab/update-wrap
2 parents f191388 + 6cc16a3 commit 2dfd15e

File tree

18 files changed

+295
-152
lines changed

18 files changed

+295
-152
lines changed

gtsam/3rdparty/cephes/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ project(
88

99
set(CEPHES_HEADER_FILES
1010
cephes.h
11-
cephes/cephes_names.h
1211
cephes/dd_idefs.h
1312
cephes/dd_real.h
1413
cephes/dd_real_idefs.h

gtsam/3rdparty/cephes/cephes.h

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef CEPHES_H
22
#define CEPHES_H
33

4-
#include "cephes/cephes_names.h"
54
#include "dllexport.h"
65

76
#ifdef __cplusplus

gtsam/3rdparty/cephes/cephes/cephes_names.h

-114
This file was deleted.

gtsam/3rdparty/cephes/cephes/mconf.h

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
#include <math.h>
5757
#include <stdlib.h>
5858

59-
#include "cephes_names.h"
6059
#include "cephes.h"
6160
#include "polevl.h"
6261
#include "sf_error.h"

gtsam/gtsam.i

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class KeyList {
3939
void remove(size_t key);
4040

4141
void serialize() const;
42+
43+
// Special dunder methods for Python wrapping
44+
__len__();
45+
__contains__(size_t key);
46+
__iter__();
4247
};
4348

4449
// Actually a FastSet<Key>
@@ -64,6 +69,11 @@ class KeySet {
6469
bool count(size_t key) const; // returns true if value exists
6570

6671
void serialize() const;
72+
73+
// Special dunder methods for Python wrapping
74+
__len__();
75+
__contains__(size_t key);
76+
__iter__();
6777
};
6878

6979
// Actually a vector<Key>, needed for Matlab
@@ -85,6 +95,11 @@ class KeyVector {
8595
void push_back(size_t key) const;
8696

8797
void serialize() const;
98+
99+
// Special dunder methods for Python wrapping
100+
__len__();
101+
__contains__(size_t key);
102+
__iter__();
88103
};
89104

90105
// Actually a FastMap<Key,int>

python/gtsam/tests/test_Utilities.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
import unittest
1313

1414
import numpy as np
15+
from gtsam.utils.test_case import GtsamTestCase
1516

1617
import gtsam
17-
from gtsam.utils.test_case import GtsamTestCase
1818

1919

2020
class TestUtilites(GtsamTestCase):
2121
"""Test various GTSAM utilities."""
22+
2223
def test_createKeyList(self):
2324
"""Test createKeyList."""
2425
I = [0, 1, 2]
@@ -28,6 +29,17 @@ def test_createKeyList(self):
2829
kl = gtsam.utilities.createKeyList("s", I)
2930
self.assertEqual(kl.size(), 3)
3031

32+
def test_KeyList_iteration(self):
33+
"""Tests for KeyList iteration"""
34+
I = [0, 1, 2]
35+
kl = gtsam.utilities.createKeyList(I)
36+
37+
self.assertEqual(len(kl), len(I))
38+
39+
for i, key in enumerate(kl):
40+
self.assertTrue(key in kl)
41+
self.assertEqual(I[i], key)
42+
3143
def test_createKeyVector(self):
3244
"""Test createKeyVector."""
3345
I = [0, 1, 2]
@@ -37,6 +49,17 @@ def test_createKeyVector(self):
3749
kl = gtsam.utilities.createKeyVector("s", I)
3850
self.assertEqual(len(kl), 3)
3951

52+
def test_KeyVector_iteration(self):
53+
"""Tests for KeyVector iteration"""
54+
I = [0, 1, 2]
55+
kv = gtsam.utilities.createKeyVector(I)
56+
57+
self.assertEqual(len(kv), len(I))
58+
59+
for i, key in enumerate(kv):
60+
self.assertTrue(key in kv)
61+
self.assertEqual(I[i], key)
62+
4063
def test_createKeySet(self):
4164
"""Test createKeySet."""
4265
I = [0, 1, 2]
@@ -46,6 +69,17 @@ def test_createKeySet(self):
4669
kl = gtsam.utilities.createKeySet("s", I)
4770
self.assertEqual(kl.size(), 3)
4871

72+
def test_KeySet_iteration(self):
73+
"""Tests for KeySet iteration"""
74+
I = [0, 1, 2]
75+
ks = gtsam.utilities.createKeySet(I)
76+
77+
self.assertEqual(len(ks), len(I))
78+
79+
for i, key in enumerate(ks):
80+
self.assertTrue(key in ks)
81+
self.assertEqual(I[i], key)
82+
4983
def test_extractPoint2(self):
5084
"""Test extractPoint2."""
5185
initial = gtsam.Values()

wrap/gtwrap/interface_parser/classes.py

+39-9
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212

1313
from typing import Any, Iterable, List, Union
1414

15-
from pyparsing import Literal, Optional, ZeroOrMore # type: ignore
15+
from pyparsing import ZeroOrMore # type: ignore
16+
from pyparsing import Literal, Optional, Word, alphas
1617

1718
from .enum import Enum
1819
from .function import ArgumentList, ReturnType
1920
from .template import Template
20-
from .tokens import (CLASS, COLON, CONST, IDENT, LBRACE, LPAREN, OPERATOR,
21-
RBRACE, RPAREN, SEMI_COLON, STATIC, VIRTUAL)
21+
from .tokens import (CLASS, COLON, CONST, DUNDER, IDENT, LBRACE, LPAREN,
22+
OPERATOR, RBRACE, RPAREN, SEMI_COLON, STATIC, VIRTUAL)
2223
from .type import TemplatedType, Typename
2324
from .utils import collect_namespaces
2425
from .variable import Variable
@@ -212,6 +213,26 @@ def __repr__(self) -> str:
212213
)
213214

214215

216+
class DunderMethod:
217+
"""Special Python double-underscore (dunder) methods, e.g. __iter__, __contains__"""
218+
rule = (
219+
DUNDER #
220+
+ (Word(alphas))("name") #
221+
+ DUNDER #
222+
+ LPAREN #
223+
+ ArgumentList.rule("args_list") #
224+
+ RPAREN #
225+
+ SEMI_COLON # BR
226+
).setParseAction(lambda t: DunderMethod(t.name, t.args_list))
227+
228+
def __init__(self, name: str, args: ArgumentList):
229+
self.name = name
230+
self.args = args
231+
232+
def __repr__(self) -> str:
233+
return f"DunderMethod: __{self.name}__({self.args})"
234+
235+
215236
class Class:
216237
"""
217238
Rule to parse a class defined in the interface file.
@@ -223,23 +244,26 @@ class Hello {
223244
};
224245
```
225246
"""
247+
226248
class Members:
227249
"""
228250
Rule for all the members within a class.
229251
"""
230-
rule = ZeroOrMore(Constructor.rule #
252+
rule = ZeroOrMore(DunderMethod.rule #
253+
^ Constructor.rule #
231254
^ Method.rule #
232255
^ StaticMethod.rule #
233256
^ Variable.rule #
234257
^ Operator.rule #
235258
^ Enum.rule #
236259
).setParseAction(lambda t: Class.Members(t.asList()))
237260

238-
def __init__(self,
239-
members: List[Union[Constructor, Method, StaticMethod,
240-
Variable, Operator]]):
261+
def __init__(self, members: List[Union[Constructor, Method,
262+
StaticMethod, Variable,
263+
Operator, Enum, DunderMethod]]):
241264
self.ctors = []
242265
self.methods = []
266+
self.dunder_methods = []
243267
self.static_methods = []
244268
self.properties = []
245269
self.operators = []
@@ -251,6 +275,8 @@ def __init__(self,
251275
self.methods.append(m)
252276
elif isinstance(m, StaticMethod):
253277
self.static_methods.append(m)
278+
elif isinstance(m, DunderMethod):
279+
self.dunder_methods.append(m)
254280
elif isinstance(m, Variable):
255281
self.properties.append(m)
256282
elif isinstance(m, Operator):
@@ -271,8 +297,8 @@ def __init__(self,
271297
+ SEMI_COLON # BR
272298
).setParseAction(lambda t: Class(
273299
t.template, t.is_virtual, t.name, t.parent_class, t.members.ctors, t.
274-
members.methods, t.members.static_methods, t.members.properties, t.
275-
members.operators, t.members.enums))
300+
members.methods, t.members.static_methods, t.members.dunder_methods, t.
301+
members.properties, t.members.operators, t.members.enums))
276302

277303
def __init__(
278304
self,
@@ -283,6 +309,7 @@ def __init__(
283309
ctors: List[Constructor],
284310
methods: List[Method],
285311
static_methods: List[StaticMethod],
312+
dunder_methods: List[DunderMethod],
286313
properties: List[Variable],
287314
operators: List[Operator],
288315
enums: List[Enum],
@@ -308,6 +335,7 @@ def __init__(
308335
self.ctors = ctors
309336
self.methods = methods
310337
self.static_methods = static_methods
338+
self.dunder_methods = dunder_methods
311339
self.properties = properties
312340
self.operators = operators
313341
self.enums = enums
@@ -326,6 +354,8 @@ def __init__(
326354
method.parent = self
327355
for static_method in self.static_methods:
328356
static_method.parent = self
357+
for dunder_method in self.dunder_methods:
358+
dunder_method.parent = self
329359
for _property in self.properties:
330360
_property.parent = self
331361

wrap/gtwrap/interface_parser/function.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def from_parse_result(parse_result: ParseResults):
8282
return ArgumentList([])
8383

8484
def __repr__(self) -> str:
85-
return repr(tuple(self.args_list))
85+
return ",".join([repr(x) for x in self.args_list])
8686

8787
def __len__(self) -> int:
8888
return len(self.args_list)

wrap/gtwrap/interface_parser/tokens.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
LPAREN, RPAREN, LBRACE, RBRACE, COLON, SEMI_COLON = map(Suppress, "(){}:;")
2424
LOPBRACK, ROPBRACK, COMMA, EQUAL = map(Suppress, "<>,=")
25+
DUNDER = Suppress(Literal("__"))
2526

2627
# Default argument passed to functions/methods.
2728
# Allow anything up to ',' or ';' except when they

0 commit comments

Comments
 (0)