api.rst.txt 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. API Reference
  2. =============
  3. Import the *xdice* library with `import dice`
  4. The dice module
  5. ---------------
  6. dice.compile(pattern\_string)
  7. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  8. Similar to `xdice.Pattern(pattern_string).compile()`
  9. dice.roll(pattern\_string)
  10. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  11. Similar to `xdice.Pattern(pattern_string).roll()`
  12. dice.rolldice(faces, amount=1, drop\_lowest=0, drop\_highest=0)
  13. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  14. Similar to `xdice.Dice(faces, amount, drop_lowest, drop_highest).roll()`
  15. Dice class
  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. Properties
  22. ^^^^^^^^^^
  23. - `dice.sides`: number of sides of the dice
  24. - `dice.amount`: amount of dice to roll
  25. - `dice.drop_lowest`: amount of lowest scores to drop
  26. - `dice.drop_highest`: amount of highest scores to drop
  27. - `dice.name` : Descriptive name of the Dice object
  28. dice.roll()
  29. ^^^^^^^^^^^
  30. Role the dice and return a Score object
  31. *[classmethod]* Dice.parse(cls, pattern)
  32. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  33. Parse a pattern of the form ‘AdX’, where A and X are positive
  34. integers, then return the corresponding Dice object. Use
  35. ‘AdX[Ln][Hn]’ to drop the n lowest and/or highest dice when rolled.
  36. Score class
  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. Properties
  59. ^^^^^^^^^^
  60. - `score.detail`: similar to list(score), return the list of the individual results
  61. - `score.name`: descriptive name of the dice rolled
  62. - `score.dropped`: list of the dropped results
  63. score.format(verbose=False)
  64. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  65. A formatted string describing the detailed result.
  66. Pattern class
  67. -------------
  68. Dice notation pattern.
  69. Pattern.__init__ (instr)
  70. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  71. Instantiate a Pattern object.
  72. pattern.compile()
  73. ^^^^^^^^^^^^^^^^^
  74. Parse the pattern. Two properties are updated at this time:
  75. - *pattern.format\_string*
  76. The ready-to-be-formatted string built from the ``instr`` argument.
  77. *Eg: ‘1d6+4+1d4’ => ‘{0}+4-{1}’*
  78. - *pattern.dices*
  79. The list of parsed dice.
  80. *Eg: ‘1d6+4+1d4’ => [(Dice; sides=6;amount=1), (Dice;
  81. sides=4;amount=1)]*
  82. pattern.roll()
  83. ^^^^^^^^^^^^^^
  84. Compile the pattern if it has not been yet, then roll the dice.
  85. Return a PatternScore object.
  86. PatternScore class
  87. -------------------
  88. PatternScore is a subclass of **integer**, you can then manipulate
  89. it as you would do with an integer.
  90. Moreover, you can get the list of the scores with the score(i) or
  91. scores() methods, and retrieve a formatted result with the format()
  92. method.
  93. pattern\_score.scores()
  94. ^^^^^^^^^^^^^^^^^^^^^^^
  95. Returns the list of Score objects extracted from the pattern and
  96. rolled.
  97. pattern\_score.format(verbose=False)
  98. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  99. A formatted string describing the detailed result.