-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Bernardo-MG/merge_master
Merge master
- Loading branch information
Showing
52 changed files
with
2,105 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,5 @@ | |
:license: MIT, see LICENSE for more details. | ||
""" | ||
|
||
__version__ = '0.1.0' | ||
__version__ = '1.0.0' | ||
__license__ = 'MIT' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import sys | ||
from abc import ABCMeta, abstractmethod | ||
from random import randint | ||
|
||
""" | ||
Dice classes. | ||
These are just plain dice, not dice notation, even thought they may be used | ||
for handling that too. | ||
The classes in this file will allow creating and using dice and rollable | ||
entities, which are able to generate a random value. | ||
There are two dice classes, the Dice is just the bare dice, while the | ||
RollableDice is an extension, which allows rolling the dice. | ||
""" | ||
|
||
__author__ = 'Benardo Martínez Garrido' | ||
__license__ = 'MIT' | ||
|
||
if sys.version_info[0] >= 3: | ||
xrange = range | ||
|
||
|
||
class Rollable(object): | ||
""" | ||
Interface for rollable classes. | ||
While rolling implies using dice to generate a random value, this interface | ||
just takes care of generating a random value. | ||
This way it not only can support any kind of dice, but also more complex | ||
constructions such as dice notation expressions, where calling the roll | ||
method would execute the full expression. | ||
As such, the value generated by rolling may be anything. | ||
""" | ||
__metaclass__ = ABCMeta | ||
|
||
def __init__(self): | ||
pass | ||
|
||
@abstractmethod | ||
def roll(self): | ||
""" | ||
Generates a random value. | ||
This can be anything, the only expectation is that the output | ||
is randomized somehow. | ||
""" | ||
raise NotImplementedError('The roll method must be implemented') | ||
|
||
|
||
class Dice(object): | ||
""" | ||
A group of dice, all with the same number of sides. Such a group is just | ||
composed of a quantity of dice, and their number of sides. | ||
Both the quantity and the number of sides are expected to be positive, as | ||
any other value would make no sense. | ||
No other limitation is expected. In the real world the number of sides | ||
which a die may physically have are limited by the rules of geometry, | ||
but there is no reason to take care of that. | ||
""" | ||
|
||
def __init__(self, quantity, sides): | ||
super(Dice, self).__init__() | ||
self._quantity = quantity | ||
self._sides = sides | ||
|
||
def __str__(self): | ||
return '%sd%s' % (self.quantity, self.sides) | ||
|
||
def __repr__(self): | ||
return '<class %s>(quantity=%r, sides=%r)' % \ | ||
(self.__class__.__name__, self.quantity, self.sides) | ||
|
||
@property | ||
def quantity(self): | ||
""" | ||
The number of dice which compose this group. | ||
This is expected to be a positive value or zero. | ||
:return: the number of dice | ||
""" | ||
return self._quantity | ||
|
||
@quantity.setter | ||
def quantity(self, quantity): | ||
self._quantity = quantity | ||
|
||
@property | ||
def sides(self): | ||
""" | ||
The number of sides each die has. | ||
All the dice in the group have the same number of sides. | ||
This is expected to be a positive value or zero. | ||
:return: the number of sides | ||
""" | ||
return self._sides | ||
|
||
@sides.setter | ||
def sides(self, sides): | ||
self._sides = sides | ||
|
||
|
||
class RollableDice(Dice, Rollable): | ||
""" | ||
A rollable dice group. | ||
The result of calling the roll method will be an integer, which will be | ||
between 1 and the number of sides. Actually one number will be generated | ||
like that as many times as the value of the quantity field, and all those | ||
values will be added, and then returned. | ||
""" | ||
|
||
def __init__(self, quantity, sides): | ||
super(RollableDice, self).__init__(quantity, sides) | ||
|
||
def roll(self): | ||
result = 0 | ||
|
||
if self.quantity == 0 or self.sides == 0: | ||
result = 0 | ||
elif self.quantity is None or self.sides is None: | ||
result = None | ||
elif self.quantity > 0 and self.sides > 0: | ||
for x in xrange(self.quantity): | ||
result += randint(1, self.sides) | ||
else: | ||
result = None | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from dice_notation.parser.dice import DiceParser | ||
|
||
__all__ = ['DiceParser'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
from abc import ABCMeta, abstractmethod | ||
|
||
import ply.lex as lex | ||
import ply.yacc as yacc | ||
|
||
""" | ||
Base classes for parsers. | ||
It contains some interfaces, along a base parser for the parsing library being | ||
used. | ||
""" | ||
|
||
__author__ = 'Bernardo Martínez Garrido' | ||
__license__ = 'MIT' | ||
|
||
|
||
class Parser(object): | ||
""" | ||
Interface for implementing parsers. | ||
It just contains a single method, 'parse', which will receive a value | ||
and take care of parsing it into another. | ||
""" | ||
|
||
__metaclass__ = ABCMeta | ||
|
||
def __init__(self): | ||
pass | ||
|
||
@abstractmethod | ||
def parse(self, value): | ||
raise NotImplementedError('The parse method must be implemented') | ||
|
||
|
||
class PlyParser(Parser): | ||
""" | ||
Base class for a lexer/parser that has the rules defined as methods. | ||
It makes use of Ply for the parsing. | ||
""" | ||
|
||
tokens = () | ||
precedence = () | ||
|
||
def __init__(self, **kw): | ||
super(PlyParser, self).__init__() | ||
self.debug = kw.get('debug', 0) | ||
self.names = {} | ||
try: | ||
modname = os.path.split(os.path.splitext(__file__)[0])[ | ||
1] + "_" + self.__class__.__name__ | ||
except: | ||
modname = "parser" + "_" + self.__class__.__name__ | ||
self.debugfile = modname + ".dbg" | ||
self.tabmodule = modname + "_" + "parsetab" | ||
# print self.debugfile, self.tabmodule | ||
|
||
# Builds the lexer and parser | ||
lex.lex(module=self, debug=self.debug) | ||
yacc.yacc(module=self, | ||
debug=self.debug, | ||
debugfile=self.debugfile, | ||
tabmodule=self.tabmodule) | ||
|
||
def parse(self, value): | ||
return yacc.parse(value) |
Oops, something went wrong.