<commit_before>import time
import datetime
import RPi.GPIO as GPIO

class DS1302:
    CLK_DELAY = 0.00001

    def __init__(self, clk_pin=11, data_pin=13, ce_pin=15):
        # init GPIO
        # TODO no warnings
        GPIO.setwarnings(False)
        # use safer pin number (avoid GPIO renumber on each Pi release)
        GPIO.setmode(GPIO.BOARD)
        # set GPIO pins
        self._clk_pin = clk_pin
        self._data_pin = data_pin
        self._ce_pin = ce_pin
        # CLK and CE (sometime call RST) pin are always output
        GPIO.setup(self._clk_pin, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(self._ce_pin, GPIO.OUT, initial=GPIO.LOW)
        # turn off WP (write protect)
        self._start_tx()
        self._w_byte(0x8e)
        self._w_byte(0x00)
        self._end_tx()
        # charge mode is disabled
        self._start_tx()
        self._w_byte(0x90)
        self._w_byte(0x00)
        self._end_tx()

    def _start_tx(self):
        """
        Start of transaction.
        """
        GPIO.setup(self._data_pin, GPIO.OUT, initial=GPIO.LOW)
        GPIO.output(self._clk_pin, GPIO.LOW)
        GPIO.output(self._data_pin, GPIO.LOW)
        time.sleep(self.CLK_DELAY)
        GPIO.output(self._ce_pin, GPIO.HIGH)

    def _end_tx(self):
        """
        End of transaction.
        """
        GPIO.setup(self._data_pin, GPIO.OUT, initial=GPIO.LOW)
        GPIO.output(self._clk_pin, GPIO.LOW)
        GPIO.output(self._data_pin, GPIO.LOW)
        time.sleep(self.CLK_DELAY)
        GPIO.output(self._ce_pin, GPIO.LOW)

    def _r_byte(self):
        """
        Read byte from the chip.

        :return: byte value
        :rtype: int
        """
        # data pin is now input with pull-up resistor
        GPIO.setup(self._data_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        # clock the byte from chip
        byte = 0
        for i in range(8):
            # make a pulse on CLK pin
            time.sleep(self.CLK_DELAY)
            GPIO.output(self._clk_pin, GPIO.HIGH)
            time.sleep(self.CLK_DELAY)
            GPIO.output(self._clk_pin, GPIO.LOW)
            # store current bit into byte
            time.sleep(self.CLK_DELAY)
            bit = GPIO.input(self._data_pin)
            byte |= ((2 ** i) * bit)
        # return byte value
        return byte

    def _w_byte(self, byte):
        """
        Write byte to the chip.

        :param byte: byte value
        :type byte: int
        """
        # data pin is now output
        GPIO.setup(self._data_pin, GPIO.OUT, initial=GPIO.LOW)
        # clock the byte to chip
        for _ in range(8):
            GPIO.output(self._clk_pin, GPIO.LOW)
            time.sleep(self.CLK_DELAY)
            GPIO.output(self._data_pin, byte & 0x01)
            byte >>= 1
            GPIO.output(self._clk_pin, GPIO.HIGH)
            time.sleep(self.CLK_DELAY)

    def read_ram(self):
        """
        Read RAM as bytes

        :return: RAM dumps
        :rtype: bytearray
        """
        # start of message
        self._start_tx()
        # read ram burst
        self._w_byte(0xff)
        # Read data bytes.
        bytes = bytearray()
        for _ in range(31):
            bytes.append(self._r_byte())
        # end of message
        self._end_tx()
        return bytes

    def write_ram(self, bytes):
        """
        Write RAM with bytes

        :param bytes: bytes to write
        :type bytes: bytearray
        """
        # start message
        self._start_tx()
        # write ram burst
        self._w_byte(0xfe)
        # Write data bytes.
        for i in range(min(len(bytes), 31)):
            self._w_byte(ord(bytes[i:i + 1]))
        # for _ in range(31 - len(bytes)):
        #     self._w_byte(ord(' '))
        # end of message
        self._end_tx()

    def read_datetime(self):
        """
        Read current date and time from RTC chip.

        :return: date and time
        :rtype: datetime
        """
        # start message
        self._start_tx()
        # read clock burst
        self._w_byte(0xbf)
        byte_l = []
        for _ in range(7):
            byte_l.append(self._r_byte())
        # end of message
        self._end_tx()
        # decode bytes
        second = ((byte_l[0] & 0x70) >> 4) * 10 + (byte_l[0] & 0x0f)
        minute = ((byte_l[1] & 0x70) >> 4) * 10 + (byte_l[1] & 0x0f)
        hour = ((byte_l[2] & 0x30) >> 4) * 10 + (byte_l[2] & 0x0f)
        day = ((byte_l[3] & 0x30) >> 4) * 10 + (byte_l[3] & 0x0f)
        month = ((byte_l[4] & 0x10) >> 4) * 10 + (byte_l[4] & 0x0f)
        year = ((byte_l[6] & 0xf0) >> 4) * 10 + (byte_l[6] & 0x0f) + 2000
        # return datetime value
        return datetime.datetime(year, month, day, hour, minute, second)

    def write_datetime(self, dt):
        """
        Write a python datetime to RTC chip.

        :param dt: datetime to write
        :type dt: datetime
        """
        # format message
        byte_l = [0] * 9
        byte_l[0] = (dt.second // 10) << 4 | dt.second % 10
        byte_l[1] = (dt.minute // 10) << 4 | dt.minute % 10
        byte_l[2] = (dt.hour // 10) << 4 | dt.hour % 10
        byte_l[3] = (dt.day // 10) << 4 | dt.day % 10
        byte_l[4] = (dt.month // 10) << 4 | dt.month % 10
        byte_l[5] = (dt.weekday() // 10) << 4 | dt.weekday() % 10
        byte_l[6] = ((dt.year-2000) // 10) << 4 | (dt.year-2000) % 10
        # start message
        self._start_tx()
        # write clock burst
        self._w_byte(0xbe)
        # Write all data
        for byte in byte_l:
            self._w_byte(byte)
        # end of message
        self._end_tx()

    @staticmethod
    def close():
        """
        Clean all GPIOs.
        """
        GPIO.cleanup()
<commit_msg>fix cosmetic<commit_after>import time
import datetime
import RPi.GPIO as GPIO

class DS1302:
    CLK_DELAY = 0.00001

    def __init__(self, clk_pin=11, data_pin=13, ce_pin=15):
        # init GPIO
        # no warnings
        GPIO.setwarnings(False)
        # use safer pin number (avoid GPIO renumber on each Pi release)
        GPIO.setmode(GPIO.BOARD)
        # set GPIO pins
        self._clk_pin = clk_pin
        self._data_pin = data_pin
        self._ce_pin = ce_pin
        # CLK and CE (sometime call RST) pin are always output
        GPIO.setup(self._clk_pin, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(self._ce_pin, GPIO.OUT, initial=GPIO.LOW)
        # turn off WP (write protect)
        self._start_tx()
        self._w_byte(0x8e)
        self._w_byte(0x00)
        self._end_tx()
        # charge mode is disabled
        self._start_tx()
        self._w_byte(0x90)
        self._w_byte(0x00)
        self._end_tx()

    def _start_tx(self):
        """
        Start of transaction.
        """
        GPIO.setup(self._data_pin, GPIO.OUT, initial=GPIO.LOW)
        GPIO.output(self._clk_pin, GPIO.LOW)
        GPIO.output(self._data_pin, GPIO.LOW)
        time.sleep(self.CLK_DELAY)
        GPIO.output(self._ce_pin, GPIO.HIGH)

    def _end_tx(self):
        """
        End of transaction.
        """
        GPIO.setup(self._data_pin, GPIO.OUT, initial=GPIO.LOW)
        GPIO.output(self._clk_pin, GPIO.LOW)
        GPIO.output(self._data_pin, GPIO.LOW)
        time.sleep(self.CLK_DELAY)
        GPIO.output(self._ce_pin, GPIO.LOW)

    def _r_byte(self):
        """
        Read byte from the chip.

        :return: byte value
        :rtype: int
        """
        # data pin is now input with pull-up resistor
        GPIO.setup(self._data_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        # clock the byte from chip
        byte = 0
        for i in range(8):
            # make a pulse on CLK pin
            time.sleep(self.CLK_DELAY)
            GPIO.output(self._clk_pin, GPIO.HIGH)
            time.sleep(self.CLK_DELAY)
            GPIO.output(self._clk_pin, GPIO.LOW)
            # store current bit into byte
            time.sleep(self.CLK_DELAY)
            bit = GPIO.input(self._data_pin)
            byte |= ((2 ** i) * bit)
        # return byte value
        return byte

    def _w_byte(self, byte):
        """
        Write byte to the chip.

        :param byte: byte value
        :type byte: int
        """
        # data pin is now output
        GPIO.setup(self._data_pin, GPIO.OUT, initial=GPIO.LOW)
        # clock the byte to chip
        for _ in range(8):
            GPIO.output(self._clk_pin, GPIO.LOW)
            time.sleep(self.CLK_DELAY)
            GPIO.output(self._data_pin, byte & 0x01)
            byte >>= 1
            GPIO.output(self._clk_pin, GPIO.HIGH)
            time.sleep(self.CLK_DELAY)

    def read_ram(self):
        """
        Read RAM as bytes

        :return: RAM dumps
        :rtype: bytearray
        """
        # start of message
        self._start_tx()
        # read ram burst
        self._w_byte(0xff)
        # read data bytes
        bytes = bytearray()
        for _ in range(31):
            bytes.append(self._r_byte())
        # end of message
        self._end_tx()
        return bytes

    def write_ram(self, bytes):
        """
        Write RAM with bytes

        :param bytes: bytes to write
        :type bytes: bytearray
        """
        # start message
        self._start_tx()
        # write ram burst
        self._w_byte(0xfe)
        # write data bytes
        for i in range(min(len(bytes), 31)):
            self._w_byte(ord(bytes[i:i + 1]))
        # end of message
        self._end_tx()

    def read_datetime(self):
        """
        Read current date and time from RTC chip.

        :return: date and time
        :rtype: datetime
        """
        # start message
        self._start_tx()
        # read clock burst
        self._w_byte(0xbf)
        byte_l = []
        for _ in range(7):
            byte_l.append(self._r_byte())
        # end of message
        self._end_tx()
        # decode bytes
        second = ((byte_l[0] & 0x70) >> 4) * 10 + (byte_l[0] & 0x0f)
        minute = ((byte_l[1] & 0x70) >> 4) * 10 + (byte_l[1] & 0x0f)
        hour = ((byte_l[2] & 0x30) >> 4) * 10 + (byte_l[2] & 0x0f)
        day = ((byte_l[3] & 0x30) >> 4) * 10 + (byte_l[3] & 0x0f)
        month = ((byte_l[4] & 0x10) >> 4) * 10 + (byte_l[4] & 0x0f)
        year = ((byte_l[6] & 0xf0) >> 4) * 10 + (byte_l[6] & 0x0f) + 2000
        # return datetime value
        return datetime.datetime(year, month, day, hour, minute, second)

    def write_datetime(self, dt):
        """
        Write a python datetime to RTC chip.

        :param dt: datetime to write
        :type dt: datetime
        """
        # format message
        byte_l = [0] * 9
        byte_l[0] = (dt.second // 10) << 4 | dt.second % 10
        byte_l[1] = (dt.minute // 10) << 4 | dt.minute % 10
        byte_l[2] = (dt.hour // 10) << 4 | dt.hour % 10
        byte_l[3] = (dt.day // 10) << 4 | dt.day % 10
        byte_l[4] = (dt.month // 10) << 4 | dt.month % 10
        byte_l[5] = (dt.weekday() // 10) << 4 | dt.weekday() % 10
        byte_l[6] = ((dt.year-2000) // 10) << 4 | (dt.year-2000) % 10
        # start message
        self._start_tx()
        # write clock burst
        self._w_byte(0xbe)
        # write all data
        for byte in byte_l:
            self._w_byte(byte)
        # end of message
        self._end_tx()

    @staticmethod
    def close():
        """
        Clean all GPIOs.
        """
        GPIO.cleanup()
<|endoftext|>


<commit_before># ===========================================================================
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# ===========================================================================

import clr
clr.AddReference('Python.Test')
clr.AddReference('System.Data')

# testImplicitAssemblyLoad() passes on deprecation warning; perfect! #
##clr.AddReference('System.Windows.Forms')
import sys, os, string, unittest, types

class ModuleTests(unittest.TestCase):
    """Test CLR modules and the CLR import hook."""

    def isCLRModule(self, object):
        return type(object).__name__ == 'ModuleObject'

    def isCLRRootModule(self, object):
        return type(object).__name__ == 'CLRModule'

    def isCLRClass(self, object):
        return type(object).__name__ == 'CLR Metatype' # for now

    def testAAAImportHookWorks(self):
        """Test that the import hook works correctly both using the
           included runtime and an external runtime. This must be
           the first test run in the unit tests!"""

        from System import String

    def test000importClr(self):
        import clr
        self.assertTrue(self.isCLRRootModule(clr))

    def testPreloadVar(self):
        import clr
        self.assertTrue(clr.getPreload() is False, clr.getPreload())
    	clr.setPreload(False)
    	self.assertTrue(clr.getPreload() is False, clr.getPreload())
        try:
            clr.setPreload(True)
            self.assertTrue(clr.getPreload() is True, clr.getPreload())
            clr.setPreload(0)
            self.assertTrue(clr.getPreload() is False, clr.getPreload())
            clr.setPreload(1)
            self.assertTrue(clr.getPreload() is True, clr.getPreload())

            import System.Configuration
            content = dir(System.Configuration)
            self.assertTrue(len(content) > 10, content)
        finally:
            clr.setPreload(False)

    def testModuleInterface(self):
        """Test the interface exposed by CLR module objects."""
        import System
        self.assertEquals(type(System.__dict__), type({}))
        self.assertEquals(System.__name__, 'System')
        self.assertEquals(System.__file__, None)
        self.assertEquals(System.__doc__, None)
        self.assertTrue(self.isCLRClass(System.String))
        self.assertTrue(self.isCLRClass(System.Int32))

    def testSimpleImport(self):
        """Test simple import."""
        import System
        self.assertTrue(self.isCLRModule(System))
        self.assertTrue(System.__name__ == 'System')

        import sys
        self.assertTrue(type(sys) == types.ModuleType)
        self.assertTrue(sys.__name__ == 'sys')

        import httplib
        self.assertTrue(type(httplib) == types.ModuleType)
        self.assertTrue(httplib.__name__ == 'httplib')

    def testSimpleImportWithAlias(self):
        """Test simple import with aliasing."""
        import System as mySystem
        self.assertTrue(self.isCLRModule(mySystem))
        self.assertTrue(mySystem.__name__ == 'System')

        import sys as mySys
        self.assertTrue(type(mySys) == types.ModuleType)
        self.assertTrue(mySys.__name__ == 'sys')

        import httplib as myHttplib
        self.assertTrue(type(myHttplib) == types.ModuleType)
        self.assertTrue(myHttplib.__name__ == 'httplib')

    def testDottedNameImport(self):
        """Test dotted-name import."""
        import System.Reflection
        self.assertTrue(self.isCLRModule(System.Reflection))
        self.assertTrue(System.Reflection.__name__ == 'System.Reflection')

        import xml.dom
        self.assertTrue(type(xml.dom) == types.ModuleType)
        self.assertTrue(xml.dom.__name__ == 'xml.dom')

    def testMultipleDottedNameImport(self):
        """Test an import bug with multiple dotted imports."""
        import System.Data
        self.assertTrue(self.isCLRModule(System.Data))
        self.assertTrue(System.Data.__name__ == 'System.Data')
        import System.Data
        self.assertTrue(self.isCLRModule(System.Data))
        self.assertTrue(System.Data.__name__ == 'System.Data')

    def testDottedNameImportWithAlias(self):
        """Test dotted-name import with aliasing."""
        import System.Reflection as SysRef
        self.assertTrue(self.isCLRModule(SysRef))
        self.assertTrue(SysRef.__name__ == 'System.Reflection')

        import xml.dom as myDom
        self.assertTrue(type(myDom) == types.ModuleType)
        self.assertTrue(myDom.__name__ == 'xml.dom')

    def testSimpleImportFrom(self):
        """Test simple 'import from'."""
        from System import Reflection
        self.assertTrue(self.isCLRModule(Reflection))
        self.assertTrue(Reflection.__name__ == 'System.Reflection')

        from xml import dom
        self.assertTrue(type(dom) == types.ModuleType)
        self.assertTrue(dom.__name__ == 'xml.dom')

    def testSimpleImportFromWithAlias(self):
        """Test simple 'import from' with aliasing."""
        from System import Collections as Coll
        self.assertTrue(self.isCLRModule(Coll))
        self.assertTrue(Coll.__name__ == 'System.Collections')

        from xml import dom as myDom
        self.assertTrue(type(myDom) == types.ModuleType)
        self.assertTrue(myDom.__name__ == 'xml.dom')

    def testDottedNameImportFrom(self):
        """Test dotted-name 'import from'."""
        from System.Collections import Specialized
        self.assertTrue(self.isCLRModule(Specialized))
        self.assertTrue(
            Specialized.__name__ == 'System.Collections.Specialized'
            )

        from System.Collections.Specialized import StringCollection
        self.assertTrue(self.isCLRClass(StringCollection))
        self.assertTrue(StringCollection.__name__ == 'StringCollection')

        from xml.dom import pulldom
        self.assertTrue(type(pulldom) == types.ModuleType)
        self.assertTrue(pulldom.__name__ == 'xml.dom.pulldom')

        from xml.dom.pulldom import PullDOM
        self.assertTrue(type(PullDOM) == types.ClassType)
        self.assertTrue(PullDOM.__name__ == 'PullDOM')

    def testDottedNameImportFromWithAlias(self):
        """Test dotted-name 'import from' with aliasing."""
        from System.Collections import Specialized as Spec
        self.assertTrue(self.isCLRModule(Spec))
        self.assertTrue(Spec.__name__ == 'System.Collections.Specialized')

        from System.Collections.Specialized import StringCollection as SC
        self.assertTrue(self.isCLRClass(SC))
        self.assertTrue(SC.__name__ == 'StringCollection')

        from xml.dom import pulldom as myPulldom
        self.assertTrue(type(myPulldom) == types.ModuleType)
        self.assertTrue(myPulldom.__name__ == 'xml.dom.pulldom')

        from xml.dom.pulldom import PullDOM as myPullDOM
        self.assertTrue(type(myPullDOM) == types.ClassType)
        self.assertTrue(myPullDOM.__name__ == 'PullDOM')

    def testFromModuleImportStar(self):
        """Test from module import * behavior."""
        count = len(locals().keys())
        m = __import__('System.Xml', globals(), locals(), ['*'])
        self.assertTrue(m.__name__ == 'System.Xml')
        self.assertTrue(self.isCLRModule(m))
        self.assertTrue(len(locals().keys()) > count + 1)

    def testImplicitAssemblyLoad(self):
        """Test implicit assembly loading via import."""

        def test():
            # This should fail until System.Windows.Forms has been
            # imported or that assembly has been explicitly loaded.
            import System.Windows

        # The test fails when the project is compiled with MS VS 2005. Dunno why :(
        self.assertRaises(ImportError, test)

        clr.AddReference("System.Windows.Forms")
        import System.Windows.Forms as Forms
        self.assertTrue(self.isCLRModule(Forms))
        self.assertTrue(Forms.__name__ == 'System.Windows.Forms')
        from System.Windows.Forms import Form
        self.assertTrue(self.isCLRClass(Form))
        self.assertTrue(Form.__name__ == 'Form')

    def testExplicitAssemblyLoad(self):
        """Test explicit assembly loading using standard CLR tools."""
        from System.Reflection import Assembly
        import System, sys

        assembly = Assembly.LoadWithPartialName('System.Data')
        self.assertTrue(assembly != None)

        import System.Data
        self.assertTrue(sys.modules.has_key('System.Data'))

        assembly = Assembly.LoadWithPartialName('SpamSpamSpamSpamEggsAndSpam')
        self.assertTrue(assembly == None)

    def testImplicitLoadAlreadyValidNamespace(self):
        """Test implicit assembly load over an already valid namespace."""
        # In this case, the mscorlib assembly (loaded by default) defines
        # a number of types in the System namespace. There is also a System
        # assembly, which is _not_ loaded by default, which also contains
        # types in the System namespace. The desired behavior is for the
        # Python runtime to "do the right thing", allowing types from both
        # assemblies to be found in the System module implicitly.
        import System
        self.assertTrue(self.isCLRClass(System.UriBuilder))

    def testImportNonExistantModule(self):
        """Test import failure for a non-existant module."""
        def test():
            import System.SpamSpamSpam

        self.assertTrue(ImportError, test)

    def testLookupNoNamespaceType(self):
        """Test lookup of types without a qualified namespace."""
        import Python.Test
        import clr
        self.assertTrue(self.isCLRClass(clr.NoNamespaceType))

    def testModuleLookupRecursion(self):
        """Test for recursive lookup handling."""
        def test1():
            from System import System

        self.assertTrue(ImportError, test1)

        def test2():
            import System
            x = System.System

        self.assertTrue(AttributeError, test2)

    def testModuleGetAttr(self):
        """Test module getattr behavior."""
        import System

        int_type = System.Int32
        self.assertTrue(self.isCLRClass(int_type))

        module = System.Xml
        self.assertTrue(self.isCLRModule(module))

        def test():
            spam = System.Spam

        self.assertTrue(AttributeError, test)

        def test():
            spam = getattr(System, 1)

        self.assertTrue(TypeError, test)

    def testModuleAttrAbuse(self):
        """Test handling of attempts to set module attributes."""

        # It would be safer to use a dict-proxy as the __dict__ for CLR
        # modules, but as of Python 2.3 some parts of the CPython runtime
        # like dir() will fail if a module dict is not a real dictionary.

        def test():
            import System
            System.__dict__['foo'] = 0
            return 1

        self.assertTrue(test())

    def testModuleTypeAbuse(self):
        """Test handling of attempts to break the module type."""
        import System
        mtype = type(System)

        def test():
            mtype.__getattribute__(0, 'spam')

        self.assertTrue(TypeError, test)

        def test():
            mtype.__setattr__(0, 'spam', 1)

        self.assertTrue(TypeError, test)

        def test():
            mtype.__repr__(0)

        self.assertTrue(TypeError, test)

    def test_ClrListAssemblies(self):
        from clr import ListAssemblies
        verbose = list(ListAssemblies(True))
        short = list(ListAssemblies(False))
        self.assertTrue(u'mscorlib' in short)
        self.assertTrue(u'System' in short)
        self.assertTrue('Culture=' in verbose[0])
        self.assertTrue('Version=' in verbose[0])

    def test_ClrAddReference(self):
        from clr import AddReference
        from System.IO import FileNotFoundException
        for name in ("System", "Python.Runtime"):
            mod = AddReference(name)
            self.assertEqual(mod.__name__, name)

        self.assertRaises(FileNotFoundException,
            AddReference, "somethingtotallysilly")

def test_suite():
    return unittest.makeSuite(ModuleTests)

def main():
    unittest.TextTestRunner().run(test_suite())

if __name__ == '__main__':
    main()

<commit_msg>test_module.test_ClrAddReference() now expects an assembly instead of a module (like my branch does).<commit_after># ===========================================================================
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
# ===========================================================================

import clr
clr.AddReference('Python.Test')
clr.AddReference('System.Data')

# testImplicitAssemblyLoad() passes on deprecation warning; perfect! #
##clr.AddReference('System.Windows.Forms')
import sys, os, string, unittest, types

class ModuleTests(unittest.TestCase):
    """Test CLR modules and the CLR import hook."""

    def isCLRModule(self, object):
        return type(object).__name__ == 'ModuleObject'

    def isCLRRootModule(self, object):
        return type(object).__name__ == 'CLRModule'

    def isCLRClass(self, object):
        return type(object).__name__ == 'CLR Metatype' # for now

    def testAAAImportHookWorks(self):
        """Test that the import hook works correctly both using the
           included runtime and an external runtime. This must be
           the first test run in the unit tests!"""

        from System import String

    def test000importClr(self):
        import clr
        self.assertTrue(self.isCLRRootModule(clr))

    def testPreloadVar(self):
        import clr
        self.assertTrue(clr.getPreload() is False, clr.getPreload())
    	clr.setPreload(False)
    	self.assertTrue(clr.getPreload() is False, clr.getPreload())
        try:
            clr.setPreload(True)
            self.assertTrue(clr.getPreload() is True, clr.getPreload())
            clr.setPreload(0)
            self.assertTrue(clr.getPreload() is False, clr.getPreload())
            clr.setPreload(1)
            self.assertTrue(clr.getPreload() is True, clr.getPreload())

            import System.Configuration
            content = dir(System.Configuration)
            self.assertTrue(len(content) > 10, content)
        finally:
            clr.setPreload(False)

    def testModuleInterface(self):
        """Test the interface exposed by CLR module objects."""
        import System
        self.assertEquals(type(System.__dict__), type({}))
        self.assertEquals(System.__name__, 'System')
        self.assertEquals(System.__file__, None)
        self.assertEquals(System.__doc__, None)
        self.assertTrue(self.isCLRClass(System.String))
        self.assertTrue(self.isCLRClass(System.Int32))

    def testSimpleImport(self):
        """Test simple import."""
        import System
        self.assertTrue(self.isCLRModule(System))
        self.assertTrue(System.__name__ == 'System')

        import sys
        self.assertTrue(type(sys) == types.ModuleType)
        self.assertTrue(sys.__name__ == 'sys')

        import httplib
        self.assertTrue(type(httplib) == types.ModuleType)
        self.assertTrue(httplib.__name__ == 'httplib')

    def testSimpleImportWithAlias(self):
        """Test simple import with aliasing."""
        import System as mySystem
        self.assertTrue(self.isCLRModule(mySystem))
        self.assertTrue(mySystem.__name__ == 'System')

        import sys as mySys
        self.assertTrue(type(mySys) == types.ModuleType)
        self.assertTrue(mySys.__name__ == 'sys')

        import httplib as myHttplib
        self.assertTrue(type(myHttplib) == types.ModuleType)
        self.assertTrue(myHttplib.__name__ == 'httplib')

    def testDottedNameImport(self):
        """Test dotted-name import."""
        import System.Reflection
        self.assertTrue(self.isCLRModule(System.Reflection))
        self.assertTrue(System.Reflection.__name__ == 'System.Reflection')

        import xml.dom
        self.assertTrue(type(xml.dom) == types.ModuleType)
        self.assertTrue(xml.dom.__name__ == 'xml.dom')

    def testMultipleDottedNameImport(self):
        """Test an import bug with multiple dotted imports."""
        import System.Data
        self.assertTrue(self.isCLRModule(System.Data))
        self.assertTrue(System.Data.__name__ == 'System.Data')
        import System.Data
        self.assertTrue(self.isCLRModule(System.Data))
        self.assertTrue(System.Data.__name__ == 'System.Data')

    def testDottedNameImportWithAlias(self):
        """Test dotted-name import with aliasing."""
        import System.Reflection as SysRef
        self.assertTrue(self.isCLRModule(SysRef))
        self.assertTrue(SysRef.__name__ == 'System.Reflection')

        import xml.dom as myDom
        self.assertTrue(type(myDom) == types.ModuleType)
        self.assertTrue(myDom.__name__ == 'xml.dom')

    def testSimpleImportFrom(self):
        """Test simple 'import from'."""
        from System import Reflection
        self.assertTrue(self.isCLRModule(Reflection))
        self.assertTrue(Reflection.__name__ == 'System.Reflection')

        from xml import dom
        self.assertTrue(type(dom) == types.ModuleType)
        self.assertTrue(dom.__name__ == 'xml.dom')

    def testSimpleImportFromWithAlias(self):
        """Test simple 'import from' with aliasing."""
        from System import Collections as Coll
        self.assertTrue(self.isCLRModule(Coll))
        self.assertTrue(Coll.__name__ == 'System.Collections')

        from xml import dom as myDom
        self.assertTrue(type(myDom) == types.ModuleType)
        self.assertTrue(myDom.__name__ == 'xml.dom')

    def testDottedNameImportFrom(self):
        """Test dotted-name 'import from'."""
        from System.Collections import Specialized
        self.assertTrue(self.isCLRModule(Specialized))
        self.assertTrue(
            Specialized.__name__ == 'System.Collections.Specialized'
            )

        from System.Collections.Specialized import StringCollection
        self.assertTrue(self.isCLRClass(StringCollection))
        self.assertTrue(StringCollection.__name__ == 'StringCollection')

        from xml.dom import pulldom
        self.assertTrue(type(pulldom) == types.ModuleType)
        self.assertTrue(pulldom.__name__ == 'xml.dom.pulldom')

        from xml.dom.pulldom import PullDOM
        self.assertTrue(type(PullDOM) == types.ClassType)
        self.assertTrue(PullDOM.__name__ == 'PullDOM')

    def testDottedNameImportFromWithAlias(self):
        """Test dotted-name 'import from' with aliasing."""
        from System.Collections import Specialized as Spec
        self.assertTrue(self.isCLRModule(Spec))
        self.assertTrue(Spec.__name__ == 'System.Collections.Specialized')

        from System.Collections.Specialized import StringCollection as SC
        self.assertTrue(self.isCLRClass(SC))
        self.assertTrue(SC.__name__ == 'StringCollection')

        from xml.dom import pulldom as myPulldom
        self.assertTrue(type(myPulldom) == types.ModuleType)
        self.assertTrue(myPulldom.__name__ == 'xml.dom.pulldom')

        from xml.dom.pulldom import PullDOM as myPullDOM
        self.assertTrue(type(myPullDOM) == types.ClassType)
        self.assertTrue(myPullDOM.__name__ == 'PullDOM')

    def testFromModuleImportStar(self):
        """Test from module import * behavior."""
        count = len(locals().keys())
        m = __import__('System.Xml', globals(), locals(), ['*'])
        self.assertTrue(m.__name__ == 'System.Xml')
        self.assertTrue(self.isCLRModule(m))
        self.assertTrue(len(locals().keys()) > count + 1)

    def testImplicitAssemblyLoad(self):
        """Test implicit assembly loading via import."""

        def test():
            # This should fail until System.Windows.Forms has been
            # imported or that assembly has been explicitly loaded.
            import System.Windows

        # The test fails when the project is compiled with MS VS 2005. Dunno why :(
        self.assertRaises(ImportError, test)

        clr.AddReference("System.Windows.Forms")
        import System.Windows.Forms as Forms
        self.assertTrue(self.isCLRModule(Forms))
        self.assertTrue(Forms.__name__ == 'System.Windows.Forms')
        from System.Windows.Forms import Form
        self.assertTrue(self.isCLRClass(Form))
        self.assertTrue(Form.__name__ == 'Form')

    def testExplicitAssemblyLoad(self):
        """Test explicit assembly loading using standard CLR tools."""
        from System.Reflection import Assembly
        import System, sys

        assembly = Assembly.LoadWithPartialName('System.Data')
        self.assertTrue(assembly != None)

        import System.Data
        self.assertTrue(sys.modules.has_key('System.Data'))

        assembly = Assembly.LoadWithPartialName('SpamSpamSpamSpamEggsAndSpam')
        self.assertTrue(assembly == None)

    def testImplicitLoadAlreadyValidNamespace(self):
        """Test implicit assembly load over an already valid namespace."""
        # In this case, the mscorlib assembly (loaded by default) defines
        # a number of types in the System namespace. There is also a System
        # assembly, which is _not_ loaded by default, which also contains
        # types in the System namespace. The desired behavior is for the
        # Python runtime to "do the right thing", allowing types from both
        # assemblies to be found in the System module implicitly.
        import System
        self.assertTrue(self.isCLRClass(System.UriBuilder))

    def testImportNonExistantModule(self):
        """Test import failure for a non-existant module."""
        def test():
            import System.SpamSpamSpam

        self.assertTrue(ImportError, test)

    def testLookupNoNamespaceType(self):
        """Test lookup of types without a qualified namespace."""
        import Python.Test
        import clr
        self.assertTrue(self.isCLRClass(clr.NoNamespaceType))

    def testModuleLookupRecursion(self):
        """Test for recursive lookup handling."""
        def test1():
            from System import System

        self.assertTrue(ImportError, test1)

        def test2():
            import System
            x = System.System

        self.assertTrue(AttributeError, test2)

    def testModuleGetAttr(self):
        """Test module getattr behavior."""
        import System

        int_type = System.Int32
        self.assertTrue(self.isCLRClass(int_type))

        module = System.Xml
        self.assertTrue(self.isCLRModule(module))

        def test():
            spam = System.Spam

        self.assertTrue(AttributeError, test)

        def test():
            spam = getattr(System, 1)

        self.assertTrue(TypeError, test)

    def testModuleAttrAbuse(self):
        """Test handling of attempts to set module attributes."""

        # It would be safer to use a dict-proxy as the __dict__ for CLR
        # modules, but as of Python 2.3 some parts of the CPython runtime
        # like dir() will fail if a module dict is not a real dictionary.

        def test():
            import System
            System.__dict__['foo'] = 0
            return 1

        self.assertTrue(test())

    def testModuleTypeAbuse(self):
        """Test handling of attempts to break the module type."""
        import System
        mtype = type(System)

        def test():
            mtype.__getattribute__(0, 'spam')

        self.assertTrue(TypeError, test)

        def test():
            mtype.__setattr__(0, 'spam', 1)

        self.assertTrue(TypeError, test)

        def test():
            mtype.__repr__(0)

        self.assertTrue(TypeError, test)

    def test_ClrListAssemblies(self):
        from clr import ListAssemblies
        verbose = list(ListAssemblies(True))
        short = list(ListAssemblies(False))
        self.assertTrue(u'mscorlib' in short)
        self.assertTrue(u'System' in short)
        self.assertTrue('Culture=' in verbose[0])
        self.assertTrue('Version=' in verbose[0])

    def test_ClrAddReference(self):
        from clr import AddReference
        from System.IO import FileNotFoundException
        for name in ("System", "Python.Runtime"):
            assy = AddReference(name)
            assyName = assy.GetName().Name
            self.assertEqual(assyName, name)

        self.assertRaises(FileNotFoundException,
            AddReference, "somethingtotallysilly")

def test_suite():
    return unittest.makeSuite(ModuleTests)

def main():
    unittest.TextTestRunner().run(test_suite())

if __name__ == '__main__':
    main()

<|endoftext|>


<commit_before>from rest_framework import viewsets
from rest_framework.exceptions import AuthenticationFailed
from django.utils.translation import ugettext_lazy as _

from services.api import ServiceSerializer, UnitSerializer
from .serializers import ObservationSerializer, ObservablePropertySerializer
from . import models

from services.api import (
    JSONAPIViewSetMixin, ServiceViewSet, UnitViewSet)

class ObservationViewSet(JSONAPIViewSetMixin, viewsets.ModelViewSet):
    queryset = models.Observation.objects.all()
    serializer_class = ObservationSerializer

    def create(self, request, *args, **kwargs):
        if request.auth is None:
            raise AuthenticationFailed(_('Authentication required.'))

        return super(ObservationViewSet, self).create(request, *args, **kwargs)

    def get_serializer_context(self):
        return {'user': self.request.user, 'auth': self.request.auth}

class ObservableSerializerMixin:
    def to_representation(self, obj):
        data = super(ObservableSerializerMixin, self).to_representation(obj)
        if 'observable_properties' in self.context.get('include', []):
            data['observable_properties'] = ObservablePropertySerializer(
                self.get_observable_properties(obj), many=True).data
        if 'observations' in self.context.get('include', []):
            observations = self.get_observations(obj)
            if observations:
                data['observations'] = ObservationSerializer(
                    observations, many=True).data

        return data

class ObservableServiceSerializer(ObservableSerializerMixin, ServiceSerializer):
    def get_observable_properties(self, service):
        return service.observable_properties.all()

    def get_observations(self, service):
        return None

class ObservableUnitSerializer(ObservableSerializerMixin, UnitSerializer):
    def get_observable_properties(self, unit):
        return models.ObservableProperty.objects.filter(services__in=unit.services.all())

    def get_observations(self, unit):
        return unit.observation_set

UnitViewSet.serializer_class = ObservableUnitSerializer

ServiceViewSet.serializer_class = ObservableServiceSerializer

views = [
    {'class': ObservationViewSet, 'name': 'observation'}
]
<commit_msg>Fix html browsable API rendering for observations endpoint (#94)<commit_after>from rest_framework import viewsets
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from django.utils.translation import ugettext_lazy as _

from services.api import ServiceSerializer, UnitSerializer
from .serializers import ObservationSerializer, ObservablePropertySerializer
from . import models

from services.api import (
    JSONAPIViewSetMixin, ServiceViewSet, UnitViewSet)

class ObservationViewSet(JSONAPIViewSetMixin, viewsets.ModelViewSet):
    queryset = models.Observation.objects.all()
    serializer_class = ObservationSerializer
    permission_classes = [IsAuthenticatedOrReadOnly]

    def create(self, request, *args, **kwargs):
        if request.auth is None:
            raise AuthenticationFailed(_('Authentication required.'))

        return super(ObservationViewSet, self).create(request, *args, **kwargs)

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context.update({'user': self.request.user, 'auth': self.request.auth})
        return context

class ObservableSerializerMixin:
    def to_representation(self, obj):
        data = super(ObservableSerializerMixin, self).to_representation(obj)
        if 'observable_properties' in self.context.get('include', []):
            data['observable_properties'] = ObservablePropertySerializer(
                self.get_observable_properties(obj), many=True).data
        if 'observations' in self.context.get('include', []):
            observations = self.get_observations(obj)
            if observations:
                data['observations'] = ObservationSerializer(
                    observations, many=True).data

        return data

class ObservableServiceSerializer(ObservableSerializerMixin, ServiceSerializer):
    def get_observable_properties(self, service):
        return service.observable_properties.all()

    def get_observations(self, service):
        return None

class ObservableUnitSerializer(ObservableSerializerMixin, UnitSerializer):
    def get_observable_properties(self, unit):
        return models.ObservableProperty.objects.filter(services__in=unit.services.all())

    def get_observations(self, unit):
        return unit.observation_set

UnitViewSet.serializer_class = ObservableUnitSerializer

ServiceViewSet.serializer_class = ObservableServiceSerializer

views = [
    {'class': ObservationViewSet, 'name': 'observation'}
]
<|endoftext|>


<commit_before># -*- coding: utf-8  -*-

__version__ = '$Id$'

import family

# The Wikia Search family
# user-config.py: usernames['wikia']['wikia'] = 'User name'

class Family(family.Family):
    def __init__(self):
        family.Family.__init__(self)
        self.name = u'wikia'

        self.langs = {
            u'wikia': None,
        }

        self.namespaces[4] = {
            '_default': [u'search', self.namespaces[4]['_default']],
        }
        self.namespaces[5] = {
            '_default': [u'search talk', self.namespaces[5]['_default']],
        }
        self.namespaces[100] = {
            '_default': u'Forum',
        }
        self.namespaces[101] = {
            '_default': u'Forum talk',
        }
        self.namespaces[112] = {
            '_default': u'Mini',
        }
        self.namespaces[113] = {
            '_default': u'Mini talk',
        }

    def hostname(self, code):
        return u'www.wikia.com'

    def version(self, code):
        return "1.15.1"

    def scriptpath(self, code):
        return ''

    def apipath(self, code):
        return '/api.php'
<commit_msg>remove obsolete namespace dict.<commit_after># -*- coding: utf-8  -*-

__version__ = '$Id$'

import family

# The Wikia Search family
# user-config.py: usernames['wikia']['wikia'] = 'User name'

class Family(family.Family):
    def __init__(self):
        family.Family.__init__(self)
        self.name = u'wikia'

        self.langs = {
            u'wikia': None,
        }

    def hostname(self, code):
        return u'www.wikia.com'

    def version(self, code):
        return "1.15.1"

    def scriptpath(self, code):
        return ''

    def apipath(self, code):
        return '/api.php'
<|endoftext|>


<commit_before>print "Hello GIT!"
print "What is 2+2", 2+2
print "We are learning about GIT"
print "Christmas is in", 25-10 "days"<commit_msg>added hello world!<commit_after>print "Hello GIT!"
print "What is 2+2", 2+2
print "We are learning about GIT"
print "Christmas is in", 25-10 "days"
print "hello world"
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# deco.py
#
# purpose:
# author:   Filipe P. A. Fernandes
# e-mail:   ocefpaf@gmail
# web:      http://ocefpaf.tiddlyspot.com/
# created:  22-Jun-2011
# modified: Thu 13 Oct 2011 02:44:02 PM EDT
#
# obs:
#

import numpy as np

class match_args_return(object):
    """
    Function decorator to homogenize input arguments and to
    make the output match the original input with respect to
    scalar versus array, and masked versus ndarray.
    """
    def __init__(self, func):
        self.func = func
        self.__doc__ = func.__doc__
        self.__name__ = func.__name__

    def __call__(self, *args, **kw):
        # get pressure from keywords
        # and throw it in the list of args
        p = kw.get('p', None)
        if p is not None:
            args = list(args)
            args.append(p)

        # check if is array
        self.array = np.any([hasattr(a, '__iter__') for a in args])
        # check if is masked
        self.masked = np.any([np.ma.isMaskedArray(a) for a in args])
        newargs = [np.ma.atleast_1d(np.ma.masked_invalid(a)) for a in args]
        newargs = [a.astype(np.float) for a in newargs]
        # return p to keywords
        if p is not None:
            kw['p'] = newargs.pop()
        ret = self.func(*newargs, **kw)
        if not self.masked:  # Return a filled array if not masked.
            ret = np.ma.filled(ret, np.nan)
        if not self.array:  # Return scalar if not array.
            ret = ret[0]

        return ret
<commit_msg>Some clean-ups, still need to add at_least1d and make sure lists becomes arrays.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# deco.py
#
# purpose:
# author:   Filipe P. A. Fernandes
# e-mail:   ocefpaf@gmail
# web:      http://ocefpaf.tiddlyspot.com/
# created:  22-Jun-2011
# modified: Thu 13 Oct 2011 02:44:02 PM EDT
#
# obs:
#

import numpy as np

class match_args_return(object):
    """
    Function decorator to homogenize input arguments and to
    make the output match the original input with respect to
    scalar versus array, and masked versus ndarray.
    """
    def __init__(self, func):
        self.func = func
        self.__doc__ = func.__doc__
        self.__name__ = func.__name__

    def __call__(self, *args, **kw):
        # Check if is array
        self.array = np.any([hasattr(a, '__iter__') for a in args])

        # Check if is masked
        self.masked = np.any([np.ma.isMaskedArray(a) for a in args])
        newargs = [np.ma.atleast_1d(np.ma.masked_invalid(a)) for a in args]
        newargs = [a.astype(np.float) for a in newargs]
        ret = self.func(*newargs, **kw)
        if not self.masked:  # Return a filled array if not masked.
            ret = np.ma.filled(ret, np.nan)
        if not self.array:  # Return scalar if not array.
            ret = ret[0]

        return ret
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

import json
import logging
import re
import sys
import requests
try:
    from __builtin__ import unicode
except ImportError:
    unicode = lambda x, *args: x

from scrapper_helpers.utils import caching, normalize_text, key_sha1

from otodom import BASE_URL

if sys.version_info < (3, 2):
    from urllib import quote
else:
    from urllib.parse import quote

REGION_DATA_KEYS = ["city", "voivodeship", "[district_id]", "[street_id]"]

log = logging.getLogger(__file__)

def get_region_from_autosuggest(region_part):
    """
    This method makes a request to the OtoDom api, asking for the best fitting region for the supplied
    region_part string.

    :param region_part: input string, it should be a part of an existing region in Poland, either city, street,
                        district or voivodeship
    :rtype: dict
    :return: A dictionary which contents depend on the API response.
    """
    if not region_part:
        return {}
    url = u"https://www.otodom.pl/ajax/geo6/autosuggest/?data={0}".format(
        normalize_text(region_part, lower=False, replace_spaces=''))
    response = json.loads(get_response_for_url(url).text)[0]
    region_type = response["level"]
    text = response["text"].replace("", "").replace("", "").split(", ")

    region_dict = {}

    if region_type == "CITY":
        region_dict["city"] = u"{0}{1}{2}".format(normalize_text(text[0]), "_", response["city_id"])
    elif region_type == "DISTRICT":
        region_dict["city"] = u"{0}{1}{2}".format(normalize_text(text[1]), "_", response["city_id"])
        region_dict["[district_id]"] = response["district_id"]
    elif region_type == "REGION":
        region_dict["voivodeship"] = normalize_text(text[0])
    elif region_type == "STREET":
        region_dict["city"] = u"{0}{1}{2}".format(normalize_text(text[0]), "_", response["city_id"])
        region_dict["[street_id]"] = response["street_id"]

    return region_dict

def get_region_from_filters(filters):
    """
    This method does a similiar thing as :meth:`scrape.utils.get_region_from_autosuggest` but instead of calling the
    API, it uses the data provided in the filters

    :param filters: dict, see :meth:`scrape.category.get_category` for reference
    :rtype: dict
    :return: A dictionary which contents depend on the filters content.
    """
    region_dict = {
        region_data: filters.get(region_data)
        for region_data in REGION_DATA_KEYS
        if region_data in filters
    }
    return region_dict

def _float(number):
    return get_number_from_string(number, float)

def _int(number):
    return get_number_from_string(number, int)

def get_number_from_string(s, number_type):
    try:
        return number_type(s.replace(",", "."))
    except ValueError:
        return s if s else None

def get_url(main_category, detail_category, region, ads_per_page="", page=None, **filters):
    """
    This method builds a ready-to-use url based on the input parameters.

    :param main_category: see :meth:`scrape.category.get_category` for reference
    :param detail_category: see :meth:`scrape.category.get_category` for reference
    :param region: see :meth:`scrape.category.get_category` for reference
    :param ads_per_page: "?nrAdsPerPage=72" can be used to lower the amount of requests
    :param page: page number
    :param filters: see :meth:`scrape.category.get_category` for reference
    :rtype: string
    :return: the url
    """
    page = "page={0}".format(page) if page is not None else ""

    # skip using autosuggest if any region data present in the filters
    if any([region_key in filters for region_key in REGION_DATA_KEYS]):
        region_data = get_region_from_filters(filters)
    else:
        region_data = get_region_from_autosuggest(region)

    city_or_voivodeship = region_data["city"] if "city" in region_data else region_data[
        "voivodeship"] if "voivodeship" in region_data else ""

    if "[district_id]" in region_data:
        filters["[district_id]"] = region_data["[district_id]"]

    if "[street_id]" in region_data:
        filters["[street_id]"] = region_data["[street_id]"]

    # creating base url
    url = "/".join([BASE_URL, main_category, detail_category, city_or_voivodeship])

    # adding building type if exists in filters
    if "building_type" in filters:
        url = url + "/" + filters["building_type"]

    # adding description fragment search if exists in filters
    if "description_fragment" in filters:
        url = url + "/q-" + "-".join(filters["description_fragment"].split())

    # preparing the rest of filters for addition to the url
    filter_list = []
    for key, value in filters.items():
        if isinstance(value, list):
            for item in value:
                filter_list.append("search{}={}".format(quote(key), item))
        else:
            filter_list.append("search{}={}".format(quote(key), value))
    url = url + "&".join([ads_per_page, page] + filter_list)
    log.info(url)
    return url

@caching(key_func=key_sha1)
def get_response_for_url(url):
    """
    :param url: an url, most likely from the :meth:`scrape.utils.get_url` method
    :return: a requests.response object
    """
    return requests.get(url)

def get_cookie_from(response):
    """
    :param response: a requests.response object
    :rtype: string
    :return: cookie information as string
    """
    cookie = response.headers['Set-Cookie'].split(';')[0]
    return cookie

def get_csrf_token(html_content):
    """
    :param html_content: a requests.response.content object
    :rtype: string
    :return: the CSRF token as string
    """
    found = re.match(r".*csrfToken\s+=(\\|\s)+'(?P<csrf_token>\w+).*", str(html_content))
    csrf_token = found.groupdict().get('csrf_token')
    return csrf_token
<commit_msg>Refactoring<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

import json
import logging
import re
import sys
import requests
try:
    from __builtin__ import unicode
except ImportError:
    unicode = lambda x, *args: x

from scrapper_helpers.utils import caching, normalize_text, key_sha1

from otodom import BASE_URL

if sys.version_info < (3, 2):
    from urllib import quote
else:
    from urllib.parse import quote

REGION_DATA_KEYS = ["city", "voivodeship", "[district_id]", "[street_id]"]

log = logging.getLogger(__file__)

def get_region_from_autosuggest(region_part):
    """
    This method makes a request to the OtoDom api, asking for the best fitting region for the supplied
    region_part string.

    :param region_part: input string, it should be a part of an existing region in Poland, either city, street,
                        district or voivodeship
    :rtype: dict
    :return: A dictionary which contents depend on the API response.
    """
    if not region_part:
        return {}
    url = u"https://www.otodom.pl/ajax/geo6/autosuggest/?data={0}".format(
        normalize_text(region_part, lower=False, replace_spaces=''))
    response = json.loads(get_response_for_url(url).text)[0]
    region_type = response["level"]
    text = response["text"].replace("", "").replace("", "").split(", ")

    region_dict = {}

    if region_type == "CITY":
        region_dict["city"] = u"{0}{1}{2}".format(normalize_text(text[0]), "_", response["city_id"])
    elif region_type == "DISTRICT":
        region_dict["city"] = u"{0}{1}{2}".format(normalize_text(text[1]), "_", response["city_id"])
        region_dict["[district_id]"] = response["district_id"]
    elif region_type == "REGION":
        region_dict["voivodeship"] = normalize_text(text[0])
    elif region_type == "STREET":
        region_dict["city"] = u"{0}{1}{2}".format(normalize_text(text[0]), "_", response["city_id"])
        region_dict["[street_id]"] = response["street_id"]

    return region_dict

def get_region_from_filters(filters):
    """
    This method does a similiar thing as :meth:`scrape.utils.get_region_from_autosuggest` but instead of calling the
    API, it uses the data provided in the filters

    :param filters: dict, see :meth:`scrape.category.get_category` for reference
    :rtype: dict
    :return: A dictionary which contents depend on the filters content.
    """
    region_dict = {
        region_data: filters.get(region_data)
        for region_data in REGION_DATA_KEYS
        if region_data in filters
    }
    return region_dict

def _float(number, default=None):
    return get_number_from_string(number, float, default)

def _int(number, default=None):
    return get_number_from_string(number, int, default)

def get_number_from_string(s, number_type, default):
    try:
        return number_type(s.replace(",", "."))
    except ValueError:
        return default

def get_url(main_category, detail_category, region, ads_per_page="", page=None, **filters):
    """
    This method builds a ready-to-use url based on the input parameters.

    :param main_category: see :meth:`scrape.category.get_category` for reference
    :param detail_category: see :meth:`scrape.category.get_category` for reference
    :param region: see :meth:`scrape.category.get_category` for reference
    :param ads_per_page: "?nrAdsPerPage=72" can be used to lower the amount of requests
    :param page: page number
    :param filters: see :meth:`scrape.category.get_category` for reference
    :rtype: string
    :return: the url
    """
    page = "page={0}".format(page) if page is not None else ""

    # skip using autosuggest if any region data present in the filters
    if any([region_key in filters for region_key in REGION_DATA_KEYS]):
        region_data = get_region_from_filters(filters)
    else:
        region_data = get_region_from_autosuggest(region)

    city_or_voivodeship = region_data["city"] if "city" in region_data else region_data[
        "voivodeship"] if "voivodeship" in region_data else ""

    if "[district_id]" in region_data:
        filters["[district_id]"] = region_data["[district_id]"]

    if "[street_id]" in region_data:
        filters["[street_id]"] = region_data["[street_id]"]

    # creating base url
    url = "/".join([BASE_URL, main_category, detail_category, city_or_voivodeship])

    # adding building type if exists in filters
    if "building_type" in filters:
        url = url + "/" + filters["building_type"]

    # adding description fragment search if exists in filters
    if "description_fragment" in filters:
        url = url + "/q-" + "-".join(filters["description_fragment"].split())

    # preparing the rest of filters for addition to the url
    filter_list = []
    for key, value in filters.items():
        if isinstance(value, list):
            for item in value:
                filter_list.append("search{}={}".format(quote(key), item))
        else:
            filter_list.append("search{}={}".format(quote(key), value))
    url = url + "&".join([ads_per_page, page] + filter_list)
    log.info(url)
    return url

@caching(key_func=key_sha1)
def get_response_for_url(url):
    """
    :param url: an url, most likely from the :meth:`scrape.utils.get_url` method
    :return: a requests.response object
    """
    return requests.get(url)

def get_cookie_from(response):
    """
    :param response: a requests.response object
    :rtype: string
    :return: cookie information as string
    """
    cookie = response.headers['Set-Cookie'].split(';')[0]
    return cookie

def get_csrf_token(html_content):
    """
    :param html_content: a requests.response.content object
    :rtype: string
    :return: the CSRF token as string
    """
    found = re.match(r".*csrfToken\s+=(\\|\s)+'(?P<csrf_token>\w+).*", str(html_content))
    csrf_token = found.groupdict().get('csrf_token')
    return csrf_token
<|endoftext|>


<commit_before>import urllib

url = "http://ernestmarples.com/"

def find(postcode):
    resource = "%s?%s" % (url, urllib.urlencode([('p',postcode), ('f','csv')]))
    resource = urllib.urlopen(resource)
    if resource.info()['Content-Type'] != "text/csv":
        raise Exception()

    p, lat, long = resource.read().split(',')
    return float(lat), float(long)

<commit_msg>made the returing a bit more funky<commit_after>import urllib

url = "http://ernestmarples.com/"

def find(postcode):
    resource = "%s?%s" % (url, urllib.urlencode([('p',postcode), ('f','csv')]))
    resource = urllib.urlopen(resource)
    if resource.info()['Content-Type'] != "text/csv":
        raise Exception()

    return [float(x) for x in resource.read().split(',')[1:]]

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright 2014, 2015 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from twisted.internet import defer

from synapse.api.errors import StoreError

from ._base import SQLBaseStore

import collections
import logging

logger = logging.getLogger(__name__)

OpsLevel = collections.namedtuple(
    "OpsLevel",
    ("ban_level", "kick_level", "redact_level",)
)

class RoomStore(SQLBaseStore):

    @defer.inlineCallbacks
    def store_room(self, room_id, room_creator_user_id, is_public):
        """Stores a room.

        Args:
            room_id (str): The desired room ID, can be None.
            room_creator_user_id (str): The user ID of the room creator.
            is_public (bool): True to indicate that this room should appear in
            public room lists.
        Raises:
            StoreError if the room could not be stored.
        """
        try:
            yield self._simple_insert(
                RoomsTable.table_name,
                {
                    "room_id": room_id,
                    "creator": room_creator_user_id,
                    "is_public": is_public,
                },
                desc="store_room",
            )
        except Exception as e:
            logger.error("store_room with room_id=%s failed: %s", room_id, e)
            raise StoreError(500, "Problem creating room.")

    def get_room(self, room_id):
        """Retrieve a room.

        Args:
            room_id (str): The ID of the room to retrieve.
        Returns:
            A namedtuple containing the room information, or an empty list.
        """
        return self._simple_select_one(
            table=RoomsTable.table_name,
            keyvalues={"room_id": room_id},
            retcols=RoomsTable.fields,
            desc="get_room",
            allow_none=True,
        )

    def get_public_room_ids(self):
        return self._simple_select_onecol(
            table="rooms",
            keyvalues={
                "is_public": True,
            },
            retcol="room_id",
            desc="get_public_room_ids",
        )

    @defer.inlineCallbacks
    def get_rooms(self, is_public):
        """Retrieve a list of all public rooms.

        Args:
            is_public (bool): True if the rooms returned should be public.
        Returns:
            A list of room dicts containing at least a "room_id" key, a
            "topic" key if one is set, and a "name" key if one is set
        """

        def f(txn):
            topic_subquery = (
                "SELECT topics.event_id as event_id, "
                "topics.room_id as room_id, topic "
                "FROM topics "
                "INNER JOIN current_state_events as c "
                "ON c.event_id = topics.event_id "
            )

            name_subquery = (
                "SELECT room_names.event_id as event_id, "
                "room_names.room_id as room_id, name "
                "FROM room_names "
                "INNER JOIN current_state_events as c "
                "ON c.event_id = room_names.event_id "
            )

            # We use non printing ascii character US (\x1F) as a separator
            sql = (
                "SELECT r.room_id, max(n.name), max(t.topic)"
                " FROM rooms AS r"
                " LEFT JOIN (%(topic)s) AS t ON t.room_id = r.room_id"
                " LEFT JOIN (%(name)s) AS n ON n.room_id = r.room_id"
                " WHERE r.is_public = ?"
                " GROUP BY r.room_id"
            ) % {
                "topic": topic_subquery,
                "name": name_subquery,
            }

            txn.execute(sql, (is_public,))

            rows = txn.fetchall()

            for i, row in enumerate(rows):
                room_id = row[0]
                aliases = self._simple_select_onecol_txn(
                    txn,
                    table="room_aliases",
                    keyvalues={
                        "room_id": room_id
                    },
                    retcol="room_alias",
                )

                rows[i] = list(row) + [aliases]

            return rows

        rows = yield self.runInteraction(
            "get_rooms", f
        )

        ret = [
            {
                "room_id": r[0],
                "name": r[1],
                "topic": r[2],
                "aliases": r[3],
            }
            for r in rows
            if r[3]  # We only return rooms that have at least one alias.
        ]

        defer.returnValue(ret)

    def _store_room_topic_txn(self, txn, event):
        if hasattr(event, "content") and "topic" in event.content:
            self._simple_insert_txn(
                txn,
                "topics",
                {
                    "event_id": event.event_id,
                    "room_id": event.room_id,
                    "topic": event.content["topic"],
                },
            )

    def _store_room_name_txn(self, txn, event):
        if hasattr(event, "content") and "name" in event.content:
            self._simple_insert_txn(
                txn,
                "room_names",
                {
                    "event_id": event.event_id,
                    "room_id": event.room_id,
                    "name": event.content["name"],
                }
            )

    @defer.inlineCallbacks
    def get_room_name_and_aliases(self, room_id):
        def f(txn):
            sql = (
                "SELECT event_id FROM events current_state_events "
                "WHERE room_id = ? "
            )

            sql += " AND ((type = 'm.room.name' AND state_key = '')"
            sql += " OR type = 'm.room.aliases')"

            txn.execute(sql, (room_id,))
            results = self.cursor_to_dict(txn)

            return self._parse_events_txn(txn, results)

        events = yield self.runInteraction("get_room_name_and_aliases", f)

        name = None
        aliases = []

        for e in events:
            if e.type == 'm.room.name':
                if 'name' in e.content:
                    name = e.content['name']
            elif e.type == 'm.room.aliases':
                if 'aliases' in e.content:
                    aliases.extend(e.content['aliases'])

        defer.returnValue((name, aliases))

class RoomsTable(object):
    table_name = "rooms"

    fields = [
        "room_id",
        "is_public",
        "creator"
    ]
<commit_msg>Fix broken SQL<commit_after># -*- coding: utf-8 -*-
# Copyright 2014, 2015 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from twisted.internet import defer

from synapse.api.errors import StoreError

from ._base import SQLBaseStore

import collections
import logging

logger = logging.getLogger(__name__)

OpsLevel = collections.namedtuple(
    "OpsLevel",
    ("ban_level", "kick_level", "redact_level",)
)

class RoomStore(SQLBaseStore):

    @defer.inlineCallbacks
    def store_room(self, room_id, room_creator_user_id, is_public):
        """Stores a room.

        Args:
            room_id (str): The desired room ID, can be None.
            room_creator_user_id (str): The user ID of the room creator.
            is_public (bool): True to indicate that this room should appear in
            public room lists.
        Raises:
            StoreError if the room could not be stored.
        """
        try:
            yield self._simple_insert(
                RoomsTable.table_name,
                {
                    "room_id": room_id,
                    "creator": room_creator_user_id,
                    "is_public": is_public,
                },
                desc="store_room",
            )
        except Exception as e:
            logger.error("store_room with room_id=%s failed: %s", room_id, e)
            raise StoreError(500, "Problem creating room.")

    def get_room(self, room_id):
        """Retrieve a room.

        Args:
            room_id (str): The ID of the room to retrieve.
        Returns:
            A namedtuple containing the room information, or an empty list.
        """
        return self._simple_select_one(
            table=RoomsTable.table_name,
            keyvalues={"room_id": room_id},
            retcols=RoomsTable.fields,
            desc="get_room",
            allow_none=True,
        )

    def get_public_room_ids(self):
        return self._simple_select_onecol(
            table="rooms",
            keyvalues={
                "is_public": True,
            },
            retcol="room_id",
            desc="get_public_room_ids",
        )

    @defer.inlineCallbacks
    def get_rooms(self, is_public):
        """Retrieve a list of all public rooms.

        Args:
            is_public (bool): True if the rooms returned should be public.
        Returns:
            A list of room dicts containing at least a "room_id" key, a
            "topic" key if one is set, and a "name" key if one is set
        """

        def f(txn):
            topic_subquery = (
                "SELECT topics.event_id as event_id, "
                "topics.room_id as room_id, topic "
                "FROM topics "
                "INNER JOIN current_state_events as c "
                "ON c.event_id = topics.event_id "
            )

            name_subquery = (
                "SELECT room_names.event_id as event_id, "
                "room_names.room_id as room_id, name "
                "FROM room_names "
                "INNER JOIN current_state_events as c "
                "ON c.event_id = room_names.event_id "
            )

            # We use non printing ascii character US (\x1F) as a separator
            sql = (
                "SELECT r.room_id, max(n.name), max(t.topic)"
                " FROM rooms AS r"
                " LEFT JOIN (%(topic)s) AS t ON t.room_id = r.room_id"
                " LEFT JOIN (%(name)s) AS n ON n.room_id = r.room_id"
                " WHERE r.is_public = ?"
                " GROUP BY r.room_id"
            ) % {
                "topic": topic_subquery,
                "name": name_subquery,
            }

            txn.execute(sql, (is_public,))

            rows = txn.fetchall()

            for i, row in enumerate(rows):
                room_id = row[0]
                aliases = self._simple_select_onecol_txn(
                    txn,
                    table="room_aliases",
                    keyvalues={
                        "room_id": room_id
                    },
                    retcol="room_alias",
                )

                rows[i] = list(row) + [aliases]

            return rows

        rows = yield self.runInteraction(
            "get_rooms", f
        )

        ret = [
            {
                "room_id": r[0],
                "name": r[1],
                "topic": r[2],
                "aliases": r[3],
            }
            for r in rows
            if r[3]  # We only return rooms that have at least one alias.
        ]

        defer.returnValue(ret)

    def _store_room_topic_txn(self, txn, event):
        if hasattr(event, "content") and "topic" in event.content:
            self._simple_insert_txn(
                txn,
                "topics",
                {
                    "event_id": event.event_id,
                    "room_id": event.room_id,
                    "topic": event.content["topic"],
                },
            )

    def _store_room_name_txn(self, txn, event):
        if hasattr(event, "content") and "name" in event.content:
            self._simple_insert_txn(
                txn,
                "room_names",
                {
                    "event_id": event.event_id,
                    "room_id": event.room_id,
                    "name": event.content["name"],
                }
            )

    @defer.inlineCallbacks
    def get_room_name_and_aliases(self, room_id):
        def f(txn):
            sql = (
                "SELECT event_id FROM current_state_events "
                "WHERE room_id = ? "
            )

            sql += " AND ((type = 'm.room.name' AND state_key = '')"
            sql += " OR type = 'm.room.aliases')"

            txn.execute(sql, (room_id,))
            results = self.cursor_to_dict(txn)

            return self._parse_events_txn(txn, results)

        events = yield self.runInteraction("get_room_name_and_aliases", f)

        name = None
        aliases = []

        for e in events:
            if e.type == 'm.room.name':
                if 'name' in e.content:
                    name = e.content['name']
            elif e.type == 'm.room.aliases':
                if 'aliases' in e.content:
                    aliases.extend(e.content['aliases'])

        defer.returnValue((name, aliases))

class RoomsTable(object):
    table_name = "rooms"

    fields = [
        "room_id",
        "is_public",
        "creator"
    ]
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Copyright 2016 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from functools import wraps
from typing import Any, Callable, Optional, TypeVar, cast

from prometheus_client import Counter

from synapse.logging.context import LoggingContext, current_context
from synapse.metrics import InFlightGauge

logger = logging.getLogger(__name__)

block_counter = Counter("synapse_util_metrics_block_count", "", ["block_name"])

block_timer = Counter("synapse_util_metrics_block_time_seconds", "", ["block_name"])

block_ru_utime = Counter(
    "synapse_util_metrics_block_ru_utime_seconds", "", ["block_name"]
)

block_ru_stime = Counter(
    "synapse_util_metrics_block_ru_stime_seconds", "", ["block_name"]
)

block_db_txn_count = Counter(
    "synapse_util_metrics_block_db_txn_count", "", ["block_name"]
)

# seconds spent waiting for db txns, excluding scheduling time, in this block
block_db_txn_duration = Counter(
    "synapse_util_metrics_block_db_txn_duration_seconds", "", ["block_name"]
)

# seconds spent waiting for a db connection, in this block
block_db_sched_duration = Counter(
    "synapse_util_metrics_block_db_sched_duration_seconds", "", ["block_name"]
)

# Tracks the number of blocks currently active
in_flight = InFlightGauge(
    "synapse_util_metrics_block_in_flight",
    "",
    labels=["block_name"],
    sub_metrics=["real_time_max", "real_time_sum"],
)

T = TypeVar("T", bound=Callable[..., Any])

def measure_func(name: Optional[str] = None) -> Callable[[T], T]:
    """
    Used to decorate an async function with a `Measure` context manager.

    Usage:

    @measure_func()
    async def foo(...):
        ...

    Which is analogous to:

    async def foo(...):
        with Measure(...):
            ...

    """

    def wrapper(func: T) -> T:
        block_name = func.__name__ if name is None else name

        @wraps(func)
        async def measured_func(self, *args, **kwargs):
            with Measure(self.clock, block_name):
                r = await func(self, *args, **kwargs)
            return r

        return cast(T, measured_func)

    return wrapper

class Measure:
    __slots__ = [
        "clock",
        "name",
        "_logging_context",
        "start",
    ]

    def __init__(self, clock, name):
        self.clock = clock
        self.name = name
        self._logging_context = None
        self.start = None

    def __enter__(self):
        if self._logging_context:
            raise RuntimeError("Measure() objects cannot be re-used")

        self.start = self.clock.time()
        parent_context = current_context()
        self._logging_context = LoggingContext(
            "Measure[%s]" % (self.name,), parent_context
        )
        self._logging_context.__enter__()
        in_flight.register((self.name,), self._update_in_flight)

    def __exit__(self, exc_type, exc_val, exc_tb):
        if not self._logging_context:
            raise RuntimeError("Measure() block exited without being entered")

        duration = self.clock.time() - self.start
        usage = self._logging_context.get_resource_usage()

        in_flight.unregister((self.name,), self._update_in_flight)
        self._logging_context.__exit__(exc_type, exc_val, exc_tb)

        try:
            block_counter.labels(self.name).inc()
            block_timer.labels(self.name).inc(duration)
            block_ru_utime.labels(self.name).inc(usage.ru_utime)
            block_ru_stime.labels(self.name).inc(usage.ru_stime)
            block_db_txn_count.labels(self.name).inc(usage.db_txn_count)
            block_db_txn_duration.labels(self.name).inc(usage.db_txn_duration_sec)
            block_db_sched_duration.labels(self.name).inc(usage.db_sched_duration_sec)
        except ValueError:
            logger.warning("Failed to save metrics! Usage: %s", usage)

    def _update_in_flight(self, metrics):
        """Gets called when processing in flight metrics
        """
        duration = self.clock.time() - self.start

        metrics.real_time_max = max(metrics.real_time_max, duration)
        metrics.real_time_sum += duration

        # TODO: Add other in flight metrics.
<commit_msg>Expose a `get_resource_usage` method in `Measure`<commit_after># -*- coding: utf-8 -*-
# Copyright 2016 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from functools import wraps
from typing import Any, Callable, Optional, TypeVar, cast

from prometheus_client import Counter

from synapse.logging.context import (
    ContextResourceUsage,
    LoggingContext,
    current_context,
)
from synapse.metrics import InFlightGauge

logger = logging.getLogger(__name__)

block_counter = Counter("synapse_util_metrics_block_count", "", ["block_name"])

block_timer = Counter("synapse_util_metrics_block_time_seconds", "", ["block_name"])

block_ru_utime = Counter(
    "synapse_util_metrics_block_ru_utime_seconds", "", ["block_name"]
)

block_ru_stime = Counter(
    "synapse_util_metrics_block_ru_stime_seconds", "", ["block_name"]
)

block_db_txn_count = Counter(
    "synapse_util_metrics_block_db_txn_count", "", ["block_name"]
)

# seconds spent waiting for db txns, excluding scheduling time, in this block
block_db_txn_duration = Counter(
    "synapse_util_metrics_block_db_txn_duration_seconds", "", ["block_name"]
)

# seconds spent waiting for a db connection, in this block
block_db_sched_duration = Counter(
    "synapse_util_metrics_block_db_sched_duration_seconds", "", ["block_name"]
)

# Tracks the number of blocks currently active
in_flight = InFlightGauge(
    "synapse_util_metrics_block_in_flight",
    "",
    labels=["block_name"],
    sub_metrics=["real_time_max", "real_time_sum"],
)

T = TypeVar("T", bound=Callable[..., Any])

def measure_func(name: Optional[str] = None) -> Callable[[T], T]:
    """
    Used to decorate an async function with a `Measure` context manager.

    Usage:

    @measure_func()
    async def foo(...):
        ...

    Which is analogous to:

    async def foo(...):
        with Measure(...):
            ...

    """

    def wrapper(func: T) -> T:
        block_name = func.__name__ if name is None else name

        @wraps(func)
        async def measured_func(self, *args, **kwargs):
            with Measure(self.clock, block_name):
                r = await func(self, *args, **kwargs)
            return r

        return cast(T, measured_func)

    return wrapper

class Measure:
    __slots__ = [
        "clock",
        "name",
        "_logging_context",
        "start",
    ]

    def __init__(self, clock, name):
        self.clock = clock
        self.name = name
        parent_context = current_context()
        self._logging_context = LoggingContext(
            "Measure[%s]" % (self.name,), parent_context
        )
        self.start = None

    def __enter__(self) -> "Measure":
        if self.start is not None:
            raise RuntimeError("Measure() objects cannot be re-used")

        self.start = self.clock.time()
        self._logging_context.__enter__()
        in_flight.register((self.name,), self._update_in_flight)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.start is None:
            raise RuntimeError("Measure() block exited without being entered")

        duration = self.clock.time() - self.start
        usage = self.get_resource_usage()

        in_flight.unregister((self.name,), self._update_in_flight)
        self._logging_context.__exit__(exc_type, exc_val, exc_tb)

        try:
            block_counter.labels(self.name).inc()
            block_timer.labels(self.name).inc(duration)
            block_ru_utime.labels(self.name).inc(usage.ru_utime)
            block_ru_stime.labels(self.name).inc(usage.ru_stime)
            block_db_txn_count.labels(self.name).inc(usage.db_txn_count)
            block_db_txn_duration.labels(self.name).inc(usage.db_txn_duration_sec)
            block_db_sched_duration.labels(self.name).inc(usage.db_sched_duration_sec)
        except ValueError:
            logger.warning("Failed to save metrics! Usage: %s", usage)

    def get_resource_usage(self) -> ContextResourceUsage:
        """Get the resources used within this Measure block

        If the Measure block is still active, returns the resource usage so far.
        """
        return self._logging_context.get_resource_usage()

    def _update_in_flight(self, metrics):
        """Gets called when processing in flight metrics
        """
        duration = self.clock.time() - self.start

        metrics.real_time_max = max(metrics.real_time_max, duration)
        metrics.real_time_sum += duration

        # TODO: Add other in flight metrics.
<|endoftext|>


<commit_before>#!/usr/bin/env python

__author__ = 'Jason Bristol'

import argparse, random, sys
from pypicker_exceptions import *

parser = argparse.ArgumentParser(
	description='Randomly pick n items from a list',
	epilog='Created by Jason Bristol <Gethsemane369@gmail.com>')

parser.add_argument('n',   metavar='N', type=int,   default=1,   help='number of items to pick (default 1)')
parser.add_argument('i',   metavar='I', type=str,                help='input file of items to choose from')
parser.add_argument('-ng',              type=int,   default=1,   help='number of result groups to generate (default 1)')
parser.add_argument('-o',               type=str,                help='output file to write results to')
parser.add_argument('-j',               type=str,                help='join results using specified delimeter')
parser.add_argument('-v',               action='store_true',     help='verbose output')
parser.add_argument('-u',								action='store_true',		 help='force uniqueness across result groups')

args = parser.parse_args()

options      = []
resultString = []
sampleSize   = 0

''' Validates and orchestrates result picks '''
def pick(n, num_groups, input_file, output_file, verbose, join, unique_groups):

	if n not in range (0, len(options)): raise BoundsException("n out of bounds, please select a number within 1 <= n <= total items\n")

	sampleSize = len(options)

	for i in range(0, num_groups):
		results = random.sample(options, n)

		if unique_groups:
			if (n * num_groups) > sampleSize: raise ResultSetException("Requested result set is larger than sample size\n")
			else:
				for result in results:
					while result in options:
						options.remove(result)

		if verbose: resultString.append("\nPicking {} individuals out of {}:".format(n, len(options) + n))
		resultString.append("\n".join(results) if join == None else "{} ".format(join).join(results))

	if output_file == None: print '\n'.join(resultString)
	else:
		try:
			with open(output_file,'w') as f:
				f.write('\n'.join(resultString))
		except:  raise OutputException(sys.exc_info[0])
		finally: f.close

''' Main Method '''
if __name__ == "__main__":
	try:
		options = [line.strip() for line in open(args.i)]
		pick(args.n, args.ng, args.i, args.o, args.v, args.j, args.u)
	except BoundsException      as e: print "\n{}: {}".format(type(e).__name__, e.msg)
	except OutputException 	    as e: print "\n{}: {}".format(type(e).__name__, e.msg)
	except ResultSetException   as e: print "\n{}: {}".format(type(e).__name__, e.msg)
	except                          : print "\n{}: {}".format("Unexpected error", sys.exc_info()[0])
	finally                         : sys.exit(0)
<commit_msg>Added version number<commit_after>#!/usr/bin/env python

__author__  = 'Jason Bristol'
__version__ = '1.0'

import argparse, random, sys
from pypicker_exceptions import *

parser = argparse.ArgumentParser(
	description='Randomly pick n items from a list',
	epilog='Created by Jason Bristol <Gethsemane369@gmail.com>')

parser.add_argument('n',   metavar='N', type=int,   default=1,   help='number of items to pick (default 1)')
parser.add_argument('i',   metavar='I', type=str,                help='input file of items to choose from')
parser.add_argument('-ng',              type=int,   default=1,   help='number of result groups to generate (default 1)')
parser.add_argument('-o',               type=str,                help='output file to write results to')
parser.add_argument('-j',               type=str,                help='join results using specified delimeter')
parser.add_argument('-v',               action='store_true',     help='verbose output')
parser.add_argument('-u',								action='store_true',		 help='force uniqueness across result groups')

args = parser.parse_args()

options      = []
resultString = []
sampleSize   = 0

''' Validates and orchestrates result picks '''
def pick(n, num_groups, input_file, output_file, verbose, join, unique_groups):

	if n not in range (0, len(options)): raise BoundsException("n out of bounds, please select a number within 1 <= n <= total items\n")

	sampleSize = len(options)

	for i in range(0, num_groups):
		results = random.sample(options, n)

		if unique_groups:
			if (n * num_groups) > sampleSize: raise ResultSetException("Requested result set is larger than sample size\n")
			else:
				for result in results:
					while result in options:
						options.remove(result)

		if verbose: resultString.append("\nPicking {} individuals out of {}:".format(n, len(options) + n))
		resultString.append("\n".join(results) if join == None else "{} ".format(join).join(results))

	if output_file == None: print '\n'.join(resultString)
	else:
		try:
			with open(output_file,'w') as f:
				f.write('\n'.join(resultString))
		except:  raise OutputException(sys.exc_info[0])
		finally: f.close

''' Main Method '''
if __name__ == "__main__":
	try:
		options = [line.strip() for line in open(args.i)]
		pick(args.n, args.ng, args.i, args.o, args.v, args.j, args.u)
	except BoundsException      as e: print "\n{}: {}".format(type(e).__name__, e.msg)
	except OutputException 	    as e: print "\n{}: {}".format(type(e).__name__, e.msg)
	except ResultSetException   as e: print "\n{}: {}".format(type(e).__name__, e.msg)
	except                          : print "\n{}: {}".format("Unexpected error", sys.exc_info()[0])
	finally                         : sys.exit(0)
<|endoftext|>


<commit_before>#!/usr/bin/env jython
# This requires Jython and Apache POI
import os, sys, getopt, ConfigParser

try:
    config = ConfigParser.ConfigParser()
    config.read(os.path.join(os.path.dirname(os.path.abspath(__file__)), "apache.cfg"))
    APACHE_POI_JAR_PATH = config.get("POI", "path")
except Exception:
    APACHE_POI_JAR_PATH = os.environ["APACHE_POI_JAR_PATH"]

for root, dirs, files in os.walk(APACHE_POI_JAR_PATH):
    for name in files:
        if name.endswith(".jar"):
            sys.path.append(os.path.join(root, name))

from org.apache.poi.xssf.usermodel import *
from java.io import FileInputStream
from java.io import FileOutputStream

def main(argv):
    password = ''
    input_file = ''
    output_file = ''

    def message():
        print '\nUsage: jython %s -p <password> -i <inputfile> -o <outputfile>\n' % sys.argv[0]
        sys.exit()

    try:
        opts, args = getopt.getopt(argv,"hp:i:o:",["password=","input=","output="])
    except getopt.GetoptError:
        message()
    for opt, arg in opts:
        if opt == '-h':
            message()
        elif opt in ("-p", "--password"):
            password = arg
        elif opt in ("-i", "--input"):
            input_file = arg
        elif opt in ("-o", "--output"):
            output_file = arg

    if password and input_file and output_file:
        fileIn = FileInputStream(input_file)
        fileOut = FileOutputStream(output_file)

        workbook = XSSFWorkbook(fileIn)
        worksheets = workbook.getNumberOfSheets()

        for worksheet in range(0,worksheets):
            sheet = workbook.getSheetAt(worksheet)
            # sheet.sheetName.title()
            sheet.lockDeleteColumns()
            sheet.lockDeleteRows()
            sheet.lockFormatCells()
            sheet.lockFormatColumns()
            sheet.lockFormatRows()
            sheet.lockInsertColumns()
            sheet.lockInsertRows()
            sheet.protectSheet(password)
            sheet.enableLocking();

        workbook.lockStructure();
        workbook.write(fileOut)
        fileIn.close()
    else:
        message()

if __name__ == "__main__":
    main(sys.argv[1:])
<commit_msg>Added Support for Classic XLS File<commit_after>#!/usr/bin/env jython
# This requires Jython and Apache POI
import os, sys, getopt, ConfigParser, mimetypes

try:
    config = ConfigParser.ConfigParser()
    config.read(os.path.join(os.path.dirname(os.path.abspath(__file__)), "apache.cfg"))
    APACHE_POI_JAR_PATH = config.get("POI", "path")
except Exception:
    APACHE_POI_JAR_PATH = os.environ["APACHE_POI_JAR_PATH"]

for root, dirs, files in os.walk(APACHE_POI_JAR_PATH):
    for name in files:
        if name.endswith(".jar"):
            sys.path.append(os.path.join(root, name))

from org.apache.poi.xssf.usermodel import *
from org.apache.poi.hssf.usermodel import *
from java.io import FileInputStream
from java.io import FileOutputStream

xssf_supported_mimetypes = [
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
]

hssf_supported_mimetypes = [
    "application/vnd.ms-excel"
]

def main(argv):
    password = ''
    input_file = ''
    output_file = ''

    def get_mimetype(input_file):
        file_mimetype, file_mime_encoding = mimetypes.guess_type(input_file)
        return file_mimetype, file_mime_encoding

    def usage():
        print '\nUsage: jython %s -p <password> -i <inputfile> -o <outputfile>\n' % sys.argv[0]
        sys.exit()

    try:
        opts, args = getopt.getopt(argv,"hp:i:o:",["password=","input=","output="])
    except getopt.GetoptError:
        usage()
    for opt, arg in opts:
        if opt == '-h':
            usage()
        elif opt in ("-p", "--password"):
            password = arg
        elif opt in ("-i", "--input"):
            input_file = arg
        elif opt in ("-o", "--output"):
            output_file = arg

    if password and input_file and output_file:
        mimetype = get_mimetype(input_file)[0]
        # print "Office File MimeType: %s" % mimetype
        fileIn = FileInputStream(input_file)
        fileOut = FileOutputStream(output_file)

        if mimetype in xssf_supported_mimetypes:
            workbook = XSSFWorkbook(fileIn)
            worksheets = workbook.getNumberOfSheets()
            for worksheet in range(0,worksheets):
                sheet = workbook.getSheetAt(worksheet)
                sheet.lockDeleteColumns()
                sheet.lockDeleteRows()
                sheet.lockFormatCells()
                sheet.lockFormatColumns()
                sheet.lockFormatRows()
                sheet.lockInsertColumns()
                sheet.lockInsertRows()
                sheet.protectSheet(password)
                sheet.enableLocking();
            workbook.lockStructure();

        elif mimetype in hssf_supported_mimetypes:
            workbook = HSSFWorkbook(fileIn)
            workbook.writeProtectWorkbook(password, password)

        workbook.write(fileOut)
        fileIn.close()

    else:
        usage()

if __name__ == "__main__":
    main(sys.argv[1:])
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import math

def magnitudeSquared(p):
    return p[0] ** 2 + p[1] ** 2

def magnitude(p):
    return math.sqrt(magnitudeSquared(p))

def add(left, right):
    return [left[0] + right[0], left[1] + right[1]]

def subtract(left, right):
    return [left[0] - right[0], left[1] - right[1]]

def distanceSquared(p1, p2):
    return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2

def distance(p1, p2):
    return math.sqrt(distanceSquared(p1, p2))

def multiplyByScalar(p, scalar):
    return [p[0] * scalar, p[1] * scalar]
<commit_msg>Remove unused file<commit_after><|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import os, re
from urllib import parse

def getDifference (setA, setB):
  return list(filter(lambda el: el not in setB, setA))

def getHomePath (fl = ""):
  if isinstance(fl, str) and fl != "":
    return joinPath((os.path.expanduser('~'), fl))
  return os.path.expanduser('~')

def existsPath(path, isFolder = False):
  opath = os.path
  if isinstance(path, str) and path != "" and opath.exists(path):
    if (isFolder and opath.isdir(path)): return True
    if (not isFolder and opath.isfile(path)): return True
  return False

def joinPath (pathTuple):
  if isinstance(pathTuple, tuple) and len(pathTuple) > 1:
    return os.path.join(*pathTuple)
  return None

def getFiles (path):
  if existsPath(path, True):
    f = []
    for root, dirs, files in os.walk(path):
      f.extend([joinPath((root, file)) for file in files])
    return f
  return []

def excludeFilesByPatterns (elements, patterns):
  isValidElements = isinstance(elements, list) and len(elements) > 0
  isValidPattern = isinstance(patterns, list) and len(patterns) > 0
  results = []

  if isValidElements and isValidPattern:
    for element in elements:
      for pattern in patterns:
        extension = '.' + element.split(os.extsep)[-1]
        filename = os.path.basename(element)

        if element.startswith(pattern) and existsPath(pattern, True) and existsPath(joinPath((pattern, filename))):
          results.append(element)
        elif (extension == pattern or element == pattern) and existsPath(element):
          results.append(element)
    return getDifference(elements, results)
  return elements

def encodePath(path):
  if isinstance(path, str) and len(path) > 0:
    return parse.quote(path)
  return None

def decodePath(path):
  if isinstance(path, str) and len(path) > 0:
    return parse.unquote(path)
  return None
<commit_msg>add function to check if the current platform is windows<commit_after># -*- coding: utf-8 -*-

import os, re
from urllib import parse

def getDifference (setA, setB):
  return list(filter(lambda el: el not in setB, setA))

def getHomePath (fl = ""):
  if isinstance(fl, str) and fl != "":
    return joinPath((os.path.expanduser('~'), fl))
  return os.path.expanduser('~')

def existsPath(path, isFolder = False):
  opath = os.path
  if isinstance(path, str) and path != "" and opath.exists(path):
    if (isFolder and opath.isdir(path)): return True
    if (not isFolder and opath.isfile(path)): return True
  return False

def joinPath (pathTuple):
  if isinstance(pathTuple, tuple) and len(pathTuple) > 1:
    return os.path.join(*pathTuple)
  return None

def getFiles (path):
  if existsPath(path, True):
    f = []
    for root, dirs, files in os.walk(path):
      f.extend([joinPath((root, file)) for file in files])
    return f
  return []

def excludeFilesByPatterns (elements, patterns):
  isValidElements = isinstance(elements, list) and len(elements) > 0
  isValidPattern = isinstance(patterns, list) and len(patterns) > 0
  results = []

  if isValidElements and isValidPattern:
    for element in elements:
      for pattern in patterns:
        extension = '.' + element.split(os.extsep)[-1]
        filename = os.path.basename(element)

        if element.startswith(pattern) and existsPath(pattern, True) and existsPath(joinPath((pattern, filename))):
          results.append(element)
        elif (extension == pattern or element == pattern) and existsPath(element):
          results.append(element)
    return getDifference(elements, results)
  return elements

def encodePath(path):
  if isinstance(path, str) and len(path) > 0:
    return parse.quote(path)
  return None

def decodePath(path):
  if isinstance(path, str) and len(path) > 0:
    return parse.unquote(path)
  return None

def isWindows ():
  return sys.platform.startswith('win')
<|endoftext|>