Exemple #1
0
        def do_list(self, args):
                try:
                        doParser = self.arg_list()
                        doArgs = doParser.parse_args(shlex.split(args))

                        org = org_utils.org_get(self.api, doArgs.org)
                        printer.out("Getting user list for ["+org.name+"] . . .")
                        allUsers = self.api.Orgs(org.dbId).Members.Getall()
                        allUsers = order_list_object_by(allUsers.users.user, "loginName")

                        table = Texttable(200)
                        table.set_cols_align(["l", "l", "c"])
                        table.header(["Login", "Email", "Active"])

                        for item in allUsers:
                                if item.active:
                                        active = "X"
                                else:
                                        active = ""
                                table.add_row([item.loginName, item.email, active])

                        print table.draw() + "\n"
                        return 0

                except ArgumentParserError as e:
                        printer.out("In Arguments: "+str(e), printer.ERROR)
                        self.help_list()
                except Exception as e:
                        return handle_uforge_exception(e)
    def do_promote(self, args):
        try:
            doParser = self.arg_promote()
            doArgs = doParser.parse_args(shlex.split(args))
            orgSpecified = org_utils.org_get(api=self.api, name=doArgs.org)

            adminUser = self.api.Users(doArgs.account).Get()

            if adminUser == None:
                printer.out("User [" + doArgs.account + "] doesn't exist.", printer.ERROR)
            else:
                self.api.Orgs(orgSpecified.dbId).Members(adminUser.loginName).Change(Admin=True, body=adminUser)
                printer.out("User [" + doArgs.account + "] has been promoted in [" + orgSpecified.name + "] :",
                            printer.OK)

            if adminUser.active == True:
                active = "X"
            else:
                active = ""

            printer.out("Informations about [" + adminUser.loginName + "] :")
            table = Texttable(200)
            table.set_cols_align(["c", "l", "c", "c", "c", "c", "c", "c"])
            table.header(
                ["Login", "Email", "Lastname", "Firstname", "Created", "Active", "Promo Code", "Creation Code"])
            table.add_row([adminUser.loginName, adminUser.email, adminUser.surname, adminUser.firstName,
                           adminUser.created.strftime("%Y-%m-%d %H:%M:%S"), active, adminUser.promoCode,
                           adminUser.creationCode])
            print table.draw() + "\n"
            return 0
        except ArgumentParserError as e:
            printer.out("In Arguments: " + str(e), printer.ERROR)
            self.help_promote()
        except Exception as e:
            return marketplace_utils.handle_uforge_exception(e)
Exemple #3
0
 def do_list(self, args):
     try:
         #call UForge API
         printer.out("Getting distributions for ["+self.login+"] ...")
         distributions = self.api.Users(self.login).Distros.Getall()
         distributions = distributions.distributions
         if distributions is None or not hasattr(distributions, "distribution"):
             printer.out("No distributions available")
         else:
             table = Texttable(800)
             table.set_cols_dtype(["t","t","t","t","t", "t"])
             table.header(["Id", "Name", "Version", "Architecture", "Release Date", "Profiles"])
             distributions = generics_utils.order_list_object_by(distributions.distribution, "name")
             for distribution in distributions:
                 profiles = self.api.Distributions(distribution.dbId).Profiles.Getall()
                 profiles = profiles.distribProfiles.distribProfile
                 if len(profiles) > 0:
                     profile_text=""
                     for profile in profiles:
                         profile_text+=profile.name+"\n"
                     table.add_row([distribution.dbId, distribution.name, distribution.version, distribution.arch, distribution.releaseDate.strftime("%Y-%m-%d %H:%M:%S") if distribution.releaseDate is not None else "", profile_text])
                 else:
                     table.add_row([distribution.dbId, distribution.name, distribution.version, distribution.arch, distribution.releaseDate.strftime("%Y-%m-%d %H:%M:%S") if distribution.releaseDate is not None else "", "-"])
             print table.draw() + "\n"
             printer.out("Found "+str(len(distributions))+" distributions")
         return 0
     except ArgumentParserError as e:
         printer.out("ERROR: In Arguments: "+str(e), printer.ERROR)
         self.help_list()
     except Exception as e:
         return handle_uforge_exception(e)
Exemple #4
0
 def do_list_changesets(self, arg, opts=None):
     """Show changesets needing review."""
     changesets = requests.get(
         "http://%s/api/v1/changeset/" % self.site, params={"review_status": "needs"}, auth=self.api_auth
     )
     objects = changesets.json().get("objects")
     table = Texttable()
     table.set_deco(Texttable.HEADER)
     table.set_cols_align(["c", "c", "c", "c", "c"])
     table.set_cols_width([5, 20, 15, 15, 10])
     rows = [["ID", "Type", "Classification", "Version Control URL", "Submitted By"]]
     for cs in objects:
         user = requests.get("http://%s%s" % (self.site, cs.get("submitted_by")), auth=self.api_auth)
         user_detail = user.json()
         rows.append(
             [
                 cs.get("id"),
                 cs.get("type"),
                 cs.get("classification"),
                 cs.get("version_control_url"),
                 user_detail.get("name"),
             ]
         )
     table.add_rows(rows)
     print "Changesets That Need To Be Reviewed:"
     print table.draw()
Exemple #5
0
    def do_search(self, args):
        try:
            #add arguments
            doParser = self.arg_search()
            doArgs = doParser.parse_args(shlex.split(args))

            #if the help command is called, parse_args returns None object
            if not doArgs:
                    return 2

            #call UForge API
            printer.out("Search package '"+doArgs.pkg+"' ...")
            distribution = self.api.Distributions(doArgs.id).Get()
            printer.out("for OS '"+distribution.name+"', version "+distribution.version)
            pkgs = self.api.Distributions(distribution.dbId).Pkgs.Getall(Query="name=="+doArgs.pkg)
            pkgs = pkgs.pkgs.pkg
            if pkgs is None or len(pkgs) == 0:
                printer.out("No package found")
            else:
                table = Texttable(800)
                table.set_cols_dtype(["t","t","t","t","t","t","t"])
                table.header(["Name", "Version", "Arch", "Release", "Build date", "Size", "FullName"])
                pkgs = generics_utils.order_list_object_by(pkgs, "name")
                for pkg in pkgs:
                    table.add_row([pkg.name, pkg.version, pkg.arch, pkg.release, pkg.pkgBuildDate.strftime("%Y-%m-%d %H:%M:%S"), size(pkg.size), pkg.fullName])
                print table.draw() + "\n"
                printer.out("Found "+str(len(pkgs))+" packages")
        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: "+str(e), printer.ERROR)
            self.help_search()
        except Exception as e:
            return handle_uforge_exception(e)
Exemple #6
0
def print_price_data(data):

    # Current BTC Price
    # --------------------
    print '\n%s' % colorize('CaVirtex Market\n---------------', colors.CYAN)
    

    status_color = colors.GREEN if data['net'] > 0 else colors.RED

    print '\n%s' % colorize('Price', colors.BLUE)
    
    print '\n%s' % colorize('$%.2f CAD/BTC' % data['current_price'], status_color)

    # Latest Trades
    # ----------------
    print '\n%s\n' % colorize('Latest Trades', colors.BLUE)
    
    trades_table = Texttable()
    trades_table.set_deco(Texttable.HEADER)
    trades_table.set_precision(2)
    trades_table.set_cols_dtype(['f', 'f', 'f', 't'])
    trades_table.add_rows(data['latest_trades'])

    print trades_table.draw()
    
    # Investment Returns
    # ---------------------
    print '\n%s' % colorize('Your Investment', colors.BLUE)

    print '\nNet: %s' % colorize('$%.2f CAD' % data['net'], status_color)
    print '\nVOI: %s' % colorize('$%.2f CAD' % data['voi'], status_color)
    print '\nROI: %s' % colorize('%.2f%%' % data['roi'], status_color)
Exemple #7
0
 def do_list(self, args):
         try:                        
                 #call UForge API
                 printer.out("Getting scans for ["+self.login+"] ...")
                 myScannedInstances = self.api.Users(self.login).Scannedinstances.Get(None, Includescans="true")
                 if myScannedInstances is None or not hasattr(myScannedInstances, 'get_scannedInstance'):
                         printer.out("No scans available")
                         return
                 else:
                         table = Texttable(800)
                         table.set_cols_dtype(["t","t","t","t"])
                         table.header(["Id", "Name", "Status", "Distribution"])
                         myScannedInstances = generics_utils.oder_list_object_by(myScannedInstances.get_scannedInstance(), "name")
                         for myScannedInstance in myScannedInstances:
                                     table.add_row([myScannedInstance.dbId, myScannedInstance.name, "", myScannedInstance.distribution.name + " "+ myScannedInstance.distribution.version + " " + myScannedInstance.distribution.arch])
                                     scans = generics_utils.oder_list_object_by(myScannedInstance.get_scans().get_scan(), "name")
                                     for scan in scans:
                                                 if (scan.status.complete and not scan.status.error and not scan.status.cancelled):
                                                         status = "Done"
                                                 elif(not scan.status.complete and not scan.status.error and not scan.status.cancelled):
                                                         status = str(scan.status.percentage)+"%"
                                                 else:
                                                         status = "Error"
                                                 table.add_row([scan.dbId, "\t"+scan.name, status, "" ])
                                                 
                         print table.draw() + "\n"
                         printer.out("Found "+str(len(myScannedInstances))+" scans")
         except ArgumentParserError as e:
                 printer.out("ERROR: In Arguments: "+str(e), printer.ERROR)
                 self.help_list()
         except Exception as e:        
                 return generics_utils.handle_uforge_exception(e)
Exemple #8
0
    def do_delete(self, args):
        try:
            # add arguments
            doParser = self.arg_delete()
            doArgs = doParser.parse_args(shlex.split(args))

            #if the help command is called, parse_args returns None object
            if not doArgs:
                    return 2

            # call UForge API
            printer.out("Searching account with id [" + doArgs.id + "] ...")
            account = self.api.Users(self.login).Accounts(doArgs.id).Get()
            if account is None:
                printer.out("No Account available", printer.WARNING)
            else:
                table = Texttable(800)
                table.set_cols_dtype(["t", "t", "t", "t"])
                table.header(["Id", "Name", "Type", "Created"])
                table.add_row(
                    [account.dbId, account.name, account.targetPlatform.name, account.created.strftime("%Y-%m-%d %H:%M:%S")])
                print table.draw() + "\n"
                if doArgs.no_confirm:
                    self.api.Users(self.login).Accounts(doArgs.id).Delete()
                    printer.out("Account deleted", printer.OK)
                elif generics_utils.query_yes_no("Do you really want to delete account with id " + str(account.dbId)):
                    self.api.Users(self.login).Accounts(doArgs.id).Delete()
                    printer.out("Account deleted", printer.OK)
            return 0

        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_delete()
        except Exception as e:
            return handle_uforge_exception(e)
Exemple #9
0
    def do_delete(self, args):
        try:
            #add arguments
            doParser = self.arg_delete()
            doArgs = doParser.parse_args(shlex.split(args))

            #if the help command is called, parse_args returns None object
            if not doArgs:
                return 2

            #call UForge API
            printer.out("Searching bundle with id ["+doArgs.id+"] ...")
            myBundle = self.api.Users(self.login).Mysoftware(doArgs.id).Get()
            if myBundle is None or type(myBundle) is not MySoftware:
                printer.out("Bundle not found", printer.WARNING)
            else:
                table = Texttable(800)
                table.set_cols_dtype(["t","t","t", "t","t", "t"])
                table.header(["Id", "Name", "Version", "Description", "Size", "Imported"])
                table.add_row([myBundle.dbId, myBundle.name, myBundle.version, myBundle.description, size(myBundle.size), "X" if myBundle.imported else ""])
                print table.draw() + "\n"
                if generics_utils.query_yes_no("Do you really want to delete bundle with id "+str(myBundle.dbId)):
                    self.api.Users(self.login).Mysoftware(myBundle.dbId).Delete()
                    printer.out("Bundle deleted", printer.OK)


        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: "+str(e), printer.ERROR)
            self.help_delete()
        except Exception as e:
            return handle_uforge_exception(e)
def print_table(prefix, items):
    table = Texttable(max_width=160)
    table.set_deco(Texttable.HEADER)
    table.header(['%s_id' % prefix, '%s_updated' % prefix, '%s_fk' % prefix])
    for key, values in items.iteritems():
        table.add_row([key, values.get('updated'), values.get('opposite_id')])
    print table.draw() + "\n"
def log_api_exception(sender, exception, **extra):
    fallback_message = u'Exception raised inside `log_api_exception`!'
    try:
        messages = []

        # If debugging or testing, log verbose request, browser, and user information
        if (sender.debug or sender.testing) and sender.config.get('API_LOG_EXTRA_REQUEST_INFO_ON_REQUEST_EXCEPTION'):
            request_info, browser_info, user_info = gather_api_exception_log_data()

            if request_info:
                table = Texttable()
                table.set_cols_width([10, 62])  # Accommodates an overall table width of 79 characters
                table.add_rows([('Request', 'Information')] + request_info)
                messages.append(table.draw())

            if browser_info:
                table = Texttable()
                table.add_rows([('Browser', 'Information')] + browser_info)
                messages.append(table.draw())

            if user_info:
                table = Texttable()
                table.add_rows([('User', 'Information')] + user_info)
                messages.append(table.draw())
        else:
            messages.append(u'{0}'.format(exception))

        message = '\n\n'.join(messages) if messages else None
    except Exception:
        message = fallback_message
    sender.logger.exception(message, **extra)
Exemple #12
0
    def print_diff_as_table(self, include=None, exclude=None,
                            deco_border=False, deco_header=False,
                            deco_hlines=False, deco_vlines=False):
        diffdict = self.diff(include, exclude)
        if not diffdict:
            return

        from texttable import Texttable
        table = Texttable()
        deco = 0
        if deco_border:
            deco |= Texttable.BORDER
        if deco_header:
            deco |= Texttable.HEADER
        if deco_hlines:
            deco |= Texttable.HLINES
        if deco_vlines:
            deco |= Texttable.VLINES
        table.set_deco(deco)

        sortedkey = sorted(diffdict)
        table.add_rows(
            [[''] + self._name] +
            [[keystr] + [self._getrepr(diffdict[keystr], name)
                         for name in self._name]
             for keystr in sortedkey]
            )
        print table.draw()
def print_mapping(prefix, key, items):
    table = Texttable(max_width=160)
    table.set_deco(Texttable.HEADER)
    table.header(['%s_%s' % (prefix, key),  '%s_fk' % prefix])
    for key, value in items.iteritems():
        table.add_row([key, value])
    print table.draw() + "\n"
Exemple #14
0
 def do_list(self, args):
     try:
         #call UForge API
         printer.out("Getting templates for ["+self.login+"] ...")
         appliances = self.api.Users(self.login).Appliances().Getall()
         appliances = appliances.appliances
         if appliances is None or not hasattr(appliances, 'appliance'):
             printer.out("No template")
         else:
             images = self.api.Users(self.login).Images.Get()
             images = images.images
             table = Texttable(800)
             table.set_cols_dtype(["t","t","t","t","t","t","t","t","t","t"])
             table.header(["Id", "Name", "Version", "OS", "Created", "Last modified", "# Imgs", "Updates", "Imp", "Shared"])
             appliances = generics_utils.order_list_object_by(appliances.appliance, "name")
             for appliance in appliances:
                 nbImage=0
                 if images is not None and hasattr(images, 'image'):
                     for image in images.image:
                         if hasattr(image, 'applianceUri') and image.applianceUri == appliance.uri:
                             nbImage+=1
                 table.add_row([appliance.dbId, appliance.name, str(appliance.version), appliance.distributionName+" "+appliance.archName,
                                appliance.created.strftime("%Y-%m-%d %H:%M:%S"), appliance.lastModified.strftime("%Y-%m-%d %H:%M:%S"), nbImage, appliance.nbUpdates, "X" if appliance.imported else "", "X" if appliance.shared else ""])
             print table.draw() + "\n"
             printer.out("Found "+str(len(appliances))+" templates")
         return 0
     except ArgumentParserError as e:
         printer.out("ERROR: In Arguments: "+str(e), printer.ERROR)
         self.help_list()
     except Exception as e:
         return handle_uforge_exception(e)
Exemple #15
0
    def do_delete(self, args):
        try:
            #add arguments
            doParser = self.arg_delete()
            try:
                doArgs = doParser.parse_args(args.split())
            except SystemExit as e:
                return
            #call UForge API
            printer.out("Searching template with id ["+doArgs.id+"] ...")
            myAppliance = self.api.Users(self.login).Appliances(doArgs.id).Get()
            if myAppliance is None or type(myAppliance) is not Appliance:
                printer.out("Template not found")
            else:
                table = Texttable(800)
                table.set_cols_dtype(["t","t","t","t","t","t","t","t","t","t"])
                table.header(["Id", "Name", "Version", "OS", "Created", "Last modified", "# Imgs", "Updates", "Imp", "Shared"])
                table.add_row([myAppliance.dbId, myAppliance.name, str(myAppliance.version), myAppliance.distributionName+" "+myAppliance.archName,
                               myAppliance.created.strftime("%Y-%m-%d %H:%M:%S"), myAppliance.lastModified.strftime("%Y-%m-%d %H:%M:%S"), len(myAppliance.imageUris.uri),myAppliance.nbUpdates, "X" if myAppliance.imported else "", "X" if myAppliance.shared else ""])
                print table.draw() + "\n"

                if doArgs.no_confirm:
                    self.api.Users(self.login).Appliances(myAppliance.dbId).Delete()
                    printer.out("Template deleted", printer.OK)
                elif generics_utils.query_yes_no("Do you really want to delete template with id "+str(myAppliance.dbId)):
                    self.api.Users(self.login).Appliances(myAppliance.dbId).Delete()
                    printer.out("Template deleted", printer.OK)
            return 0
        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: "+str(e), printer.ERROR)
            self.help_delete()
        except Exception as e:
            return handle_uforge_exception(e)
def main(args):
    """
    process each argument
    """
    table = Texttable()
    table.set_cols_align(["r", "r", "r", "r", "r"])
    rows = [["Number", "File Name", "File Size", "Video Duration (H:MM:SS)", "Conversion Time"]]
    total_time = 0.0
    total_file_size = 0

    for index, arg in enumerate(args, start=1):
        timer = utils.Timer()
        with timer:
            result = resize(arg, (index, len(args)))
        #
        result.elapsed_time = timer.elapsed_time()
        rows.append([index,
                     result.file_name,
                     utils.sizeof_fmt(result.file_size),
                     utils.sec_to_hh_mm_ss(utils.get_video_length(result.file_name)) if result.file_name else "--",
                     "{0:.1f} sec.".format(result.elapsed_time) if result.status else FAILED])
        #
        if rows[-1][-1] != FAILED:
            total_time += result.elapsed_time
        total_file_size += result.file_size

    table.add_rows(rows)
    print table.draw()
    print 'Total file size:', utils.sizeof_fmt(total_file_size)
    print 'Total time: {0} (H:MM:SS)'.format(utils.sec_to_hh_mm_ss(total_time))
    print utils.get_unix_date()
Exemple #17
0
    def do_list(self, args):
        try:
            #call UForge API
            printer.out("Getting generation formats for ["+self.login+"] ...")
            targetFormatsUser = self.api.Users(self.login).Targetformats.Getall()
            if targetFormatsUser is None or len(targetFormatsUser.targetFormats.targetFormat) == 0:
                printer.out("No generation formats available")
                return 0
            else:
                targetFormatsUser = generics_utils.order_list_object_by(targetFormatsUser.targetFormats.targetFormat,"name")
                table = Texttable(200)
                table.set_cols_align(["l", "l", "l", "l", "l", "c"])
                table.header(["Name", "Format", "Category", "Type", "CredAccountType", "Access"])
                for item in targetFormatsUser:
                    if item.access:
                        access = "X"
                    else:
                        access = ""
                    if item.credAccountType is None:
                        credAccountType = ""
                    else:
                        credAccountType = item.credAccountType
                    table.add_row(
                        [item.name, item.format.name, item.category.name, item.type, credAccountType,
                         access])
                print table.draw() + "\n"
            return 0

        except ArgumentParserError as e:
            printer.out("In Arguments: " + str(e), printer.ERROR)
            self.help_list()
        except Exception as e:
            return handle_uforge_exception(e)
Exemple #18
0
        def do_info(self, args):
                try:
                        doParser = self.arg_info()
                        doArgs = doParser.parse_args(shlex.split(args))
                        
                        printer.out("Getting user ["+doArgs.account+"] ...")
                        user = self.api.Users(doArgs.account).Get()
                        if user is None:
                                printer.out("user "+ doArgs.account +" does not exist", printer.ERROR)
                        else:
                                if user.active:
                                        active = "X"
                                else:
                                        active = ""

                                printer.out("Informations about " + doArgs.account + ":",)
                                table = Texttable(200)
                                table.set_cols_align(["c", "l", "c", "c", "c", "c", "c", "c"])
                                table.header(["Login", "Email", "Lastname",  "Firstname",  "Created", "Active", "Promo Code", "Creation Code"])
                                table.add_row([user.loginName, user.email, user.surname , user.firstName, user.created.strftime("%Y-%m-%d %H:%M:%S"), active, user.promoCode, user.creationCode])
                                print table.draw() + "\n"
                        return 0

                except ArgumentParserError as e:
                        printer.out("In Arguments: "+str(e), printer.ERROR)
                        self.help_info()
                except Exception as e:
                        return handle_uforge_exception(e)
    def do_disable(self, args):
        try:
            doParser = self.arg_disable()
            doArgs = doParser.parse_args(shlex.split(args))

            printer.out("Disabling user [" + doArgs.account + "] ...")
            user = self.api.Users(doArgs.account).Get()
            if user is None:
                printer.out("user " + doArgs.account + "does not exist", printer.ERROR)
            else:
                if user.active == False:
                    printer.out("User [" + doArgs.account + "] is already disabled", printer.ERROR)
                else:
                    user.active = False
                    self.api.Users(doArgs.account).Update(body=user)
                    printer.out("User [" + doArgs.account + "] is now disabled", printer.OK)
                if user.active == True:
                    actived = "X"
                else:
                    actived = ""
                printer.out("Informations about [" + doArgs.account + "] :")
                table = Texttable(200)
                table.set_cols_align(["c", "l", "c", "c", "c", "c", "c", "c"])
                table.header(
                    ["Login", "Email", "Lastname", "Firstname", "Created", "Active", "Promo Code", "Creation Code"])
                table.add_row([user.loginName, user.email, user.surname, user.firstName,
                               user.created.strftime("%Y-%m-%d %H:%M:%S"), actived, user.promoCode, user.creationCode])
                print table.draw() + "\n"
            return 0
        except ArgumentParserError as e:
            printer.out("In Arguments: " + str(e), printer.ERROR)
            self.help_disable()
        except Exception as e:
            return marketplace_utils.handle_uforge_exception(e)
Exemple #20
0
def top(db):
    count_query = '''
    SELECT count(*)
    FROM commands
    WHERE
        timestamp > ?
    '''
    percentage = 100 / float(execute_scalar(db, count_query, TIMESTAMP))

    query = '''
    SELECT count(*) AS counts, command
    FROM commands
    WHERE timestamp > ?
    GROUP BY command
    ORDER BY counts DESC
    LIMIT 20
    '''

    table = Texttable()
    table.set_deco(Texttable.HEADER)
    table.set_cols_align(('r', 'r', 'l'))
    table.header(('count', '%', 'command'))
    for row in db.execute(query, (TIMESTAMP,)):
        table.add_row((row[0], int(row[0]) * percentage, row[1]))
    print table.draw()
Exemple #21
0
def sub(db, command, *filters):
    counts = collections.defaultdict(int)
    user_filter = ' '.join(itertools.chain([command], filters))
    total = 0

    query = '''
    SELECT user_string
    FROM commands
    WHERE
        timestamp > ?
        AND command = ?
    '''

    for row in db.execute(query, (TIMESTAMP, command)):
        command = normalize_user_string(row[0])
        if command.startswith(user_filter):
            counts[command] += 1
            total += 1
    percentage = 100 / float(total)

    table = Texttable()
    table.set_deco(Texttable.HEADER)
    table.set_cols_align(('r', 'r', 'l'))
    table.set_cols_width((5, 6, 75))
    table.header(('count', '%', 'command'))
    for key, value in sorted(counts.iteritems(), key=lambda (k, v): (v, k), reverse=True)[:20]:
        table.add_row((value, value * percentage, key))
    print table.draw()
Exemple #22
0
 def do_search(self, args):
         try:
                 #add arguments
                 doParser = self.arg_search()
                 try:
                         doArgs = doParser.parse_args(args.split())
                 except SystemExit as e:
                         return
                 #call UForge API
                 printer.out("Search package '"+doArgs.pkg+"' ...")
                 distribution = self.api.Distributions(doArgs.id).Get()
                 printer.out("for OS '"+distribution.name+"', version "+distribution.version)
                 pkgs = self.api.Distributions(distribution.dbId).Pkgs.Getall(Search=doArgs.pkg, Version=distribution.version)
                 
                 if pkgs is None or not hasattr(pkgs, 'pkgs'):
                         printer.out("No package found")
                 else:
                     table = Texttable(800)
                     table.set_cols_dtype(["t","t","t","t","t","t"])
                     table.header(["Name", "Version", "Arch", "Release", "Build date", "Size"])
                     pkgs = generics_utils.oder_list_object_by(pkgs.get_pkgs().get_pkg(), "name")
                     for pkg in pkgs:
                             table.add_row([pkg.name, pkg.version, pkg.arch, pkg.release, pkg.pkgBuildDate.strftime("%Y-%m-%d %H:%M:%S"), size(pkg.size)])
                     print table.draw() + "\n"
                     printer.out("Found "+str(len(pkgs))+" packages")
         except ArgumentParserError as e:
                 printer.out("ERROR: In Arguments: "+str(e), printer.ERROR)
                 self.help_search()
         except Exception as e:        
                 generics_utils.print_uforge_exception(e)
def dump(relation):
  width,height = term_size()
  table = Texttable(width)


  sample, iterator = tee(relation)


  table.add_rows(take(1000,sample))
  table._compute_cols_width()
  del sample
  
  table.reset()

  table.set_deco(Texttable.HEADER)
  table.header([f.name for f in relation.schema.fields])



  rows = take(height-3, iterator)

  try:
    while rows:
      table.add_rows(rows, header=False)
      print table.draw()
      rows = take(height-3, iterator)
      if rows:
        raw_input("-- enter for more ^c to quit --")
  except KeyboardInterrupt:
    print
Exemple #24
0
    def do_info_draw_general(self, info_image):
        table = Texttable(0)
        table.set_cols_dtype(["a", "t"])
        table.set_cols_align(["l", "l"])

        table.add_row(["Name", info_image.name])
        table.add_row(["Format", info_image.targetFormat.name])
        table.add_row(["Id", info_image.dbId])
        table.add_row(["Version", info_image.version])
        table.add_row(["Revision", info_image.revision])
        table.add_row(["Uri", info_image.uri])

        self.do_info_draw_source(info_image.parentUri, table)

        table.add_row(["Created", info_image.created.strftime("%Y-%m-%d %H:%M:%S")])
        table.add_row(["Size", size(info_image.fileSize)])
        table.add_row(["Compressed", "Yes" if info_image.compress else "No"])

        if self.is_docker_based(info_image.targetFormat.format.name):
            registring_name = None
            if info_image.status.complete:
                registring_name = info_image.registeringName
            table.add_row(["RegisteringName",registring_name])
            table.add_row(["Entrypoint", info_image.entrypoint.replace("\\", "")])

        self.do_info_draw_generation(info_image, table)

        print table.draw() + "\n"
Exemple #25
0
    def do_info_draw_publication(self, info_image):
        printer.out("Information about publications:")
        pimages = self.api.Users(self.login).Pimages.Getall()
        table = Texttable(0)
        table.set_cols_align(["l", "l"])

        has_pimage = False
        for pimage in pimages.publishImages.publishImage:
            if pimage.imageUri == info_image.uri:
                has_pimage = True
                cloud_id = None
                publish_status = image_utils.get_message_from_status(pimage.status)
                if not publish_status:
                    publish_status = "Publishing"

                if publish_status == "Done":
                    cloud_id = pimage.cloudId
                    format_name = info_image.targetFormat.format.name
                    if format_name == "docker" or format_name == "openshift":
                        cloud_id = pimage.namespace + "/" + pimage.repositoryName + ":" + pimage.tagName

                table.add_row([publish_status, cloud_id])

        if has_pimage:
            table.header(["Status", "Cloud Id"])
            print table.draw() + "\n"
        else:
            printer.out("No publication")
        def do_list(self, args):
                try:
                        doParser = self.arg_list()
                        doArgs = doParser.parse_args(shlex.split(args))

                        printer.out("Getting entitlements list of the UForge :")
                        entList = self.api.Entitlements.Getall()
                        if entList is None:
                                printer.out("No entitlements found.", printer.OK)
                        else:
                                entList=generics_utils.order_list_object_by(entList.entitlements.entitlement, "name")
                                printer.out("Entitlement list for the UForge :")
                                table = Texttable(200)
                                table.set_cols_align(["l", "l"])
                                table.header(["Name", "Description"])
                                table.set_cols_width([30,60])
                                for item in entList:
                                        table.add_row([item.name, item.description])
                                print table.draw() + "\n"
                        return 0

                except ArgumentParserError as e:
                        printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
                        self.help_list()
                except Exception as e:
                        return handle_uforge_exception(e)
Exemple #27
0
def find_commands(db, *filters):
    user_filter = '\s+'.join(filters)
    user_re = re.compile(user_filter)
    RE_CACHE[user_filter] = user_re

    query = '''
    SELECT hostname, timestamp, duration, user_string
    FROM commands
    WHERE timestamp > ? AND user_string REGEXP ?
    ORDER BY timestamp
    '''

    table = Texttable()
    table.set_deco(Texttable.HEADER)
    table.set_cols_align(('l', 'r', 'r', 'l'))
    table.header(('host', 'date', 'duration', 'command'))

    host_width = 6
    max_command_width = 9
    now = time.time()
    for row in db.execute(query, (TIMESTAMP, user_filter)):
        host_width = max(host_width, len(row[0]))
        max_command_width = max(max_command_width, len(row[3]))
        table.add_row((
            row[0],
            format_time(row[1], now),
            format_duration(row[2]) if row[2] > 0 else '',
            highlight(row[3], user_re)))

    table.set_cols_width((host_width, 30, 10, max_command_width + 2))

    print table.draw()
def getAuccuracy( train, testSet, k ):
	totalCount = len(testSet)
	correctCount = 0.0;

	# Init ConfusionMatrix
	confusionMatrix = { }
	for i in featuresList:
		for j in featuresList:
			confusionMatrix[ (i,j) ] = 0

	for i in range(len(testSet)):
		predition = getPrediction( getDistancesOfKSimilarSets( train, testSet[i], k ) )
		if predition == testSet[i][-1]:
			correctCount+=1;
		confusionMatrix[ testSet[i][-1], predition ] += 1

	print "Confusion Matrix"
	from texttable import Texttable
	table=[]
	row=[""]
	row.extend(featuresList)
	table.append(row)
	for i in featuresList:
		row=[i]
		for j in featuresList:
			row.append( confusionMatrix[ (i,j) ])
		table.append(row)
	T=Texttable();
	T.add_rows(table)
	print T.draw();

	return correctCount*1.0/totalCount;
Exemple #29
0
    def do_list(self, args):
        try:
            org_name = None
            if args:
                do_parser = self.arg_list()
                try:
                    do_args = do_parser.parse_args(shlex.split(args))
                except SystemExit as e:
                    return
                org_name = do_args.org

            # call UForge API
            printer.out("Getting all the roles for the organization...")
            org = org_utils.org_get(self.api, org_name)
            all_roles = self.api.Orgs(org.dbId).Roles().Getall(None)

            table = Texttable(200)
            table.set_cols_align(["c", "c"])
            table.header(["Name", "Description"])
            for role in all_roles.roles.role:
                table.add_row([role.name, role.description])
            print table.draw() + "\n"
            return 0
        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_list()
        except Exception as e:
            return marketplace_utils.handle_uforge_exception(e)
Exemple #30
0
        def do_list(self, args):
                try:
                        doParser = self.arg_list()
                        doArgs = doParser.parse_args(shlex.split(args))

                        printer.out("Getting roles and their entitlements for user [" + doArgs.account + "]:\n")
                        roles = self.api.Users(doArgs.account).Roles.Getall()

                        table = Texttable(200)
                        table.set_cols_align(["l", "l"])
                        table.header(["Name", "Description"])
                        table.set_cols_width([30,60])
                        for role in roles.roles.role:
                                table.add_row([role.name.upper(), role.description])
                                for entitlement in role.entitlements.entitlement:
                                        table.add_row(["===> " + entitlement.name, entitlement.description])

                        printer.out("Role entitlements are represented with \"===>\".", printer.INFO)
                        print table.draw() + "\n"
                        return 0

                except ArgumentParserError as e:
                        printer.out("In Arguments: "+str(e), printer.ERROR)
                        self.help_list()
                except Exception as e:
                        return handle_uforge_exception(e)
Exemple #31
0
    def display_results(self,
                        endpoints,
                        fields,
                        sort_by=0,
                        max_width=0,
                        unique=False,
                        nonzero=False,
                        output_format='table',
                        ipv4_only=True,
                        ipv6_only=False,
                        ipv4_and_ipv6=False):
        matrix = []
        fields_lookup = {
            'id': (GetData._get_name, 0),
            'mac': (GetData._get_mac, 1),
            'mac address': (GetData._get_mac, 1),
            'switch': (GetData._get_switch, 2),
            'port': (GetData._get_port, 3),
            'vlan': (GetData._get_vlan, 4),
            'ipv4': (GetData._get_ipv4, 5),
            'ipv4 subnet': (GetData._get_ipv4_subnet, 6),
            'ipv6': (GetData._get_ipv6, 7),
            'ipv6 subnet': (GetData._get_ipv6_subnet, 8),
            'ethernet vendor': (GetData._get_ether_vendor, 9),
            'ignored': (GetData._get_ignored, 10),
            'state': (GetData._get_state, 11),
            'next state': (GetData._get_next_state, 12),
            'first seen': (GetData._get_first_seen, 13),
            'last seen': (GetData._get_last_seen, 14),
            'previous states': (GetData._get_prev_states, 15),
            'ipv4 os': (GetData._get_ipv4_os, 16),
            'ipv4 os\n(p0f)': (GetData._get_ipv4_os, 16),
            'ipv6 os': (GetData._get_ipv6_os, 17),
            'ipv6 os\n(p0f)': (GetData._get_ipv6_os, 17),
            'previous ipv4 oses': (GetData._get_prev_ipv4_oses, 18),
            'previous ipv4 oses\n(p0f)': (GetData._get_prev_ipv4_oses, 18),
            'previous ipv6 oses': (GetData._get_prev_ipv6_oses, 19),
            'previous ipv6 oses\n(p0f)': (GetData._get_prev_ipv6_oses, 19),
            'role': (GetData._get_role, 20),
            'role\n(networkml)': (GetData._get_role, 20),
            'role confidence': (GetData._get_role_confidence, 21),
            'role confidence\n(networkml)': (GetData._get_role_confidence, 21),
            'previous roles': (GetData._get_prev_roles, 22),
            'previous roles\n(networkml)': (GetData._get_prev_roles, 22),
            'previous role confidences':
            (GetData._get_prev_role_confidences, 23),
            'previous role confidences\n(networkml)':
            (GetData._get_prev_role_confidences, 23),
            'behavior': (GetData._get_behavior, 24),
            'behavior\n(networkml)': (GetData._get_behavior, 24),
            'previous behaviors': (GetData._get_prev_behaviors, 25),
            'previous behaviors\n(networkml)':
            (GetData._get_prev_behaviors, 25),
            'ipv4 rdns': (GetData._get_ipv4_rdns, 26),
            'ipv6 rdns': (GetData._get_ipv6_rdns, 27),
            'sdn controller type': (GetData._get_controller_type, 28),
            'sdn controller uri': (GetData._get_controller, 29)
        }
        for index, field in enumerate(fields):
            if ipv4_only:
                if '6' in field:
                    fields[index] = field.replace('6', '4')
            if ipv6_only:
                if '4' in field:
                    fields[index] = field.replace('4', '6')
        if ipv4_and_ipv6:
            for index, field in enumerate(fields):
                if '4' in field:
                    if field.replace('4', '6') not in fields:
                        fields.insert(index + 1, field.replace('4', '6'))
                if '6' in field:
                    if field.replace('6', '4') not in fields:
                        fields.insert(index + 1, field.replace('6', '4'))

        if nonzero or unique:
            records = []
            for endpoint in endpoints:
                record = []
                for field in fields:
                    record.append(fields_lookup[field.lower()][0](endpoint))
                # remove rows that are all zero or 'NO DATA'
                if not nonzero or not all(item == '0' or item == 'NO DATA'
                                          for item in record):
                    records.append(record)

            # remove columns that are all zero or 'NO DATA'
            del_columns = []
            for i in range(len(fields)):
                marked = False
                if nonzero and all(item[i] == '0' or item[i] == 'NO DATA'
                                   for item in records):
                    del_columns.append(i)
                    marked = True
                if unique and not marked:
                    column_vals = [item[i] for item in records]
                    if len(set(column_vals)) == 1:
                        del_columns.append(i)
            del_columns.reverse()
            for val in del_columns:
                for row in records:
                    del row[val]
                del fields[val]
            if len(fields) > 0:
                if unique:
                    u_records = set(map(tuple, records))
                    matrix = list(map(list, u_records))
                else:
                    matrix = records
        if not nonzero and not unique:
            for endpoint in endpoints:
                record = []
                for field in fields:
                    record.append(fields_lookup[field.lower()][0](endpoint))
                matrix.append(record)
        results = ''
        if len(matrix) > 0:
            matrix = sorted(matrix, key=lambda endpoint: endpoint[sort_by])
            # swap out field names for header
            fields_header = []
            for field in fields:
                fields_header.append(
                    self.all_fields[fields_lookup[field.lower()][1]])
            # set the header
            matrix.insert(0, fields_header)
            table = Texttable(max_width=max_width)
            # make all the column types be text
            table.set_cols_dtype(['t'] * len(fields))
            table.add_rows(matrix)
            results = table.draw()
        else:
            results = 'No results found for that query.'
        return results
Exemple #32
0
done = False
#here is the animation
def animate():
    for c in itertools.cycle(['|', '/', '-', "\\"]):
        if done:
            break
        sys.stdout.write('\r  loading ' + c)
        sys.stdout.flush()
        time.sleep(0.1)
    sys.stdout.write('\rStay Home     \n')
    sys.stdout.write('\n')

tim = threading.Thread(target=animate)
tim.start()



worldmetter = requests.get('https://www.worldometers.info/coronavirus/')
soup = BeautifulSoup(worldmetter.content, 'html.parser')
totalNumber = soup.findAll("div", {"class" : "maincounter-number"})
totalcases = totalNumber[0].text
death = totalNumber[1].text
recovred = totalNumber[2].text

t = Texttable()
t.add_rows([['Total cases', 'Deaths', 'Recovered'], [totalcases, death, recovred]])
print('\n')
print(t.draw())

done = True
Exemple #33
0
def scrapeRanges(rangeUrl):
    Headers = { "User-Agent": 'Mozila/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/75.0.3770.100 Safari/537.36' }
    page = requests.get(rangeUrl, headers = Headers)
    soup = bs(page.content,'html.parser')

    for i in soup.findAll('div',{'class','finder_snipet_wrap'}):
        for x in i.findAll('div',{'class','filter filer_finder'}):
            for y in x.findAll('div',{'class','filter-grey-bar'}):
                for rating in y.findAll('div',{'class','rating_box_new_list'}):
                    lapSpecScore.append(rating.getText())
                for nt in y.findAll('h3'):
                    for name in nt.findAll('a'):
                        lapUrls.append('https://91mobiles.com'+name['href'])
                        laptops.append(name.getText())
                for a in y.findAll('div',{'class':'filter-right'}):
                    for amt in a.findAll('span',{'class':'price price_padding'}):
                        lapPrice.append(amt.getText())

    t = Texttable()
    ro = []
    ro.append(['S.no','Name', 'Age'])
    for ins in range(0,len(lapPrice)):
        ro.append([str(ins+1),laptops[ins],lapPrice[ins]])

    t.add_rows(ro)
    os.system('cls')
    print(f'{bcolors.WARNING}'+t.draw())

    theOne = int(input('Which Laptop?'))
    theOnePage = requests.get(lapUrls[theOne-1])
    theOneSoup = bs(theOnePage.content, 'html.parser')

    try:
        price = theOneSoup.find('span',{'itemprop':'price'}).getText()
        name = theOneSoup.find('span',{'itemprop':'name'}).getText()
    except AttributeError:
        print('wait')

    properties(theOneSoup)
    valus(theOneSoup)

    print(len(propertys))
    print(len(values))

    if len(propertys) == len(values):
        arrow = "----->"
        det = f"{bcolors.HEADER}Laptop Details{bcolors.ENDC}"
        spec = f"{bcolors.WARNING}Specifications{bcolors.ENDC}"
        print('\n')
        print(f"{det:^100}")
        print('\nName: \t\t\t'+f'{bcolors.BOLD}'+name+f'{bcolors.ENDC}'+'\nPrice(Today):\t\t'+f'{bcolors.BOLD}'+price+f'{bcolors.ENDC}'+'\n\n')
        spcaer = int(os.get_terminal_size().columns/2)
        print('\n')
        
        specTable = PrettyTable()
        specTable.field_names = [Back.WHITE+'Property'+Style.RESET_ALL,Back.WHITE+'Value'+Style.RESET_ALL] 
        specRow = []
        for ins in range(0,len(propertys)):
            specRow.append([propertys[ins],values[ins]])

        specTable.align[Back.WHITE+'Property'+Style.RESET_ALL] = 'l'
        specTable.align[Back.WHITE+'Value'+Style.RESET_ALL] = 'l'
        specTable.align['value'] = 'l'
        specTable.add_rows(specRow)
        print(specTable)
        input("Press Enter to continue...")

    else:
        print("There is an un-known error occurred")
Exemple #34
0
table.set_cols_width(["20", "20", "8", "8", "20", "8", "8"])
header = [
    "Pool", "Image", "Size(Mb)", "Features", "Lockers", "Str_size", "Str_cnt"
]
keys = ["features", "list_lockers", "stripe_unit", "stripe_count"]
table.header(map(lambda x: get_color_string(bcolors.YELLOW, x), header))

with rados.Rados(conffile='/etc/ceph/ceph.conf') as cluster:
    pool_list = cluster.list_pools()
    for pool in pool_list:
        table.add_row(
            [get_color_string(bcolors.GREEN, pool), "", "", "", "", "", ""])
        with cluster.open_ioctx(pool) as ioctx:
            rbd_inst = rbd.RBD()
            image_list = rbd_inst.list(ioctx)
            for image_name in image_list:
                with rbd.Image(ioctx, image_name) as image:
                    image_size = str(image.size() / 1024**2)
                    table.add_row(["", image_name, image_size] +
                                  map(lambda x: str(getattr(image, x)
                                                    ()), keys))
        if pool != pool_list[-1]:
            table.add_row([
                "-" * 20, "-" * 20, "-" * 8, "-" * 8, "-" * 20, "-" * 8,
                "-" * 8
            ])

print(pools_table.draw())
print
print(table.draw())
Exemple #35
0
    return movies_info


#主程序
#输入 : 测试数据集合
if __name__ == '__main__':
    reload(sys)
    sys.setdefaultencoding('utf-8')
    movies = getMoviesList("/Users/wuyinghao/Downloads/ml-100k/u.item")
    recommend_list, user_movie, items_movie, neighbors = recommendByUserFC(
        "/Users/wuyinghao/Downloads/ml-100k/u.data", 179, 80)
    neighbors_id = [i[1] for i in neighbors]
    table = Texttable()
    table.set_deco(Texttable.HEADER)
    table.set_cols_dtype([
        't',  # text 
        't',  # float (decimal)
        't'
    ])  # automatic
    table.set_cols_align(["l", "l", "l"])
    rows = []
    rows.append([u"movie name", u"release", u"from userid"])
    for movie_id in recommend_list[:20]:
        from_user = []
        for user_id in items_movie[movie_id]:
            if user_id in neighbors_id:
                from_user.append(user_id)
        rows.append([movies[movie_id][0], movies[movie_id][1], ""])
    table.add_rows(rows)
    print table.draw()
Exemple #36
0
def crearGrafo():
    g = Grafo()
    while 1:

        menu = qprompt.Menu()
        menu.add("1", "Agregar nodo")
        menu.add("2", "Agregar arista")
        menu.add("3", "Bellman-Ford")
        menu.add("4", "Mostrar")
        menu.add("5", "Salir")
        choice = menu.show()

        if choice == "1":
            valor = input("Ingrese el numero del nodo: ")
            if valor not in g:
                g.agregarNodo(valor)
            else:
                print('El nodo ya existe.')
        elif choice == '2':
            origen = input("Ingrese el nodo origen: ")
            destino = input("Ingrese el nodo destino: ")
            peso = input("Ingrese el peso: ")
            if origen not in g.nodos:
                print('El nodo {} no existe'.format(origen))
            elif destino not in g.nodos:
                print('El nodo {} no existe.'.format(destino))
            else:
                if not g.existe(origen, destino):
                    g.agregarArista(origen, destino, peso)
                else:
                    print('La arista ya existe.')
     
        elif choice == '3':
            valor = input("Ingrese el nodo: ")
            origen = g.getNodo(valor)
            distancia = bellman_ford(g, origen)    
            
            if distancia != 1:
                
            
                nodos = []
                for nodo in distancia:
                    nodos.append((nodo.get_key(), distancia[nodo]))
                nodos =sorted(nodos, key=lambda tup: tup[0])        
                
                tabla = Texttable()
                row = ["Nodo"]
                row2 = ["Distancia"]
                for nodo in nodos:
                    row.append(nodo[0])
                    row2.append(nodo[1])
                tabla.add_row(row)
                tabla.add_row(row2)
                print(tabla.draw())
            else:
                print("Contiene pesos negativos")
        elif choice == "4":
            print('Nodos: ')
            for x in g:
                print(x.get_key())
            print()
     
            print('Aristas: ')
            for x in g:
                for destino in x.getNodoAdyacentes():
                    w = x.peso(destino)
                    print('(Origen=: {}, Destino: {}, peso: {}) '.format(x.get_key(),
                                                                 destino.get_key(), w))
            print()
     
        elif choice == '5':
            break
        # mean_accuracy.insert(0, "Average")
        median_accuracy.append(list(np.median(results, axis=0)))
        results = list(np.median(results, axis=0))
        results.insert(0, "Median")
        results.append(start + k * step_size)
        # times_the_best.insert(0, "times the best")
        # mean_accuracy_outliers = list(np.mean(results_outlier, axis=0))
        # mean_accuracy_outliers.insert(0, "Average")
        median_accuracy_outliers.append(
            list(np.median(results_outlier, axis=0)))
        results_outliers = list(np.median(results_outlier, axis=0))
        results_outliers.insert(0, "Median")
        results_outliers.append(start + k * step_size)
        # times_the_best_outliers.insert(0, "times the best")
        # table.add_row(mean_accuracy)
        table.add_row(results)
        # table.add_row(times_the_best)
        # table.add_row(mean_accuracy_outliers)
        table.add_row(results_outliers)
        # table.add_row(times_the_best_outliers)
    median_accuracy = np.array(median_accuracy)
    median_accuracy_outliers = np.array(median_accuracy_outliers)
    np.savetxt("results_synth/median_" + parameter_name + ".csv",
               median_accuracy,
               delimiter=", ")
    np.savetxt("results_synth/median_" + parameter_name + "_outliers.csv",
               median_accuracy_outliers,
               delimiter=", ")
    print(table.draw() + "\n")
    print(draw_latex(table) + "\n")
    async def run(self):

      msgCnt = 0

      # Set device client from Azure IoT SDK and connect
      device_client = None
      first = True
      self.setColor(self.RedColor)
      await asyncio.sleep(5.0)

      try:
        
        # Connect our Device for Telemetry and Updates
        for device in self.device_cache["Devices"]:
          self.logger.info("[Refrideration Monitor] CONNECTING TO IOT CENTRAL: %s" % device["Device"]["Name"])
          device_client = DeviceClient(self.logger, device["Device"]["Name"])
          await device_client.connect()

        # Subscribe to the Telemetry Server Publication of Telemetry Data
        pub.subscribe(self.listener, pub.ALL_TOPICS)

        while True:

          if first == False:
            print("Waiting [%s] Seconds before reading Sensors (defined by TelemetryFrequencyInSeconds)" % self.config["TelemetryFrequencyInSeconds"])
            self.setColor(self.BlueColor)
            await asyncio.sleep(self.config["TelemetryFrequencyInSeconds"])
          else:
            first = False

          msgCnt = msgCnt + 1

          # READ TEMP & HUMIDITY
          self.Temperature = self.sensor.temperature
          self.Humidity = self.sensor.relative_humidity
          
          # Subscribe to the Telemetry Server Publication of Telemetry Data
          self.subscribed_payload = self.listener.read_payload()
          print("***")
          print(self.subscribed_payload)
          print("***")
          
          # READ FROM SERIAL TELEMETRY
          serial_read = self.serial_emulator_port.readline()
          print("read serial")
          print(serial_read)

          # Conversions
          if self.config["TemperatureFormat"] == "F":
            self.Temperature = self.convert2fahrenheit(self.Temperature)

          table = Texttable()
          table.set_deco(Texttable.HEADER)
          table.set_cols_dtype(["t", "f", "f", "f"])
          table.set_cols_align(["l", "r", "r", "r"])
          table.add_rows([["Sensor",  "Temperature[{0}]".format(self.config["TemperatureFormat"]), "Last Temperature[{0}]".format(self.config["TemperatureFormat"]), "Smoothing"],
                          ["Temperature", self.Temperature, self.LastTemperature, self.Temperature / self.smoothing_value],
                          ["Humidity", self.Humidity, self.LastHumidity, self.Humidity  / self.smoothing_value]])

          print(table.draw())
          print("***")

          # Capture Last Values
          self.LastTemperature = self.Temperature
          self.LastHumidity = self.Humidity

          # Smooth Values
          self.Temperature = self.Temperature / self.smoothing_value
          self.Humidity = self.Humidity / self.smoothing_value

          # Send Data to IoT Central
          self.telemetry_dict = {}
          self.telemetry_dict[self.TemperatureMapName] = self.Temperature
          self.telemetry_dict[self.HumidityMapName] = self.Humidity
          #self.telemetry_dict = {**self.telemetry_dict, **self.serial_emulator.translate(serial_read)}
          self.logger.info("[Refrideration Monitor] SENDING PAYLOAD IOT CENTRAL")
          #await device_client.send_telemetry(self.telemetry_dict, self.config["Model"]["DeviceCapabilityModelId"], self.config["Model"]["NameSpace"])
          await device_client.send_telemetry(self.telemetry_dict, "dtmi:RefriderationMonitorStorage:ambient;1", "ambient")
          
          
          self.logger.info("[Refrideration Monitor] SUCCESS")
          self.setColor(self.GreenColor)
          await asyncio.sleep(5.0)

        return

      except Exception as ex:
        self.logger.error("[ERROR] %s" % ex)
        self.logger.error("[TERMINATING] We encountered an error in Refrideration Monitor Monitor Run::run()" )
      
      except KeyboardInterrupt:
        self.p_R.stop()
        self.p_G.stop()
        self.p_B.stop()
        for i in self.pins:
          GPIO.output(self.pins[i], GPIO.HIGH)    # Turn off all leds
        GPIO.cleanup()

      finally:
        await device_client.disconnect()
from texttable import Texttable
table = Texttable()
jawab = "y"
no = 0
nama = []
nim = []
nilai_tugas = []
nilai_uts = []
nilai_uas = []
while (jawab == "y"):
    nama.append(input("Masukan Nama  :"))
    nim.append(input("Masukan Nim   :"))
    nilai_tugas.append(input("Nilai Tugas   :"))
    nilai_uts.append(input("Nilai UTS     :"))
    nilai_uas.append(input("Nilai UAS     :"))
    jawab = input("Tambah data (y/t)?")
    no += 1
for i in range(no):
    tugas = int(nilai_tugas[i])
    uts = int(nilai_uts[i])
    uas = int(nilai_uas[i])
    akhir = (tugas * 30 / 100) + (uts * 35 / 100) + (uas * 35 / 100)
    table.add_rows([['No', 'Nama', 'Nim', 'Tugas', 'UTS', 'UAS', 'AKHIR'],
                    [
                        i + 1, nama[i], nim[i], nilai_tugas[i], nilai_uts[i],
                        nilai_uas[i], akhir
                    ]])
print(table.draw())
Exemple #40
0
    def do_create(self, args):
        try:
            # add arguments
            do_parser = self.arg_create()
            try:
                do_args = do_parser.parse_args(shlex.split(args))
            except SystemExit as e:
                return

            # call UForge API
            printer.out("Creating user account [" + do_args.account + "] ...")

            # create a user manually
            new_user = user()
            new_user.loginName = do_args.account
            new_user.password = do_args.accountPassword
            new_user.creationCode = do_args.code
            new_user.email = do_args.email
            new_user.password = do_args.accountPassword

            if do_args.org:
                org = do_args.org
            else:
                org = None
            if do_args.disable:
                new_user.active = False
            else:
                new_user.active = True

            if do_args.accountPassword:
                auto_password = "******"
            else:
                auto_password = "******"

            # Send the create user request to the server
            new_user = self.api.Users(self.login).Create(
                "true", "true", org, "false", "false", auto_password, new_user)

            if new_user is None:
                printer.out("No information about new user available",
                            printer.ERROR)
            else:
                table = Texttable(200)
                table.set_cols_align(["c", "l", "c", "c", "c", "c", "c", "c"])
                table.header([
                    "Login", "Email", "Lastname", "Firstname", "Created",
                    "Active", "Promo Code", "Creation Code"
                ])
                table.add_row([
                    new_user.loginName, new_user.email, new_user.surname,
                    new_user.firstName,
                    new_user.created.strftime("%Y-%m-%d %H:%M:%S"), "X",
                    new_user.promoCode, new_user.creationCode
                ])
                print table.draw() + "\n"
            return 0
        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_create()
        except Exception as e:
            return marketplace_utils.handle_uforge_exception(e)
def run_model(V0, Z0, DA0, task, outfile, \
              seed=None, paramfile='parameters.py', symsyn=True, verbose=True, ram_use=0.2,\
              **kwargs):
    """
    Run a simulation using the neural population model

    Parameters
    ----------
    V0 : NumPy 1darray
        Initial conditions for excitatory neurons. If `N` regions are simulated then `V0` has 
        to have length `N`.
    Z0 : NumPy 1darray
        Initial conditions for inhibitory neurons. If `N` regions are simulated then `Z0` has 
        to have length `N`.
    DA0 : NumPy 1darray
        Initial conditions for dopamine levels. If `N` regions are simulated then `DA0` has 
        to have length `N`.
    task : str
        Specify which task should be simulated. Currently, only 'rest' and 'speech' are supported. 
    outfile : str
        File-name (including path if not in working directory) of HDF5 container that will be created to 
        save simulation results. See Notes for the structure of the generated container. Any existing 
        file will be renamed. The user has to have writing permissions for the given location.
    seed : int
        Random number generator seed. To make meaningful comparisons between successive simulation
        runs, the random number seed should be fixed so that the solver uses the same Wiener process
        realizations. Also, if synaptic coupling strengths are sampled from a probability distribution,
        simulation results will vary from run to run unless the seed is fixed. 
    paramfile : str
        Parameter file-name (including path if not in working directory) that should be used for simulation. 
        The parameter file has to be a Python file (.py extension). For more details refer to `Examples` 
        below. You should have received a sample parameter file (`parameters.py`) with this copy 
        of `sim_tools.py`. 
    symsyn : bool
        Boolean switch determining whether synaptic coupling strengths should be symmetrized between 
        hemispheres (`symsyn=True`) or not. 
    verbose : bool
        If `True` the code will print a summary of the most important parameters and all used 
        keyword arguments (see below) in the simulation together with a progress bar to (roughly)
        estimate run time (requires the `progressbar` module). 
    ram_use : float
        Fraction of memory to use for caching simulation results before writing to disk 
        (0 < `ram_use` < 1). More available memory means fewer disk-writes and thus better performance, 
        i.e, the larger `ram_use` the faster this code runs. However, if too much RAM is allocated by 
        this routine it may stall the executing computer. By default, `ram_use = 0.2`, i.e., around 20% 
        of available memory is used. 
    kwargs : additional keyword arguments
        Instead of relying solely on a static file to define parameter values, it is also possible
        to pass on parameters to the code using keyword arguments (see `Examples` below). Note: parameters
        given as keyword arguments have higher priority than values set in `paramfile`, i.e., if 
        `p1 = 1` is defined in `paramfile` but `p1 = 2` is a keyword argument, the code will use 
        `p1 = 2` in the simulation. This behavior was intentionally implemented to enable the use 
        of this function within a parameter identification framework. 

    Returns
    -------
    Nothing : None
        Simulation results are saved in the HDF5 container specified by `outfile`. See `Notes` for details. 

    Notes
    -----
    Due to the (usually) high temporal resolution of simulations, results are not kept in memory (and 
    thus returned as variable in the caller's work-space) but saved directly to disk using the HDF5
    container `outfile`. The code uses the HDF library's data chunking feature to save entire
    segments on disk while running. By default the code will allocate around 20% of available 
    memory to cache simulation results. Hence, more memory leads to fewer disk-writes during 
    run-time and thus faster performance. 
    The structure of the generated output container is as follows: all state variables and the dopaminergic 
    gain `Beta` are stored at the top-level of the file. Additionally, the employed coupling 
    matrix `C` and dopamine connection matrix `D` are also saved in the top level group. All used parameters
    are saved in the subgroup `params`. 

    Examples
    --------
    Let `V0`, `Z0`, and `DA0` (NumPy 1darrays of length `N`) be initial conditions of the
    model. Assuming that a valid parameter file (called `parameters.py`) is located in the current 
    working directory, the following call will run a resting state simulation and save the output
    in the HDF5 container `sim_rest.h5`

    >>> run_model(V0,Z0,DA0,'rest','sim_rest.h5')

    Assume another parameter file, say, `par_patho.py` hold parameter settings simulating a 
    certain pathology. Then the command

    >>> run_model(V0,Z0,DA0,'rest','patho/sim_rest_patho.h5',paramfile='par_patho.py')

    runs a resting state simulation with the same initial conditions and saves the result in
    the container `sim_rest_patho.h5` in the sub-directory `patho` (which must already exist, otherwise
    an error is raised). 

    If only one or two parameters should be changed from their values found in a given parameter file, 
    it is probably more handy to change the value of these parameters from the command line, rather
    than to write a separate parameter file (that is identical to the original one except for two 
    values). Thus, assume the values of `VK` and `VL` should be -0.4 and -0.9 respectively, i.e., 
    different than those found in (the otherwise fine) `par_patho.py`. Then the command

    >>> run_model(V0,Z0,DA0,'rest','patho/sim_rest_patho.h5',paramfile='par_patho.py',VK=-0.4,VL=-0.9)
    
    runs the same resting state simulation as above but with `VK=-0.4` and `VL=-0.9`. This feature
    can also be used to efficiently embed `run_model` in a parameter identification framework.

    See also
    --------
    plot_sim : plot simulations generated by run_model

    References
    ----------
    .. [1] S. Fuertinger, J. C. Zinn, and K. Simonyan. A Neural Population Model Incorporating 
           Dopaminergic Neurotransmission during Complex Voluntary Behaviors. PLoS Computational Biology, 
           10(11), 2014. 
    """

    # Sanity checks for initial conditions
    n = np.zeros((3, ))
    vnames = ['V0', 'Z0', 'DA0']
    for i, vec in enumerate([V0, Z0, DA0]):
        arrcheck(vec, 'vector', vnames[i])
        n[i] = vec.size
    if np.unique(n).size > 1:
        raise ValueError(
            'The initial conditions for `V`, `Z`, and `DA` have to have the same length!'
        )

    # Check the `task` string
    if not isinstance(task, (str, unicode)):
        raise TypeError('Task has to be specified as string, not ' +
                        type(task).__name__ + '!')
    task = str(task)
    if task != 'rest' and task != 'speech':
        raise ValueError(
            "The value of `task` has to be either 'rest' or 'speech'!")

    # The path to the output file should be a valid
    if not isinstance(outfile, (str, unicode)):
        raise TypeError('Output filename has to be a string!')
    outfile = str(outfile)
    if outfile.find("~") == 0:
        outfile = os.path.expanduser('~') + outfile[1:]
    slash = outfile.rfind(os.sep)
    if slash >= 0 and not os.path.isdir(outfile[:outfile.rfind(os.sep)]):
        raise ValueError('Invalid path for output file: ' + outfile + '!')

    # Set or get random number generator seed
    if seed is not None:
        scalarcheck(seed, 'seed', kind='int')
    else:
        seed = np.random.get_state()[1][0]
    seed = int(seed)

    # Make sure `paramfile` is a valid path
    if not isinstance(paramfile, (str, unicode)):
        raise TypeError('Parameter file has to be specified using a string!')
    paramfile = str(paramfile)
    if paramfile.find("~") == 0:
        paramfile = os.path.expanduser('~') + paramfile[1:]
    if not os.path.isfile(paramfile):
        raise ValueError('Parameter file: ' + paramfile + ' does not exist!')

    # Make sure `symsyn` and `verbose` are Boolean
    if not isinstance(symsyn, bool):
        raise TypeError("The switch `symsyn` has to be Boolean!")
    if not isinstance(verbose, bool):
        raise TypeError("The switch `verbose` has to be Boolean!")

    # Finally, check `ram_use`
    scalarcheck(ram_use, 'ram_use', bounds=[0, 1])

    # Append '.h5' extension to `outfile` if necessary
    if outfile[-3:] != '.h5':
        outfile = outfile + '.h5'

    # Check if `paramfile` has an extension, if yes, rip it off
    if paramfile[-3:] == '.py':
        paramfile = paramfile[0:-3]

    # Divide `paramfile` into file-name and path
    slash = paramfile.rfind(os.sep)
    if slash < 0:
        pth = '.'
        fname = paramfile
    else:
        pth = paramfile[0:slash + 1]
        fname = paramfile[slash + 1:]

    # Import parameters module and initialize corresponding dictionary (remove `__file__`, etc)
    param_py = imp.load_module(fname, *imp.find_module(fname, [pth]))
    p_dict = {}
    for key, value in param_py.__dict__.items():
        if key[0:2] != "__":
            p_dict[key] = value

    # Try to load coupling and dopamine pathway matrices
    mfile = "None"
    vnames = ['C', 'D']
    for mat_str in vnames:
        if kwargs.has_key(mat_str):
            p_dict[mat_str] = kwargs[mat_str]
        else:
            try:
                p_dict[mat_str] = h5py.File(param_py.matrices,
                                            'r')[mat_str].value
                mfile = p_dict['matrices']
            except:
                raise ValueError("Error reading `" + param_py.matrices + "`!")
            arrcheck(p_dict[mat_str], 'matrix', mat_str)

    # Try to load ROI names
    try:
        names = h5py.File(param_py.matrices, 'r')['names'].value
        mfile = p_dict['matrices']
    except:
        try:
            names = kwargs['names']
        except:
            raise ValueError("A NumPy 1darray or Python list of ROI names has to be either specified "+\
                             "in a matrix container or provided as keyword argument!")
    p_dict['names'] = names

    # See if we have an (optional) list/array of ROI-shorthand labels
    try:
        p_dict['labels'] = h5py.File(param_py.matrices, 'r')['labels'].value
        mfile = p_dict['matrices']
    except:
        if kwargs.has_key('labels'):
            p_dict['labels'] = kwargs['labels']

    # Put ones on the diagonal of the coupling matrix to ensure compatibility with the code
    np.fill_diagonal(p_dict['C'], 1.0)

    # Get dimension of matrix and check correspondence
    N = p_dict['C'].shape[0]
    if N != p_dict['D'].shape[0]:
        raise ValueError(
            "Dopamine and coupling matrices don't have the same dimension!")
    if len(names) != N:
        raise ValueError("Matrix is " + str(N) + "-by-" + str(N) +
                         " but `names` has length " + str(len(names)) + "!")
    for nm in names:
        if not isinstance(nm, (str, unicode)):
            raise ValueError(
                "Names have to be provided as Python list/NumPy array of strings!"
            )

    # If user provided some additional parameters as keyword arguments, copy them to `p_dict`
    for key, value in kwargs.items():
        p_dict[key] = value

    # Get synaptic couplings (and set seed of random number generator)
    np.random.seed(seed)
    if kwargs.has_key('aei'):
        aei = kwargs['aei']
    else:
        aei = eval(param_py.aei)
    if kwargs.has_key('aie'):
        aie = kwargs['aie']
    else:
        aie = eval(param_py.aie)
    if kwargs.has_key('ani'):
        ani = kwargs['ani']
    else:
        ani = eval(param_py.ani)
    if kwargs.has_key('ane'):
        ane = kwargs['ane']
    else:
        ane = eval(param_py.ane)

    # If wanted, make sure left/right hemispheres have balanced coupling strengths
    if symsyn:

        # Get indices of left-hemispheric regions and throw a warning if left/right don't match up
        regex = re.compile("[Ll]_*")
        match = np.vectorize(lambda x: bool(regex.match(x)))(names)
        l_ind = np.where(match == True)[0]
        r_ind = np.where(match == False)[0]
        if l_ind.size != r_ind.size:
            print "WARNING: Number of left-side regions = "+str(l_ind.size)+\
                  " not equal to number of right-side regions = "+str(r_ind.size)

        # Equalize coupling strengths
        aei[l_ind] = aei[r_ind]
        aie[l_ind] = aie[r_ind]
        ani[l_ind] = ani[r_ind]
        ane[l_ind] = ane[r_ind]

    # Save updated coupling strengths and random number generator seed in dictionary
    p_dict['aei'] = aei
    p_dict['aie'] = aie
    p_dict['ani'] = ani
    p_dict['ane'] = ane
    p_dict['seed'] = seed

    # If a resting state simulation is done, make sure dopamine doesn't kick in, i.e., enforce `rmax == rmin`
    if task == 'rest':
        rmax = np.ones((N, )) * p_dict['rmin']
    else:
        if not kwargs.has_key('rmax'):
            p_dict['rmax'] = eval(param_py.rmax)

    # Save given task in dictionary
    p_dict['task'] = task

    # Get ion channel parameters
    if not kwargs.has_key('TCa'):
        p_dict['TCa'] = eval(param_py.TCa)

    # Compute length for simulation and speech on-/offset times
    len_cycle = p_dict['stimulus'] + p_dict['production'] + p_dict[
        'acquisition']
    speechon = p_dict['stimulus']
    speechoff = p_dict['stimulus'] + p_dict['production']

    # Save that stuff
    p_dict['len_cycle'] = len_cycle
    p_dict['speechon'] = speechon
    p_dict['speechoff'] = speechoff

    # Set/get initial time for simulation
    if p_dict.has_key('tstart'):
        tstart = p_dict[
            'tstart']  # Use `p_dict` here, since `tstart` could be a kwarg!
        if verbose:
            print "WARNING: Using custom initial time of  " + str(
                tstart) + " (has to be in ms)!"
    else:
        tstart = 0

    # Set/get step-size for simulation
    if p_dict.has_key('dt'):
        dt = p_dict['dt']
        if verbose:
            print "WARNING: Using custom step-size of " + str(
                dt) + " (has to be in ms)!"
    else:
        dt = 1e-1

    # Get sampling step size (in ms) and check if "original" step-size makes sense
    ds = 1 / p_dict['s_rate'] * 1000
    if dt > ds:
        print "WARNING: Step-size dt = "+str(dt)+\
              " larger than chosen sampling frequency of "+str(s_rate)+"Hz."+\
              " Using dt = "+str(ds)+"ms instead. "
        dt = ds

    # Compute sampling rate (w.r.t `dt`)
    s_step = int(np.round(ds / dt))

    # Save step-size and sampling rate in dictionary for later reference
    p_dict['dt'] = dt
    p_dict['s_step'] = s_step

    # Compute end time for simulation (in ms) and allocate time-step array
    tend = tstart + len_cycle * p_dict['n_cycles'] * 1000
    tsteps = np.arange(tstart, tend, dt)

    # Get the size of the time-array
    tsize = tsteps.size

    # Before laying out output HDF5 container, rename existing files to not accidentally overwrite 'em
    moveit(outfile)

    # Chunk outifle depending on available memory (eat up ~ 100*`ram_use`% of RAM)
    datype = np.dtype('float64')
    meminfo = psutil.virtual_memory()
    maxmem = int(meminfo.available * ram_use / (5 * N) / datype.itemsize)
    maxmem += s_step - np.mod(maxmem, s_step)

    # If the whole array fits into memory load it once, otherwise chunk it up
    if tsteps.size <= maxmem:
        blocksize = [tsize]
        csize = int(np.ceil(tsize / s_step))
        chunksize = [csize]
        chunks = True
    else:
        bsize = int(tsize // maxmem)
        rest = int(np.mod(tsize, maxmem))
        blocksize = [maxmem] * bsize
        if rest > 0: blocksize = blocksize + [rest]
        numblocks = len(blocksize)
        csize = int(np.ceil(maxmem / s_step))
        restc = int(np.ceil(blocksize[-1] / s_step))
        chunksize = [csize] * (numblocks - 1) + [restc]
        chunks = (N, csize)

    # Convert blocksize and chunksize to NumPy arrays
    blocksize = np.array(blocksize)
    chunksize = np.array(chunksize)

    # Get the number of elements that will be actually saved
    n_elems = chunksize.sum()

    # Create output HDF5 container
    f = h5py.File(outfile)

    # Create datasets for numeric variables
    f.create_dataset('C', data=p_dict['C'], dtype=datype)
    f.create_dataset('D', data=p_dict['D'], dtype=datype)
    f.create_dataset('V', shape=(N, n_elems), chunks=chunks, dtype=datype)
    f.create_dataset('Z', shape=(N, n_elems), chunks=chunks, dtype=datype)
    f.create_dataset('DA', shape=(N, n_elems), chunks=chunks, dtype=datype)
    f.create_dataset('QV', shape=(N, n_elems), chunks=chunks, dtype=datype)
    f.create_dataset('Beta', shape=(N, n_elems), chunks=chunks, dtype=datype)
    f.create_dataset('t',
                     data=np.linspace(tstart, tend, n_elems),
                     dtype=datype)

    # Save parameters (but exclude stuff imported in the parameter file)
    pg = f.create_group('params')
    for key, value in p_dict.items():
        valuetype = type(value).__name__
        if valuetype != 'instance' and valuetype != 'module' and valuetype != 'function':
            pg.create_dataset(key, data=value)

    # Close container and write to disk
    f.close()

    # Initialize parameter C-class (struct) for the model
    params = par(p_dict)

    # Concatenate initial conditions for the "calibration" run
    VZD0 = np.hstack([V0.squeeze(), Z0.squeeze(), DA0.squeeze()])

    # Set up parameters for an initial `len_init` (in ms) long resting state simulation to "calibrate" the model
    len_init = 100
    dt = 0.1
    s_step = 10
    rmax = np.zeros((N, ))
    tinit = np.arange(0, len_init, dt)
    tsize = tinit.size
    csize = int(np.ceil(tsize / s_step))

    # Update `p_dict` (we don't use it anymore, so just overwrite stuff)
    p_dict['dt'] = dt
    p_dict['s_step'] = s_step
    p_dict['rmax'] = rmax
    parinit = par(p_dict)

    # Create a temporary container for the simulation
    tmpname = tempfile.mktemp() + '.h5'
    tmpfile = h5py.File(tmpname)
    tmpfile.create_dataset('V', shape=(N, csize), dtype=datype)
    tmpfile.create_dataset('Z', shape=(N, csize), dtype=datype)
    tmpfile.create_dataset('DA', shape=(N, csize), dtype=datype)
    tmpfile.create_dataset('QV', shape=(N, csize), dtype=datype)
    tmpfile.create_dataset('Beta', shape=(N, csize), dtype=datype)
    tmpfile.flush()

    # Run 100ms of resting state to get model to a "steady state" for the initial conditions
    solve_model(VZD0, tinit, parinit, np.array([tsize]), np.array([csize]),
                seed, 0, str(tmpfile.filename))

    # Use final values of `V`, `Z` and `DA` as initial conditions for the "real" simulation
    V0 = tmpfile['V'][:, -1]
    Z0 = tmpfile['Z'][:, -1]
    DA0 = tmpfile['DA'][:, -1]
    VZD0 = np.hstack([V0.squeeze(), Z0.squeeze(), DA0.squeeze()])

    # Close and delete the temporary container
    tmpfile.close()
    os.remove(tmpname)

    # Let the user know what's going to happen...
    pstr = "--"
    if len(kwargs) > 0:
        pstr = str(kwargs.keys())
        pstr = pstr.replace("[", "")
        pstr = pstr.replace("]", "")
        pstr = pstr.replace("'", "")
    table = Texttable()
    table.set_deco(Texttable.HEADER)
    table.set_cols_align(["l", "l"])
    table.add_rows([["Simulating ",task.upper()],\
                    ["#cycles: ",str(p_dict['n_cycles'])],\
                    ["parameter file:",paramfile+".py"],\
                    ["keyword args:",pstr],\
                    ["matrix file:",mfile],\
                    ["output:",outfile]])
    if verbose: print "\n" + table.draw() + "\n"

    # Finally... run the actual simulation
    solve_model(VZD0, tsteps, params, blocksize, chunksize, seed, int(verbose),
                outfile)

    # Done!
    if verbose: print "\nDone\n"
Exemple #42
0
def kalkulator():
    from texttable import Texttable
    table = Texttable()

    #dictionary
    jawab = "y"
    no = 0
    num1 = []
    operator = []
    num2 = []

    # menu operasi
    print("=== PROGRAM KALKULATOR ===\n")
    print("Pilih Operasi.")
    print("+.Jumlah")
    print("-.Kurang")
    print("*.Kali")
    print("/.Bagi")

    #pilihan
    while (jawab == "y"):

        bil1 = input("masukan angka pertama : ")

        num1.append(bil1)

        pilih = input("\nMasukkan operator : ")

        if pilih == '+':
            operator.append("+")
            bil2 = input("\nmasukan angka kedua : ")
            num2.append(bil2)
            hasil = float(bil1) + float(bil2)
            jawab = input("\nada lagi? (y/t)")
            no += 1

        elif pilih == '-':
            operator.append("-")
            bil2 = input("\nmasukan angka kedua : ")
            num2.append(bil2)
            hasil = float(bil1) - float(bil2)
            jawab = input("\nada lagi? (y/t)")
            no += 1

        elif pilih == '*':
            operator.append("*")
            bil2 = input("\nmasukan angka kedua : ")
            num2.append(bil2)
            hasil = float(bil1) * float(bil2)
            jawab = input("\nada lagi? (y/t)")
            no += 1

        elif pilih == '/':
            operator.append("/")
            bil2 = input("\nmasukan angka kedua : ")
            num2.append(bil2)
            hasil = float(bil1) / float(bil2)
            jawab = input("\nada lagi? (y/t)")
            no += 1
        else:
            print("input salah!")
            break

        for i in range(no):
            bil1 = (num1[i])
            bil2 = (num2[i])

        table.add_rows([["bilangan 1", "operator", "bilangan kedua", "hasil"],
                        [num1[i], operator[i], num2[i], hasil]])
    print(table.draw())
def display_text(p_data):
    """
    Display profile in text format
    """
    # p_data = profile_data[0]["store"][profile_name]
    logging.debug("Data keys: %s", p_data.keys())

    # Single value data
    singletons = Texttable()
    singletons.set_deco(Texttable.HEADER)
    singletons.set_cols_align(["c", "c", "c", "c", "c", "c"])
    singletons.add_rows([
        ["Profile name", "Timezone", "Units", "DIA", "Delay", "Start date"],
        [
            p_data["name"],
            p_data["timezone"],
            p_data["units"],
            p_data["dia"],
            p_data["delay"],
            p_data["startDate"],
        ],
    ])
    print(singletons.draw() + "\n")

    times = {}
    tgt_low = {v["time"]: v["value"] for v in p_data["target_low"]}
    tgt_high = {v["time"]: v["value"] for v in p_data["target_high"]}
    carb_ratio = {v["time"]: v["value"] for v in p_data["carbratio"]}
    sens = {v["time"]: v["value"] for v in p_data["sens"]}
    basal = {v["time"]: v["value"] for v in p_data["basal"]}
    logging.debug(tgt_high, tgt_low, carb_ratio, sens, basal)
    for (time, basal) in basal.items():
        times.setdefault(time, {})
        times[time]["basal"] = basal
    for (time, sens) in sens.items():
        times.setdefault(time, {})
        times[time]["sens"] = sens
    for (time, c_r) in carb_ratio.items():
        times.setdefault(time, {})
        times[time]["carbratio"] = c_r
    for (time, tgt_h) in tgt_high.items():
        times.setdefault(time, {})
        times[time]["tgt_high"] = tgt_h
    for (time, tgt_l) in tgt_low.items():
        times.setdefault(time, {})
        times[time]["tgt_low"] = tgt_l
    logging.debug("Times: %s", times)

    times_list = [["Time", "Basal", "ISF", "CR", "Target Low", "Target High"]]
    for time in sorted(times.keys()):
        times_list.append([
            time,
            times[time].get("basal", ""),
            times[time].get("sens", ""),
            times[time].get("carbratio", ""),
            times[time].get("tgt_low", ""),
            times[time].get("tgt_high", ""),
        ])
    times_table = Texttable()
    times_table.set_cols_align(["c", "c", "c", "c", "c", "c"])
    times_table.add_rows(times_list)
    print(times_table.draw() + "\n")
Exemple #44
0
res_table = pd.DataFrame({
    'Flower':
    ['sepal length ∆', 'sepal width ∆', 'petal length ∆', 'petal width ∆'],
    'Versicolor': [
        str(round((contr_c2_all - contr_c2_f1) * 100, 2)) + "%",
        str(round((contr_c2_all - contr_c2_f2) * 100, 2)) + "%",
        str(round((contr_c2_all - contr_c2_f3) * 100, 2)) + "%",
        str(round((contr_c2_all - contr_c2_f4) * 100, 2)) + "%"
    ],
    'Setosa': [
        str(round((contr_c1_all - contr_c1_f1) * 100, 2)) + "%",
        str(round((contr_c1_all - contr_c1_f2) * 100, 2)) + "%",
        str(round((contr_c1_all - contr_c1_f3) * 100, 2)) + "%",
        str(round((contr_c1_all - contr_c1_f4) * 100, 2)) + "%"
    ],
    'Virginica': [
        str(round((contr_c3_all - contr_c3_f1) * 100, 2)) + "%",
        str(round((contr_c3_all - contr_c3_f2) * 100, 2)) + "%",
        str(round((contr_c3_all - contr_c3_f3) * 100, 2)) + "%",
        str(round((contr_c3_all - contr_c3_f4) * 100, 2)) + "%"
    ]
})
tb = Texttable()
tb.set_cols_align(['l', 'r', 'r', 'r'])
tb.set_cols_dtype(['t', 'i', 'i', 'i'])
tb.header(res_table.columns)
tb.add_rows(res_table.values, header=False)

print(tb.draw())
    def command_config(self, params):
        '''<esxi|pdu> [<list/update/add/delete> | <set/get>] [<param.1> ... < param.n>]

        configure vPDU
        ----------------------------------
        config pdu set <name>
        - set PDU name
        e.g.
        config pdu set hawk

        config pdu set database <database file>
        - set pdu database file name
        e.g.
        config pdu set database ipia.db

        config pdu set datadir <snmp data dir>
        - set snmp data directory name
        e.g.
        config pdu set datadir hawk

        config pdu list
        -list pdu configurations


        config esxi
        --------------------------------------
        config esxi list
         - list configuration

        config esxi update <option name> <value>
         - update configuration
         e.g.
            Update esxi ip address in configuration file, run below command:
            config esxi update host 10.62.59.124

            Update esxi host "username"
            config esxi update uesrname root

            Update esxi host "password"
            config esxi update password root

        config esxi add <host> <uesrname> <password>
         - add configuration
         e.g.
            Add an ESXi host information including ip, username and passowrd
            config esxi add 10.62.59.128 root 1234567

        config esxi delete
         - delete configuration
         e.g.
            Delete section "esxihost"
            config esxi delete esxihost

        Note: After update/add the configuration, please run 'config list' to
        be sure that the changes you made are correct.
        '''
        if len(params) == 0:
            return

        if params[0] == "pdu":
            if params[1] == 'set':
                if params[2] == 'name':
                    self.config_instance.pdu_name = params[3]
                elif params[2] == 'database':
                    self.config_instance.db_file = params[3]
                elif params[2] == 'datadir':
                    self.config_instance.snmp_data_dir = params[3]

                self.config_instance.update()
            elif params[1] == 'get':
                self.config_instance.init()
                table = Texttable()
                table.add_row(['name', self.config_instance.pdu_name])
                table.add_row(['database', self.config_instance.db_file])
                table.add_row(
                    ['snmp data dir', self.config_instance.snmp_data_dir])
                table_str = table.draw()
                self.writeresponse(table_str)
                logger.info("\n" + table_str)
            elif params[1] == 'list':
                self.config_instance.init()
                table = Texttable()
                table.add_row(['pdu name', self.config_instance.pdu_name])
                table.add_row(['dbtype', self.config_instance.db_type])
                table.add_row(['database', self.config_instance.db_file])
                table.add_row(['snmpdata', self.config_instance.snmp_data_dir])
                table.add_row(['simfile', self.config_instance.sim_file])
                table_str = table.draw()
                self.writeresponse(table_str)
                logger.info("\n" + table_str)

            else:
                logger.error("Unknown command {0}".format(params[0]))
        elif params[0] == "esxi":
            if params[1] == "list":
                self.config_instance.init()
                esxi_info = self.config_instance.esxi_info
                if esxi_info is not None:
                    table = Texttable()
                    table.header(["esxi host", "username", "password"])
                    table.add_row([
                        get_color_string(bcolors.GREEN, esxi_info['host']),
                        get_color_string(bcolors.GREEN, esxi_info["username"]),
                        get_color_string(bcolors.GREEN, esxi_info['password'])
                    ])
                    table_str = table.draw()
                    self.writeresponse(table_str)
                    logger.info("\n" + table_str)
                    return
                else:
                    self.writeresponse("%sNo ESXi host info in configuration \
                                       file.%s" % (colors.RED, colors.NORMAL))
            elif params[1] == "update":
                if len(params[2:]) == 2:
                    esxi_info = self.config_instance.esxi_info
                    if esxi_info is not None:
                        esxi_info[params[2]] = params[3]
                        self.config_instance.update()
                    else:
                        self.writeresponse("%sNo %s found in configuration \
                            file.%s" % (colors.RED, params[1], colors.NORMAL))
            elif params[1] == "add":
                if len(params[2:]) != 3:
                    return

                if self.config_instance.esxi_info is None:
                    esxi_info = {}
                    logger.info("Adding esxi host: {0}, {1}, {2}".format(
                        params[2], params[3], params[4]))
                    esxi_info['host'] = params[2]
                    esxi_info['username'] = params[3]
                    esxi_info['password'] = params[4]
                    self.config_instance.esxi_info = esxi_info
                    self.config_instance.update()
                else:
                    self.writeresponse("ESXi info already exists.")
            elif params[1] == "delete":
                if self.config_instance.esxi_info is not None:
                    self.config_instance.delete()
                else:
                    self.writeresponse("ESXi info already deleted.")
            else:
                self.writeresponse("unknown parameters.")
        else:
            self.writeresponse("unknown parameters: {0}.".format(params[0]))
def pretty_print(matrix):
    t = Texttable(100000)
    t.add_rows(matrix)
    print(t.draw())
Exemple #47
0
def generate_report(proj_conf):

    d = {
        'runname': proj_conf['run'],
        'project_id': proj_conf['id'],
        'samplenames': ' '.join(proj_conf['samples']),
        'latex_opt': "",
        'uppnex': "",
        'mapping': "",
        'dup_rem': "",
        'read_count': "",
        'quantifyer': "",
        'gene_body_cov': "",
        'FPKM_heatmap': "",
        'FPKM_PCAplot': "",
        'Mapping_statistics': "",
        'Read_Distribution': "",
        'rRNA_table': ""
    }

    ## Latex option (no of floats per page)
    floats_per_page = '.. raw:: latex\n\n   \setcounter{totalnumber}{8}'
    d['latex_opt'] = floats_per_page

    ## Metadata fetched from the 'Genomics project list' on Google Docs
    try:
        proj_data = ProjectMetaData(proj_conf['id'], proj_conf['config'])
        uppnex_proj = proj_data.uppnex_id
    except:
        uppnex_proj = "b201YYXX"
        print "No uppnex ID fetched"
        pass
    if not uppnex_proj:
        uppnex_proj = "b201YYXX"
        print "No uppnex ID fetched"
    d['uppnex'] = uppnex_proj

    ## RNA-seq tools fetched from config file post_process.yaml
    try:
        tools = proj_conf['config']['custom_algorithms']['RNA-seq analysis']
        d['mapping'] = os.path.join(tools['aligner'], tools['aligner_version'])
        d['dup_rem'] = os.path.join(tools['dup_remover'],
                                    tools['dup_remover_version'])
        d['read_count'] = os.path.join(tools['counts'],
                                       tools['counts_version'])
        d['quantifyer'] = os.path.join(tools['quantifyer'],
                                       tools['quantifyer_version'])
    except:
        print "Could not fetched RNA-seq tools from config file post_process.yaml"
        d['mapping'] = "X"
        d['dup_rem'] = "X"
        d['read_count'] = "X"
        d['quantifyer'] = "X"
        pass

    ## Mapping Statistics
    tab = Texttable()
    tab.set_cols_dtype(['t', 't', 't', 't'])
    tab.add_row([
        'Sample', 'tot_#_read_pairs', '%_uniquely_mapped_reads',
        '%_uniquely_mapped_reads_left_after_dup_rem'
    ])
    try:
        for sample_name in proj_conf['samples']:
            f = open('tophat_out_' + sample_name + '/stat_' + sample_name, 'r')
            data = f.readlines()
            tab.add_row([
                sample_name, data[1].split()[1], data[2].split()[1],
                data[3].split()[1]
            ])
            f.close()
        d['Mapping_statistics'] = tab.draw()
    except:
        try:
            f = open('stat', 'r')
            data = f.readlines()
            D = dict(
                zip(data[0].split(),
                    zip(data[1].split(), data[2].split(), data[3].split())))
            for sample_name in proj_conf['samples']:
                if D.has_key(sample_name):
                    tab.add_row([
                        sample_name, D[sample_name][0], D[sample_name][1],
                        D[sample_name][2]
                    ])
                else:
                    print 'kould not find ' + sample_name + ' in stat'
            d['Mapping_statistics'] = tab.draw()
            f.close()
        except:
            print "Could not make Mapping Statistics table"
            pass

    ## Read Distribution
    try:
        tab = Texttable()
        json = open('Ever_rd.json', 'a')
        print >> json, '{'
        Groups = [
            "Sample:", "CDS Exons:", "5'UTR Exons:", "3'UTR Exons:",
            "Intronic region:", "TSS up 1kb:", "TES down 1kb:"
        ]

        tab.set_cols_dtype(['t', 't', 't', 't', 't', 't', 't', 't'])
        tab.add_row([
            "Sample", "CDS Exon", "5'UTR Exon", "3'UTR Exon", "Intron",
            "TSS up 1kb", "TES down 1kb", "mRNA frac"
        ])

        for i in range(len(proj_conf['samples'])):
            sample_name = proj_conf['samples'][i]
            print >> json, sample_name + ': {'
            row = [sample_name]
            Reads_counts = []
            try:
                f = open('RSeQC_rd_' + sample_name + '.err', 'r')
            except:
                f = open('Ever_rd_' + sample_name + '.err', 'r')
                pass
            for line in f:
                Group = line.split('\t')[0]
                if Group in Groups:
                    if Group == "TES down 1kb:":
                        print >> json, '"' + Group + '"' + ':' + str(
                            line.split('\t')[3].strip())
                    else:
                        print >> json, '"' + Group + '"' + ':' + str(
                            line.split('\t')[3].strip()) + ','
                    row.append(str(line.split('\t')[3].strip()) + ' ')
                    Reads_counts.append(float(line.split('\t')[2].strip()))
            if os.path.exists('RSeQC_rd_' + sample_name + '.err'):
                t = os.popen("grep 'Total Fragments' 'RSeQC_rd_" +
                             sample_name +
                             ".err'|sed 's/Total Fragments               //g'")
            else:
                try:
                    t = os.popen(
                        "grep 'Total Fragments' 'Ever_rd_" + sample_name +
                        ".err'|sed 's/Total Fragments               //g'")
                except:
                    pass
            tot = float(t.readline())
            frac = (Reads_counts[0] + Reads_counts[1] + Reads_counts[2]) / tot
            row.append(
                str(
                    round(
                        (Reads_counts[0] + Reads_counts[1] + Reads_counts[2]) /
                        tot, 2)))
            tab.add_row(row)
            f.close()
            if i == (len(proj_conf['samples']) - 1):
                print >> json, '}'
            else:
                print >> json, '},'
        print >> json, '}'
        json.close()
        d['Read_Distribution'] = tab.draw()

    except:
        print "Could not make Read Distribution table"
        pass

    ## FPKM_PCAplot, FPKM_heatmap
    if os.path.exists("FPKM_PCAplot.pdf") and os.path.exists(
            "FPKM_heatmap.pdf"):
        d['FPKM_PCAplot'] = m2r.image("FPKM_PCAplot.pdf", width="100%")
        d['FPKM_heatmap'] = m2r.image("FPKM_heatmap.pdf", width="100%")
    else:
        print "could not make FPKM PCAplot and FPKM heatmap"

    ## rRNA_table
    try:
        tab = Texttable()
        tab.set_cols_dtype(['t', 't'])
        tab.add_row(["Sample", "rRNA"])
        f = open('rRNA.quantification', 'r')
        D = {}
        for line in f:
            D[str(line.split('\t')[0].strip())] = str(
                line.split('\t')[1].strip())
        for sample_name in proj_conf['samples']:
            if D.has_key(sample_name):
                tab.add_row([sample_name, D[sample_name]])
        d['rRNA_table'] = tab.draw()
        f.close()
    except:
        print "could not generate rRNA table"
        pass

    return d
    def command_map(self, params):
        '''[<add/list/delete/update>] [<param.1> ... <param.n>]
        list/update/add mappings between VM name and PDU port
        map add <datastore> <vm> <pdu> <port>
         - Add an entry for VM and vPDU port
        e.g.:
            Add an entry.
            map add datastore1 vquanta_auto1 1 2

        map update <datastore> <vm> <pdu> <port>
         - update an entry for VM and vPDU port
        e.g.:
            Update an existing datastore entry
            map update datastore1 vquanta_auto1 3 1

        map delete <datastore> <vm>
         - Delete a datastore or a mapping for vm
         e.g.:
            Delete "datastore1"
            map delete datastore1

            Delete a mapping "vquanta_auto1 = 2" in datastore1
            map delete datastore1 vquanta_auto1

        map list
         - List all mappings between VMs and vPDU ports

        Note: when you are done to make changes, please run 'map list' to be
        sure eveything is correct.
        '''
        if len(params) == 0:
            return

        if params[0] == "add" or params[0] == "update":
            if len(params) != 5:
                self.writeresponse(colors.RED + "Invalid parameters." +
                                   colors.NORMAL)
                return

            self.mapping_file_handle.update(params[1], params[2], params[3],
                                            params[4])

        elif params[0] == "delete":
            if len(params) == 2:
                self.mapping_file_handle.delete(params[1])
            elif len(params) == 3:
                self.mapping_file_handle.delete(params[1], params[2])
            else:
                self.writeresponse("Invalid parameters.")
        elif params[0] == "list":
            table = Texttable()
            table.header(["PDU", "Port", "VM Name", "Datastore"])
            table.set_cols_align(['c', 'c', 'c', 'c'])

            for node_list in self.mapping_file_handle.nodes_list:
                datastore = node_list.keys()[0]
                for ni in node_list[datastore]:
                    table.add_row([
                        get_color_string(bcolors.GREEN, ni["control_pdu"]),
                        get_color_string(bcolors.GREEN, ni["control_port"]),
                        get_color_string(bcolors.GREEN, ni['node_name']),
                        get_color_string(bcolors.GREEN, datastore)
                    ])

            self.writeresponse(table.draw())
def print_uncertainty(predictions, uncertainties, metric_parameters):
    """Visualize the predictions with uncertainties.
    
    Args:
        - predictions: predictions of each patient
        - uncertainties: uncertainties of each prediction
        - metric_parameters: parameters for the problem and labels
        
    Returns:        
        - For online predictions, returns graphs
        - For one-shot predictions, returns table
    """
    # Parameters
    label_sets = metric_parameters["label_name"]
    problem = metric_parameters["problem"]
    graph_format = ["bo-", "r+--", "gs-.", "cp:", "m*-"]

    # For one-shot prediction setting
    if problem == "one-shot":
        # Initialize table
        perf_table = Texttable()
        first_row = ["id/label"] + label_sets
        perf_table.set_cols_align(["c" for _ in range(len(first_row))])
        multi_rows = [first_row]

        for i in range(predictions.shape[0]):
            curr_row = [str(i + 1)]

            # For each label
            for j in range(len(label_sets)):
                label_name = label_sets[j]
                curr_row = curr_row + [
                    str(np.round(predictions[i, j], 4)) + "+-" +
                    str(np.round(uncertainties[i, j], 4))
                ]
            multi_rows = multi_rows + [curr_row]

        perf_table.add_rows(multi_rows)
        # Print table
        print(perf_table.draw())
        # Return table
        return perf_table.draw()

    # For online prediction setting
    elif problem == "online":
        # Initialize the graph
        figs = []

        for i in range(predictions.shape[0]):
            fig = plt.figure(i + 10, figsize=(8, 5))
            legend_set = []

            # For each label
            for j in range(len(label_sets)):
                label_name = label_sets[j]

                curr_perf = predictions[i][:, j]
                under_line = curr_perf - uncertainties[i][:, j]
                over_line = curr_perf + uncertainties[i][:, j]
                legend_set = legend_set + [label_name]
                plt.plot(range(len(curr_perf) - 1), curr_perf[:-1],
                         graph_format[j])
                plt.fill_between(range(len(curr_perf) - 1),
                                 under_line[:-1],
                                 over_line[:-1],
                                 alpha=0.5)

            plt.xlabel("Sequence Length", fontsize=10)
            plt.ylabel("Predictions", fontsize=10)
            plt.legend(legend_set, fontsize=10)
            plt.title("ID: " + str(i + 1), fontsize=10)
            plt.grid()
            # Print graph
            plt.show()

            fig.patch.set_facecolor("#f0f2f6")
            figs.append(fig)
        # Return graph
        return figs
def main():
    Train_set, Test_set = [
        pd.read_csv('Dataset' + '/' + i, index_col=0)
        for i in ['train.csv', 'test.csv']
    ]
    X_train = Train_set.drop(['Survived'], axis=1)
    Y_train = Train_set.Survived
    """ Table of information """
    print("\n/////////////////////// Table : Summary ///////////////////////")
    t = Texttable()
    t.add_rows([['# Name', ' # Shape'], ['X_train', X_train.shape],
                ['Y_train', Y_train.shape], ['X_tset', Test_set.shape]])
    print(t.draw())
    print('\n Featurenames :{}'.format(X_train.columns))
    print("//////////////////////////////////////////////////////////////\n")

    X_train, Test_set = reform_data(X_train, Test_set)

    X_train_splitted, X_test_splitted, Y_train_splitted, Y_test_splitted = train_test_split(
        X_train, Y_train, test_size=0.15, random_state=4)
    """ Table of information """
    print(
        "\n/////////////////////// Table : Summary After split ///////////////////////"
    )
    t = Texttable()
    t.add_rows([['# Name', ' # Shape'], ['X_train', X_train_splitted.shape],
                ['Y_train', Y_train_splitted.shape],
                ['X_val', X_test_splitted.shape],
                ['Y_val', Y_test_splitted.shape], ['X_tset', Test_set.shape]])
    print(t.draw())
    print('\n Featurenames :{}'.format(X_train.columns))
    print("//////////////////////////////////////////////////////////////\n")

    DoLogreg.classify_LogReg(X_train_splitted, Y_train_splitted,
                             X_test_splitted, Y_test_splitted)
    DoLogreg.Classify_LogReg_kfold(X_train, Y_train)
    #DoKNN.find_K(X_train, Y_train,X_test,Y_test)
    DoKNN.classify_KNN(X_train_splitted,
                       Y_train_splitted,
                       X_test_splitted,
                       Y_test_splitted,
                       k=13)
    DoKNN.Classify_KNN_kfold(X_train, Y_train, k=7)
    #DoKNN.find_K_GridseacrhCV(X_train, Y_train)

    #model = ANN.simple(X_train, Y_train)
    #model.save('my_model.h5')
    from keras.models import load_model
    # load model
    model = load_model('my_model.h5')
    predicted = model.predict(Test_set)
    predicted[predicted > 0.5] = 1
    predicted[predicted < 0.5] = 0
    predicted = predicted.astype('uint8')
    submission = pd.read_csv('Dataset/gender_submission.csv')
    submission['Survived'] = predicted
    submission.to_csv('submission.csv', index=False)

    import io
    import requests
    url = "https://github.com/thisisjasonjafari/my-datascientise-handcode/raw/master/005-datavisualization/titanic.csv"
    s = requests.get(url).content
    c = pd.read_csv(io.StringIO(s.decode('utf-8')))

    test_data_with_labels = c
    print(2)
Exemple #51
0
def print_stats(all_predicates, verbose=0):
    predicates_by_pred = defaultdict(list)

    for predicate in all_predicates:
        predicates_by_pred[predicate.n_pred].append(predicate)

    num_dict = {}

    for n_pred, predicates in predicates_by_pred.items():
        num_dict[n_pred] = [len(predicates)]
        num_dict[n_pred].append(
            sum([predicate.num_imp_arg() for predicate in predicates]))
        if verbose >= 1:
            num_dict[n_pred].append(
                sum([predicate.num_imp_arg(2) for predicate in predicates]))
            num_dict[n_pred].append(
                sum([predicate.num_oracle() for predicate in predicates]))
        if verbose >= 2:
            num_dict[n_pred].append(
                sum([
                    1 for predicate in predicates
                    if 'arg0' in predicate.imp_args
                ]))
            num_dict[n_pred].append(
                sum([
                    1 for predicate in predicates
                    if 'arg1' in predicate.imp_args
                ]))
            num_dict[n_pred].append(
                sum([
                    1 for predicate in predicates
                    if 'arg2' in predicate.imp_args
                ]))
            num_dict[n_pred].append(
                sum([
                    1 for predicate in predicates
                    if 'arg3' in predicate.imp_args
                ]))
            num_dict[n_pred].append(
                sum([
                    1 for predicate in predicates
                    if 'arg4' in predicate.imp_args
                ]))

    total_pred = 0
    total_arg = 0
    total_arg_in_range = 0
    total_oracle_arg = 0
    total_imp_arg0 = 0
    total_imp_arg1 = 0
    total_imp_arg2 = 0
    total_imp_arg3 = 0
    total_imp_arg4 = 0

    table_content = []

    for n_pred, num in num_dict.items():
        table_row = [n_pred] + num[:2]
        table_row.append(float(num[1]) / num[0])
        if verbose >= 1:
            table_row.append(num[2])
            table_row.append(num[3])
            table_row.append(100. * float(num[3]) / num[1])
        if verbose >= 2:
            table_row += num[4:]
        table_content.append(table_row)

        total_pred += num[0]
        total_arg += num[1]
        if verbose >= 1:
            total_arg_in_range += num[2]
            total_oracle_arg += num[3]
        if verbose >= 2:
            total_imp_arg0 += num[4]
            total_imp_arg1 += num[5]
            total_imp_arg2 += num[6]
            total_imp_arg3 += num[7]
            total_imp_arg4 += num[8]

    table_content.sort(key=itemgetter(2), reverse=True)
    table_row = [
        'Overall', total_pred, total_arg,
        float(total_arg) / total_pred
    ]
    if verbose >= 1:
        table_row.extend([
            total_arg_in_range, total_oracle_arg,
            100. * float(total_oracle_arg) / total_arg
        ])
    if verbose >= 2:
        table_row.extend([
            total_imp_arg0, total_imp_arg1, total_imp_arg2, total_imp_arg3,
            total_imp_arg4
        ])
    table_content.append([''] * len(table_row))
    table_content.append(table_row)

    table_header = ['Pred.', '# Pred.', '# Imp.Arg.', '# Imp./pred.']
    if verbose >= 1:
        table_header.extend(
            ['# Imp.Arg.in.range', '# Oracle', 'Oracle Recall'])
    if verbose >= 2:
        table_header.extend([
            '# Imp.Arg.0', '# Imp.Arg.1', '# Imp.Arg.2', '# Imp.Arg.3',
            '# Imp.Arg.4'
        ])

    table = Texttable()
    table.set_deco(Texttable.BORDER | Texttable.HEADER)
    table.set_cols_align(['c'] * len(table_header))
    table.set_cols_valign(['m'] * len(table_header))
    table.set_cols_width([15] * len(table_header))
    table.set_precision(2)

    table.header(table_header)
    for row in table_content:
        table.add_row(row)

    print table.draw()
def print_performance(performance, metric_sets, metric_parameters):
    """Visualize the overall performance.
    
    Args:
        - performance: dictionary based performances
        - metric_sets: sets of metric
        - metric_parameters: parameters for the problem and labels
        
    Returns:        
        - For online prediction, returns graphs
        - For one-shot prediction, returns table
    """
    # Parameters
    label_sets = metric_parameters["label_name"]
    problem = metric_parameters["problem"]
    graph_format = ["bo-", "r+--", "gs-.", "cp:", "m*-"]

    # For one-shot prediction setting
    if problem == "one-shot":
        # Initialize table
        perf_table = Texttable()
        first_row = ["metric/label"] + label_sets
        perf_table.set_cols_align(["c" for _ in range(len(first_row))])
        multi_rows = [first_row]

        # For each metric
        for i in range(len(metric_sets)):
            metric_name = metric_sets[i]
            curr_row = [metric_name]

            # For each label
            for j in range(len(label_sets)):
                label_name = label_sets[j]
                curr_key = label_name + " + " + metric_name
                curr_row = curr_row + [str(performance[curr_key])]
                multi_rows = multi_rows + [curr_row]

        perf_table.add_rows(multi_rows)
        # Print table
        print(perf_table.draw())
        # Return table
        return perf_table.draw()

    # For online prediction setting
    elif problem == "online":
        # Initialize the graph
        figs = []

        # For each metric
        for i in range(len(metric_sets)):
            metric_name = metric_sets[i]
            curr_row = [metric_name]

            fig = plt.figure(i, figsize=(8, 5))
            legend_set = []

            # For each label
            for j in range(len(label_sets)):
                label_name = label_sets[j]
                curr_key = label_name + " + " + metric_name
                curr_row = curr_row + [str(performance[curr_key])]
                curr_perf = performance[curr_key]
                legend_set = legend_set + [label_name]

                plt.plot(range(len(curr_perf) - 1), curr_perf[:-1],
                         graph_format[j])
                plt.xlabel("Sequence Length", fontsize=10)
                plt.ylabel("Performance", fontsize=10)
                plt.legend(legend_set, fontsize=10)
                plt.title("Performance metric: " + metric_name, fontsize=10)
                plt.grid()
                # Print figure
                plt.show()

            fig.patch.set_facecolor("#f0f2f6")
            figs.append(fig)
        # Return figure
        return figs
Exemple #53
0
        'valid_chats', 'total_turns', 'max_turns', 'min_turns',
        'non-valid_chats'
    ]
    rows = ['username'] + header_order
    indv_rows = [[user] + [user_dict[user][p] for p in rows[1:]]
                 for user in order]
    with open('leaderboard.csv', 'w') as fp:
        writer = csv.writer(fp)
        writer.writerow(rows)
        for row in indv_rows:
            writer.writerow(row)
    with open('leaderboard.md', 'w') as fp:
        page_header = '''
---
layout: project_page
title: RLLChatBot Leaderboard
---

Updated every 24 hours.

        '''
        headers = '|'.join(rows) + '\n'
        sep = '|'.join(['-' * len(row) for row in rows]) + '\n'
        tds = '\n'.join(
            ['|'.join([str(item) for item in row]) for row in indv_rows])
        fp.write(page_header + '\n' + headers + sep + tds + '\n')
    rows = [rows]
    rows.extend(indv_rows)
    t.add_rows(rows)
    print t.draw()
Exemple #54
0
def execute_triggers():
    mycursor = mydb.cursor()
    sql = "INSERT INTO tempTrigger VALUES (2)"
    mycursor.execute(sql)
    mydb.commit()

    sql_select_Query = "select * from tempdata"
    cursor = mydb.cursor()
    cursor.execute(sql_select_Query)
    records = cursor.fetchall()
    index = 1

    table = Texttable()
    table.set_max_width(max_width=110)

    table.add_row([
        'Index', 'SSN', 'Name', 'Phone Number', 'Card Number',
        'Card expiry date'
    ])
    for row in records:
        table.add_row([index, row[0], row[1], row[2], row[3], row[4]])
        index += 1

    print(
        "Triggered data for membership renewal saved in trigger5_1.txt file!")
    file = open("trigger5_1.txt", "w+")

    file.write("Membership renewal data!\n\n")

    file.write(table.draw())

    file.close()

    sql_select_Query = "select * from tempdata2"
    cursor = mydb.cursor()
    cursor.execute(sql_select_Query)
    records = cursor.fetchall()
    index = 1

    table = Texttable()
    table.set_max_width(max_width=100)

    table.add_row([
        'Index', 'ISBN', 'Issue date', 'Book Due Date', 'Over due days',
        'Author', 'Title', 'SSN', 'Name'
    ])
    for row in records:
        table.add_row([
            index, row[0], row[1], row[2], row[3], row[4], row[5], row[6],
            row[7]
        ])
        index += 1

    print(
        "Triggered data for outstanding overdue book in trigger5_2.txt file!")
    file = open("trigger5_2.txt", "w+", encoding='utf-8')

    file.write("Outstanding overdue data!\n\n")

    file.write(table.draw())

    file.close()
Exemple #55
0
 def output_table(data, columns):
     table = Texttable(max_width=get_terminal_size()[1])
     table.set_deco(Texttable.BORDER | Texttable.VLINES | Texttable.HEADER)
     table.header([i.label for i in columns])
     table.add_rows([[TableOutputFormatter.format_value(resolve_cell(row, i.accessor), i.vt) for i in columns] for row in data], False)
     print table.draw()
Exemple #56
0
def print_eval_stats(all_rich_predicates):
    predicates_by_pred = defaultdict(list)

    for rich_predicate in all_rich_predicates:
        predicates_by_pred[rich_predicate.n_pred].append(rich_predicate)

    num_dict = {}

    total_dice = 0.0
    total_gt = 0.0
    total_model = 0.0

    for n_pred, predicates in predicates_by_pred.items():
        num_dict[n_pred] = [len(predicates)]
        num_dict[n_pred].append(
            sum([predicate.num_imp_args() for predicate in predicates]))

        pred_dice = 0.0
        pred_gt = 0.0
        pred_model = 0.0
        for predicate in predicates:
            pred_dice += predicate.sum_dice
            pred_gt += predicate.num_gt
            pred_model += predicate.num_model

        total_dice += pred_dice
        total_gt += pred_gt
        total_model += pred_model

        precision, recall, f1 = compute_f1(pred_dice, pred_gt, pred_model)

        num_dict[n_pred].append(precision * 100)
        num_dict[n_pred].append(recall * 100)
        num_dict[n_pred].append(f1 * 100)

    total_precision, total_recall, total_f1 = \
        compute_f1(total_dice, total_gt, total_model)

    total_pred = 0
    total_arg = 0

    table_content = []

    for n_pred, num in num_dict.items():
        table_row = [n_pred] + num
        table_content.append(table_row)

        total_pred += num[0]
        total_arg += num[1]

    table_content.sort(key=itemgetter(2), reverse=True)
    table_content.append([''] * 6)
    table_content.append([
        'Overall', total_pred, total_arg, total_precision * 100,
        total_recall * 100, total_f1 * 100
    ])

    table_header = [
        'Pred.', '# Pred.', '# Imp.Arg.', 'Precision', 'Recall', 'F1'
    ]

    table = Texttable()
    table.set_deco(Texttable.BORDER | Texttable.HEADER)
    table.set_precision(2)

    table.header(table_header)
    for row in table_content:
        table.add_row(row)

    print table.draw()
Exemple #57
0
 def output_list(data, label, vt=ValueType.STRING):
     table = Texttable(max_width=get_terminal_size()[1])
     table.set_deco(Texttable.BORDER | Texttable.VLINES | Texttable.HEADER)
     table.header([label])
     table.add_rows([[i] for i in data], False)
     print table.draw()
Exemple #58
0
def report(env, filters):
    '''Summarise the dependency tree of the current project'''

    lCmdHeaders = ['path', 'flags', 'package', 'component', 'map', 'lib']

    lFilterFormat = re.compile('([^=]*)=(.*)')
    lFilterFormatErrors = []
    lFieldNotFound = []
    lFilters = []

    # print ( filters )

    for f in filters:
        m = lFilterFormat.match(f)
        if not m:
            lFilterFormatErrors.append(f)
            continue
        # print (m.group(1))

        if m.group(1) not in lCmdHeaders:
            lFieldNotFound.append(m.group(1))
            continue

        try:
            i = lCmdHeaders.index(m.group(1))
            r = re.compile(m.group(2))
            lFilters.append((i, r))
        except RuntimeError as e:
            lFilterFormatErrors.append(f)

    if lFilterFormatErrors:
        raise click.ClickException(
            "Filter syntax errors: " +
            ' '.join(['\'' + e + '\'' for e in lFilterFormatErrors]))

    if lFieldNotFound:
        raise click.ClickException(
            "Filter syntax errors: fields not found {}. Expected one of {}".
            format(', '.join("'" + s + "'" for s in lFieldNotFound), ', '.join(
                ("'" + s + "'" for s in lCmdHeaders))))

    # return
    lParser = env.depParser

    # lTitle = Texttable(max_width=0)
    # lTitle.header(['Commands'])
    # lTitle.set_chars(['-', '|', '+', '-'])
    # lTitle.set_deco(Texttable.BORDER)
    # secho(lTitle.draw(), fg='blue')

    echo()
    secho('* Parsed commands', fg='blue')

    lPrepend = re.compile('(^|\n)')
    for k in lParser.commands:
        echo('  + {0} ({1})'.format(k, len(lParser.commands[k])))
        if not lParser.commands[k]:
            echo()
            continue

        lCmdTable = Texttable(max_width=0)
        lCmdTable.header(lCmdHeaders)
        lCmdTable.set_deco(Texttable.HEADER | Texttable.BORDER)
        lCmdTable.set_chars(['-', '|', '+', '-'])
        for lCmd in lParser.commands[k]:
            # print(lCmd)
            # lCmdTable.add_row([str(lCmd)])
            lRow = [
                relpath(lCmd.FilePath, env.srcdir),
                ','.join(lCmd.flags()),
                lCmd.Package,
                lCmd.Component,
                lCmd.Map,
                lCmd.Lib,
            ]

            if lFilters and not all(
                [rxp.match(lRow[i]) for i, rxp in lFilters]):
                continue

            lCmdTable.add_row(lRow)

        echo(lPrepend.sub('\g<1>  ', lCmdTable.draw()))
        echo()

    string = ''

    string += '+----------------------------------+\n'
    string += '|  Resolved packages & components  |\n'
    string += '+----------------------------------+\n'
    string += 'packages: ' + str(list(lParser.components.iterkeys())) + '\n'
    string += 'components:\n'
    for pkg in sorted(lParser.components):
        string += '+ %s (%d)\n' % (pkg, len(lParser.components[pkg]))
        for cmp in sorted(lParser.components[pkg]):
            string += '  > ' + str(cmp) + '\n'

    if lParser.missing:
        string += '\n'
        string += '+----------------------------------------+\n'
        string += '|  Missing packages, components & files  |\n'
        string += '+----------------------------------------+\n'

        if lParser.missingPackages:
            string += 'packages: ' + \
                str(list(lParser.missingPackages)) + '\n'

        # ------
        lCNF = lParser.missingComponents
        if lCNF:
            string += 'components: \n'

            for pkg in sorted(lCNF):
                string += '+ %s (%d)\n' % (pkg, len(lCNF[pkg]))

                for cmp in sorted(lCNF[pkg]):
                    string += '  > ' + str(cmp) + '\n'
        # ------

        # ------
    echo(string)

    lFNF = lParser.missingFiles

    if lFNF:

        lFNFTable = Texttable(max_width=0)
        lFNFTable.header(
            ['path expression', 'package', 'component', 'included by'])
        lFNFTable.set_deco(Texttable.HEADER | Texttable.BORDER)

        for pkg in sorted(lFNF):
            lCmps = lFNF[pkg]
            for cmp in sorted(lCmps):
                lPathExps = lCmps[cmp]
                for pathexp in sorted(lPathExps):

                    lFNFTable.add_row([
                        relpath(pathexp, env.srcdir),
                        pkg,
                        cmp,
                        '\n'.join([
                            relpath(src, env.srcdir)
                            for src in lPathExps[pathexp]
                        ]),
                    ])
        echo(lPrepend.sub('\g<1>  ', lFNFTable.draw()))
Exemple #59
0
 def printTable(self, board):
     table = Texttable()
     table.header(self.__bHeader)
     for i in range(8):
         table.add_row([str(i + 1)] + [board[i][j] for j in range(8)])
     print(table.draw() + '\n')
Exemple #60
0
 def output_dict(data, key_label, value_label, value_vt=ValueType.STRING):
     table = Texttable(max_width=get_terminal_size()[1])
     table.set_deco(Texttable.BORDER | Texttable.VLINES | Texttable.HEADER)
     table.header([key_label, value_label])
     table.add_rows([[row[0], TableOutputFormatter.format_value(row[1], value_vt)] for row in data.items()], False)
     print table.draw()