Skip to content

Commit e83f91f

Browse files
author
Mike Dirolf
committed
binary wrapper
1 parent b660722 commit e83f91f

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

pymongo/binary.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2009 10gen, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Representation of binary data to be stored in or retrieved from Mongo.
16+
17+
This is necessary because we want to store normal strings as the Mongo string
18+
type. We need to wrap binary so we can tell the difference between what should
19+
be considered binary and what should be considered a string.
20+
"""
21+
22+
def is_binary(data):
23+
"""Check if the given data is binary or not.
24+
"""
25+
return isinstance(data, Binary)
26+
27+
class Binary(str):
28+
"""Binary data stored in or retrieved from Mongo.
29+
"""
30+
pass

test/test_binary.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2009 10gen, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Tests for the Binary wrapper."""
16+
17+
import unittest
18+
19+
from pymongo import binary
20+
21+
class TestBinary(unittest.TestCase):
22+
def setUp(self):
23+
pass
24+
25+
def test_binary(self):
26+
a_string = "hello world"
27+
a_binary = binary.Binary("hello world")
28+
self.assertTrue(a_binary.startswith("hello"))
29+
self.assertTrue(a_binary.endswith("world"))
30+
self.assertTrue(binary.is_binary(a_binary))
31+
self.assertFalse(binary.is_binary(a_string))
32+
33+
if __name__ == "__main__":
34+
unittest.main()

0 commit comments

Comments
 (0)