I have a list of events. Each event as a Start and End which are both datetime objects. I now want a list of "spare time" events which is created off the existing events, for the purposes of display. I hope that makes sense. This is the best I can come up with so far. Any help appreciated!
class Event():
Start = datetime.datetime
End = datetime.datetime
Reserved = boolean
# create a couple of events with some spare time between them...
ev1 = Event()
ev1.Start = datetime.datetime(2011,1,1,5)
ev1.End = datetime.datetime(2011,1,1,6)
ev1.Reserved = True
ev2 = Event()
ev2.Start = datetime.datetime(2011,1,1,8)
ev2.End = datetime.datetime(2011,1,1,9)
ev2.Reserved = True
events = [ev1,ev2]
# sort the events list...
events.sort(key=lambda e: e.Start)
# create a 'spare time' event based on the existing events...
allevents = []
for event in events:
spare = Event()
spare.Start = events[events.index(event)-1].End+datetime.timedelta(seconds=1)
spare.End = event.Start-datetime.timedelta(seconds=1)
spare.Reserved = False
allevents.append(spare)
allevents.append(event)
allevents.sort(key=lambda e: e.Start)
for event in allevents:
print str(event.Start), str(event.End), event.Reserved
So, this works, but I have yet to figure out how to stop and start the spare time at midnight, but before I do that, I want to know if I'm heading in the right direction to start with. Or is there a much better way?? (There usually is!)..
MFB
I would do it more this way - just to give you some more concepts (it needs more error handling, but for now it should just show the idea):
from datetime import datetime
class Venue(object):
"""Collection of events"""
def __init__(self):
self.events_list = []
def add_events(self,*args):
for e in args:
self.events_list.append(e)
def sort_list(self):
self.events_list.sort(key=lambda e: e.start_date)
def find_spare_times(self):
self.sort_list()
for event in self.events_list:
yield event.start_date
yield event.end_date
def add_spare_events(self):
l = list(self.find_spare_times())
print l
new_start,new_end = None, None
for e in l[1:]:
if new_start:
new_end = e
self.add_events(Event(new_start,new_end,res=False))
new_start = None
else:
new_start = e
new_end = None
def get_events(self):
return self.events_list
class Event(object):
"""Holds the data for an individual event"""
def __init__(self,start_date, end_date,res=True):
self.start_date = start_date
self.end_date = end_date
self.reserved = res
# create a couple of events with some spare time between them...
def main():
ev1 = Event(datetime(2011,1,1,5),datetime(2011,1,1,6))
ev2 = Event(datetime(2011,1,1,8),datetime(2011,1,1,9))
v = Venue()
v.add_events(ev1,ev2)
print v.get_events()
v.add_spare_events()
print v.get_events()
if __name__ == "__main__":
main()
Related
For my first self directed python project I have chosen to try to get some stock data into a Series. I have created a method that creates days when the market is not open(nonbizday) and another method that creates datetime.dates going backward from the day the script is run(date_maker). Within the method that goes back one day, it is supposed to check if that day is on the list created by .nonbizday(). It will not remove any days and I cant figure out why.
Heres the two methods:
(the nobizday is saved as an attribute to the class the methods are within... if that makes sense)
def nonbizday(self):
newnew = datetime.date(year=2021, day=2, month=1)
nobiz = [datetime.date(year=2021, day=2, month=1)]
while newnew.year == 2021:
newyears = datetime.date(year=2021, day=1, month=1)
newnew = newnew + datetime.timedelta(days=1)
nobiz.append(newnew)
newnew = newnew + datetime.timedelta(days=6)
nobiz.append(newnew)
return nobiz
def date_maker(self):
btime = self.data['Meta Data']['3. Last Refreshed']
btime = datetime.datetime.strptime(btime, dtnotime)
btime = btime.date()
benchlist = []
for i in range(10):
motime = i + 1
benchie = btime - (datetime.timedelta(days=motime))
# benchie = benchie.date()
adding = False
print(benchie)
for offdays in self.nobiz:
print(offdays)
if benchie == offdays:
adding = False
else:
adding = True
print(adding)
print(isinstance(benchie, datetime.date))
print(isinstance(offdays, datetime.date))
if adding == True:
benchlist.append(benchie)
return benchlist
I know this is probably very crude to the more fluent pythonista, any other suggestions are also helpful
I have a problem that involves collecting data continuously from multiple sources.
My setup as it is currently, writes each data entry from each source to a MySQL db, and then, with another python program, does Select's that bring all the data together. I need to make INSERT's at roughly 1000/second and as it is my SELECT's can take 15-20 seconds each.
The whole process takes so long the data is obsolete before I get to do anything useful with it.
I have created a toy example to try and demonstrate what I am looking for.
program 1 'generateClasses':
import time
import random
from datetime import datetime
class Race:
def __init__(self,name):
hist = {}
now = datetime.now()
self.name = name
self.now = now
hist[now] = 0
self.v = 0
self.hist = hist # example variable's.
def update(self,name,v):
now = datetime.now()
hist = self.hist
hist[now] = v
self.v = v
self.now - now
self.hist = hist
class Looper:
def __init__(self,name):
self.a = Race(name)
def loop(self,name):
# simulating the streaming API
while True:
v = self.a.v
v += 1
self.a.update(name,v)
print(a,datetime.now(),v) # can i access this stream using the location displayed with the print(a)?
time.sleep(0.1) # this should be more like time.sleep(0.001)
def pickData(self,name):
v = self.v
self.loop(name)
print('The state at {} {} = '.format(self.now,self.v))
return self.hist
if __name__ == "__main__":
x = 'Some_ID'
a = Looper(x)
a.loop(x)
program 2:
from generateClasses import Looper
from datetime import datetime
import time
start_time = int((datetime.now() - datetime(1970, 1, 1)).total_seconds())
print(start_time)
x = 'Some_orher_ID'
a = Looper(x)
print('this will print')
a.loop(x)
print('this wont ever print')
a.pickdata(x)
# this last section is the functionality i am looking for in this program, but, as it is, it will never run.
x = ‘Some_ID’
while True:
now_time = int((datetime.now() - datetime(1970, 1, 1)).total_seconds())
print(start_time)
if int(now_time-start_time) == 10:
a.pickData(x)
# b.pickData(x)
# c.pickData(x)
# d.pickData(x)
# make further actions.....
What happens currently in my examples is that it creates its own loop using the class structure from the first program.
What I want it to do is call the the pickData() method from program 2 at timely intervals of my choosing on a loop running in another program.
Is my best option picking a db located in memory and getting a faster computer?
Maybe something can be done with the object location shown when you print the instance name?
I have uploaded to github if anybody fancies it..
I would be grateful of any suggestions.
also, recommendations for further reading would be appreciated also.
class Return_Thread_Value(object):
def __init__(self,target = None,args = (),**kwargs):
self._que = queue.Queue()
self._t = Thread(target = lambda q,arg1,kwargs1: q.put(target(*arg1,**kwargs1)),
args=(self._que,args,kwargs), )
self._t.start()
def Return_Value(self):
self._t.join()
return self._que.get()
Thread_1 = Return_Thread_Value(target = Walking_Inputs,args = (
WINDOW,CLOCK,Hero,FRAME,INTERACTING,TOP_SCREEN,POSITION_DATA,BACKGROUND,
FOREGROUND_OPAQUE,FOREGROUND_TRANSLUCENT,INPUT,INPUT_SHIFT,PROMPT_SHIFT,Input,
ENTERED))
INTERACTING,TOP_SCREEN,Input,ENTERED = Thread_1.Return_Value()
Thread_2 = Return_Thread_Value(target = Key_Inputs,args = (
WINDOW,ENTERED,PROMPT_SHIFT,INPUT,INPUT_SHIFT,CAPITAL,Input))
ENTERED,PROMPT_SHIFT,INPUT,INPUT_SHIFT,CAPITAL,Input = Thread_2.Return_Value()
Trying to run two functions, one that lets you walk about the village and another that accepts key inputs, both functions are running, but I'm not sure if the values are being returned.
They threads will each have their own scopes, to pass data back to their parent thread, the easiest way is to define any object for example a dict return_value = {} and pass that as an arg to your thread.
Set the value you want to return as a key in the dict (
return_value['thread1return']='something'), and you should be able to access it in the parent thread
I have a python 2.7 script which is getting pretty unweildly.
The script runs constantly, in each loop it checks a few things, compares them to the previous run and makes some decisions.
The biggest problem I have is with the variables, I have half a dozen and I am limited in how I use them due to scope.
eg;
import time
import os
LOG_FILE = "/var/log/sc.log"
CHECK_FILE_TMP = "/tmp/myfile"
CHECK_FILE_USR = "/home/snoppy/testfile"
CHECK_FILE_TMP_TIME_INTERVAL = 20
CHECK_FILE_USR_TIME_INTERVAL = 30
def main():
try:
last_file_tmp_size = 0
last_file_usr_size = 0
last_file_tmp_mtime = 0
last_file_usr_mtime = 0
last_file_tmp_check_time = 0
last_file_usr_check_time = 0
tmp_file_changed = False
usr_file_changed = False
loop_start_time = 0
print "Starting loop"
while True:
loop_start_time = time.time()
if (time.time() - last_file_tmp_check_time > CHECK_FILE_TMP_TIME_INTERVAL):
tmp_file_changed = checkFileChanged(CHECK_FILE_TMP, last_file_tmp_size, last_file_tmp_mtime)
last_file_tmp_size = getFileSize(CHECK_FILE_TMP)
last_file_tmp_mtime = getFileMTime(CHECK_FILE_TMP)
if(tmp_file_changed):
logChange(CHECK_FILE_TMP, last_file_tmp_size, last_file_tmp_mtime)
last_file_tmp_check_time = time.time()
....
....
sleep(1)
...
So thats sort of what I am dealing with.
I have local variables, which I seem to be stuck with, I have to pass them around into functions - I don't want to call them global.
Ideally.... if I could get a main() function like
try:
checkFile(CHECK_FILE_TMP)
checkFile(CHECK_FILE_USR)
sleep(0.1)
except:
...
as the main is so big! and I have to pass around the variables everywhere... it feels like the main function is so bloated!
Perhaps.... I might have to go for a class?
You need to identify the parts of your program that can be abstracted from their specific details and reused multiple times.
This is an art, don't expect it to be easy. You can find many books on Object oriented programming that explain the basics and give you some direction.
Here's just a quick example:
class FileChecker(object):
def __init__(self, path, interval):
self.path = path
self.interval = interval
self.last_size = None
self.last_mtime = None
self.last_check_time = None
def hasChanged(self):
...
def logChange(self):
...
class MultiFileChecker(object):
DELAY = 1
def __init__(self):
self.files = []
self.loop_start_time = 0
def addFile(self, f):
self.files.append(f)
def loop(self):
try:
print "Starting loop"
while True:
now = time.time()
self.loop_start_time = now
for f in self.files:
if now - f.last_check_time > f.interval:
if f.hasChanged():
f.logChange()
...
sleep(self.DELAY)
...
if __name__ == '__main__':
checker = MultiFileChecker()
checker.add(FileChecker("/var/log/sc.log", 10))
checker.add(FileChecker("/tmp/myfile", 20))
checker.add(FileChecker("/home/snoppy/testfile", 30))
checker.loop()
I'm trying code nearly identical to the example from the manual to enable a download counter but I get an exception:
File "/media/Lexar/montao/wwwblob/handler.py", line 117, in FileInfo
download_count = db.IntegerProperty(required=True, count=0) TypeError: init() got an unexpected keyword argument 'count'
Here's the code I try to run:
from google.appengine.ext import deferred
from google.appengine.runtime import DeadlineExceededError
class Mapper(object):
# Subclasses should replace this with a model class (eg, model.Person).
KIND = None
# Subclasses can replace this with a list of (property, value) tuples to filter by.
FILTERS = []
def __init__(self):
self.to_put = []
self.to_delete = []
def map(self, entity):
"""Updates a single entity.
Implementers should return a tuple containing two iterables (to_update, to_delete).
"""
return ([], [])
def finish(self):
"""Called when the mapper has finished, to allow for any final work to be done."""
pass
def get_query(self):
"""Returns a query over the specified kind, with any appropriate filters applied."""
q = self.KIND.all()
for (prop, value) in self.FILTERS:
q.filter('%s =' % prop, value)
q.order('__key__')
return q
def run(self, batch_size=100):
"""Starts the mapper running."""
self._continue(None, batch_size)
def _batch_write(self):
"""Writes updates and deletes entities in a batch."""
if self.to_put:
db.put(self.to_put)
self.to_put = []
if self.to_delete:
db.delete(self.to_delete)
self.to_delete = []
def _continue(self, start_key, batch_size):
q = self.get_query()
# If we're resuming, pick up where we left off last time.
if start_key:
q.filter('__key__ >', start_key)
# Keep updating records until we run out of time.
try:
# Steps over the results, returning each entity and its index.
for (i, entity) in enumerate(q):
(map_updates, map_deletes) = self.map(entity)
self.to_put.extend(map_updates)
self.to_delete.extend(map_deletes)
# Do updates and deletes in batches.
if (i + 1) % batch_size == 0:
self._batch_write()
# Record the last entity we processed.
start_key = entity.key()
self._batch_write()
except DeadlineExceededError:
# Write any unfinished updates to the datastore.
self._batch_write()
# Queue a new task to pick up where we left off.
deferred.defer(self._continue, start_key, batch_size)
return
self.finish()
class FileInfo(db.Model):
blob = blobstore.BlobReferenceProperty(required=True)
download_count = db.IntegerProperty(required=True, count=0)
uploaded_by = db.UserProperty(required=True)
uploaded_at = db.DateTimeProperty(required=True, auto_now_add=True)
class DailyTotal(db.Model):
date = db.DateProperty(required=True, auto_now_add=True)
file_count = db.IntegerProperty(required=True)
download_count = db.IntegerProperty(required=True)
class DownloadCountMapper(Mapper):
KIND = FileInfo
def __init__(self):
self.file_count = 0
self.download_count = 0
def map(self, file):
self.file_count += 1
self.download_count += file.download_count
def finish(self):
total = DailyTotal(file_count=self.file_count,
download_count=self.download_count)
total.put()
Can you tell me what I should do?
Thank you
This line is the culprit:
download_count = db.IntegerProperty(required=True, count=0)
The IntegerProperty constructor doesn't know what to do with count. Maybe you meant this:
download_count = db.IntegerProperty(required=True, default=0)