Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix block.py and add test for file block.py #25

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ed6a196
Fix block.py and add test for file
hashbangstudio Oct 1, 2014
ff8a464
Added missing enum34 dependency
doismellburning Oct 1, 2014
4d18907
Rename test to indicate not py.test form
hashbangstudio Oct 1, 2014
d6db56f
Revert "Added missing enum34 dependency"
doismellburning Oct 1, 2014
9a1edc7
Remove runner code in test
hashbangstudio Oct 1, 2014
b69f607
Remove block ordering comparisons
hashbangstudio Oct 1, 2014
9182af3
Remove ordered comparison tests
hashbangstudio Oct 1, 2014
1c6ec04
Undo unintentional indent change
hashbangstudio Oct 1, 2014
810f99d
Update setup.py to inline with master
hashbangstudio Oct 1, 2014
728b943
Fix comparison and typo
hashbangstudio Oct 1, 2014
acf013c
Use PEP8 method names and clarify enum test
hashbangstudio Oct 1, 2014
bed58c6
Fix missing func def and class naming
hashbangstudio Oct 1, 2014
11bb748
Hopefully correct unintentional indent change
hashbangstudio Oct 1, 2014
c8dad24
Move to PEP8 compliant naming
hashbangstudio Oct 2, 2014
2d3500c
Move to py.test style
hashbangstudio Oct 2, 2014
3c1fd01
Rename for clarity and PEP8
hashbangstudio Oct 2, 2014
f42e1f3
Add eval repr test and use constant string
hashbangstudio Oct 2, 2014
ffb7ca3
Merge remote-tracking branch 'upstream/block-update' into block-update
hashbangstudio Oct 19, 2014
b677dc6
Merge remote-tracking branch 'upstream/master' into block-update
hashbangstudio Oct 19, 2014
a3e3a00
Fix conflict between files
hashbangstudio Oct 19, 2014
8cb35c0
Add new block elements
hashbangstudio Oct 19, 2014
23f43fe
Remove default type assignment
hashbangstudio Oct 19, 2014
2f58179
Also remove the comment line
hashbangstudio Oct 19, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions minecraft/block.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,68 @@
import sys
if sys.version_info < (3, 4):
from flufl.enum import Enum
else:
from enum import Enum
from enum import IntEnum


class Block:
"""
Minecraft PI block description. Can be sent to Minecraft.setBlock/s
block.type = the blockID of a block (It's meterial)
block.data = A unknown member that does something
block.type = the blockID of a block (It's material)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're fixing the spelling could you fix the apostrophe too please!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(yes "it's" technically works, but it's incredibly misleading...)

block.data = The variant of the type of block.
For example the colour of wool or the orientation of stairs
The default type for blocks is dirt
"""

def __init__(self, type=3, data=0):
self.type = type
self.data = data

def __cmp__(self, rhs):
return hash(self) - hash(rhs)
def __eq__(self, rhs):
"""
Equality override
Two blocks are equal only if their hashes are equal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except that's only true in this case where hash doesn't collide, and not generally true - would you mind making a proper __eq__ please?

"""
return hash(self) == hash(rhs)

def __ne__(self, rhs):
"""
not equal override
"""
return not (self == rhs)

def __hash__(self):
return (self.id << 8) + self.data
"""
Override of hash generation
Returns a unique representation of contents of block
"""
return (self.type << 8) + self.data

# TODO: This looks wierd, does it have any use?
def __iter__(self):
"""Allows a Block to be sent whenever id [and data] is needed"""
return iter((self.id, self.data))
"""
Returns an Iterator of the contents of the Block class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this changes a bit of the API we don't want to change, but we aren't testing thoroughly enough :s

Makes the Block an Iterable object
This means that Block can be treated like a list/tuple in places
list/tuple of type and data
For example when flattening lists or *args
Allows a Block to be sent whenever id [and data] is needed
"""
return iter((self.type, self.data))

def __repr__(self):
return 'Block({}, {:.2f})'.format(self.id, self.data)
""" Override string representation """
return 'Block({:d}, {:d})'.format(self.type, self.data)

# TODO: find out if this has any use
def withData(self, data):
return Block(self.id, data)
"""
Returns a new block with the same type as the invoking block but
with the passed in data value
"""
return Block(self.type, data)

@property
def id(self):
""" Here for backwards compatibility for previous attribute name """
return self.type


class blockType(Enum):
class blockType(IntEnum):
AIR = 0
STONE = 1
GRASS = 2
Expand Down
34 changes: 21 additions & 13 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#!/usr/bin/env python
import io
from setuptools import setup, find_packages
import sys

with io.open('README.rst', mode='r', encoding='utf8') as f:
readme = f.read()


dependencies = []
if sys.version_info[:2] < (3, 4):
dependencies.append('enum34')


setup(name='py3minepi',
version='0.0.1',
description='A better minecraft pi library.',
Expand All @@ -14,17 +21,18 @@
include_package_data=True,
keywords='minecraft raspberry pi mcpi py3minepi',
long_description=readme,
install_requires=dependencies,
classifiers=[
'Development Status :: Development Status :: 3 - Alpha',
'Environment :: X11 Applications',
'Intended Audience :: Education',
'Intended Audience :: Developers',
'License :: OSI Approved :: Other/Proprietary License', # TODO fix
'Operating System :: POSIX',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
],
)
'Development Status :: Development Status :: 3 - Alpha',
'Environment :: X11 Applications',
'Intended Audience :: Education',
'Intended Audience :: Developers',
'License :: OSI Approved :: Other/Proprietary License', # TODO fix
'Operating System :: POSIX',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
],
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:(

121 changes: 121 additions & 0 deletions tests/test_unit_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import unittest
from minecraft.block import Block
from minecraft.block import blockType


class TestBlock(unittest.TestCase):

def testRepresentation(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 / Pythonic method names please

# Test repr
b = Block(2, 8)
expectedString = "Block({:d}, {:d})".format(b.type, b.data)
rep = repr(b)
self.assertEqual(rep, expectedString)

def testInstantiationAndWithDataFunction(self):
blck = Block(12)
self.assertEqual(blck.type, 12)
self.assertEqual(blck.data, 0)
blckWithData = Block(12, 4)
self.assertEqual(blckWithData.type, 12)
self.assertEqual(blckWithData.data, 4)
otherBlckWithData = blck.withData(8)
self.assertEqual(otherBlckWithData.type, 12)
self.assertEqual(otherBlckWithData.data, 8)

def testBackwardsCompatibility(self):
blck = Block(12)
self.assertEqual(blck.type, blck.id)

def testComparison(self):
b1 = Block(8, 3)
bSame = Block(8, 3)
bDiffId = Block(12, 3)
bDiffData = Block(8, 7)
bDiffIdAndData = Block(51, 7)

self.assertTrue(b1 == b1)
self.assertTrue(b1 == bSame)
self.assertTrue(b1 != bDiffId)
self.assertTrue(b1 != bDiffData)
self.assertTrue(b1 != bDiffIdAndData)

def testIteration(self):
idAndData = [35, 4]
b = Block(idAndData[0], idAndData[1])
for index, attr in enumerate(b):
self.assertEqual(attr, idAndData[index])

def testBlockConstants(self):
self.assertEqual(blockType.AIR, 0)
self.assertEqual(blockType.STONE, 1)
self.assertEqual(blockType.GRASS, 2)
self.assertEqual(blockType.DIRT, 3)
self.assertEqual(blockType.COBBLESTONE, 4)
self.assertEqual(blockType.WOOD_PLANKS, 5)
self.assertEqual(blockType.SAPLING, 6)
self.assertEqual(blockType.BEDROCK, 7)
self.assertEqual(blockType.WATER_FLOWING, 8)
self.assertEqual(blockType.WATER, blockType.WATER_FLOWING)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you pull this out into a distinct test please? (as it's somewhat different to the other "is the id the one hardcoded here" tests but easy to not notice!)

self.assertEqual(blockType.WATER_STATIONARY, 9)
self.assertEqual(blockType.LAVA_FLOWING, 10)
self.assertEqual(blockType.LAVA, blockType.LAVA_FLOWING)
self.assertEqual(blockType.LAVA_STATIONARY, 11)
self.assertEqual(blockType.SAND, 12)
self.assertEqual(blockType.GRAVEL, 13)
self.assertEqual(blockType.GOLD_ORE, 14)
self.assertEqual(blockType.IRON_ORE, 15)
self.assertEqual(blockType.COAL_ORE, 16)
self.assertEqual(blockType.WOOD, 17)
self.assertEqual(blockType.LEAVES, 18)
self.assertEqual(blockType.GLASS, 20)
self.assertEqual(blockType.LAPIS_LAZULI_ORE, 21)
self.assertEqual(blockType.LAPIS_LAZULI_BLOCK, 22)
self.assertEqual(blockType.SANDSTONE, 24)
self.assertEqual(blockType.BED, 26)
self.assertEqual(blockType.COBWEB, 30)
self.assertEqual(blockType.GRASS_TALL, 31)
self.assertEqual(blockType.WOOL, 35)
self.assertEqual(blockType.FLOWER_YELLOW, 37)
self.assertEqual(blockType.FLOWER_CYAN, 38)
self.assertEqual(blockType.MUSHROOM_BROWN, 39)
self.assertEqual(blockType.MUSHROOM_RED, 40)
self.assertEqual(blockType.GOLD_BLOCK, 41)
self.assertEqual(blockType.IRON_BLOCK, 42)
self.assertEqual(blockType.STONE_SLAB_DOUBLE, 43)
self.assertEqual(blockType.STONE_SLAB, 44)
self.assertEqual(blockType.BRICK_BLOCK, 45)
self.assertEqual(blockType.TNT, 46)
self.assertEqual(blockType.BOOKSHELF, 47)
self.assertEqual(blockType.MOSS_STONE, 48)
self.assertEqual(blockType.OBSIDIAN, 49)
self.assertEqual(blockType.TORCH, 50)
self.assertEqual(blockType.FIRE, 51)
self.assertEqual(blockType.STAIRS_WOOD, 53)
self.assertEqual(blockType.CHEST, 54)
self.assertEqual(blockType.DIAMOND_ORE, 56)
self.assertEqual(blockType.DIAMOND_BLOCK, 57)
self.assertEqual(blockType.CRAFTING_TABLE, 58)
self.assertEqual(blockType.FARMLAND, 60)
self.assertEqual(blockType.FURNACE_INACTIVE, 61)
self.assertEqual(blockType.FURNACE_ACTIVE, 62)
self.assertEqual(blockType.DOOR_WOOD, 64)
self.assertEqual(blockType.LADDER, 65)
self.assertEqual(blockType.STAIRS_COBBLESTONE, 67)
self.assertEqual(blockType.DOOR_IRON, 71)
self.assertEqual(blockType.REDSTONE_ORE, 73)
self.assertEqual(blockType.SNOW, 78)
self.assertEqual(blockType.ICE, 79)
self.assertEqual(blockType.SNOW_BLOCK, 80)
self.assertEqual(blockType.CACTUS, 81)
self.assertEqual(blockType.CLAY, 82)
self.assertEqual(blockType.SUGAR_CANE, 83)
self.assertEqual(blockType.FENCE, 85)
self.assertEqual(blockType.GLOWSTONE_BLOCK, 89)
self.assertEqual(blockType.BEDROCK_INVISIBLE, 95)
self.assertEqual(blockType.STONE_BRICK, 98)
self.assertEqual(blockType.GLASS_PANE, 102)
self.assertEqual(blockType.MELON, 103)
self.assertEqual(blockType.FENCE_GATE, 107)
self.assertEqual(blockType.GLOWING_OBSIDIAN, 246)
self.assertEqual(blockType.NETHER_REACTOR_CORE, 247)