api.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. API
  2. ===
  3. xdice.compile(pattern\_string)
  4. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  5. *Similar to ``xdice.Pattern(pattern_string).compile()``*
  6. Returns a compiled Pattern object.
  7. Pattern object can then be rolled to obtain a PatternScore object.
  8. xdice.roll(pattern\_string)
  9. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  10. *Similar to ``xdice.Pattern(pattern_string).roll()``*
  11. xdice.rolldice(faces, amount=1, drop\_lowest=0, drop\_highest=0)
  12. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  13. *Similar to
  14. ``xdice.Dice(faces, amount, drop_lowest, drop_highest).roll()``*
  15. Dice object
  16. -----------
  17. Set of dice.
  18. Dice.\ **init**\ (sides, amount=1, drop\_lowest=0, drop\_highest=0)
  19. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  20. Instantiate a set of dice.
  21. dice.roll()
  22. ^^^^^^^^^^^
  23. Role the dice and return a Score object
  24. *[classmethod]* Dice.parse(cls, pattern)
  25. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  26. Parse a pattern of the form ‘AdX’, where A and X are positive
  27. integers, then return the corresponding Dice object. Use
  28. ‘AdX[Ln][Hn]’ to drop the n lowest and/or highest dice when rolled.
  29. Properties
  30. ~~~~~~~~~~
  31. - ``dice.sides``: number of sides of the dice
  32. - ``dice.amount``: amount of dice to roll
  33. - ``dice.drop_lowest``: amount of lowest scores to drop
  34. - ``dice.drop_highest``: amount of highest scores to drop
  35. - ``dice.name`` : Decsriptive name of the Dice object
  36. Score object
  37. ------------
  38. Score is a subclass of integer, you can then manipulate it as you
  39. would do with an integer.
  40. | It also provides an access to the detailed score with the property
  41. ‘detail’.
  42. | ‘detail’ is the list of the scores obtained by each dice.
  43. Score class can also be used as an iterable, to walk trough the
  44. individual scores.
  45. ::
  46. eg:
  47. >>> s = Score([1,2,3])
  48. >>> print(s)
  49. 6
  50. >>> s + 1
  51. 7
  52. >>> list(s)
  53. [1,2,3]
  54. Score.\ **new**\ (iterable, dropped=\ ``_, name=“”)
  55. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  56. *``iterable`` should only contain integers*
  57. Score value will be the sum of the list’s values.
  58. score.format(verbose=False)
  59. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  60. A formatted string describing the detailed result.
  61. Properties
  62. ~~~~~~~~~~
  63. - score.detail: similar to list(score), return the list of the
  64. individual results
  65. - score.name: descriptive name of the dice rolled
  66. - score.dropped: list of the dropped results
  67. Pattern object
  68. --------------
  69. Dice notation pattern.
  70. Pattern.\ **init**\ (instr)
  71. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  72. Instantiate a Pattern object.
  73. pattern.compile()
  74. ^^^^^^^^^^^^^^^^^
  75. Parse the pattern. Two properties are updated at this time:
  76. - *pattern.format\_string*
  77. The ready-to-be-formatted string built from the ``instr`` argument.
  78. *Eg: ‘1d6+4+1d4’ => ‘{0}+4-{1}’*
  79. - *pattern.dices*
  80. The list of parsed dice.
  81. *Eg: ‘1d6+4+1d4’ => [(Dice; sides=6;amount=1), (Dice;
  82. sides=4;amount=1)]*
  83. pattern.roll()
  84. ^^^^^^^^^^^^^^
  85. Compile the pattern if it has not been yet, then roll the dice.
  86. Return a PatternScore object.
  87. PatternScore object
  88. -------------------
  89. PatternScore is a subclass of **integer**, you can then manipulate
  90. it as you would do with an integer.
  91. Moreover, you can get the list of the scores with the score(i) or
  92. scores() methods, and retrieve a formatted result with the format()
  93. method.
  94. pattern\_score.scores()
  95. ^^^^^^^^^^^^^^^^^^^^^^^
  96. Returns the list of Score objects extracted from the pattern and
  97. rolled.
  98. .. _: #section