def _status(self, final=False):
    if not final:
        new = (green(len(self._completed)),
           white(len(self._running)),
           yellow(len(self._queued)),
           green('finished'),
           white('running'),
           yellow('queued'))
        if hasattr(self, 'last_status') and new == self.last_status:
            return
        self.last_status = (green(len(self._completed)),
            white(len(self._running)),
            yellow(len(self._queued)),
            green('finished'),
            white('running'),
            yellow('queued'))
        print WHIPE, "[%s/%s/%s] %s, %s, %s" % new
    else:
        print "\n[ %s OK / %s ERROR ] in %s seconds" % (
                green(self._num_of_jobs - self._errors, True),
                red(self._errors),
                time.time() - self._time_start)
        if self._errors:
            print red("Failures:", True)
            for job in self._completed:
                if job.exitcode != 0:
                    print red(job.name)
    sys.stdout.flush()
Example #2
0
  def open(self):
    os.system("clear")
    print blue("Welcome to %s" % green(self.name))
    print blue("========================")
    while True:
      print red("How may I help you?")
      print yellow("1. Buy a ticket\n"
                   " 2. Book a ticket by phone\n"
                   " 3. Book a ticket\n"
                   " 4. Cancel booking\n"
                   " 5. See how many money do you have\n"
                   " 6. Goodbye!\n")

      option = raw_input(green("I want to: "))

      if option == "1":
        self.buy_ticket()

      if option == "2":
        self.book_ticket_by_phone()

      if option == "3":
        self.book_ticket()

      if option == "4":
        self.cancel_booking()

      if option == "5":
        self.money()

      if option == "6":
        break
Example #3
0
def show_diff(old, new):
    for k, v in new.iteritems():
        if k in old.keys() and v == old[k]:
            continue
        if k in old.keys() and v != old[k]:
            red(" - ['{}'] = {}".format(k, old[k]))
        green(" + ['{}'] = {}".format(k, v))
Example #4
0
  def start(self):
    os.system("clear")
    while True:
      print green("How can I help you, %s?" % self.user)
      print yellow("1. Transfer some money\n"
                   " 2. Pay\n"
                   " 3. Retrieve\n"
                   " 4. Show balance\n"
                   " 5. Logout\n")

      option = int(raw_input(blue("I want to: ")))
      if option == 1 or option == 2:
        to = raw_input(red("to: "))
        money = raw_input(red("sum: "))
        try:
          print self.bank.transfer_from(self.user, to, money)
        except ValueError as ve:
          print red(ve)
      elif option == 3:
        money = raw_input(red("sum: "))
        try:
          if self.bank.transfer_from(self.user, self.name, money):
            print green("Operation finished with success!\n")
          else:
            print red("Something went wrong...try again later\n")
        except ValueError as ve:
          print red(ve)
      elif option == 4:
        print self.bank.show_balance(self.user)
      elif option == 5:
        self.user = None
        return
  def run(self):
    if self.is_negative:
      if self._from != 10:
        self.log.error(red("I dont know how to convert negative numbers if"
                           " there are not in decimal base"))
        return False
      else:
        number = self.transform_negative()
    else:
      number = self.transform_positive()

    print green("Your number is %s" % red(number))
Example #6
0
 def login(self):
   print "\n\n"
   username = raw_input(green("username: "))
   password = getpass(green("password: "))
   with open("db/clients.txt", "r") as f:
     clients = f.read()
     credentials = "%s:%s" % (username, sha1(password).hexdigest())
     if credentials in clients:
       print green("Welcome!")
       self.user = username
       self.start()
     else:
       print red("Wrong credentials!")
Example #7
0
 def path_exists(self, path, host=None, negate=False):
     containers = [self.containers[host]] if host else self.containers.itervalues()
     for container in containers:
         if negate:
             if not path_exists(path, container):
                 continue
             print(utils.red("Found path <{}> on host <{}>".format(path, self.host_from_container(container))))
         else:
             if path_exists(path, container):
                 continue
             print(utils.red("Path <{}> not found on host <{}>".format(path, self.host_from_container(container))))
         return False
     return True
Example #8
0
def show_unencrypted_diff(diff_part, password_file=None):
    intense(get_head(diff_part).strip())
    old, new = decrypt_diff(diff_part, password_file)
    diff = difflib.unified_diff(old.split('\n'), new.split('\n'), lineterm='')
    # ... we'll take the git filenames from git's diff output rather than
    # ... difflib
    for line in islice(diff, 2, None):
        if line.startswith('-'):
            red(line)
        elif line.startswith('+'):
            green(line)
        elif line.startswith('@@'):
            cyan(line)
        else:
            print line
Example #9
0
def handle(socket, address):
    print green("Client connected")

    while True:
        data = socket.recv(2048)
        if not data:
            print red("Client disconnected")
            return

        message = parser(data)
        if message is None:
            return

        if message[0] == "transfer":
            transfer(message[1], socket)
Example #10
0
  def open(self):
    os.system("clear")
    print blue("Welcome to ") + yellow(self.bank.name)
    print blue("========================\n\n")

    while True:
      print green("Please choose one of the action:")
      print red("1. Register\n"
                " 2. Login\n\n")
      option = int(raw_input(yellow("I want to: ")))
      if option == 1:
        self.register()
      elif option == 2:
        self.login()
      else:
        print red("I dont't understand you! Please repeat")
Example #11
0
 def run(self):
     pid = 0
     try:
         if self.silent:
             self.proc = subprocess.Popen(
                 [self.path] + self.args,
                 stdout=subprocess.PIPE,
                 stderr=subprocess.PIPE)
         else:
             self.proc = subprocess.Popen([self.path] + self.args)
         pid = self.proc.pid
         self.started = True
     except Exception as e:
         print(utils.red("Process start failed:") + " %s" % self.name)
         print(str(e))
         sys.exit(1)
     try:
         while self.proc.poll() is None:
             self.started = True
             time.sleep(self.interval)
         self.started = True
         self.retcode = -1 if self.proc is None else self.proc.poll()
         self.proc = None
     except Exception as e:
         return
Example #12
0
def main(args = None):
	opts = docopt.docopt(
		doc = __doc__,
		argv = args)
	try:
		if opts["--no-color"]:
			utils.disable_colors()
		if opts["--verbose"]:
			utils.enable_tracing()
		for name in opts["NAMES"]:
			dist = get_distribution(name)
			for name in required(dist):
				raise Error(dist.project_name, "required by", name)
			for req in dist.requires():
				names = filter(lambda name: name != dist.project_name, required(req))
				if not names:
					uninstall(req.project_name, dryrun = opts["--dry-run"])
				else:
					utils.trace(req.project_name, "not uninstalled, used by", names)
			uninstall(dist.project_name, dryrun = opts["--dry-run"])
#		else:
#			for dist in pip.get_installed_distributions():
#				print utils.magenta(dist.project_name), dist.location
	except utils.Error as exc:
		raise SystemExit(utils.red(exc))
Example #13
0
  def register(self):
    print "\n\n"
    username = raw_input(green("username: "))
    password = getpass(green("password: "))

    with open("db/clients.txt", "ab+") as f:
      clients = f.read()
      if "%s:" % username in clients:
        print red("Username already taken")
        return

      f.write("%s:%s\n" % (username, sha1(password).hexdigest()))
      initial_deposit = raw_input(green("Initial deposit: "))
      with open("clients/%s" % sha1(username).hexdigest(), "w") as g:
        g.write(initial_deposit)

    print green("Succesfully register!")
    self.user = username
    self.start()
Example #14
0
    def get_str(self, models_by_name=None, print_name=True):
        s = ''
        if print_name:
            s = '%s : %s \t<%s>' % (red(self.name.ljust(20)), green(self.superclass.ljust(15)),
                                    blue(self.filename))
        for field in self.fields:
            s += '\n%s' % field.__repr__()

        if self.superclass != 'MTLModel' and models_by_name:
            # Also print the fields of the superclass
            s += '\n%s' % models_by_name[self.superclass].get_str(models_by_name=models_by_name,
                                                                  print_name=False)
        return s
Example #15
0
    def on_post_item(self, response, **kwargs):
        input_file = kwargs.get('input_file', '')
        source = kwargs.get('source', '')

        filename = self.format_filename(input_file.replace(source, ''))
        if response.success:
            self._optimized += 1
            self._input_bytes += response.input_size
            self._output_bytes += response.output_size
            print("%s %16s %37s" % (filename, green("OK"),
                                    response.output_ratio))
        else:
            self._failed += 1
            print("%s %18s %30s" % (filename, red("FAIL"), "-"))
Example #16
0
 def run(self):
     try:
         if self.silent:
             self.proc = subprocess.Popen([self.path] + self.args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         else:
             self.proc = subprocess.Popen([self.path] + self.args)
     except Exception as e:
         print(utils.red("Process start failed:") + " %s" % self.name)
         print(str(e))
         sys.exit(1)
     try:
         while self.proc.poll() is None:
             time.sleep(self.interval)
         self.proc = None
     except:
         return
     print("Process %s ended" % self.name)
Example #17
0
def profile_leaks(shell, queries, count=1, rounds=1, supp_file=None):
    report = {}
    for name, query in queries.iteritems():
        print("Analyzing leaks in query: %s" % query)
        # Apply count (optionally run the query several times).
        summary = check_leaks(shell, query, count, supp_file)
        display = []
        for key in summary:
            output = summary[key]
            if output is not None and output[0] != "0":
                # Add some fun colored output if leaking.
                if key == "definitely":
                    output = utils.red(output)
                    report[name] = "LEAKING"
                if key == "indirectly":
                    output = utils.yellow(output)
                    report[name] = "WARNING"
            else:
                report[name] = "SAFE"
            display.append("%s: %s" % (key, output))
        print("  %s" % "; ".join(display))
    return report
Example #18
0
 def images_delete(self, uproot=False):
     func = image_delete_and_containers if uproot else image_delete
     for image in self.get_real_images():
         print(utils.red("Delete image {}".format(image)))
         func(image)
     return self
Example #19
0
    def run(self, platform, precision=None, devices=None,
            use_dispersion_correction=True):
        """
        Runs the test on the given platform with the given precision model.

        Parameters
        ----------
        platform : str
            Name of the OpenMM platform to use (CPU, Reference, CUDA, or OpenCL)

        precision : str
            Precision model to use for CUDA or OpenCL (single, double, or mixed)

        devices : int or tuple of ints
            Which GPUs to run on (a tuple will run in parallel)
        """
        if not platform in ('CPU', 'Reference', 'CUDA', 'OpenCL'):
            raise ValueError('Platform %s not recognized.' % platform)
        # Define our platform and our platform properties
        plat = mm.Platform.getPlatformByName(platform)
        properties = None
        if platform == 'CUDA':
            if not precision in ('mixed', 'double', 'single'):
                raise ValueError('You must set the precision to single, '
                                 'double, or mixed for the CUDA platform.')
            properties = dict(CudaPrecision=precision)
            if devices is not None:
                properties['CudaDeviceIndex'] = str(devices)
        elif platform == 'OpenCL':
            if not precision in ('mixed', 'double', 'single'):
                raise ValueError('You must set the precision to single, '
                                 'double, or mixed for the CUDA platform.')
            properties = dict(OpenCLPrecision=precision)
            if devices is not None:
                properties['OpenCLDeviceIndex'] = str(devices)

        # Create a new system with no charges so we can compare vdW and EEL
        # energies to Amber
        parmcopy = copy(self.parm)
        for i in range(len(parmcopy.parm_data['CHARGE'])):
            parmcopy.parm_data['CHARGE'][i] = 0.0
        system = parmcopy.createSystem(nonbondedCutoff=8.0*u.angstroms,
                                       nonbondedMethod=app.PME)
        system.setDefaultPeriodicBoxVectors(*self.parm.box_vectors)

        # Test serialization
        xmlsys = mm.XmlSerializer.deserialize(
                        mm.XmlSerializer.serialize(self.system)
        )

        # Loop through all systems and turn on or off the dispersion correction
        print 'Trying to set PME parameters...',
        succeeded = None
        for sysmod in (self.system, self.systemapp, system, xmlsys):
            for force in sysmod.getForces():
                if isinstance(force, mm.NonbondedForce):
                    force.setUseDispersionCorrection(use_dispersion_correction)
                    # See if we can set the PME parameters
                    try:
                        force.setPMEParameters(3.285326106/u.nanometers,
                                               60, 64, 60)
                        succeeded = True
                    except AttributeError:
                        # This version of OpenMM does not support setting PME
                        # parameters
                        succeeded = False
        if succeeded:
            print 'Changed.'
        elif succeeded is None:
            print 'No NonbondedForce detected.'
        else:
            print 'OpenMM is too old. Could not change PME parameters.'
        # Define some integrators
        dummyint1 = mm.VerletIntegrator(1.0e-6*u.picoseconds)
        dummyint2 = mm.VerletIntegrator(1.0e-6*u.picoseconds)
        dummyint3 = mm.VerletIntegrator(1.0e-6*u.picoseconds)
        dummyint4 = mm.VerletIntegrator(1.0e-6*u.picoseconds)
        # Define the contexts
        if properties is None:
            context1 = mm.Context(self.system, dummyint1, plat)
            context2 = mm.Context(system, dummyint2, plat)
            context3 = mm.Context(self.systemapp, dummyint3, plat)
            context4 = mm.Context(xmlsys, dummyint4, plat)
        else:
            context1 = mm.Context(self.system, dummyint1, plat, properties)
            context2 = mm.Context(system, dummyint2, plat, properties)
            context3 = mm.Context(self.systemapp, dummyint3, plat,
                                  properties)
            context4 = mm.Context(xmlsys, dummyint4, plat, properties)
        # Set the context positions
        context1.setPositions(self.parm.positions)
        context2.setPositions(self.parm.positions)
        context3.setPositions(self.crdapp.getPositions())
        context4.setPositions(self.parm.positions)
        # Get the energies
        eunit = u.kilocalories_per_mole
        state = context1.getState(getEnergy=True, getForces=True,
                                  enforcePeriodicBox=True)
        funit = eunit / u.angstrom
        forces = state.getForces().value_in_unit(funit)
        tote = state.getPotentialEnergy().value_in_unit(eunit)
        state = context3.getState(getEnergy=True, enforcePeriodicBox=True)
        toteapp = state.getPotentialEnergy().value_in_unit(eunit)
        state = context4.getState(getEnergy=True, enforcePeriodicBox=True)
        xmltote = state.getPotentialEnergy().value_in_unit(eunit)
        # Now get the decomposed energies from both the system and the
        # deserialized system to check that serialization and deserialization
        # behave as expected with these force objects and force groups
        state = context1.getState(getEnergy=True, enforcePeriodicBox=True,
                                  groups=2**self.parm.BOND_FORCE_GROUP)
        bonde = state.getPotentialEnergy().value_in_unit(eunit)
        state = context4.getState(getEnergy=True, enforcePeriodicBox=True,
                                  groups=2**self.parm.BOND_FORCE_GROUP)
        xmlbonde = state.getPotentialEnergy().value_in_unit(eunit)

        state = context1.getState(getEnergy=True, enforcePeriodicBox=True,
                                  groups=2**self.parm.ANGLE_FORCE_GROUP)
        anglee = state.getPotentialEnergy().value_in_unit(eunit)
        state = context4.getState(getEnergy=True, enforcePeriodicBox=True,
                                  groups=2**self.parm.ANGLE_FORCE_GROUP)
        xmlanglee = state.getPotentialEnergy().value_in_unit(eunit)

        state = context1.getState(getEnergy=True, enforcePeriodicBox=True,
                                  groups=2**self.parm.DIHEDRAL_FORCE_GROUP)
        dihede = state.getPotentialEnergy().value_in_unit(eunit)
        state = context4.getState(getEnergy=True, enforcePeriodicBox=True,
                                  groups=2**self.parm.DIHEDRAL_FORCE_GROUP)
        xmldihede = state.getPotentialEnergy().value_in_unit(eunit)

        state = context1.getState(getEnergy=True, enforcePeriodicBox=True,
                                  groups=2**self.parm.NONBONDED_FORCE_GROUP)
        nonbe = state.getPotentialEnergy().value_in_unit(eunit)
        state = context4.getState(getEnergy=True, enforcePeriodicBox=True,
                                  groups=2**self.parm.NONBONDED_FORCE_GROUP)
        xmlnonbe = state.getPotentialEnergy().value_in_unit(eunit)

        state = context2.getState(getEnergy=True, enforcePeriodicBox=True,
                                  groups=2**self.parm.NONBONDED_FORCE_GROUP)
        vdwe = state.getPotentialEnergy().value_in_unit(eunit)

        eele = nonbe - vdwe
        # Now get the sander forces and compare them
        traj = netcdf_file(get_fn('sander_pme.nc'), 'r')
        sander_forces = traj.variables['forces'][0]
        maxdif = [abs(ofrc-sfrc)
                  for ofrc, sfrc in zip(forces[0], sander_forces[0])]
        maxrel = [abs(ofrc-sfrc)/sfrc
                  for ofrc, sfrc in zip(forces[0], sander_forces[0])]
        avgdif = [0, 0, 0]
        avgrel = [0, 0, 0]
        n = 0
        for ofrcs, sfrcs in zip(forces, sander_forces):
            for i, sfrc in enumerate(sfrcs):
                ofrc = ofrcs[i]
                dif = abs(ofrc-sfrc)
                rel = dif/sfrc
                maxdif[i] = max(maxdif[i], dif)
                maxrel[i] = max(maxrel[i], rel)
                avgdif[i] += dif
                avgrel[i] += rel
            n += 1
        avgdif = [x/n for x in avgdif]
        avgrel = [x/n for x in avgrel]
        # The sander energies are:
# Etot   =    -69285.4160  EKtot   =         0.0000  EPtot      =    -69285.4160
# BOND   =       404.9439  ANGLE   =      1003.4499  DIHED      =      2231.7367
# 1-4 NB =       440.7084  1-4 EEL =      3818.2959  VDWAALS    =      8271.5191
# EELEC  =    -85456.0701  EHBOND  =         0.0000  RESTRAINT  =         0.0000
        sander = dict(bond=404.9439, angle=1003.4499, dihedral=2231.7367,
                      vdw=8271.5191+440.7084, eel=-85456.0701+3818.2959,
                      total=-69285.4160)
        if not use_dispersion_correction:
            # Without the long-range dispersion correction, VDWAALS = 8943.8420
            sander['total'] -= sander['vdw']
            sander['vdw'] = 8943.8420 + 440.7084
            sander['total'] += sander['vdw']
        bonddif = bonde - sander['bond']
        angledif = anglee - sander['angle']
        diheddif = dihede - sander['dihedral']
        vdwdif = vdwe - sander['vdw'] # includes 1-4 also
        eeldif = eele - sander['eel'] # Includes 1-4 also
        totaldif = tote - sander['total']
        appdif = tote - toteapp
        print 'Energy differences compared to sander/Amber (kcal/mol)'
        print '             Absolute     Relative    sander'
        print '------------------------------------------------------'
        print 'Bond     =', colorize_error(bonddif), \
              colorize_error(bonddif/sander['bond'], 1e-6), \
              '%12.4f'%sander['bond']
        print 'Angle    =', colorize_error(angledif), \
              colorize_error(angledif/sander['angle'], 1e-6), \
              '%12.4f'%sander['angle']
        print 'Dihedral =', colorize_error(diheddif), \
              colorize_error(diheddif/sander['dihedral'], 1e-6), \
              '%12.4f'%sander['dihedral']
        if use_dispersion_correction:
            # The dispersion correction in Amber neglects the repulsive part of
            # the correction, but OpenMM does not. Therefore, when we are using
            # the dispersion correction we should allow for a slightly larger
            # energy difference.
            print 'vdWaals  =', colorize_error(vdwdif, 1.0), \
                   colorize_error(vdwdif/sander['vdw'], 1e-4), \
                   '%12.4f'%sander['vdw']
        else:
            print 'vdWaals  =', colorize_error(vdwdif, 1e-2), \
                   colorize_error(vdwdif/sander['vdw'], 1e-6), \
                   '%12.4f'%sander['vdw']
        print 'Elec     =', colorize_error(eeldif, 4e0), \
               colorize_error(eeldif/sander['eel'], 1e-3), '%12.4f'%sander['eel']
        print 'Total    =', colorize_error(totaldif, 4e0), \
               colorize_error(totaldif/sander['total'], 1e-3), \
               '%12.4f'%sander['total']
        print ''
        print 'Difference b/w ParmEd and OpenMM App layer'
        print '------------------------------------------'
        print 'Total    =', colorize_error(appdif, tolerance=5e-3)
        print ''
        print 'Difference b/w sander and OpenMM forces'
        print '---------------------------------------'
        print 'Maximum deviation = [%12s, %12s, %12s]' % colorize_list(maxdif,2e0)
        print 'Maximum rel. dev. = [%12s, %12s, %12s]' % colorize_list(maxrel,2e0)
        print 'Average deviation = [%12s, %12s, %12s]' % colorize_list(avgdif,1e-1)
        print 'Average rel. dev. = [%12s, %12s, %12s]' % colorize_list(avgrel,5e-1)

        # Now test serialization
        CUTOFF = 1e-5
        CUTOFFNB = 1e-2
        print ''
        print 'Serialization tests'
        print '-------------------'
        print 'Bond........',
        if abs(xmlbonde - bonde) < CUTOFF:
            print green('OK')
        else:
            dif = xmlbonde - bonde
            print red('off by %.4e (%f%%)' % (dif, 100*dif/(bonde or xmlbonde)))
        print 'Angle.......',
        if abs(xmlanglee - anglee) < CUTOFF:
            print green('OK')
        else:
            dif = xmlanglee - anglee
            print red('off by %.4e (%f%%)' % (dif,100*dif/(anglee or xmlanglee)))
        print 'Dihedral....',
        if abs(xmldihede - dihede) < CUTOFF:
            print green('OK')
        else:
            dif = xmldihede - dihede
            print red('off by %.4e (%f%%)' % (dif,100*dif/(dihede or xmldihede)))
        print 'Nonbonded...',
        if abs(xmlnonbe - nonbe) < CUTOFFNB:
            print green('OK')
        else:
            dif = xmlnonbe - nonbe
            print red('off by %.4e (%f%%)' % (dif,100*dif/(nonbe or xmlnonbe)))
Example #20
0
def _dump_data(name, data):
    try:
        with open(name, 'wb') as fh: fh.write(data)
        print "Wrote: %s" % (red(name))
    except Exception, e:
        print "Error: could not write (%s), (%s)." % (name, str(e))
Example #21
0
            for match in call[crawler].crawl_full():
                with matchfile(match) as fw:
                    fw.write(str(match) + '\n')
                if 0 < match.tostart < 3600 and not tbd in match.teams:
                    pool[match.webpage] = match
        except KeyboardInterrupt:
            sys.exit(0)
        except Exception as e:
            cooldown[crawler] = time.time() + cd
            print('{0} cooldowned for {1}s for exception'.format(crawler, cd))
            print(e)
            cd += 0
    with open('httpalias', 'a') as fw:
        G = networkx.Graph()
        print('{0} matches start <1h'.format(len(G)))
        for s1, s2 in itertools.combinations(pool.values(), 2):
            if domain(s1) == domain(s2) or not s1 == s2:
                continue
            G.add_edge(s1.webpage, s2.webpage)
        for idx, c in enumerate(networkx.connected_components(G)):
            fw.write(' '.join(c) + '\n')
            print('###MATCHED### {0}: '.format(idx) + ' '.join(c))
            profit = (max([pool[w].returns[0] for w in c]) - 1) * (max([pool[w].returns[1] for w in c]) - 1) - 1
            if profit < profit_trs:
                continue
            print(red('    PROFIT {0:.3}'.format(profit)))
            for w in c:
                s = pool[w]
                print(red('    {0} {1} ({2}): {3}/{4} {5} {6}'.format(s.teams[0], s.teams[1], s.series, s.returns, s.poolsize, domain(s), s.tostart)))
    time.sleep(5)