Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

""" 

Multiplex calls to one object to calls to many objects 

 

AUTHORS: 

 

- Martin Albrecht (2011): initial version 

""" 

 

#***************************************************************************** 

# Copyright (C) 2011 Martin Albrecht <martinralbrecht@googlemail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

class MultiplexFunction(object): 

""" 

A simple wrapper object for functions that are called on a list of 

objects. 

""" 

def __init__(self, multiplexer, name): 

""" 

EXAMPLES:: 

 

sage: from sage.misc.object_multiplexer import Multiplex, MultiplexFunction 

sage: m = Multiplex(1,1/2) 

sage: f = MultiplexFunction(m,'str') 

sage: f 

<sage.misc.object_multiplexer.MultiplexFunction object at 0x...> 

""" 

self.multiplexer = multiplexer 

self.name = name 

 

def __call__(self, *args, **kwds): 

""" 

EXAMPLES:: 

 

sage: from sage.misc.object_multiplexer import Multiplex, MultiplexFunction 

sage: m = Multiplex(1,1/2) 

sage: f = MultiplexFunction(m,'str') 

sage: f() 

('1', '1/2') 

""" 

l = [] 

for child in self.multiplexer.children: 

l.append(getattr(child,self.name)(*args, **kwds)) 

if all(e is None for e in l): 

return None 

else: 

return tuple(l) 

 

class Multiplex(object): 

""" 

Object for a list of children such that function calls on this 

new object implies that the same function is called on all 

children. 

""" 

def __init__(self, *args): 

""" 

EXAMPLES:: 

 

sage: from sage.misc.object_multiplexer import Multiplex 

sage: m = Multiplex(1,1/2) 

sage: m.str() 

('1', '1/2') 

""" 

self.children = [arg for arg in args if arg is not None] 

 

def __getattr__(self, name): 

""" 

EXAMPLES:: 

 

sage: from sage.misc.object_multiplexer import Multiplex 

sage: m = Multiplex(1,1/2) 

sage: m.str 

<sage.misc.object_multiplexer.MultiplexFunction object at 0x...> 

sage: m.trait_names 

Traceback (most recent call last): 

... 

AttributeError: 'Multiplex' has no attribute 'trait_names' 

""" 

if name.startswith("__") or name == "trait_names": 

raise AttributeError("'Multiplex' has no attribute '%s'"%name) 

return MultiplexFunction(self,name)