def __init__(self, dot_op, iter_max=1000, eps_min=1.0e-10, logger=logger_basic): self.dot_op = dot_op self.iter_max = iter_max self.eps_min = eps_min self.logger = logger self.watch = util.stopwatch()
def solve(self, soltn, tpn_map): self.watch = util.stopwatch() self.iter_tot = 0 self.prev_eps = None logger = (lambda iter, eps, stage=self.bstage, **kwargs: self.log( stage, iter, eps, **kwargs)) monitor = cd_monitors.monitor_basic(self.opfilt.dot_op(), logger=logger, iter_max=self.bstage.iter_max, eps_min=self.bstage.eps_min) tpn_alm = self.opfilt.calc_prep(tpn_map, self.s_cls, self.n_inv_filt) fwd_op = self.opfilt.fwd_op(self.s_cls, self.n_inv_filt) cd_solve.cd_solve(soltn, tpn_alm, fwd_op, self.bstage.pre_ops, self.opfilt.dot_op(), monitor, tr=self.bstage.tr, cache=self.bstage.cache) self.opfilt.apply_fini(soltn, self.s_cls, self.n_inv_filt)
def levelup(client, levels, is_natural=True, warp=True): def run(): change_stats(client, levels) change_skills(client, levels) change_keymap(client, levels) change_equips(client, levels) send_levelup(client) change_job(client, levels) if 1 in levels: handle_rebirth(client, is_natural) if warp and change_map(client, levels): stopwatch(0.8, run) return True else: run()
def use_skill(client, skill_id, wzskill): char = client.s.char if skill_id in char.skills.active: char.skills.active.pop(skill_id).cancel() data = skilldata.get(skill_id) if data is not None: types, attrs = data attrs = map(wzskill.get, attrs) client.send('use_skill', skill_id, wzskill.time * 1000, types, attrs) char.skills.active[skill_id] = stopwatch(wzskill.time, end_skill, client, skill_id, is_timer=True) return True
def solve( self, soltn, tpn_map ): self.watch = util.stopwatch() self.iter_tot = 0 self.prev_eps = None logger = (lambda iter, eps, stage=self.bstage, **kwargs : self.log(stage, iter, eps, **kwargs)) monitor = cd_monitors.monitor_basic(self.opfilt.dot_op(), logger=logger, iter_max=self.bstage.iter_max, eps_min=self.bstage.eps_min) tpn_alm = self.opfilt.calc_prep(tpn_map, self.s_cls, self.n_inv_filt) fwd_op = self.opfilt.fwd_op(self.s_cls, self.n_inv_filt) cd_solve.cd_solve( soltn, tpn_alm, fwd_op, self.bstage.pre_ops, self.opfilt.dot_op(), monitor, tr=self.bstage.tr, cache=self.bstage.cache ) self.opfilt.apply_fini( soltn, self.s_cls, self.n_inv_filt )
""" Created on Mar 31, 2012 @author: erik """ import ctypes import os import util if __name__ == "__main__": # A simple stopwatch I made for measuring execution time. sw = util.stopwatch() LIB_FILENAME = "libcloudiness.so" LIBPATH = os.path.join(os.getcwd(), "lib", LIB_FILENAME) # Load the shared library. This library has been compiled with some # particular flags. Look at the Makefile accompanying it. libcloudiness = ctypes.cdll.LoadLibrary(LIBPATH) # Download the image image = util.download_image("http://i.imgur.com/MDsb5.jpg") image_size = len(image) # This is one of the functions declared in the library f = libcloudiness.calc_cloudiness f.restype = ctypes.c_double # I think all functions default to int as return type, therefore I set this # to match the actual return type of the C function (using ctypes.c_<type>) # cloudiness.restype = ctypes.c_double
def do_GET(self): try: client_hostname = self.headers["Client-Hostname"] # get permission from C3Server if not self.server.get_serve_permission(client_hostname): # send 503 service unavailable response self.send_response(httplib.SERVICE_UNAVAILABLE, self.responses[httplib.OK][0]) msg = "Can't serve you. C3 told me not to." self.send_header("Content-Type", "text/plain") self.send_header("Content-Length", len(msg)) self.end_headers() self.wfile.write(msg) return # permission was granted! proceed # read parameters which are found as headers in the HTTP request cloudiness_forecast_time = self.headers["Cloudiness-Forecast-Time"] cloudiness_sample_size = self.headers["Cloudiness-Sample-Size"] sample_size = int(cloudiness_sample_size) forecast_dt = datetime.datetime.strptime(cloudiness_forecast_time, '%Y-%m-%dT%H:%M') if forecast_dt.hour == 23 and forecast_dt.minute == 45: pass # sample size defines how many surrounding images should be used # when doing the cloudiness calculation and finding an average if sample_size > 0: sample_times = date_helper.get_surrounding_datetimes_n_years( forecast_dt, constants.WTHR_IMG_FREQ_MINS, constants.WTHR_IMG_FREQ_MINS * sample_size, sample_size) # OLD VARIANT: ONLY SAMPLE SAME YEAR # sample_times = get_surrounding_datetimes(forecast_dt, # constants.WTHR_IMG_FREQ_MINS, # constants.WTHR_IMG_FREQ_MINS * sample_size) else: sample_times = [forecast_dt] sw = util.stopwatch() sw.start() # temporary dict to hold time/cloudiness key/value pairs t_c_temp_dict = {} # temporary dict to hold time/raw-image key/value pairs t_img_temp_dict = {} # for each sample time, check cache for cloudiness value # download image and calc if not there actual_image = None for t in sample_times: # Check cache. t_c_temp_dict[t] = self._getCachedCloudinessValue(t) if t_c_temp_dict[t] != None: continue # Cache hit! else: # No cigar. Have to download image and calculate. t_img_temp_dict[t] = self.server.dl_weather_image(t) if not t_img_temp_dict[t]: continue # couldn't get image if t == forecast_dt: actual_image = t_img_temp_dict[t] sw.stop() time_taken_imgdl = sw.getresult_ms() cloudiness_values = [] for c in t_c_temp_dict.itervalues(): if c != None: cloudiness_values.append(c) else: pass sw.start() # Calculate the cloudiness values for all images. img = None for t, img in t_img_temp_dict.iteritems(): if img: # Might be None, if the server replied 404 Not Found cloudiness_value = self.server.get_image_cloudiness(img) cloudiness_values.append(cloudiness_value) self._saveCloudinessValue(t, cloudiness_value) self._saveCloudyImage(cloudiness_value, img) sw.stop() time_taken_calc = sw.getresult_ms() if len(cloudiness_values) == 0: pass # the average cloudiness value cloudiness_value = float(sum(cloudiness_values) / len(cloudiness_values)) # TODO: find an image that matches the forecasted cloudiness value if not img: # first check image cache img = self._getCachedCloudyImage(cloudiness_value) if img: print "Reqtime: %s - Cloudy image cache hit!" % str(cloudiness_forecast_time) else: # actually have to download an image... while img == None: print "Reqtime: %s - Cache fail, having to DL..." % str(cloudiness_forecast_time) t = forecast_dt img = self.server.dl_weather_image(t) t = date_helper.add_minutes(t, constants.WTHR_IMG_FREQ_MINS) forecasted_image = img if not forecasted_image: print "I have no image to return!" raise Exception actual_cloudiness = self._getCachedCloudinessValue(forecast_dt) # in case of cache-hit, the actual image is not in memory if actual_image == None: sw.start() actual_image = self.server.dl_weather_image(forecast_dt) sw.stop() time_taken_imgdl += sw.getresult_ms() if actual_image == None: actual_image = self._getCachedCloudyImage(actual_cloudiness) if actual_image == None: actual_image = forecasted_image # send reponse back to client # the payload is a blob containing both images.. their sizes # defined in HTTP headers Blob1-Size and Blob2-Size self.send_response(httplib.OK, self.responses[httplib.OK][0]) self.send_header("Content-Type", "ela035/2ximageblob") self.send_header("Content-Length", len(forecasted_image) + len(actual_image)) self.send_header("Blob1-Size", len(forecasted_image)) self.send_header("Blob2-Size", len(actual_image)) self.send_header("Cloudiness-Forecast", str(cloudiness_value)) self.send_header("Actual-Cloudiness", str(actual_cloudiness)) self.send_header("Image-Download-Time", str(time_taken_imgdl)) self.send_header("Calc-Time", str(time_taken_calc)) self.end_headers() self.wfile.write(forecasted_image) self.wfile.write(actual_image) self.server.imgdltime = time_taken_imgdl self.server.cloudcalctime = time_taken_calc except Exception as e: print e msg = "Internal server error :-(" self.send_response(httplib.INTERNAL_SERVER_ERROR, self.responses[httplib.INTERNAL_SERVER_ERROR][0]) self.send_header("Content-Length", len(msg)) self.end_headers() self.wfile.write(msg)