API.md.txt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # API
  2. #### xdice.compile(pattern_string)
  3. > *Similar to `xdice.Pattern(pattern_string).compile()`*
  4. > Returns a compiled Pattern object.
  5. > Pattern object can then be rolled to obtain a PatternScore object.
  6. #### xdice.roll(pattern_string)
  7. > *Similar to `xdice.Pattern(pattern_string).roll()`*
  8. #### xdice.rolldice(faces, amount=1)
  9. > *Similar to `xdice.Dice(faces, amount).roll()`*
  10. ## Dice object
  11. > Set of dice.
  12. #### Dice.__init__(sides, amount=1)
  13. > Instantiate a set of dice.
  14. #### dice.roll()
  15. > Role the dice and return a Score object
  16. ####*[classmethod]* Dice.parse(cls, pattern)
  17. > Parse a pattern of the form 'AdX', where A and X are positive integers.
  18. > Returns the corresponding Dice object.
  19. ## Score object
  20. > Score is a subclass of integer, you can then manipulate it as you would do with an integer.
  21. > It also provides an access to the detailed score with the property 'detail'.
  22. 'detail' is the list of the scores obtained by each dice.
  23. > Score class can also be used as an iterable, to walk trough the individual scores.
  24. eg:
  25. >>> s = Score([1,2,3])
  26. >>> print(s)
  27. 6
  28. >>> s + 1
  29. 7
  30. >>> list(s)
  31. [1,2,3]
  32. #### Score.__new__(iterable)
  33. >*`iterable` should only contain integers*
  34. > Score value will be the sum of the list's values.
  35. ## Pattern object
  36. > Dice notation pattern.
  37. #### Pattern.__init__(instr)
  38. > Instantiate a Pattern object.
  39. #### pattern.compile()
  40. > Parse the pattern. Two properties are updated at this time:
  41. * *pattern.format_string*
  42. > The ready-to-be-formatted string built from the `instr` argument.
  43. > *Eg: '1d6+4+1d4' => '{0}+4-{1}'*
  44. * *pattern.dices*
  45. > The list of parsed dice.
  46. > *Eg: '1d6+4+1d4' => [(Dice; sides=6;amount=1), (Dice; sides=4;amount=1)]*
  47. #### pattern.roll()
  48. > Compile the pattern if it has not been yet, then roll the dice.
  49. > Return a PatternScore object.
  50. ## PatternScore object
  51. > PatternScore is a subclass of **integer**, you can then manipulate it as you would do with an integer.
  52. > Moreover, you can get the list of the scores with the score(i) or scores() methods, and retrieve a formatted result with the format() method.
  53. #### pattern_score.scores()
  54. > Returns the list of Score objects extracted from the pattern and rolled.
  55. #### pattern_score.score(i)
  56. > Returns the Score object at index i.
  57. #### pattern_score.format()
  58. > Return a formatted string detailing the result of the roll.
  59. > *Eg: '3d6+4' => '[1,5,6]+4'*