Ejemplo n.º 1
0
 def test_basic(self):
     t = Timestamp()
     self.assertTrue(t is not None)
     ms = t.ms_int()
     self.assertTrue(ms is not None)
     st = str(t)
     self.assertTrue(st is not None)
Ejemplo n.º 2
0
    def test_1(self):
        t1 = Timestamp({"id": 0})
        t2 = Timestamp({"id": 1})

        l = list(t1.compare(t2))

        self.assertEqual(l[0], "id")
Ejemplo n.º 3
0
 def test_basic(self):
     t = Timestamp()
     self.assertTrue(t is not None)
     ms = t.ms_int()
     self.assertTrue(ms is not None)
     st = str(t)
     self.assertTrue(st is not None)
Ejemplo n.º 4
0
    def test_2(self):
        t1 = Timestamp()
        t2 = Timestamp({"id": 1})

        t1.merge(t2)

        self.assertEqual(t1.replicas["id"], 1)
Ejemplo n.º 5
0
    def test_3(self):
        t1 = Timestamp({"id": 0})
        t2 = Timestamp({"id": 1})

        t2.merge(t1)

        self.assertNotEqual(t2.replicas["id"], 0)
Ejemplo n.º 6
0
 def testSelfDecrement( self ):
     ticks = self.ts1.getLocalTicks() + 30000
     self.ts2 = Timestamp( ticks )
     self.ts2 -= 30000
     assertEquals( 'Ticks',
                   self.ts2.getLocalTicks() ,
                   self.ts1.getLocalTicks() )
Ejemplo n.º 7
0
def ptp_data():
    t = timespec()
    utc_time = time.time()
    _ts = Timestamp(int(utc_time), int((utc_time % 1) * 1e9))
    nanosec = _ts.to_nanosec()
    t.tv_sec = int(nanosec * 1e-9)
    t.tv_nsec = int(nanosec - (t.tv_sec * 1e9))
    return t
Ejemplo n.º 8
0
def validate(data, tags, created):
    tags = tags.split(' ')

    if re.search('\.', created):
        ts = Timestamp(created)
    else:
        ts = Timestamp(compact=created)

    return [data, tags, ts]
Ejemplo n.º 9
0
    def from_dict(classname: 'str', dict: Dict) -> 'Record':
        """
        Used for serpent deserialisation
        :return: A Record
        """

        return Record(
            dict["i"], Timestamp.from_dict("Timestamp", dict["ts"]),
            ClientRequest.from_dict("ClientRequest", dict["request"]),
            Timestamp.from_dict("Timestamp", dict["prev"]), dict["id"])
Ejemplo n.º 10
0
    def range(self, start=None, end=None):
        """
        """
        expect_entries = True
        if end:
            start = Timestamp(start)
            end = Timestamp(end)
            url = '%s/range/%s/%s' % (self.source, start.compact(),
                                      end.compact())
        elif start:
            start = Timestamp(start)
            url = '%s/range/%s' % (self.source, start.compact())
        else:
            url = '%s/range' % (self.source)
            expect_entries = False

        #print url

        req = urllib2.urlopen(url)
        response = req.read()
        req.close()
        if expect_entries:
            result = json.loads(response)
            elist = []
            for e in result['entries']:
                m = Moment(data=e['data'],
                           tags=e['tags'],
                           created=e['created'],
                           path=e['path'])
                #m = Moment(data=e['data'], tags=e['tags'], created=e['created'])
                elist.append(m)
            return elist
        else:
            return response
    def __init__(self):

        self.id = "frontend-" + str(uuid.uuid4())  # The ID of this FE

        # Reflects the version of the replicated data accessed by the FE; contains an entry for every RM. The FE sends
        # this with every query or update operation. When a RM returns a value as the result of a query operation, it
        # supplies a new vector timestamp, since the RMs may have been updated since the last operation. Each returned
        # timestamp is merged with the FE's previous timestamp to record the version the data observed by the client.
        self.prev = Timestamp()

        self.ns = Pyro4.locateNS()  # The Pyro Name Server
Ejemplo n.º 12
0
 def testSubtract( self ):
     ticks = self.ts1.getLocalTicks() + 30000
     self.ts2 = Timestamp( ticks )
     self.ts1 = self.ts2 - 30000
     assertEquals( 'Ticks' ,
                   self.ts1.getLocalTicks(),
                   ticks1 )
     # Make sure ts2 was unchanged by the operation.
     assertEquals( 'Ticks' ,
                   self.ts2.getLocalTicks() ,
                   ticks )
Ejemplo n.º 13
0
def log(text, timestamp = None):
    def path_leaf(path):
        head, tail = ntpath.split(path)
        return tail or ntpath.basename(head)
    frame,filename,line_number,function_name,lines,index = inspect.getouterframes(inspect.currentframe())[2] # log caller stack
    filename = path_leaf(filename)
    _,_,_,log_type,_,_ = inspect.getouterframes(inspect.currentframe())[1] # log function (info, error...)
    log_type = log_type[0].upper() * 2 # II for info, EE for error...
    if timestamp is None:
        timestamp = Timestamp()
    text = "%s %s %s:%s: %s" %(timestamp.to_human_str(), log_type, filename, line_number, text)
    print text
    logfile.write("\n%s" %text)
Ejemplo n.º 14
0
 def setData(self, index, value, role):
     if index.isValid() and role == Qt.EditRole:
         row, col = index.row(), index.column()
         ch = self._mp3.chapters[row]
         if col == 0:
             ch.title = value
         elif col == 1:
             ch.start = Timestamp.from_string(value)
         elif col == 2:
             ch.end = Timestamp.from_string(value)
         self.dataChanged.emit(index, index, [role])
         return True
     else:
         return False
Ejemplo n.º 15
0
    def test_parse(self):
        t = Timestamp(ms_int = 10)
        self.assertEqual(t.ms_int(), 10)
        self.assertEqual(str(t), "10")

        t = Timestamp(ms_str = "10")
        self.assertEqual(t.ms_int(), 10)
        self.assertEqual(str(t), "10")

        t2 = Timestamp(ms_int = 10)
        self.assertEqual(t.ms_int(), t2.ms_int())

        t3 = Timestamp(ms_str = str(t))
        self.assertEqual(t.ms_int(), t3.ms_int())
Ejemplo n.º 16
0
    def range(self, start=None, end=None):
        """
        if no start *and* end specified
        return the time range for the entries in the currently loaded journal

        if only start
        return the entries in range for the accuracy of the start (e.g. 1 day)

        if start and end
        return all entries in the journal that fall in that range

        should accept a string, a datetime object, or a Timestamp object
        """

        if start is None and end is None:
            dates = self._dates.keys()
            dates.sort()
            start = dates[0]
            end = dates[-1]
            #might have entries with no timestamp first:
            if start is None:
                start = dates[1]
            print start, end
            return Timerange(start=start, end=end)

        else:
            start = Timestamp(start)
            if end:
                end = Timestamp(end)
            else:
                relative = Timerange(start)
                end = relative.end

            times = self._dates.keys()
            times.sort()

            matches = []
            for t in times:
                #not sure why we're using just time here
                #seems like we would want to use the date too?
                #pytime = Timestamp(t).time

                #sometimes t is None... those don't fit in a range.
                if t:
                    pytime = Timestamp(t).datetime
                    if (pytime >= start.datetime) and (pytime <= end.datetime):
                        matches.extend(self._dates[t])
            return matches
Ejemplo n.º 17
0
    def _search_for_quantity_in_price_level_total(self, tick_entry, quantity_to_trade, order):
        if tick_entry.quantity <= Quantity(0):
            return quantity_to_trade, []

        trading_quantity = quantity_to_trade
        quantity_to_trade = Quantity(0)

        self._logger.debug("Match with the id (%s) was found for order (%s). Price: %i, Quantity: %i)",
                           str(tick_entry.order_id), str(order.order_id), int(tick_entry.price), int(trading_quantity))

        reserved = order.reserve_quantity_for_tick(tick_entry.tick.order_id, trading_quantity)

        if not reserved:  # Error happened
            self._logger.warn("Something went wrong")
            return self._search_for_quantity_in_price_level(tick_entry.next_tick(), quantity_to_trade, order)

        proposed_trades = [Trade.propose(
            self.order_book.message_repository.next_identity(),
            order.order_id,
            tick_entry.order_id,
            tick_entry.price,
            trading_quantity,
            Timestamp.now()
        )]

        return quantity_to_trade, proposed_trades
Ejemplo n.º 18
0
    def _search_for_quantity_in_price_level_partial(self, tick_entry, quantity_to_trade, order):
        quantity_to_trade -= tick_entry.quantity

        self._logger.debug("Match with the id (%s) was found for order (%s) ", str(tick_entry.order_id),
                           str(order.order_id))

        reserved = order.reserve_quantity_for_tick(tick_entry.tick.order_id, tick_entry.quantity)

        if not reserved:  # Error happened
            self._logger.warn("Something went wrong")
            return self._search_for_quantity_in_price_level(tick_entry.next_tick(), quantity_to_trade, order)

        proposed_trades = [Trade.propose(
            self.order_book.message_repository.next_identity(),
            order.order_id,
            tick_entry.order_id,
            tick_entry.price,
            tick_entry.quantity,
            Timestamp.now()
        )]

        # Search the next tick
        quantity_to_trade, trades = self._search_for_quantity_in_price_level(tick_entry.next_tick(),
                                                                             quantity_to_trade,
                                                                             order)

        proposed_trades = proposed_trades + trades
        return quantity_to_trade, proposed_trades
Ejemplo n.º 19
0
    def create_bitcoin_payment(self, message_id, transaction, multi_chain_payment):
        bitcoin_payment = BitcoinPayment(message_id, transaction.transaction_id, multi_chain_payment.bitcoin_address,
                                         multi_chain_payment.transferee_price, Timestamp.now())
        transaction.add_payment(bitcoin_payment)
        self.transaction_repository.update(transaction)

        return bitcoin_payment
Ejemplo n.º 20
0
 def __init__(self,
              kind=None,
              nick_full=None,
              chan=None,
              msg=None,
              time=None,
              serialized_str=None):
     if serialized_str is None:
         if time is None:
             self.time = Timestamp()
         else:
             self.time = time
         if kind not in ["wa", "irc"]:
             raise Exception("Message kind must be 'wa' or 'irc'")
         self.kind = kind
         self.nick_full = nick_full
         self.chan = chan
         self.msg = msg
         self.target = None
         try:
             split = msg.split(":", 1)
             if len(split) == 2:
                 if not split[0].endswith(("http", "https", "image")):
                     self.target = split[0]
                     self.msg = split[1].lstrip()
         except IndexError:
             pass
     else:
         self.deserialize(serialized_str)
Ejemplo n.º 21
0
 def get_times(self, box_file):
     '''adds each line from input file as an object to list'''
     del self.times[:]  #clear the list of timestamp objects
     with open(box_file) as file:
         for line in file:
             time = Timestamp(line)
             self.times.append(time)
Ejemplo n.º 22
0
    def test_comparison(self):
        t = Timestamp()
        self.assertTrue(t == t)
        self.assertTrue(not t != t)
        self.assertTrue(not t > t)
        self.assertTrue(not t < t)
        self.assertTrue(t <= t)
        self.assertTrue(t >= t)

        t2 = Timestamp()
        self.assertTrue(not t == t2)
        self.assertTrue(t != t2)
        self.assertTrue(t < t2)
        self.assertTrue(t2 > t)
        self.assertTrue(t <= t2)
        self.assertTrue(t2 >= t)
Ejemplo n.º 23
0
 def onGroup_MessageReceived(self, messageId, jid, author, messageContent, timestamp, wantsReceipt, pushName):
     message = Message(kind="wa", nick_full=author, chan=jid, msg=messageContent)
     message.time = Timestamp(ms_int = timestamp*1000)
     self.msg_handler(message)
     sendReceipts = True
     if wantsReceipt and sendReceipts:
         self.wait_connected()
         self.methodsInterface.call("message_ack", (jid, messageId))
Ejemplo n.º 24
0
class dateOnlyInit( unittest.TestCase ):
    def setUp( self ):
        self.ts = Timestamp( format3 )
        
    def testGetLocalTicks( self ):
        assertEquals( 'Ticks' ,
                      self.ts.getLocalTicks(),
                      ticks3 )
Ejemplo n.º 25
0
def log(text, timestamp=None):
    def path_leaf(path):
        head, tail = ntpath.split(path)
        return tail or ntpath.basename(head)

    frame, filename, line_number, function_name, lines, index = inspect.getouterframes(
        inspect.currentframe())[2]  # log caller stack
    filename = path_leaf(filename)
    _, _, _, log_type, _, _ = inspect.getouterframes(
        inspect.currentframe())[1]  # log function (info, error...)
    log_type = log_type[0].upper() * 2  # II for info, EE for error...
    if timestamp is None:
        timestamp = Timestamp()
    text = "%s %s %s:%s: %s" % (timestamp.to_human_str(), log_type, filename,
                                line_number, text)
    print text
    logfile.write("\n%s" % text)
Ejemplo n.º 26
0
 def deserialize(self, string):
     fields = string.split(" @@@ ")
     time = Timestamp(ms_str=fields[0])
     kind = fields[1]
     nick_full = fields[2]
     chan = fields[3]
     msg = fields[4]
     self.__init__(kind, nick_full, chan, msg, time, serialized_str=None)
Ejemplo n.º 27
0
    def __init__(self):

        self._id = "replica-" + str(uuid.uuid4())  # This RM's ID

        # Represents updates currently reflected in the value. Contains one entry for every replica manager, and is
        # updated whenever an update operation is applied to the value.
        self.value_timestamp = Timestamp({self.id: 0})

        # Represents those updates that have been accepted by the RM (placed in the RM's update log). Differs from the
        # value timestamp because not all updates in the log are stable.
        self._replica_timestamp = Timestamp({self.id: 0})

        # The Pyro name server. Storing it locally removes the overhead of re-locating it every time this RM gets
        # replicas_with_updates.
        self.ns = Pyro4.locateNS()

        # This RM's update log, containing Records.
        self._update_log = Log()
Ejemplo n.º 28
0
    def create_multi_chain_payment(self, message_id, transaction):
        payment = transaction.next_payment()
        multi_chain_payment = MultiChainPayment(message_id, transaction.transaction_id, BitcoinAddress(''), payment[0],
                                                payment[1],
                                                Timestamp.now())
        transaction.add_payment(multi_chain_payment)
        self.transaction_repository.update(transaction)

        return multi_chain_payment
Ejemplo n.º 29
0
    def dates(self):
        """
        return a dictionary with:
        all dates as keys, and
        number of entries for each date as values
        """
        ddict = {}
        for key in self._dates.keys():
            #print "KEY:", key

            #key might be blank here (i.e. no timestamp)
            if key:
                ts = Timestamp(compact=key)
                #print ts
                ddict[ts.compact()] = len(self._dates[key])
            else:
                ddict[key] = len(self._dates[key])
        return ddict
Ejemplo n.º 30
0
 def from_xml(cls, xml_txt: str):
     markers = []
     root = ET.fromstring(xml_txt)
     for marker in root.iter('Marker'):
         name = marker.find('Name')
         time = marker.find('Time')
         if name is not None and time is not None:
             markers.append(cls(name.text,
                                Timestamp.from_string(time.text)))
     return markers
Ejemplo n.º 31
0
 def test_basic(self):
     m = Message("irc", "complete_nick!foobar", "channel_name", "message contents")
     t = Timestamp()
     self.assertTrue(m.time < t)
     self.assertEquals(m.nick_full, "complete_nick!foobar")
     self.assertEquals(m.nick_full, "complete_nick!foobar")
     self.assertEquals(m.get_nick(), "complete_nick")
     self.assertEquals(m.chan, "channel_name")
     self.assertEquals(m.msg, "message contents")
     self.assertTrue(m.target is None)
Ejemplo n.º 32
0
    def __init__(self, data=u'', tags=[], created='', path=u'', now=False):

        self.data = data
        self.tags = Tags(tags)

        #could rename this to path potentially
        #self.source_file = None
        #*2011.06.21 09:59:10
        #now wishing it was just self.source
        #maybe both should be available?
        self.path = path
        self.source = path
        #*2011.08.14 18:56:17
        #path implies a source and destination

        #self.created = ''

        #*2011.07.06 08:24:43
        #this may closely mimic the way Timestamp initializes
        #may want to leverage that
        #or just pass created and now values in to there

        if now:
            self.created = Timestamp()

        #elif type(created) == type(now):
        elif isinstance(created, datetime):
            self.created = Timestamp(created)

        #passed in an actual Timestamp here:
        elif isinstance(created, Timestamp):
            self.created = created

        elif isinstance(created, str) or isinstance(created, unicode):
            if created:
                self.created = Timestamp(created)
            else:
                self.created = created

        else:
            raise TypeError, "Unknown time format for moment created value: %s type: %s" % (
                created, type(created))
Ejemplo n.º 33
0
    def __init__(self, message_repository):
        self._logger = logging.getLogger(self.__class__.__name__)

        assert isinstance(message_repository, MessageRepository), type(message_repository)

        self.message_repository = message_repository
        self._trades = deque(maxlen=100)  # List of trades with a limit of 100
        self._bids = Side()
        self._asks = Side()
        self._last_message = None  # The last message processed by this order book
        self._last_timestamp = Timestamp(0.0)  # The time at which the last message was processed
Ejemplo n.º 34
0
class TimestampXml_test(XmlTest):
    def setUp(self):
        XmlTest.setUp(self)
    def test_now(self):
        self.result = Timestamp()
        self.assertLoopThrough(Timestamp)
    def test_array(self):
        self.result = Timestamp()
        self.result = self.result.append(Timestamp())
        self.assertLoopThrough(Timestamp)
    def assertEqual(self,one,other):
        unittest.TestCase.assertTrue(self,(one == other).all())
Ejemplo n.º 35
0
class utc_true_dst_on( unittest.TestCase ):
    """Tests that specifying the init as UTC works."""
    def setUp( self ):
        self.ts = Timestamp( utcF1 , True )
        
    def testGetUTCTicks( self ):
        assertEquals( 'UTC Ticks' ,
                      self.ts.getUTCTicks(),
                      utc1 )
                      
    def testGetLocalTicks( self ):
        assertEquals( 'Local Ticks' ,
                      self.ts.getLocalTicks() ,
                      ticks1 )
                          
    def testGetUTCString( self ):
        assertEquals( 'Date' ,
                      self.ts.getUTCString() ,
                      utcF1 )
                      
    def testGetLocalString( self ):
        assertEquals( 'Date' ,
                      self.ts.getLocalString() ,
                      format1 )
                      
    def testAltSeparator( self ):
        assertEquals( 'Date' ,
                      self.ts.getUTCString( '/' ) ,
                      utcF1alt )
                      
    def testGetUTCDay( self ):
        assertEquals( 'Day of Month' ,
                      self.ts.getUTCDay() ,
                      utcDay1 )
                      
    def testGetLocalDay( self ):
        assertEquals( 'Day of Month' ,
                      self.ts.getLocalDay() ,
                      day1 )
                      
    def testGetCVSTime( self ):
        assertEquals( 'CVS Time',
                      self.ts.getLocalCVSTime() ,
                      cvstime1 )
Ejemplo n.º 36
0
class utc_false_dst_off( unittest.TestCase ):
    """Tests that specifying the init as UTC works."""
    def setUp( self ):
        self.ts = Timestamp( format2 , False )
        
    def testGetUTCTicks( self ):
        assertEquals( 'UTC Ticks' ,
                      self.ts.getUTCTicks(),
                      utc2 )
                      
    def testGetLocalTicks( self ):
        assertEquals( 'Local Ticks' ,
                      self.ts.getLocalTicks() ,
                      ticks2 )
                          
    def testGetUTCString( self ):
        assertEquals( 'Date' ,
                      self.ts.getUTCString() ,
                      utcF2 )
                      
    def testGetLocalString( self ):
        assertEquals( 'Date' ,
                      self.ts.getLocalString() ,
                      format2 )
                      
    def testAltSeparator( self ):
        assertEquals( 'Date' ,
                      self.ts.getLocalString('/') ,
                      format2alt )
                      
    def testGetLocalDay( self ):
        assertEquals( 'Day of Month' ,
                      self.ts.getLocalDay() ,
                      day2 )
                      
    def testGetUTCDay( self ):
        assertEquals( 'Day of Month' ,
                      self.ts.getUTCDay() ,
                      utcDay2 )
Ejemplo n.º 37
0
 def test_now(self):
     self.result = Timestamp()
     self.assertLoopThrough(Timestamp)
Ejemplo n.º 38
0
 def test_integrity(self):
     t = Timestamp()
     self.assertEqual(t.ms_int(), t.ms_int())
     self.assertEqual(str(t), str(t))
Ejemplo n.º 39
0
 def test_integrity2(self):
     t = Timestamp()
     t2 = Timestamp()
     self.assertNotEqual(t.ms_int(), t2.ms_int())
     t3 = Timestamp(ms_int = t.ms_int())
     self.assertEqual(t.ms_int(), t3.ms_int())
Ejemplo n.º 40
0
 def setUp( self ):
     self.ts = Timestamp( format3 )
Ejemplo n.º 41
0
 def setUp( self ):
     self.ts = Timestamp( utcF1 , True )
Ejemplo n.º 42
0
 def test_array(self):
     self.result = Timestamp()
     self.result = self.result.append(Timestamp())
     self.assertLoopThrough(Timestamp)
Ejemplo n.º 43
0
 def setUp( self ):
     self.ts = Timestamp( format2 , False )
Ejemplo n.º 44
0
class mathOps( unittest.TestCase ):
    def setUp( self ):
        self.ts1 = Timestamp( format1 )
        self.ts2 = Timestamp()
        self.ts3 = Timestamp( format1 )
        
    def testAdd( self ):
        target = self.ts1.getLocalTicks() + 29000
        self.ts1 = self.ts3 + 29000        
        assertEquals( 'Ticks' ,
                      self.ts1.getLocalTicks() ,
                      target )
        # Make sure ts3 was unchanged by the operation.
        assertEquals( 'Ticks' ,
                      self.ts3.getLocalTicks() ,
                      ticks1 )
                  
    def testSelfIncrement( self ):
        target = self.ts1.getLocalTicks() + 29000
        self.ts1 += 29000
        assertEquals( 'Ticks' ,
                      self.ts1.getLocalTicks() ,
                      target )
                      
    def testSubtract( self ):
        ticks = self.ts1.getLocalTicks() + 30000
        self.ts2 = Timestamp( ticks )
        self.ts1 = self.ts2 - 30000
        assertEquals( 'Ticks' ,
                      self.ts1.getLocalTicks(),
                      ticks1 )
        # Make sure ts2 was unchanged by the operation.
        assertEquals( 'Ticks' ,
                      self.ts2.getLocalTicks() ,
                      ticks )
                      
    def testSelfDecrement( self ):
        ticks = self.ts1.getLocalTicks() + 30000
        self.ts2 = Timestamp( ticks )
        self.ts2 -= 30000
        assertEquals( 'Ticks',
                      self.ts2.getLocalTicks() ,
                      self.ts1.getLocalTicks() )
                      
    def testLTOne( self ):
        assertEquals( '<' ,
                      self.ts1 < self.ts2,
                      True )
    
    def testLTTwo( self ):
        assertEquals( '<' ,
                      self.ts2 < self.ts3,
                      False )
                      
    def testLEOne( self ):
        assertEquals( '<=' ,
                      self.ts1 <= self.ts2,
                      True )
    
    def testLETwo( self ):
        assertEquals( '<=' ,
                      self.ts2 <= self.ts3,
                      False )
                      
    def testGTOne( self ):
        assertEquals( '>' ,
                      self.ts2 > self.ts1,
                      True )
    
    def testGTTwo( self ):
        assertEquals( '>' ,
                      self.ts3 > self.ts2,
                      False )                      

    def testGEOne( self ):
        assertEquals( '>=' ,
                      self.ts2 >= self.ts1,
                      True )
    
    def testGETwo( self ):
        assertEquals( '>=' ,
                      self.ts3 >= self.ts2,
                      False ) 
                      
    def testEQOne( self ):
        assertEquals( '==' ,
                      self.ts2 == self.ts1,
                      False )
    
    def testEQTwo( self ):
        assertEquals( '==' ,
                      self.ts1 == self.ts3,
                      True ) 
                      
    def testNEOne( self ):
        assertEquals( '==' ,
                      self.ts2 != self.ts1,
                      True )
    
    def testNETwo( self ):
        assertEquals( '==' ,
                      self.ts1 != self.ts3,
                      False ) 
Ejemplo n.º 45
0
 def setUp( self ):
     self.ts1 = Timestamp( format1 )
     self.ts2 = Timestamp()
     self.ts3 = Timestamp( format1 )