Coverage for docs/source/examples/docstringparsing.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 16:49 -0500

1"Docstring parsing example" 

2from gpkit import Model, parse_variables 

3from gpkit.tools.docstring import parse_varstring 

4 

5 

6class Cube(Model): 

7 """Demonstration of nomenclature syntax 

8 

9 Lines that end in "Variables" will be parsed as a scalar variable table 

10 until the next blank line. 

11 

12 Variables 

13 --------- 

14 A [m^2] surface area 

15 V 100 [L] minimum volume 

16 

17 Lines that end in "Variables of length $N" will be parsed as vector 

18 variables of length $N until the next blank line. 

19 

20 Variables of length 3 

21 --------------------- 

22 s [m] side length 

23 

24 Let's introduce more variables: (any line ending in "Variables" is parsed) 

25 

26 Zoning Variables 

27 ---------------- 

28 h 1 [m] minimum height 

29 

30 Upper Unbounded 

31 --------------- 

32 A 

33 

34 The ordering of these blocks doesn't affect anything; order them in the 

35 way that makes the most sense to someone else reading your model. 

36 """ 

37 @parse_variables(__doc__, globals()) 

38 def setup(self): 

39 

40 return [A >= 2*(s[0]*s[1] + s[1]*s[2] + s[2]*s[0]), 

41 s.prod() >= V, 

42 s[2] >= h] 

43 

44 

45print(parse_varstring(Cube.__doc__)) 

46c = Cube() 

47c.cost = c.A 

48print(c.solve(verbosity=0).table())