def generate(self, save=True): "Generates photo file in current format" crop_box = None if self.crop_left: crop_box = (self.crop_left, self.crop_top, \ self.crop_left + self.crop_width, self.crop_top + self.crop_height) important_box = None if self.photo.important_top is not None: p = self.photo important_box = (p.important_left, p.important_top, p.important_right, p.important_bottom) formatter = Formatter(Image.open(self.photo.image.path), self.format, crop_box=crop_box, important_box=important_box) stretched_photo, crop_box = formatter.format() self.crop_left, self.crop_top, right, bottom = crop_box self.crop_width = right - self.crop_left self.crop_height = bottom - self.crop_top self.width, self.height = stretched_photo.size stretched_photo.save(self.file(), quality=self.format.resample_quality) f = open(self.file()) file = ContentFile(f.read()) f.close() self.image.save(self.file(relative=True), file, save)
def _generate_img(self): crop_box = None if self.crop_left: crop_box = (self.crop_left, self.crop_top, \ self.crop_left + self.crop_width, self.crop_top + self.crop_height) important_box = None if self.photo.important_top is not None: p = self.photo important_box = (p.important_left, p.important_top, p.important_right, p.important_bottom) image = None if crop_box is None and self.format.master_id: try: fp = FormatedPhoto.objects.get(format=self.format.master_id, photo=self.photo) image = Image.open(fp.image) except FormatedPhoto.DoesNotExist: pass if image is None: image = self.photo._get_image() formatter = Formatter(image, self.format, crop_box=crop_box, important_box=important_box) return formatter.format()
def test_run_sav(self): runner = SerialRunner("SavannahSim", 50, 2) output = runner.run() formatter = Formatter(output) for_out = formatter.format() self.assertTrue(for_out) self.assertEqual(len(output) + 1, len(for_out))
def test_run_hama(self): runner = SerialRunner("HamadryasSim", 50, 2) output = runner.run() formatter = Formatter(output) for_out = formatter.format() self.assertTrue(for_out) self.assertEqual(len(output) + 1, len(for_out))
def run(simulation_name, file_name): runner = ParallelRunner(simulation_name, 400, 1000, 4) output = runner.run() formatter = Formatter(output) for_out = formatter.format() saver = Saver(for_out, file_name) saver.save()
class Logger(object): def __init__(self): self.formatter = Formatter() try: from cStringIO import StringIO except ImportError: from io import StringIO self.log_stream = StringIO() logging.basicConfig(stream=self.log_stream, level=logging.DEBUG, format='%(message)s') #self.log() def on_press(self, key): if str(key) == 'Key.space': logging.info(' ') elif str(key) == 'Key.esc': logging.info(' (terminated)') elif str(key) == 'Key.alt_l': logging.info(' <alt_l>') elif str(key) == 'Key.alt_r': logging.info(' <alt_r>') elif str(key) == 'Key.tab': logging.info(' <tab>') elif str(key) == 'Key.ctrl_l': logging.info(' <ctrl_l>') elif str(key) == 'Key.ctrl_r': logging.info(' <ctrl_r>') elif str(key) == 'Key.shift': logging.info(' <shift_l>') elif str(key) == 'Key.shift_r': logging.info(' <shift_r>') else: logging.info(str(key)) if key == Key.esc: print(self.formatter.format((str(self.log_stream.getvalue())))) return False def log(self): with Listener(on_press=self.on_press) as listener: listener.join() def get_formatted_log(self): return self.formatter.format((str(self.log_stream.getvalue())))
def _generate_img(self): crop_box = None if self.crop_left: crop_box = (self.crop_left, self.crop_top, \ self.crop_left + self.crop_width, self.crop_top + self.crop_height) important_box = None if self.photo.important_top is not None: p = self.photo important_box = (p.important_left, p.important_top, p.important_right, p.important_bottom) formatter = Formatter(self.photo._get_image(), self.format, crop_box=crop_box, important_box=important_box) return formatter.format()
def test_save_sav(self): runner = SerialRunner("SavannahSim", 50, 2) output = runner.run() formatter = Formatter(output) for_out = formatter.format() self.assertTrue(for_out) self.assertEqual(len(output) + 1, len(for_out)) saver = Saver(for_out, "savtest.csv") saver.save() open_file = open("savtest.csv").read() self.assertTrue(open_file)
def run(simulation_name, file_name, recognition, attraction_strength, duration, codispersal=False): runner = ParallelRunner(simulation_name, duration, recognition, attraction_strength, 100, 7, codispersal) output = runner.run() formatter = Formatter(output) for_out = formatter.format() saver = Saver(for_out, file_name) saver.save()
def generate(self): "Generates photo file in current format" crop_box = None if self.crop_left: crop_box = (self.crop_left, self.crop_top, \ self.crop_left + self.crop_width, self.crop_top + self.crop_height) important_box = None if self.photo.important_top: p = self.photo important_box = (p.important_left, p.important_top, p.important_right, p.important_bottom) formatter = Formatter(Image.open(self.filename), self.format, crop_box=crop_box, important_box=important_box) stretched_photo, crop_box = formatter.format() self.width, self.height = stretched_photo.size self.filename = self.file(relative=True) stretched_photo.save(self.file(), quality=self.format.resample_quality)
class Cli: def __init__(self, module_name, config, parser): self.__parser = parser self.formatter = Formatter(module_name) self.__option_parser = self.__get_option_parser(config, parser) self.formatter.option_parser = self.__option_parser def parse_args(self): return self.__option_parser.parse_args() def __get_option_parser(self, config, parser): option_parser = OptionParser(self.formatter.format(config.usage)) self.__add_options(config, parser, option_parser) self.__add_option_groups(config, option_parser, parser) return option_parser def __add_options(self, group, parser, option_parser): for option in group.options: self.__add_option(parser, option, option_parser) def __add_option(self, parser, option, option_parser): kwargs = self.__get_option_kwargs(parser, option.kwargs) option_parser.add_option(*option.argv, **kwargs) def __get_option_kwargs(self, parser, kwargs): if "callback" in kwargs: kwargs["callback"] = getattr(parser, kwargs["callback"]) return kwargs def __add_option_groups(self, config, option_parser, parser): for group in config.groups: self.__add_option_group(option_parser, group, parser) def __add_option_group(self, option_parser, group, parser): option_parser_group = OptionGroup(option_parser, group.title, group.description) self.__add_options(group, parser, option_parser_group) option_parser.add_option_group(option_parser_group)
def format_file(input_file, output_file, param_file, error_file): f = Formatter(param_file) tags, errors = analyze_code(input_file) result = f.format(tags) directory = os.path.dirname(output_file) if directory != '' and not os.path.exists(directory): os.makedirs(directory) file = open(output_file, mode='w') file.write(result) file.close() directory = os.path.dirname(error_file) if directory != '' and not os.path.exists(directory): os.makedirs(directory) file = open(error_file, mode='w') for e in errors: file.write(str(e) + "\n") file.close() print('HTML file succesfuly formatted and written to', output_file, '\nErrors in:', error_file)
def generate(self, save=True): "Generates photo file in current format" crop_box = None if self.crop_left: crop_box = (self.crop_left, self.crop_top, \ self.crop_left + self.crop_width, self.crop_top + self.crop_height) important_box = None if self.photo.important_top is not None: p = self.photo important_box = (p.important_left, p.important_top, p.important_right, p.important_bottom) formatter = Formatter(Image.open(self.photo.image.path), self.format, crop_box=crop_box, important_box=important_box) stretched_photo, crop_box = formatter.format() # set crop_box to (0,0,0,0) if photo not cropped if not crop_box: crop_box = 0, 0, 0, 0 self.crop_left, self.crop_top, right, bottom = crop_box self.crop_width = right - self.crop_left self.crop_height = bottom - self.crop_top self.width, self.height = stretched_photo.size stretched_photo.save(self.file(), quality=self.format.resample_quality) f = open(self.file(), 'rb') file = ContentFile(f.read()) f.close() self.image.save(self.file(relative=True), file, save)
def main(): stdout, stderr = get_encoding_safe_stdio() parser = argparse.ArgumentParser( description="Read, format and output an RSS stream.") parser.add_argument('uri', action='store', help="load feed from uri") parser.add_argument( '-o', '--format', action='store', required=True, help="python 3.x styled format string (see https://pyformat.info/)") parser.add_argument('-f', '--follow', action='store_true', help="follow feed stream updates") parser.add_argument( '-i', '--interval', action='store', type=float, default=None, help="time between each fetch for updates (in seconds)") args = parser.parse_args() last_item_link = None formatter = Formatter() format_string = args.format.decode('utf-8') while True: try: d = feedparser.parse(args.uri) except Exception: print >> stderr, "Failed to parse '{}'".format(args.uri) continue interval = args.interval if args.interval else float( d.feed.ttl) if hasattr(d.feed, 'ttl') else 60 newlines = [] for entry in d.entries: if entry.link == last_item_link: break item = dict(entry) item['enclosures'] = entry.enclosures item['published'] = datetime( *entry.published_parsed[:6]) if hasattr( entry, 'published_parsed') else None item['created'] = datetime(*entry.created_parsed[:6]) if hasattr( entry, 'created_parsed') else None item['updated'] = datetime(*entry.updated_parsed[:6]) if hasattr( entry, 'updated_parsed') else None item['expired'] = datetime(*entry.expired_parsed[:6]) if hasattr( entry, 'expired_parsed') else None newlines.append(formatter.format(format_string, **item)) if len(d.entries): last_item_link = d.entries[0].link for line in reversed(newlines): print >> stdout, line stdout.flush() if args.follow: sleep(interval) continue break
def format(self): file_contents = self.get_file_contents() formatter = Formatter() file_contents = formatter.format(file_contents, **self.get_rule_list()) Reporter.add_message(file_contents)
#!/usr/bin/env python from info import InstanceInfo from formatter import Formatter f = Formatter() i = InstanceInfo( name="some_instance", virt_name="instance-0000043c", id=1084, uuid="35315889-d4f1-4067-b14e-34b5e4bd49c8", fixed_ip="x.x.x.x", host="novacpu03.foo.bar.baz", tenant="Something", vm_state="active", task_state=None, vcpus=8, ram="16.0GB", snapshots=["6d0fd729-7034-41bb-b006-c98a2f39b275", "3acc483b-0f2f-4c73-9854-3f4b2482bee6"]) i['something'] = [1,2,3,4,5] print(f.format([i]))