class CommentIsFree(handlers.EmailTemplate): recognized_versions = ['v1'] most_shared_data_source = ds.MostSharedDataSource( most_shared_fetcher=MostSharedFetcher(ophan_client, section='commentisfree', country='au'), multi_content_data_source=ds.MultiContentDataSource( client=mr.client, name='most_shared'), shared_count_interpolator=ds.MostSharedCountInterpolator(), result_decorator=partial(add_comment_counts, discussion_client)) data_sources = immutable.make_dict({ 'v1': { 'cif_most_shared': most_shared_data_source, }, }) priority_list = immutable.make_dict({ 'v1': [ ('cif_most_shared', 5), ], }) template_names = immutable.make_dict({ 'v1': 'au/comment-is-free/v1', })
def __postproc_fn(reg): d = data.copy() d['registered_coordinates'] = reg # First, unproject the map reg_map_3dx = d['map'].unproject(reg).T reg_3dx = np.array(d['initial_registration'].coordinates, copy=True) reg_3dx[d['map'].vertex_labels] = reg_map_3dx final_reg = Registration(tohem.topology, reg_3dx) d['finished_registration'] = final_reg # Now, if need be, unresample the points: d['registration'] = d['unresample_function'](final_reg) # now convert the sub points into retinotopy points rmesh = useHemi.registration_mesh(d['registration']) pred = np.asarray( [(p,e,l) if rl > 0 and rl < 4 and e <= max_predicted_eccen else (0.0, 0.0, 0) for (p,e,l) in zip(*model.cortex_to_angle(rmesh)) for rl in [round(l)]]).T pred = (np.asarray(pred[0], dtype=np.float32), np.asarray(pred[1], dtype=np.float32), np.asarray(pred[2], dtype=np.int32)) for i in (0,1,2): pred[i].flags.writeable = False pred = make_dict({p:v for (p,v) in zip(['polar_angle', 'eccentricity', 'V123_label'], pred)}) d['prediction'] = pred rmesh.prop(pred) d['registered_mesh'] = rmesh return make_dict(d)
class Morning(handlers.EmailTemplate): recognized_versions = immutable.make_list('v1') data_sources = immutable.make_dict({ 'v1': { 'top_stories': ds.TopStoriesDataSource(clientAUS), 'world_news': dss.news.WorldNews(clientAUS), 'australian_news': dss.au.News(clientAUS), } }) priority_list = immutable.make_dict({ 'v1': [ ('top_stories', 5), ('australian_news', 7), ('world_news', 5), ], }) template_names = immutable.make_dict(v1='au/morning/v1') def additional_template_data(self): sydney_tz = pytz.timezone('Australia/Sydney') date_format = "%A %d %B %Y" return immutable.make_dict( sydney_date=datetime.datetime.now(sydney_tz).strftime(date_format))
def __init__(self, settable_vals, const_vals, lazy_vals, **opts): self.__dict__['_persistent'] = False self.__dict__['_settable_vals'] = make_dict(settable_vals).using( options=lambda s, o: make_dict(o)) self.__dict__['_const_vals'] = make_dict(const_vals) self.__dict__['_lazy_vals'] = lazy_vals self.__dict__['_lazy_deps'] = self.__lazy_deps() self.options = opts return self
def __init__(self, settable_vals, const_vals, lazy_vals, **opts): self.__dict__['_persistent'] = False self.__dict__['_settable_vals'] = make_dict(settable_vals).using( options=lambda s,o: make_dict(o)) self.__dict__['_const_vals'] = make_dict(const_vals) self.__dict__['_lazy_vals'] = lazy_vals self.__dict__['_lazy_deps'] = self.__lazy_deps() self.options = opts return self
class CommentIsFree(handlers.EmailTemplate): recognized_versions = immutable.make_list('v1', 'v2', 'v3') ad_tag = 'email-speakers-corner' ad_config = {'leaderboard': 'Top'} ophan_client = OphanClient(mr.ophan_base_url, mr.ophan_key) most_shared_data_source = ds.MostSharedDataSource( most_shared_fetcher=MostSharedFetcher(ophan_client, section='commentisfree'), multi_content_data_source=ds.MultiContentDataSource( client=client, name='most_shared'), shared_count_interpolator=ds.MostSharedCountInterpolator()) discussion_client = DiscussionClient(mr.discussion_base_url) most_commented_data_source = ds.MostCommentedDataSource( discussion_fetcher=DiscussionFetcher(discussion_client, 'commentisfree'), multi_content_data_source=ds.MultiContentDataSource( client=client, name='most_commented'), comment_count_interpolator=ds.CommentCountInterpolator()) data_sources = immutable.make_dict({ 'v1': { 'uk_opinion_front': container.for_id('uk/commentisfree/regular-stories'), 'cif_cartoon': ds.CommentIsFreeCartoonDataSource(client), }, 'v3': { 'cif_most_shared': most_shared_data_source, 'cif_cartoon': ds.CommentIsFreeCartoonDataSource(client), }, 'v2': { 'cif_most_commented': most_commented_data_source, 'cif_cartoon': ds.CommentIsFreeCartoonDataSource(client), } }) priority_list = { 'v3': [('cif_cartoon', 1), ('cif_most_shared', 5)], 'v2': [('cif_cartoon', 1), ('cif_most_commented', 5)], 'v1': [ ('uk_opinion_front', 10), ('cif_cartoon', 1), ], } template_names = immutable.make_dict({ 'v3': 'comment-is-free/v3', 'v2': 'comment-is-free/v2', 'v1': 'comment-is-free/v1', })
class Opinion(handlers.EmailTemplate): recognized_versions = ['v1', 'v2', 'v3'] most_shared_data_source = ds.MostSharedDataSource( most_shared_fetcher=MostSharedFetcher(ophan_client, section='commentisfree', country='us'), multi_content_data_source=ds.MultiContentDataSource( client=mr.client, name='most_shared'), shared_count_interpolator=ds.MostSharedCountInterpolator(), result_decorator=partial(add_comment_counts, discussion_client)) latest_us_opinion = dss.general.ItemDataSource('us/commentisfree', production_office='us') data_sources = immutable.make_dict({ 'v1': { 'cif_most_shared': most_shared_data_source, }, 'v2': { 'cif_most_shared': most_shared_data_source, 'us_opinion': container.for_id('us-alpha/contributors/feature-stories') }, 'v3': { 'cif_most_shared': most_shared_data_source, 'latest_us_opinion': latest_us_opinion, } }) priority_list = immutable.make_dict({ 'v1': [ ('cif_most_shared', 5), ], 'v2': [ ('us_opinion', 3), ('cif_most_shared', 5), ], 'v3': [ ('cif_most_shared', 2), ('latest_us_opinion', 3), ] }) template_names = immutable.make_dict({ 'v1': 'us/opinion/v1', 'v2': 'us/opinion/v2', 'v3': 'us/opinion/v3', })
class FashionStatement(handlers.EmailTemplate): recognized_versions = ['v1', 'v2', 'v3'] ad_tag = 'email-fashion-statement' ad_config = {'leaderboard_v1': 'Top', 'leaderboard_v2': 'Bottom'} base_data_sources = immutable.make_dict({ 'fashion_gallery': dss.fashion.FashionGalleryDataSource(client), 'fashion_most_viewed': dss.fashion.FashionMostViewedDataSource(client), 'jcm_on_fashion': dss.fashion.JCMOnFashion(client) }) data_sources = { 'v1': base_data_sources.using( fashion_news=dss.fashion.FashionNewsDataSource(client), fashion_blog=dss.fashion.FashionBlogDataSource(client), fashion_network=dss.fashion.FashionNetworkDataSource(client), ), 'v3': base_data_sources.using( fashion_picks=dss.fashion.FashionEditorsPicksDataSource(client), fashion_sali=dss.fashion.FashionSaliHughesDataSource(client), fashion_stylewatch=dss.fashion.FashionStylewatchDataSource(client), fashion_most_viewed=dss.fashion.FashionMostViewedDataSource( client), ) } data_sources['v2'] = data_sources['v1'] priority_list = { 'v1': [('jcm_on_fashion', 1), ('fashion_most_viewed', 6), ('fashion_news', 3), ('fashion_blog', 6), ('fashion_network', 6), ('fashion_gallery', 1)], 'v3': [('jcm_on_fashion', 1), ('fashion_sali', 1), ('fashion_stylewatch', 1), ('fashion_picks', 5), ('fashion_most_viewed', 6), ('fashion_gallery', 1)] } priority_list['v2'] = priority_list['v1'] template_names = immutable.make_dict({ 'v1': 'fashion/fashion-statement/v1', 'v2': 'fashion/fashion-statement/v2', 'v3': 'fashion/fashion-statement/v3', })
class SleeveNotes(handlers.EmailTemplate): recognized_versions = ['v1', 'v2', 'v3'] ad_tag = 'email-sleeve-notes' ad_config = { 'leaderboard': 'Top', 'leaderboard2': 'Bottom', } music_editors_picks = dss.general.ItemDataSource(content_id='music', show_editors_picks=True, tags=['-tone/news']) base_data_sources = immutable.make_dict({ 'music_most_viewed': dss.general.ItemDataSource(content_id='music', show_most_viewed=True), 'music_picks': music_editors_picks, 'music_blog': dss.general.ItemDataSource(content_id='music/musicblog'), 'music_watch_listen': dss.general.ItemDataSource(content_id='music', tags=['(type/video|type/audio)']), 'music_further': music_editors_picks, }) data_sources = immutable.make_dict({ 'v1': base_data_sources, 'v2': base_data_sources, 'v3': base_data_sources, }) priorities = immutable.make_list( ('music_most_viewed', 3), ('music_picks', 5), ('music_blog', 5), ('music_watch_listen', 5), ('music_further', 3)) priority_list = immutable.make_dict({ 'v1': priorities, 'v2': priorities, 'v3': priorities }) template_names = immutable.make_dict({ 'v1': 'culture/sleeve-notes/v1', 'v2': 'culture/sleeve-notes/v2', 'v3': 'culture/sleeve-notes/v3', })
def __init__(self, **opts): # start by getting the proper parameters params = self.__check_parameters(opts) # Now, do the translations that we need... if isinstance(params['scale'], Number) or np.issubdtype(type(params['scale']), np.float): params['scale'] = [params['scale'], params['scale']] if (isinstance(params['shear'], Number) or np.issubdtype(type(params['shear']), np.float)) \ and params['shear'] == 0: params['shear'] = [[1, 0], [0, 1]] elif params['shear'][0][0] != 1 or params['shear'][1][1] != 1: raise RuntimeError('shear matrix [0,0] elements and [1,1] elements must be 1!') if ((isinstance(params['center'], Number) or np.issubdtype(type(params['center']), np.float)) and params['center'] == 0): params['center'] = [0.0, 0.0] self.__dict__['parameters'] = make_dict(params) # Okay, let's construct the object... self.__dict__['_java_object'] = java_link().jvm.nben.neuroscience.SchiraModel( params['A'], params['B'], params['lambda'], params['psi'], params['v1size'], params['v2size'], params['v3size'], params['hv4size'], params['v3asize'], params['center'][0], params['center'][1], params['scale'][0], params['scale'][1], params['shear'][0][1], params['shear'][1][0])
def get(self, path="film"): logging.info(path) path_mapping = immutable.make_dict({ 'film': container.for_id('6d84cd8d-d159-4e9a-ba2f-8852528d2d03'), 'uk/opinion/v1': container.for_id('uk/commentisfree/regular-stories'), 'film/v1': ds.FilmTodayLatestDataSource(mr.client), }) if not path in path_mapping.keys(): webapp2.abort(404, "Path {0} not mapped to a datasource".format(path)) return stories_data_source = path_mapping[path] data_sources = {'stories': stories_data_source} priority_list = [('stories', 1)] template_data = {} retrieved_data = handlers.EmailTemplate.fetch_all(data_sources) trail_block = deduplication.build_unique_trailblocks(retrieved_data,priority_list) stories = trail_block.get('stories') headlines = [read_headline(s) for s in stories] if headlines: headline = headlines[0] template_data['headline'] = headline template = handlers.jinja_environment.get_template('headline.html') self.response.out.write(template.render(template_data))
def __init__(self, devices): for device_name, device in devices.items(): for attr in DEVICE_REQUIRED_ATTRS: assert hasattr(device, attr), f'{deivce_name} does not support the mandatory {attr}' self._devices = make_dict(devices) CollectionPackerMixin.__init__(self, tuple(self._devices.values())) # TODO: Unless we can guarantee the order of values, this approach is flawed self._group_lookup = {device_name: (device_name, ) for device_name in self._devices.keys()}
class Sport(handlers.EmailTemplate): recognized_versions = ['v1'] data_sources = immutable.make_dict({ 'v1': { 'au_sport': au.SportDataSource(client), 'uk_sport': dss.sport.UK(client), } }) priority_list = immutable.make_dict( {'v1': [('au_sport', 6), ('uk_sport', 4)]}) template_names = immutable.make_dict({ 'v1': 'au/sport/v1', })
def __init__(self, triangles, registrations): triangles = np.asarray(triangles) self.triangles = triangles if triangles.shape[1] == 3 else triangles.T self.vertex_count = np.max(triangles.flatten()) self.registrations = make_dict({name: Registration(self, np.asarray(coords)) for (name, coords) in registrations.iteritems() if coords is not None})
def __init__(self, **opts): # start by getting the proper parameters params = self.__check_parameters(opts) # Now, do the translations that we need... if isinstance(params['scale'], Number) or np.issubdtype( type(params['scale']), np.float): params['scale'] = [params['scale'], params['scale']] if (isinstance(params['shear'], Number) or np.issubdtype(type(params['shear']), np.float)) \ and params['shear'] == 0: params['shear'] = [[1, 0], [0, 1]] elif params['shear'][0][0] != 1 or params['shear'][1][1] != 1: raise RuntimeError( 'shear matrix [0,0] elements and [1,1] elements must be 1!') if ((isinstance(params['center'], Number) or np.issubdtype(type(params['center']), np.float)) and params['center'] == 0): params['center'] = [0.0, 0.0] self.__dict__['parameters'] = make_dict(params) # Okay, let's construct the object... self.__dict__['_java_object'] = java_link( ).jvm.nben.neuroscience.SchiraModel( params['A'], params['B'], params['lambda'], params['psi'], params['v1size'], params['v2size'], params['v3size'], params['hv4size'], params['v3asize'], params['center'][0], params['center'][1], params['scale'][0], params['scale'][1], params['shear'][0][1], params['shear'][1][0])
def get(self, path="film"): logging.info(path) path_mapping = immutable.make_dict({ 'film': container.for_id('6d84cd8d-d159-4e9a-ba2f-8852528d2d03'), 'uk/opinion/v1': container.for_id('uk/commentisfree/regular-stories'), 'film/v1': ds.FilmTodayLatestDataSource(mr.client), }) if not path in path_mapping.keys(): webapp2.abort(404, "Path {0} not mapped to a datasource".format(path)) return stories_data_source = path_mapping[path] data_sources = {'stories': stories_data_source} priority_list = [('stories', 1)] template_data = {} retrieved_data = handlers.EmailTemplate.fetch_all(data_sources) trail_block = deduplication.build_unique_trailblocks( retrieved_data, priority_list) stories = trail_block.get('stories') headlines = [read_headline(s) for s in stories] if headlines: headline = headlines[0] template_data['headline'] = headline template = handlers.jinja_environment.get_template('headline.html') self.response.out.write(template.render(template_data))
class LongReads(handlers.EmailTemplate): recognized_versions = ['v1'] data_sources = immutable.make_dict({ 'v1': { 'long_reads': dss.long_reads.Reads(mr.client), 'long_reads_audio': dss.long_reads.Audio(mr.client), } }) priority_list = immutable.make_dict( {'v1': [('long_reads', 3), ('long_reads_audio', 2)]}) template_names = immutable.make_dict({ 'v1': 'long_reads/v1', })
def __init__(self, cluster, session_context, session_logger, tests, min_port=ConsoleDefaults.TEST_DRIVER_MIN_PORT, max_port=ConsoleDefaults.TEST_DRIVER_MAX_PORT): # Set handler for SIGTERM (aka kill -15) # Note: it doesn't work to set a handler for SIGINT (Ctrl-C) in this parent process because the # handler is inherited by all forked child processes, and it prevents the default python behavior # of translating SIGINT into a KeyboardInterrupt exception signal.signal(signal.SIGTERM, self._propagate_sigterm) # session_logger, message logger, self.session_logger = session_logger self.cluster = cluster self.event_response = EventResponseFactory() self.hostname = "localhost" self.receiver = Receiver(min_port, max_port) self.session_context = session_context self.max_parallel = session_context.max_parallel self.results = TestResults(self.session_context, self.cluster) self.exit_first = self.session_context.exit_first self.main_process_pid = os.getpid() self.scheduler = TestScheduler(tests, self.cluster) self.test_counter = 1 self.total_tests = len(self.scheduler) # This immutable dict tracks test_id -> test_context self._test_context = pysistence.make_dict(**{t.test_id: t for t in tests}) self._test_cluster = {} # Track subcluster assigned to a particular TestKey self._client_procs = {} # track client processes running tests self.active_tests = {} self.finished_tests = {}
class Bookmarks(handlers.EmailTemplate): recognized_versions = immutable.make_list('v1', 'v2') ad_tag = 'email-bookmarks' ad_config = immutable.make_dict({ 'leaderboard': 'Top', 'leaderboard2': 'Bottom' }) base_data_sources = immutable.make_dict({ 'books_picks': dss.culture.BooksEditorsPicks(client), 'book_reviews': dss.culture.BookReviews(client), 'books_blog': dss.culture.BooksBlog(client), 'book_podcasts': dss.culture.BookPodcasts(client), 'books_most_viewed': dss.culture.BooksMostViewed(client), 'how_to_draw': dss.culture.HowToDraw(client), }) data_sources = immutable.make_dict({ 'v1': base_data_sources, 'v2': base_data_sources.using(talking_points=container.for_id( 'c04946d0-6483-4b29-ad3c-37bd2e2058c8')), }) priority_list = immutable.make_dict({ 'v1': immutable.make_list(('books_picks', 5), ('books_most_viewed', 3), ('book_reviews', 3), ('books_blog', 3), ('book_podcasts', 1), ('how_to_draw', 1)), 'v2': immutable.make_list(('books_picks', 5), ('books_most_viewed', 3), ('book_reviews', 3), ('talking_points', 6), ('book_podcasts', 1), ('how_to_draw', 1)), }) template_names = immutable.make_dict({ 'v1': 'culture/bookmarks/v1', 'v2': 'culture/bookmarks/v2', })
def test_reg_fx_fn(expect): app = Intercessor() app.reg_event_fx('set-bar')(lambda cfx, event: make_dict( {'db': cfx['db'].using(bar=event[1])})) app.dispatch(['set-bar', 10]) expect(app._db['bar']) == 10
def get_user_defined_globals(globals_str): """Parse user-defined globals into an immutable dict using globals_str :param globals_str Either a file, in which case, attempt to open the file and parse the contents as JSON, or a JSON string representing a JSON object. The parsed JSON must represent a collection of key-value pairs, i.e. a python dict. :return dict containing user-defined global variables """ if globals_str is None: return pysistence.make_dict() from_file = False if os.path.isfile(globals_str): # The string appears to be a file, so try loading JSON from file # This may raise an IOError if the file can't be read or a ValueError if the contents of the file # cannot be parsed. user_globals = json.loads(open(globals_str, "r").read()) from_file = True else: try: # try parsing directly as json if it doesn't seem to be a file user_globals = json.loads(globals_str) except ValueError as ve: message = str(ve) message += "\nglobals parameter %s is neither valid JSON nor a valid path to a JSON file." % globals_str raise ValueError(message) # Now check that the parsed JSON is a dictionary if not isinstance(user_globals, dict): if from_file: message = "The JSON contained in file %s must parse to a dict. " else: message = "JSON string referred to by globals parameter must parse to a dict. " message += "I.e. the contents of the JSON must be an object, not an array or primitive. " message += "Instead found %s, which parsed to %s" % ( str(user_globals), type(user_globals)) raise ValueError(message) # Use pysistence to create the immutable dict user_globals = pysistence.make_dict(**user_globals) return user_globals
def get_user_defined_globals(globals_str): """Parse user-defined globals into an immutable dict using globals_str :param globals_str Either a file, in which case, attempt to open the file and parse the contents as JSON, or a JSON string representing a JSON object. The parsed JSON must represent a collection of key-value pairs, i.e. a python dict. :return dict containing user-defined global variables """ if globals_str is None: return pysistence.make_dict() from_file = False if os.path.isfile(globals_str): # The string appears to be a file, so try loading JSON from file # This may raise an IOError if the file can't be read or a ValueError if the contents of the file # cannot be parsed. user_globals = json.loads(open(globals_str, "r").read()) from_file = True else: try: # try parsing directly as json if it doesn't seem to be a file user_globals = json.loads(globals_str) except ValueError as ve: message = ve.message message += "\nglobals parameter %s is neither valid JSON nor a valid path to a JSON file." % globals_str raise ValueError(message) # Now check that the parsed JSON is a dictionary if not isinstance(user_globals, dict): if from_file: message = "The JSON contained in file %s must parse to a dict. " else: message = "JSON string referred to by globals parameter must parse to a dict. " message += "I.e. the contents of the JSON must be an object, not an array or primitive. " message += "Instead found %s, which parsed to %s" % (str(user_globals), type(user_globals)) raise ValueError(message) # Use pysistence to create the immutable dict user_globals = pysistence.make_dict(**user_globals) return user_globals
class FilmToday(handlers.EmailTemplate): recognized_versions = ['v1', 'v2', 'v3', 'v4'] ad_tag = 'email-film-today' ad_config = immutable.make_dict({'leaderboard': 'Top'}) film_today_latest = immutable.make_dict( {'film_today_latest': ds.FilmTodayLatestDataSource(client)}) film_front = immutable.make_dict({ 'film_front': container.for_id('6d84cd8d-d159-4e9a-ba2f-8852528d2d03') }) data_sources = immutable.make_dict({ 'v1': film_today_latest, 'v2': film_front, 'v3': film_today_latest, 'v4': film_front, }) priority_list = immutable.make_dict({ 'v1': [('film_today_latest', 10)], 'v2': [('film_front', 10)], 'v3': [('film_today_latest', 7)], 'v4': [('film_front', 7)], }) template_names = immutable.make_dict({ 'v1': 'culture/film-today/v1', 'v2': 'culture/film-today/v2', 'v3': 'culture/film-today/v3', 'v4': 'culture/film-today/v4', })
class CloseUp(handlers.EmailTemplate): recognized_versions = ['v1', 'v2', 'v3'] ad_tag = 'email-close-up' ad_config = {'leaderboard_v1': 'Top', 'leaderboard_v2': 'Bottom'} base_data_sources = immutable.make_dict({ 'film_week': ds.FilmOfTheWeekDataSource(client), 'film_picks': ds.FilmEditorsPicksDataSource(client), 'film_most_viewed': ds.FilmMostViewedDataSource(client), 'film_interviews': ds.FilmInterviewsDataSource(client), 'film_blogs': ds.FilmBlogsDataSource(client), 'film_quiz': ds.FilmQuizDataSource(client) }) data_sources = immutable.make_dict({ 'v1': base_data_sources, 'v2': base_data_sources, 'v3': base_data_sources, }) priority_list = {} priority_list['v1'] = [('film_week', 1), ('film_interviews', 3), ('film_blogs', 5), ('film_quiz', 1), ('film_picks', 2), ('film_most_viewed', 3)] priority_list['v2'] = priority_list['v1'] priority_list['v3'] = priority_list['v1'] template_names = immutable.make_dict({ 'v1': 'culture/close-up/v1', 'v2': 'culture/close-up/v2', 'v3': 'culture/close-up/v3' })
class TheFlyer(handlers.EmailTemplate): recognized_versions = immutable.make_list('v1') ad_tag = 'email-the-flyer' ad_config = {'leaderboard': 'Top'} data_sources = immutable.make_dict({ 'v1': { 'travel_picks': ds.TravelDataSource(client), 'travel_most_viewed': ds.TravelMostViewedDataSource(client), 'travel_top_ten': ds.TravelTopTenDataSource(client), 'travel_video': ds.TravelVideoDataSource(client), 'travel_tips': ds.TravelTipsDataSource(client), } }) priority_list = immutable.make_dict({ 'v1': [('travel_video', 1), ('travel_picks', 5), ('travel_most_viewed', 3), ('travel_top_ten', 5), ('travel_tips', 1)] }) template_names = immutable.make_dict({'v1': 'travel/the-flyer'})
def __init__(self, instructions): 'See help(CommandLineParser).' wflags = {} cflags = {} wargs = {} cargs = {} defaults = {} for row in instructions: if not hasattr(row, '__iter__') or len(row) < 3 or len(row) > 4 or \ any(x is not None and not isinstance(x, basestring) for x in row[:3]): raise ValueError('Invalid instruction row: %s ' % row) (c, w, var, dflt) = row if len(row) == 4 else (list(row) + [None]) defaults[var] = dflt if dflt is True or dflt is False: if c is not None: cflags[c] = var if w is not None: wflags[w] = var else: if c is not None: cargs[c] = var if w is not None: wargs[w] = var self.default_values = make_dict(defaults) self.flag_words = make_dict(wflags) self.flag_characters = make_dict(cflags) self.option_words = make_dict(wargs) self.option_characters = make_dict(cargs)
class MediaBriefing(handlers.EmailTemplate): recognized_versions = immutable.make_list('v1') ad_tag = 'email-media-briefing' ad_config = immutable.make_dict({ 'leaderboard_v1': 'Top', 'leaderboard_v2': 'Bottom' }) data_sources = immutable.make_dict({ 'v1': { 'media_stories': ds.MediaDataSource(mr.client), 'media_monkey': ds.MediaMonkeyDataSource(mr.client), 'media_briefing': ds.MediaBriefingDataSource(mr.client) } }) priority_list = immutable.make_dict({ 'v1': immutable.make_list(('media_stories', 10), ('media_monkey', 1), ('media_briefing', 1)), }) template_names = immutable.make_dict({'v1': 'media/media-briefing'})
def __init__(self, host='localhost', mapping_config_filename=None): """ Mapping config is a YAML file with the following format alias_name_for_light: type: FunctionNameIn_dmx_devices index: 183 # Optional - special control (will default to 512 if omitted) dmx_size: 256 """ log.info(f'Init DMX Output {host}') self.artnet = ArtNet3(host) with open(mapping_config_filename, 'rt') as filehandle: self.mapping = make_dict(yaml.load(filehandle)) assert self.mapping, f'No DMX Mappings loaded from {mapping_config_filename}' self.buffer = bytearray(self.mapping.get('dmx_size', 512))
def test_cluster(): """ Check the cluster function returns the correct values """ inputs = { 'a': (4, 0, 0, 0), 'b': (0, 4, 0, 0), 'c': (0, 0, 4, 0), 'd': (0, 0, 0, 4), } # By choosing 1 cluster, we guarantee the outcome must always be the same no matter the randomness amount_of_clusters = 1 inputs = make_dict( inputs) # Attempting to modify the input would cause an Exception result = cluster(inputs, amount_of_clusters) assert len(result) == 1 assert sorted(result.get((1, 1, 1, 1))) == ['a', 'b', 'c', 'd']
def update_state(self, state, command: str, payload: T.Tuple) -> 'State': # If player is game, then simply update that key key, value, player = payload ## Special handling for field updates. Change into numpy array if key == 'field': return state.using( board=np.array(value).reshape((self.settings['field_height'], self.settings['field_width']))) if key == 'round': return state.using(round=value) # if theres a player, make the entries a dictionary if key not in state: key_dict = make_dict({}) else: key_dict = state[key] # replace that dictionary return state.using(**{key: key_dict.using(**{player: value})})
class ZipFile(handlers.EmailTemplate): recognized_versions = ['v1'] ad_tag = 'email-technology-roundup' ad_config = {'leaderboard_v1': 'Top', 'leaderboard_v2': 'Bottom'} data_sources = immutable.make_dict({ 'v1': { 'tech_news': dss.technology.TechnologyDataSource(mr.client), 'tech_games': dss.technology.TechnologyGamesDataSource(mr.client), 'tech_podcast': dss.technology.TechnologyPodcastDataSource(mr.client), 'tech_video': dss.technology.TechnologyVideoDataSource(mr.client) } }) priority_list = { 'v1': [('tech_video', 1), ('tech_news', 5), ('tech_games', 3), ('tech_podcast', 1)] } template_names = {'v1': 'technology/zip-file'}
def additional_template_data(self): sydney_tz = pytz.timezone('Australia/Sydney') date_format = "%A %d %B %Y" return immutable.make_dict( sydney_date=datetime.datetime.now(sydney_tz).strftime(date_format) )
import logging import json import urllib import traceback from google.appengine.api import urlfetch from google.appengine.api import memcache import pysistence as immutable import defaults import configuration default_params = immutable.make_dict({ 'show-fields': ",".join(defaults.content_item_fields), 'api-key': configuration.read('CAPI_KEY') }) def for_id(container_id): return ContainerDataSource(container_id) def read_capi_item(internal_id): capi_base_url = configuration.read('CAPI_BASE_URL') item_url = "{0}/{1}?{2}".format(capi_base_url, internal_id, urllib.urlencode(default_params)) #logging.info(item_url) cached_response = memcache.get(item_url) if cached_response: return cached_response
def test_using_copies(): d = make_dict() new_dict = d.using(foo='bar') assert new_dict is not d
class DailyEmailAUS(handlers.EmailTemplate): recognized_versions = ['v1', 'v2', 'v3', 'v2015', 'v4'] cache_bust = False ad_tag = 'email-guardian-today-aus' ad_config = {'leaderboard_v1': 'Top', 'leaderboard_v2': 'Bottom'} cultureDataSource = ds.ItemPlusBlogDataSource( ds.CultureDataSource(clientAUS), au.AusCultureBlogDataSource(clientAUS)) base_data_sources = immutable.make_dict({ 'top_stories_code': ds.TopStoriesDataSource(clientAUS), 'top_stories': ds.TopStoriesDataSource(clientAUS), 'most_viewed': ds.MostViewedDataSource(clientAUS), 'aus_sport': au.SportDataSource(client), 'culture': cultureDataSource, 'comment': au.AusCommentIsFreeDataSource(clientAUS), 'lifeandstyle': ds.LifeAndStyleDataSource(clientAUS), 'technology': tech_data.TechnologyDataSource(clientAUS), 'environment': au.Environment(clientAUS), 'science': ds.ScienceDataSource(clientAUS), 'video': au.AusVideoDataSource(clientAUS), }) data_sources = { 'v1': base_data_sources, 'v2': base_data_sources.using( eye_witness=ds.EyeWitnessDataSource(clientAUS)), 'v3': base_data_sources.using(eye_witness=ds.EyeWitnessDataSource(clientAUS), most_shared=dss.social.most_shared( clientAUS, ophan_client, 'au')), 'v2015': base_data_sources, 'v4': base_data_sources.using( breaking=container.for_front('au', 'breaking'), canonical=container.for_front('au', 'canonical'), special=container.for_front('au', 'special'), ), } base_priorities = immutable.make_list( ('top_stories', 6), ('most_viewed', 6), ('aus_sport', 5), ('culture', 3), ('comment', 3), ('lifeandstyle', 3), ('technology', 2), ('environment', 2), ('science', 2), ('video', 3)) priority_list = immutable.make_dict({ 'v1': base_priorities, 'v2': base_priorities.concat(immutable.make_list(('eye_witness', 1))), 'v3': base_priorities.concat(immutable.make_list(('most_shared', 6))), 'v2015': base_priorities, 'v4': immutable.make_list( ('breaking', 5), ('canonical', 6), ('special', 1), ).concat(base_priorities.without(('top_stories', 6))), }) template_names = immutable.make_dict({ 'v1': 'au/daily/v1', 'v2': 'au/daily/v2', 'v3': 'au/daily/v3', 'v2015': 'au/daily/v2015', 'v4': 'au/daily/v4', })
def additional_template_data(self): sydney_tz = pytz.timezone('Australia/Sydney') date_format = "%A %d %B %Y" return immutable.make_dict( sydney_date=datetime.datetime.now(sydney_tz).strftime(date_format))
def test_popitem(): d = make_dict(foo='bar') d.popitem()
import theano as th from theano import tensor as T import pysistence as ps import numpy as np from model import * import time empty_dict = ps.make_dict() def zipmap(seq, f): return ps.make_dict(**dict([(s,f(s)) for s in seq])) def index_plist(pl, i): return list(pl)[i] def remember(model): return simulate_prior(model, arguments=stochastics(model)) def full_trace(model, trace): r = remember(model) return ps.make_list(*[r(state) for state in trace]) class metropolis(object): def __init__(self, model, orig_x): self.x_p = orig_x + 1 self.lpd_ = logp_difference(model, {orig_x:self.x_p}, compile=False) def lpd(self, state, x_p): return self.lpd_(state.concat(ps.make_list(x_p))) @classmethod
def test_without_copies(): d = make_dict(foo='bar') empty_d = d.without('foo') assert empty_d is not d
def test_without(): d = make_dict(foo='bar') empty_d = d.without('foo') assert empty_d == {}
from pysistence import make_dict d = make_dict (foo = 'bar') print (d)
def test_using_value(): d = make_dict() new_dict = d.using(foo='bar') assert new_dict['foo'] == 'bar'
#################################################################################################### # command/__init__.py # The main function, if neuropythy is invoked directly as command. # By Noah C. Benson import os, sys, math import pysistence from .register_retinotopy import (register_retinotopy_command, register_retinotopy_help) from .benson14_retinotopy import (benson14_retinotopy_command, benson14_retinotopy_help) from .surface_to_ribbon import (surface_to_ribbon_command, surface_to_ribbon_help) # The commands that can be run by main: commands = pysistence.make_dict( register_retinotopy=register_retinotopy_command, benson14_retinotopy=benson14_retinotopy_command, surface_to_ribbon=surface_to_ribbon_command)
def custom_cmd_mapping(cls): return pysistence.make_dict(cls._custom_cmd)
def zipmap(seq, f): return ps.make_dict(**dict([(s,f(s)) for s in seq]))
def test_copy_value(): d = make_dict(foo='bar') d2 = d.copy() assert d == d2
import serial # for communication with arduino import collections # for ordered dicts (session_info) import pysistence # for immutable dict (mouse_info) import warnings import box_utils # box_utils.py must be in the same folder as this file import socket import random import traceback import colorama from colorama import Fore, Style #mouse info mouse_info = pysistence.make_dict({ 'mouseName': 'testingProtocol', 'requiredVersion': 7, 'leftVisCue': 0, 'rightVisCue': 3, 'leftAudCue': 3, 'rightAudCue': 0 }) #session info session_info = collections.OrderedDict() session_info['computer_name'] = socket.gethostname() session_info['box_number'] = 1 box_utils.set_COM_port(session_info) connection_speed = 115200 # tried 230400, and it didn't work try: del arduino except:
class Politics(handlers.EmailTemplate): recognized_versions = immutable.make_list('v1', 'v2', 'v3', 'v4') cache_bust = False ad_tag = 'email-australian-politics' ad_config = {} data_sources = immutable.make_dict({ 'v1': { 'politics_latest': au.AustralianPoliticsDataSource(client), 'politics_comment': au.AusCommentIsFreeDataSource(clientAUS), 'politics_video': au.AustralianPoliticsVideoDataSource(client) }, 'v2': { 'headlines': container.for_id('au-alpha/news/regular-stories', sort_function=sorts.au.politics_first, additional_capi_params=immutable.make_dict( {"show-tags": "keyword"})), 'politics_comment': au.AusCommentIsFreeDataSource(clientAUS), 'politics_video': au.AustralianPoliticsVideoDataSource(client), }, 'v3': { 'politics_latest': au.AustralianPoliticsDataSource(client), 'politics_most_popular': dss.mostPopular.mostPopularByTag( client, ophan_client, 'australia-news/australian-politics'), 'politics_comment': au.AusCommentIsFreeDataSource(clientAUS), 'politics_video': au.AustralianPoliticsVideoDataSource(client) }, 'v4': { 'politics_latest': au.AustralianPoliticsDataSource(client), 'politics_most_popular': dss.mostPopular.mostPopularByTag( client, ophan_client, 'australia-news/australian-politics'), 'politics_comment': au.AusCommentIsFreeDataSourceLatest(clientAUS), 'politics_video': au.AustralianPoliticsVideoDataSource(client) } }) priority_list = { 'v1': [('politics_comment', 1), ('politics_video', 1), ('politics_latest', 4)], 'v2': [ ('headlines', 4), ('politics_comment', 1), ('politics_video', 1), ], 'v3': [ ('politics_latest', 4), ('politics_most_popular', 4), ('politics_comment', 1), ('politics_video', 1), ], 'v4': [ ('politics_latest', 4), ('politics_most_popular', 4), ('politics_comment', 1), ('politics_video', 1), ] } template_names = immutable.make_dict({ 'v1': 'au/politics/v1', 'v2': 'au/politics/v2', 'v3': 'au/politics/v3', 'v4': 'au/politics/v3' })
def empty_model(): "Initializes a new empty model." return ps.make_dict(variables=ps.make_list(), factors=ps.make_list(T.constant(0)), stream=th.tensor.shared_randomstreams.RandomStreams())
def test_pop(): d = make_dict(foo='bar') d.pop('foo')
def __init__(self): self._db = make_dict() self._registry = {}
def test_clear(): d = make_dict(foo='bar') d.clear()
def _make_context(self, event, db, interceptors): coeffects = make_dict(db=self._db, event=event) return make_dict(coeffects=coeffects, queue=make_list(*interceptors), stack=make_list())
def test_copy_returns_PDict(): d = make_dict(foo='bar') d2 = d.copy() assert isinstance(d2, PDict)
def builtin_cmd_mapping(cls): """ A mapping of the built-in commands. """ return pysistence.make_dict(cls._cmd_mapping)
def additional_template_data(self): return immutable.make_dict({})
def to_namedict(variables, values): "Makes a persistent dict mapping variable name to value." return ps.make_dict(**dict([(var.name, val) for var,val in zip(variables, values)]))