예제 #1
0
def grid_to_image(grid: list, d=frozendict({'0': (255, 255, 255), '1': (0, 0, 0)})) -> Image:
    image = Image.new(mode='RGB', size=(len(grid[0]), len(grid)))

    data = []

    for row in grid:
        rgbrow = row_to_rgb(row, d)
        for item in rgbrow:
            data.append(item)

    print(data)

    image.putdata(data=tuple(data))

    return image
예제 #2
0
 def values(self, variable: Variable, bindings: Bindings) -> Set[Value]:
     assert variable == self.name or variable in self.path_template.placeholders
     assert variable not in self.locally_bound_variables
     assert not any(lvar in bindings
                    for lvar in self.locally_bound_variables)
     path_template = self.path_template.with_replacements(bindings)
     if variable == self.name:
         # TODO: important: evaluate at this point in time instead? Nope.
         aggregating = ",".join(path_template.placeholders)
         point_map = frozendict({aggregating: path_template.template})
         return set([point_map])
     else:
         paths = io.glob(path_template.glob)
         values = set(
             self.path_template.match(path)[variable] for path in paths)
         return values
예제 #3
0
def sanitize_json(input, ignored_parts):
    # types (Any,Dict) -> Any
    if isinstance(input, list):
        to_be_sorted = list(sanitize_json(x, ignored_parts) for x in input)
        return tuple(sorted(to_be_sorted, key=lambda x: x.__hash__()))
    elif isinstance(input, basestring):
        return input.replace("'", '"')
    elif isinstance(input, float):
        return round(input, 5)
    elif isinstance(input, dict):
        result = {}
        for key, value in input.items():
            sub_ignored_parts = None
            if isinstance(ignored_parts, dict):
                sub_ignored_parts = ignored_parts.get(key)
            elif isinstance(ignored_parts, str) and ignored_parts == key:
                continue
            result[key] = sanitize_json(value, sub_ignored_parts or {})
        input = frozendict(**result)
    return input
예제 #4
0
def sanitize_json(input, ignored_parts):
    # types (Any,Dict) -> Any
    if isinstance(input, list):
        to_be_sorted=list(sanitize_json(x, ignored_parts) for x in input)
        return tuple(sorted(to_be_sorted, key=lambda x: x.__hash__() ))
    elif isinstance(input,basestring):
        return input.replace("'",'"')
    elif isinstance(input,float):
        return round(input,5)
    elif isinstance(input,dict):
        result ={}
        for key, value in input.items():
            sub_ignored_parts = None
            if isinstance(ignored_parts, dict):
                sub_ignored_parts=ignored_parts.get(key)
            elif isinstance(ignored_parts,str) and ignored_parts == key:
                continue
            result[key]=sanitize_json(value, sub_ignored_parts or {})
        input = frozendict(result)
    return input
예제 #5
0
from utilspie.collectionsutils import frozendict

from hassprite import HasSprite


class Item(HasSprite):
    name = "Default Item"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.name = kwargs.get("name", self.name)

    def __str__(self):
        return self.name

    def __repr__(self):
        return f"{self.name} ({self.display_sprite()})"


# Useful list of items.
items = frozendict({
    'pebble':
        Item(sprite=".", name="pebble"),

    'stick':
        Item(sprite="/", name="stick"),
})
예제 #6
0
def freeze_dict(d: Dict) -> Dict:
    return (frozendict(d))
예제 #7
0
class Tile(HasSprite):
    """
    A Tile.
    Can have Items on it, and Entities occupying it.
    """

    name = "Unnamed Tile"
    """The name of the tile."""

    ground = []
    """A list of all Items on the ground of this Tile."""

    entities: [Entity] = []
    """A list of all Entities occupying this Tile."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.name = kwargs.get('name', self.name)
        self.ground = kwargs.get('ground', self.ground)
        self.entities = kwargs.get('entities', self.entities)

    def add_item(self, item: Item):
        self.ground.append(item)


# Useful list of tiles.
tiles = frozendict({
    "stone_floor": Tile(sprite="_", name="Stone floor"),
})