示例#1
0
文件: graph.py 项目: QuicNYC/rscript
    def select(self,type='VAV',subtype = ['RM STPT DIAL','ROOM TEMP'], floor = 0,nexp='', pattern ='{i}',cond = 'cmax(x,7)',maxn = 10, dtfilter = ''):
        env = self._genv()       
        env.init()
        l = env.getSensorsByType(type)
        lname = []

        for it in l:
            k = it.split(':')
            if floor != 0:
                fl = int(k[0].split('.')[1])
                if fl != floor:
                    continue

                if nexp != '':
                    sname = k[0].split('.')[3]
                    if not fnmatch.fnmatch(sname,nexp):
                        continue    

                if k[1] in subtype:
                    lname.append((it,env.getSensorId(it)))

        ltemp = sorted(lname,key=itemgetter(0))[:maxn]
        filt = ''
        i = 0

        f = Formatter()
        l = "{{%s}}"
        f.format(l)

        ns = ''
        exp = []
        i,m =0,len(ltemp)
        loopi=0
        while i < m:
            h = f.parse(pattern)
            for a,b,c,d in h :
                ns += a 
                if b is not None : 
                    ns += '{' + str(eval(b)) + '}'
                    i = eval(b)
                    loopi = max(i,loopi)
            if cond != '':
                ns += '.apply(lambda x:' + cond + ')'
            if loopi < m-1:
                ns += '; '
            i = loopi + 1

        cs = self.getSeries(ltemp,dtfilter)
        re = self.getExpression(ltemp,ns, cs)
        series = []
        for name ,c in re:
            dataserie = defaultdict()  
            json_s = c[['stime','svalue']].to_json(orient='values')
            dataserie['name']=name
            dataserie['data']=json_s
            series.append(dataserie)
        return series
示例#2
0
def strfdelta(tdelta,
              fmt='{D:02}d {H:02}h {M:02}m {S:02}s',
              inputtype='timedelta'):
    # convert time to our format
    if inputtype == 'timedelta':
        remainder = int(tdelta.total_seconds())
    elif inputtype in ['s', 'seconds']:
        remainder = int(tdelta)
    elif inputtype in ['m', 'minutes']:
        remainder = int(tdelta) * 60
    elif inputtype in ['h', 'hours']:
        remainder = int(tdelta) * 3600
    elif inputtype in ['d', 'days']:
        remainder = int(tdelta) * 86400
    elif inputtype in ['w', 'weeks']:
        remainder = int(tdelta) * 604800

    f = Formatter()
    desired_fields = [field_tuple[1] for field_tuple in f.parse(fmt)]
    possible_fields = ('W', 'D', 'H', 'M', 'S')
    constants = {'W': 604800, 'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    values = {}
    for field in possible_fields:
        if field in desired_fields and field in constants:
            values[field], remainder = divmod(remainder, constants[field])
    return f.format(fmt, **values)
示例#3
0
class DescriptionMapMethod(MapMethod):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.formatter = Formatter()
        self.translator = Translator()

    def map(self, *args, **kwargs):
        try:
            template = self.additional_args["template"]
            locale = kwargs["raw_ad"].feed_in.locale

            # We take all keywords (no positional) and we translate them and then we create a dict
            # eg:
            #     template = "That is an example {SALARY}: $ {0} in {COMPANY}: {1}"
            #     locale = "es_ar"
            #     translated_words = {"SALARY": "Salario", "COMPANY": "Compañía"}
            #
            translated_words = {
                parse[1]: self.translator.translate(locale, parse[1],
                                                    "addesc_label")
                for parse in self.formatter.parse(template)
                if type(parse[1]) == str and not parse[1].isdigit()
            }

            addesc = self.formatter.format(template, *args, **translated_words)

            return {"addesc": addesc}
        except KeyError:
            type_id = kwargs["raw_ad"].feed_in.feed_type.id if kwargs.get(
                "raw_ad", None) else ""
            msg = "El parámetro 'template' no se encuentra definido para " + type_id
            raise FeedMappingException(msg)
示例#4
0
    def format(self, format_string, *args, **kwargs):
        def escape_envvar(matchobj):
            value = (x for x in matchobj.groups() if x is not None).next()
            return "${{%s}}" % value

        format_string_ = re.sub(self.ENV_VAR_REGEX, escape_envvar, format_string)
        return Formatter.format(self, format_string_, *args, **kwargs)
示例#5
0
    def get(self,
            key: Text,
            count: Optional[int]=None,
            formatter: Formatter=None,
            **params) -> Text:
        """
        Get the appropriate translation given the specified parameters.

        :param key: Translation key
        :param count: Count for plurals
        :param formatter: Optional string formatter to use
        :param params: Params to be substituted
        """

        if count is not None:
            raise TranslationError('Count parameter is not supported yet')

        try:
            out = self.dict[key]
        except KeyError:
            raise MissingTranslationError('Translation "{}" does not exist'
                                          .format(key))

        try:
            if not formatter:
                out = out.format(**params)
            else:
                out = formatter.format(out, **params)
        except KeyError as e:
            raise MissingParamError(
                'Parameter "{}" missing to translate "{}"'
                .format(e.args[0], key)
            )

        return out
示例#6
0
文件: rex.py 项目: skral/rez
    def format(self, format_string, *args, **kwargs):
        def escape_envvar(matchobj):
            value = (x for x in matchobj.groups() if x is not None).next()
            return "${{%s}}" % value

        format_string_ = re.sub(self.ENV_VAR_REGEX, escape_envvar,
                                format_string)
        return Formatter.format(self, format_string_, *args, **kwargs)
示例#7
0
文件: BTG.py 项目: rmarsollier/BTG
 def strfdelta(tdelta, fmt):
     f = Formatter()
     d = {}
     lst = {'H': 3600, 'M': 60, 'S': 1}
     k = map(lambda x: x[1], list(f.parse(fmt)))
     rem = int(tdelta.total_seconds())
     for i in ('H', 'M', 'S'):
         if i in k and i in lst.keys():
             d[i], rem = divmod(rem, lst[i])
     return f.format(fmt, **d)
def strfdelta(tdelta, fmt):
    """ Get a string from a timedelta.
    """
    f, d = Formatter(), {}
    l = {"D": 86400, "H": 3600, "M": 60, "S": 1}
    k = list(map(lambda x: x[1], list(f.parse(fmt))))
    rem = int(tdelta.total_seconds())
    for i in ("D", "H", "M", "S"):
        if i in k and i in l.keys():
            d[i], rem = divmod(rem, l[i])
    return f.format(fmt, **d)
示例#9
0
class TableCell():
    def __init__(self, content=None):
        self.content = content
        self.fmtr = Formatter()
    def __repr__(self):
        return self.display()
    def display(self):
        """ type dependent string formatting """
        if isinstance(self.content, UFloat):
            return "{}".format(self.fmtr.format("{0:.1uS}", self.content))
        elif isinstance(self.content, int):
            return "{}".format(self.fmtr.format("{0:.0f}", self.content))
        elif isinstance(self.content, float):
            return "{}".format(self.fmtr.format("{0:.3f}", self.content))
        elif isinstance(self.content, basestring):
            return self.content
        elif self.content is None:
            return "None"
        else:
            return str(self.content)
示例#10
0
def strfdelta(tdelta, fmt):
    """ Get a string from a timedelta.
    """
    f, d = Formatter(), {}
    l = {'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    k = list(map(lambda x: x[1], list(f.parse(fmt))))
    rem = int(tdelta.total_seconds())
    for i in ('D', 'H', 'M', 'S'):
        if i in k and i in l.keys():
            d[i], rem = divmod(rem, l[i])
    return f.format(fmt, **d)
示例#11
0
def strfdelta(tdelta, fmt):
    """ Get a string from a timedelta.
    """
    f, d = Formatter(), {}
    l = {'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    k = list(map(lambda x: x[1], list(f.parse(fmt))))
    rem = int(tdelta.total_seconds())
    for i in ('D', 'H', 'M', 'S'):
        if i in k and i in l.keys():
            d[i], rem = divmod(rem, l[i])
    return f.format(fmt, **d)
示例#12
0
def getTimeFromTdelta(tdelta, fmt):
    f = Formatter()
    d = {}
    l = {'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    k = map(lambda x: x[1], list(f.parse(fmt)))
    rem = int(tdelta.total_seconds())

    for i in ('D', 'H', 'M', 'S'):
        if i in k and i in l.keys():
            d[i], rem = divmod(rem, l[i])

    return f.format(fmt, **d)
示例#13
0
def strfdelta(tdelta,
              fmt='{D:02}d {H:02}h {M:02}m {S:02}s',
              inputtype='timedelta'):
    """Convert a datetime.timedelta object or a regular number to a custom-
    formatted string, just like the stftime() method does for datetime.datetime
    objects.

    The fmt argument allows custom formatting to be specified.  Fields can
    include seconds, minutes, hours, days, and weeks.  Each field is optional.

    Some examples:
        '{D:02}d {H:02}h {M:02}m {S:02}s' --> '05d 08h 04m 02s' (default)
        '{W}w {D}d {H}:{M:02}:{S:02}'     --> '4w 5d 8:04:02'
        '{D:2}d {H:2}:{M:02}:{S:02}'      --> ' 5d  8:04:02'
        '{H}h {S}s'                       --> '72h 800s'

    The inputtype argument allows tdelta to be a regular number instead of the
    default, which is a datetime.timedelta object.  Valid inputtype strings:
        's', 'seconds',
        'm', 'minutes',
        'h', 'hours',
        'd', 'days',
        'w', 'weeks'

    :param tdelta: time (in datetime or integer)
    :param fmt: the desired format
    :param inputtype: type of input
    :return: formatted time
    """

    # Convert tdelta to integer seconds.
    if inputtype == 'timedelta':
        remainder = int(tdelta.total_seconds())
    elif inputtype in ['s', 'seconds']:
        remainder = int(tdelta)
    elif inputtype in ['m', 'minutes']:
        remainder = int(tdelta) * 60
    elif inputtype in ['h', 'hours']:
        remainder = int(tdelta) * 3600
    elif inputtype in ['d', 'days']:
        remainder = int(tdelta) * 86400
    elif inputtype in ['w', 'weeks']:
        remainder = int(tdelta) * 604800

    f = Formatter()
    desired_fields = [field_tuple[1] for field_tuple in f.parse(fmt)]
    possible_fields = ('W', 'D', 'H', 'M', 'S')
    constants = {'W': 604800, 'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    values = {}
    for field in possible_fields:
        if field in desired_fields and field in constants:
            values[field], remainder = divmod(remainder, constants[field])
    return f.format(fmt, **values)
示例#14
0
def strfdelta(tdelta: datetime.timedelta, fmt: str) -> str:
    f = Formatter()
    d = {}
    l = {'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    k = list(map(lambda x: x[1], list(f.parse(fmt))))
    rem = int(tdelta.total_seconds())

    for i in ('D', 'H', 'M', 'S'):
        if i in k and i in l.keys():
            d[i], rem = divmod(rem, l[i])

    return f.format(fmt, **d)
示例#15
0
    def getNews(message):
        # format is "$news site [number]", where number is the number
        # of articles to return, because discord has a character limit
        # of 2000. You could make it send multiple messages but whatever.
        split_message = message.content.split(' ')

        if len(split_message) < 2 or len(split_message) > 3 or News.isHelp(
                split_message[1]):
            return News.getHelp()

        site = split_message[1]

        try:
            n = 5
            if len(split_message) == 3:
                n = int(split_message[2])

            articles = News.getNewsList(site)[:n]

        except (UnknownSiteError, IndexError):
            sites = '\n'.join(News.sites.keys())

            result = '**Valid sites are:**\n\n' + sites
            return result

        except ValueError:
            result = '**Pick a valid number of results to return**'
            return result

        if not articles:
            return '**Sorry, there are no articles!**'

        formatter = Formatter()
        responses = []
        for i, article in enumerate(articles):
            title = article['title']

            author = ''
            if 'author' in article.keys():
                author = ' by ' + article['author']

            link = article['link']

            response = formatter.format("{num}. {_title}{_author} (<{link}>)", num=i+1, \
                                        _title=title, _author=author, \
                                        link=link)

            responses.append(response)

        result = '\n'.join(responses)

        return result
示例#16
0
class SimpleBar(object):
    """A minimal progress bar widget for stdout.

    Displays a custom message to sys.stdout, and updates it in-place on demand.
    """

    def __init__(self, message_str=None, max_width=80):
        """Create a SimpleBar with an optional message string.

        Args:
        message_str(str): An optional format string that is displayed and formatted
        in every update operation.
        max_width(int): The maximum width of the progress bar, message and all.

        """
        self.formatter = Formatter()
        if message_str:
            self.message_str = message_str
        self.max_width = max_width


    def update_args(self, *args):
        """Display the appropriately updated progress message.

        Args:
        args: Formatter-style arguments for the format string.
        """
        self.__restart_line()
        print self.__pad_string(self.formatter.format(self.message_str, *args),
                              self.max_width),

    def update(self, string, *args):
        """Update progress message and display with appropriate updates.

        Args:
        string (str): The format string for the message to be displayed.
        args (optional arguments): Formatter-style arguments for the format string.
        """
        self.message_str = string
        self.update_args(*args)

        
    def __restart_line(self):
        """Move cursor to the start of the current line immediately.
        """
        sys.stdout.write('\r')
        sys.stdout.flush()

    def __pad_string(self, s, length):
        """Pad a string with trailing spaces to the given length.
        """
        return s.ljust(length)
示例#17
0
class TableCell():
    def __init__(self, content=None):
        self.content = content
        self.fmtr = Formatter()

    def __repr__(self):
        return self.display()

    def display(self):
        """ type dependent string formatting """
        if isinstance(self.content, UFloat):
            return "{}".format(self.fmtr.format("{0:.1uS}", self.content))
        elif isinstance(self.content, int):
            return "{}".format(self.fmtr.format("{0:.0f}", self.content))
        elif isinstance(self.content, float):
            return "{}".format(self.fmtr.format("{0:.3f}", self.content))
        elif isinstance(self.content, basestring):
            return self.content
        elif self.content is None:
            return "None"
        else:
            return str(self.content)
示例#18
0
def strfdelta(tdelta,
              fmt="{D:02}d {H:02}h {M:02}m {S:02}s",
              inputtype="timedelta"):
    """Convert a datetime.timedelta object or a regular number to a custom-
    formatted string, just like the stftime() method does for datetime.datetime
    objects.

    The fmt argument allows custom formatting to be specified.  Fields can
    include seconds, minutes, hours, days, and weeks.  Each field is optional.

    Some examples:
        '{D:02}d {H:02}h {M:02}m {S:02}s' --> '05d 08h 04m 02s' (default)
        '{W}w {D}d {H}:{M:02}:{S:02}'     --> '4w 5d 8:04:02'
        '{D:2}d {H:2}:{M:02}:{S:02}'      --> ' 5d  8:04:02'
        '{H}h {S}s'                       --> '72h 800s'

    The inputtype argument allows tdelta to be a regular number instead of the
    default, which is a datetime.timedelta object.  Valid inputtype strings:
        's', 'seconds',
        'm', 'minutes',
        'h', 'hours',
        'd', 'days',
        'w', 'weeks'

    Credits: https://stackoverflow.com/a/42320260/4233693
    """

    # Convert tdelta to integer seconds.
    if inputtype == "timedelta":
        remainder = int(tdelta.total_seconds())
    elif inputtype in ["s", "seconds"]:
        remainder = int(tdelta)
    elif inputtype in ["m", "minutes"]:
        remainder = int(tdelta) * 60
    elif inputtype in ["h", "hours"]:
        remainder = int(tdelta) * 3600
    elif inputtype in ["d", "days"]:
        remainder = int(tdelta) * 86400
    elif inputtype in ["w", "weeks"]:
        remainder = int(tdelta) * 604800

    f = Formatter()
    desired_fields = [field_tuple[1] for field_tuple in f.parse(fmt)]
    possible_fields = ("W", "D", "H", "M", "S")
    constants = {"W": 604800, "D": 86400, "H": 3600, "M": 60, "S": 1}
    values = {}
    for field in possible_fields:
        if field in desired_fields and field in constants:
            values[field], remainder = divmod(remainder, constants[field])
    return f.format(fmt, **values)
示例#19
0
def strfdelta(tdelta, fmt):
    """ string representation of timedelta object.

    Code from: https://stackoverflow.com/a/17847006
    """
    f = Formatter()
    d = {}
    l = {'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    k = map(lambda x: x[1], list(f.parse(fmt)))
    rem = int(tdelta.total_seconds())

    for i in ('D', 'H', 'M', 'S'):
        if i in k and i in l.keys():
            d[i], rem = divmod(rem, l[i])

    return f.format(fmt, **d)
示例#20
0
    def get(self,
            key: Text,
            count: Optional[int] = None,
            formatter: Formatter = None,
            locale: Text = None,
            params: Optional[Dict[Text, Any]] = None,
            flags: Optional[Flags] = None) -> List[Text]:
        """
        Get the appropriate translation given the specified parameters.

        :param key: Translation key
        :param count: Count for plurals
        :param formatter: Optional string formatter to use
        :param locale: Prefered locale to get the string from
        :param params: Params to be substituted
        :param flags: Flags to help choosing one version or the other
        """

        if params is None:
            params = {}

        if count is not None:
            raise TranslationError('Count parameter is not supported yet')

        locale = self.choose_locale(locale)

        try:
            group: SentenceGroup = self.dict[locale][key]
        except KeyError:
            raise MissingTranslationError(
                'Translation "{}" does not exist'.format(key))

        try:
            trans = group.render(flags or {})
            out = []

            for line in trans:
                if not formatter:
                    out.append(line.format(**params))
                else:
                    out.append(formatter.format(line, **params))
        except KeyError as e:
            raise MissingParamError(
                'Parameter "{}" missing to translate "{}"'.format(
                    e.args[0], key))
        else:
            return out
示例#21
0
def strfdelta(tdelta, fmt=None):
    from string import Formatter
    if not fmt:
        # The standard, most human readable format.
        fmt = "{D} days {H:02} hours {M:02} minutes {S:02} seconds"
    if tdelta == timedelta():
        return "0 minutes"
    formatter = Formatter()
    return_map = {}
    div_by_map = {'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    keys = map(lambda x: x[1], list(formatter.parse(fmt)))
    remainder = int(tdelta.total_seconds())
    for unit in ('D', 'H', 'M', 'S'):
        if unit in keys and unit in div_by_map.keys():
            return_map[unit], remainder = divmod(remainder, div_by_map[unit])

    return formatter.format(fmt, **return_map)
示例#22
0
def strfdelta(tdelta, fmt=None):
    from string import Formatter
    if not fmt:
        # The standard, most human readable format.
        fmt = "{D} days {H:02} hours {M:02} minutes {S:02} seconds"
    if tdelta == timedelta():
        return "0 minutes"
    formatter = Formatter()
    return_map = {}
    div_by_map = {'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    keys = map(lambda x: x[1], list(formatter.parse(fmt)))
    remainder = int(tdelta.total_seconds())
    for unit in ('D', 'H', 'M', 'S'):
        if unit in keys and unit in div_by_map.keys():
            return_map[unit], remainder = divmod(remainder, div_by_map[unit])

    return formatter.format(fmt, **return_map)
示例#23
0
    def getNews(message):
        # format is "$news site [number]", where number is the number
        # of articles to return, because discord has a character limit
        # of 2000. You could make it send multiple messages but whatever.
        split_message = message.content.split(' ')

        if len(split_message) < 2 or len(split_message) > 3:
            return News.getHelp()

        site = split_message[1]

        try:
            n = 5
            if len(split_message) == 3:
                n = int(split_message[2])

            articles = News.getNewsList(site)[:n]

        except (UnknownSiteError, IndexError):
            result = '**Valid sites are:** \n\nbleepingcomputer\ndarkreading'
            return result

        except ValueError:
            result = '**Pick a valid number of results to return**'
            return result

        if not articles:
            return '**Sorry, there are no articles posted today!**'

        formatter = Formatter()
        responses = []
        today = date.today()
        for i, article in enumerate(articles):
            date_ = date.fromtimestamp(mktime(article['published_parsed']))
            if today > date_:
                break

            response = formatter.format("{num}. {title} by {author} ({link})", num=i+1, \
                                        title=article['title'], author=article['author'], \
                                        link=article['link'])

            responses.append(response)

        result = '\n'.join(responses)

        return result
def strfdelta(tdelta, fmt='{D:02}d {H:02}h {M:02}m {S:02}s', inputtype='timedelta'):
    """Convert a datetime.timedelta object or a regular number to a custom-
    formatted string, just like the stftime() method does for datetime.datetime
    objects.

    The fmt argument allows custom formatting to be specified.  Fields can 
    include seconds, minutes, hours, days, and weeks.  Each field is optional.

    Some examples:
        '{D:02}d {H:02}h {M:02}m {S:02}s' --> '05d 08h 04m 02s' (default)
        '{W}w {D}d {H}:{M:02}:{S:02}'     --> '4w 5d 8:04:02'
        '{D:2}d {H:2}:{M:02}:{S:02}'      --> ' 5d  8:04:02'
        '{H}h {S}s'                       --> '72h 800s'

    The inputtype argument allows tdelta to be a regular number instead of the  
    default, which is a datetime.timedelta object.  Valid inputtype strings: 
        's', 'seconds', 
        'm', 'minutes', 
        'h', 'hours', 
        'd', 'days', 
        'w', 'weeks'
    """

    # Convert tdelta to integer seconds.
    if inputtype == 'timedelta':
        remainder = int(tdelta.total_seconds())
    elif inputtype in ['s', 'seconds']:
        remainder = int(tdelta)
    elif inputtype in ['m', 'minutes']:
        remainder = int(tdelta)*60
    elif inputtype in ['h', 'hours']:
        remainder = int(tdelta)*3600
    elif inputtype in ['d', 'days']:
        remainder = int(tdelta)*86400
    elif inputtype in ['w', 'weeks']:
        remainder = int(tdelta)*604800

    f = Formatter()
    desired_fields = [field_tuple[1] for field_tuple in f.parse(fmt)]
    possible_fields = ('W', 'D', 'H', 'M', 'S')
    constants = {'W': 604800, 'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    values = {}
    for field in possible_fields:
        if field in desired_fields and field in constants:
            values[field], remainder = divmod(remainder, constants[field])
    return f.format(fmt, **values)
示例#25
0
def strfdelta(tdelta, fmt):
    f = Formatter()
    d = {}
    l = {'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    k = map(lambda x: x[1], list(f.parse(fmt)))
    rem = int(tdelta.total_seconds())

    for i in ('D', 'H', 'M', 'S'):
        if i in k and i in l.keys():
            d[i], rem = divmod(rem, l[i])

    return f.format(fmt, **d)


# Oddly, this did not work for me as written,
# I had to change map( lambda x: x[1], list(f.parse(fmt)))
# to list(map( lambda x: x[1], list(f.parse(fmt))))
示例#26
0
    def strfdelta(tsec,
                  format_str="P",
                  format_no_day="PT{H}H{M}M{S}S",
                  format_zero="PT0S"):
        """Formatting the time duration.

        Duration ISO8601 format (PnYnMnDTnHnMnS): http://en.wikipedia.org/wiki/ISO_8601
        Choosing the format P[nD]TnHnMnS where days is the total number of days (if not 0), 0 values may be omitted,
        0 duration is PT0S

        :param tsec: float, number of seconds
        :param format_str: Format string, ISO 8601 is "P{D}DT{H}H{M}M{S}S". Default is a format string "P": will
            use ISO 8601 but skip elements that have 0 value, e.g. P1H7S instead of P1H0M7S
        :param format_no_day: Format string,  ISO 8601, default "PT{H}H{M}M{S}S"
        :param format_zero: format for when tsec is 0 or None, default "PT0S"
        :return: Formatted time duration
        """
        if not tsec:
            # 0 or None
            return format_zero

        f = Formatter()
        d = {}
        l = {'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
        rem = long(tsec)

        if format_str == "P":
            # variable format
            if 0 < tsec < 86400:
                format_str = "PT"
            for i in ('D', 'H', 'M', 'S'):
                if i in l.keys():
                    d[i], rem = divmod(rem, l[i])
                    if d[i] != 0:
                        format_str = "%s{%s}%s" % (format_str, i, i)
        else:
            if 0 < tsec < 86400:
                format_str = format_no_day
            k = map(lambda x: x[1], list(f.parse(format_str)))

            for i in ('D', 'H', 'M', 'S'):
                if i in k and i in l.keys():
                    d[i], rem = divmod(rem, l[i])

        return f.format(format_str, **d)
示例#27
0
def strfdelta(timedelta, fmt=STATS_TIMEDELTA_STR):
    """
    Format a timedelta object in the given format.
    """
    f = Formatter()
    d = {}
    lst = {"D": 86400, "H": 3600, "M": 60, "S": 1}
    k = map(lambda x: x[1], list(f.parse(fmt)))
    remainder = int(timedelta.total_seconds())

    for i in ("D", "H", "M", "S"):
        if i in k and i in lst.keys():
            d[i], remainder = divmod(remainder, lst[i])

    for key, value in d.items():
        if value < 10 and "D" not in key:
            d[key] = "0" + str(value)
    return f.format(fmt, **d)
示例#28
0
    def strfdelta(tsec, format_str="P", format_no_day="PT{H}H{M}M{S}S", format_zero="PT0S"):
        """Formatting the time duration.

        Duration ISO8601 format (PnYnMnDTnHnMnS): http://en.wikipedia.org/wiki/ISO_8601
        Choosing the format P[nD]TnHnMnS where days is the total number of days (if not 0), 0 values may be omitted,
        0 duration is PT0S

        :param tsec: float, number of seconds
        :param format_str: Format string, ISO 8601 is "P{D}DT{H}H{M}M{S}S". Default is a format string "P": will
            use ISO 8601 but skip elements that have 0 value, e.g. P1H7S instead of P1H0M7S
        :param format_no_day: Format string,  ISO 8601, default "PT{H}H{M}M{S}S"
        :param format_zero: format for when tsec is 0 or None, default "PT0S"
        :return: Formatted time duration
        """
        if not tsec:
            # 0 or None
            return format_zero

        f = Formatter()
        d = {}
        l = {'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
        rem = long(tsec)

        if format_str == "P":
            # variable format
            if 0 < tsec < 86400:
                format_str = "PT"
            for i in ('D', 'H', 'M', 'S'):
                if i in l.keys():
                    d[i], rem = divmod(rem, l[i])
                    if d[i] != 0:
                        format_str = "%s{%s}%s" % (format_str, i, i)
        else:
            if 0 < tsec < 86400:
                format_str = format_no_day
            k = map(lambda x: x[1], list(f.parse(format_str)))

            for i in ('D', 'H', 'M', 'S'):
                if i in k and i in l.keys():
                    d[i], rem = divmod(rem, l[i])

        return f.format(format_str, **d)
示例#29
0
文件: rex.py 项目: rocket41/rez
    def format(self, format_string, *args, **kwargs):
        def escape_envvar(matchobj):
            value = (x for x in matchobj.groups() if x is not None).next()
            return "${{%s}}" % value

        format_string_ = re.sub(self.ENV_VAR_REGEX, escape_envvar, format_string)

        # for recursive formatting, where a field has a value we want to expand,
        # add kwargs to namespace, so format_field can use them...
        if kwargs:
            prev_namespace = self.namespace
            self.namespace = dict(prev_namespace)
            self.namespace.update(kwargs)
        else:
            prev_namespace = None
        try:
            return Formatter.format(self, format_string_, *args, **kwargs)
        finally:
            if prev_namespace is not None:
                self.namespace = prev_namespace
示例#30
0
文件: rex.py 项目: Pixomondo/rez
    def format(self, format_string, *args, **kwargs):
        def escape_envvar(matchobj):
            value = (x for x in matchobj.groups() if x is not None).next()
            return "${{%s}}" % value

        format_string_ = re.sub(self.ENV_VAR_REGEX, escape_envvar, format_string)

        # for recursive formatting, where a field has a value we want to expand,
        # add kwargs to namespace, so format_field can use them...
        if kwargs:
            prev_namespace = self.namespace
            self.namespace = dict(prev_namespace)
            self.namespace.update(kwargs)
        else:
            prev_namespace = None
        try:
            return Formatter.format(self, format_string_, *args, **kwargs)
        finally:
            if prev_namespace is not None:
                self.namespace = prev_namespace
示例#31
0
 def _tags_to_metric(self, tags):
     """
     formats metric name and removes used items from tags
     retruns tuple (new_metric_name, tags)
     """
     f = Formatter()
     #get keys from metric name
     parsed = f.parse(self.name)
     keymap = dict()
     for tup in parsed:
         if (tup[1]):
             keymap[tup[1]] = True
     if not keymap:
         return (self.name, tags)
     #create new metric string
     metric = f.format(self.name, **tags)
     #remove used tags
     for i in keymap.keys():
         del tags[i]
     return (metric, tags)
示例#32
0
    def test_formatter(self):
        """
        js_shell 'j.tools.memusagetest.test_formatter()'
        """

        from string import Formatter

        formatter = Formatter()

        args = {"name": "MYNAME", "val": "AVAL", "n": 1.9998929}

        # res = formatter.format(C,**args)

        C = "{name!s:>10} {val} {n:<10.2f}"  # floating point rounded to 2 decimals
        # python doc: string.Formatter

        j.tools.timer.start("formatter")

        nritems = 100000
        for i in range(nritems):
            if "{" in C:
                C2 = formatter.format(C, **args)

        j.tools.timer.stop(nritems)
示例#33
0
    def compute_one_task(self, itask, task):
        """
        Run the algorithm once, using the parameters specified by `task`,
        which is the `itask` iteration
    
        Parameters
        ----------
        itask : int
            the integer index of this task
        task : tuple
            a tuple of values representing this task value
        """
        # if you are the pool's root, write out the temporary parameter file
        this_config = None
        if self.workers.subcomm.rank == 0:
                
            # initialize a temporary file
            with tempfile.NamedTemporaryFile(delete=False) as ff:
                
                this_config = ff.name
                logger.debug("creating temporary file: %s" %this_config)
                
                # key/values for this task 
                if len(self.task_dims) == 1:
                    possible_kwargs = {self.task_dims[0] : task}
                else:
                    possible_kwargs = dict(zip(self.task_dims, task))
                    
                # any extra key/value pairs for this tasks
                if self.extras is not None:
                    for k in self.extras:
                        possible_kwargs[k] = self.extras[k][itask]
                        
                # use custom formatter that only formats the possible keys, ignoring other
                # occurences of curly brackets
                formatter = Formatter()
                formatter.parse = lambda l: SafeStringParse(formatter, l, list(possible_kwargs))
                kwargs = [kw for _, kw, _, _ in formatter.parse(self.template) if kw]
                        
                # do the string formatting if the key is present in template
                valid = {k:possible_kwargs[k] for k in possible_kwargs if k in kwargs}
                ff.write(formatter.format(self.template, **valid).encode())
        
        # bcast the file name to all in the worker pool
        this_config = self.workers.subcomm.bcast(this_config, root=0)

        # configuration file passed via -c
        params, extra = ReadConfigFile(open(this_config, 'r').read(), self.algorithm_class.schema)
        
        # output is required
        output = getattr(extra, 'output', None)
        if output is None:
            raise ValueError("argument `output` is required in config file")
            
        # initialize the algorithm and run
        alg = self.algorithm_class(**vars(params))
        result = alg.run()
        alg.save(output, result)

        # remove temporary files
        if self.workers.subcomm.rank == 0:
            if os.path.exists(this_config): 
                logger.debug("removing temporary file: %s" %this_config)
                os.remove(this_config)
                
        return 0
示例#34
0
class CpuUsage(IntervalModule, ColorRangeModule):
    """
    Shows CPU usage.
    The first output will be inacurate.

    Linux only
    Requires the PyPI package 'colour'.

    .. rubric:: Available formatters

    * `{usage}`      — usage average of all cores
    * `{usage_cpu*}` — usage of one specific core. replace "*" by core number starting at 0
    * `{usage_all}`  — usage of all cores separate. usess natsort when available(relevant for more than 10 cores)

    """

    format = "{usage:02}%"
    format_all = "{core}:{usage:02}%"
    exclude_average = False
    interval = 1
    color = None
    dynamic_color = False
    upper_limit = 100
    settings = (
        ("format", "format string."),
        ("format_all", ("format string used for {usage_all} per core. "
                        "Available formaters are {core} and {usage}. ")),
        ("exclude_average", ("If True usage average of all cores will "
                             "not be in format_all.")),
        ("color", "HTML color code #RRGGBB"),
        ("dynamic_color", "Set color dynamically based on CPU usage. Note: this overrides color_up"),
        ("start_color", "Hex or English name for start of color range, eg '#00FF00' or 'green'"),
        ("end_color", "Hex or English name for end of color range, eg '#FF0000' or 'red'")
    )

    def init(self):
        self.prev_total = defaultdict(int)
        self.prev_busy = defaultdict(int)
        self.formatter = Formatter()

        self.key = re.findall('usage_cpu\d+', self.format)
        if len(self.key) == 1:
            self.key = self.key[0]
        else:
            self.key = 'usage_cpu'

        if not self.color:
            self.color = '#FFFFFF'

        if not self.dynamic_color:
            self.start_color = self.color
            self.end_color = self.color
        self.colors = self.get_hex_color_range(self.start_color, self.end_color, int(self.upper_limit))

    def get_cpu_timings(self):
        """
        reads and parses /proc/stat
        returns dictionary with all available cores including global average
        """
        timings = {}
        with open('/proc/stat', 'r') as file_obj:
            for line in file_obj:
                if 'cpu' in line:
                    line = line.strip().split()
                    timings[line[0]] = [int(x) for x in line[1:]]

        return timings

    def calculate_usage(self, cpu, total, busy):
        """
        calculates usage
        """
        diff_total = total - self.prev_total[cpu]
        diff_busy = busy - self.prev_busy[cpu]

        self.prev_total[cpu] = total
        self.prev_busy[cpu] = busy

        if diff_total == 0:
            return 0
        else:
            return int(diff_busy / diff_total * 100)

    def gen_format_all(self, usage):
        """
        generates string for format all
        """
        format_string = " "
        core_strings = []
        for core, usage in usage.items():
            if core == 'usage_cpu' and self.exclude_average:
                continue
            elif core == 'usage':
                continue

            core = core.replace('usage_', '')
            string = self.formatter.format(format_string=self.format_all,
                                           core=core,
                                           usage=usage)
            core_strings.append(string)

        core_strings = sorted(core_strings)

        return format_string.join(core_strings)

    def get_usage(self):
        """
        parses /proc/stat and calcualtes total and busy time
        (more specific USER_HZ see man 5 proc for further informations )
        """
        usage = {}

        for cpu, timings in self.get_cpu_timings().items():
            cpu_total = sum(timings)
            del timings[3:5]
            cpu_busy = sum(timings)
            cpu_usage = self.calculate_usage(cpu, cpu_total, cpu_busy)

            usage['usage_' + cpu] = cpu_usage

        # for backward compatibility
        usage['usage'] = usage['usage_cpu']

        return usage

    def run(self):
        usage = self.get_usage()
        usage['usage_all'] = self.gen_format_all(usage)

        color = self.get_gradient(usage[self.key], self.colors, int(self.upper_limit))

        self.data = usage
        self.output = {
            "full_text": self.format.format_map(usage),
            "color": color
        }
示例#35
0
 def format(self, format_string, *args, **kwargs):
     self.caller = sys._getframe(self.level)
     return Formatter.format(self, format_string, *args, **kwargs)
示例#36
0
class CpuUsage(IntervalModule):
    """
    Shows CPU usage.
    The first output will be inacurate.

    Linux only

    .. rubric:: Available formatters

    * `{usage}`      — usage average of all cores
    * `{usage_cpu*}` — usage of one specific core. replace "*" by core number starting at 0
    * `{usage_all}`  — usage of all cores separate. usess natsort when available(relevant for more than 10 cores)

    """

    format = "{usage:02}%"
    format_all = "{core}:{usage:02}%"
    exclude_average = False
    interval = 1
    color = None
    settings = (
        ("format", "format string."),
        ("format_all", ("format string used for {usage_all} per core. "
                        "Available formaters are {core} and {usage}. ")),
        ("exclude_average", ("If True usage average of all cores will "
                             "not be in format_all.")),
        ("color", "HTML color code #RRGGBB")
    )

    def init(self):
        self.prev_total = defaultdict(int)
        self.prev_busy = defaultdict(int)
        self.formatter = Formatter()

    def get_cpu_timings(self):
        """
        reads and parses /proc/stat
        returns dictionary with all available cores including global average
        """
        timings = {}
        with open('/proc/stat', 'r') as file_obj:
            for line in file_obj:
                if 'cpu' in line:
                    line = line.strip().split()
                    timings[line[0]] = [int(x) for x in line[1:]]

        return timings

    def calculate_usage(self, cpu, total, busy):
        """
        calculates usage
        """
        diff_total = total - self.prev_total[cpu]
        diff_busy = busy - self.prev_busy[cpu]

        self.prev_total[cpu] = total
        self.prev_busy[cpu] = busy

        if diff_total == 0:
            return 0
        else:
            return int(diff_busy / diff_total * 100)

    def gen_format_all(self, usage):
        """
        generates string for format all
        """
        format_string = " "
        core_strings = []
        for core, usage in usage.items():
            if core == 'usage_cpu' and self.exclude_average:
                continue
            elif core == 'usage':
                continue

            core = core.replace('usage_', '')
            string = self.formatter.format(format_string=self.format_all,
                                           core=core,
                                           usage=usage)
            core_strings.append(string)

        core_strings = sorted(core_strings)

        return format_string.join(core_strings)

    def get_usage(self):
        """
        parses /proc/stat and calcualtes total and busy time
        (more specific USER_HZ see man 5 proc for further informations )
        """
        usage = {}

        for cpu, timings in self.get_cpu_timings().items():
            cpu_total = sum(timings)
            del timings[3:5]
            cpu_busy = sum(timings)
            cpu_usage = self.calculate_usage(cpu, cpu_total, cpu_busy)

            usage['usage_' + cpu] = cpu_usage

        return usage

    def run(self):
        usage = self.get_usage()
        usage['usage_all'] = self.gen_format_all(usage)

        # for backward compatibility
        usage['usage'] = usage['usage_cpu']

        self.data = usage
        self.output = {
            "full_text": self.format.format_map(usage),
            "color": self.color
        }
示例#37
0
def getImgTiles(xargs):

    #pdb.set_trace()
    in_dirs = ['gt', 'img']
    in_datapath = os.path.abspath(xargs.indir)

    if (xargs.train):
        out_dirs = ['trainannot', 'train']
    if (xargs.valid):
        out_dirs = ['validannot', 'valid']

    out_datapath = os.path.abspath(xargs.outdir + '/' + str(xargs.ph) + 'x' +
                                   str(xargs.pw))

    stride = xargs.step
    ph = xargs.ph
    pw = xargs.pw

    fmt = Formatter()
    for i_dir, o_dir in zip(in_dirs, out_dirs):
        in_dir = os.path.join(in_datapath, i_dir)
        out_dir = os.path.join(out_datapath, o_dir)

        try:
            os.stat(out_dir)
        except:
            os.makedirs(out_dir)

        onlyfiles = sorted(
            [f for f in listdir(in_dir) if isfile(join(in_dir, f))])

        #pdb.set_trace()

        for fil in onlyfiles:
            print(" ====== Processing: %s ======" % fil)

            in_fileName = os.path.join(in_dir, fil)
            res = cv2.imread(in_fileName)
            img = cv2.resize(res,
                             None,
                             fx=xargs.scale,
                             fy=xargs.scale,
                             interpolation=cv2.INTER_CUBIC)

            if (xargs.gray):
                img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

            if (img.shape[0] < ph or img.shape[1] < pw):
                print("dimensions of file %s are smaller" % (in_fileName))
                continue

            if (i_dir == 'img' and len(img.shape) == 2):
                img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)

            hstride = float(stride)
            wstride = (pw * float(stride) / ph)

            h_strides = int(np.ceil((img.shape[0] - ph) / hstride))
            w_strides = int(np.ceil((img.shape[1] - pw) / wstride))

            hrange = range(0, h_strides * int(hstride), int(hstride))
            wrange = range(0, w_strides * int(wstride), int(wstride))

            # collect patches_ta
            patch_idx = 1
            fname, ext = fil.split('.')

            for h in hrange:
                for w in wrange:
                    # print(h, w, img.shape[2], img.shape[3])
                    if (len(img.shape) > 2):
                        patch_ta = img[h:h + ph, w:w + pw, :]
                    else:
                        patch_ta = img[h:h + ph, w:w + pw]

                    #print("file %s: idx:%d" % (fil,patch_idx))
                    #pdb.set_trace()

                    out_fileName = os.path.join(
                        out_dir, fname + '_' + fmt.format("%04d" % patch_idx) +
                        '.' + ext)
                    cv2.imwrite(out_fileName, patch_ta)
                    patch_idx += 1

                # Column boundary patches
                if (len(img.shape) > 2):
                    patch_ta = img[h:h + ph, -pw:, :]
                else:
                    patch_ta = img[h:h + ph, -pw:]

                out_fileName = os.path.join(
                    out_dir,
                    fname + '_' + fmt.format("%04d" % patch_idx) + '.' + ext)
                cv2.imwrite(out_fileName, patch_ta)
                patch_idx += 1

            # Row boundary patches
            for w in wrange:
                # print(h, w, img.shape[2], img.shape[3])
                if (len(img.shape) > 2):
                    patch_ta = img[-ph:, w:w + pw, :]
                else:
                    patch_ta = img[-ph:, w:w + pw]

                out_fileName = os.path.join(
                    out_dir,
                    fname + '_' + fmt.format("%04d" % patch_idx) + '.' + ext)
                cv2.imwrite(out_fileName, patch_ta)
                patch_idx += 1

            if (len(img.shape) > 2):
                patch_ta = img[-ph:, -pw:, :]
            else:
                patch_ta = img[-ph:, -pw:]

            out_fileName = os.path.join(
                out_dir,
                fname + '_' + fmt.format("%04d" % patch_idx) + '.' + ext)
            cv2.imwrite(out_fileName, patch_ta)
            patch_idx += 1
示例#38
0
class CpuUsage(IntervalModule):
    """
    Shows CPU usage.
    The first output will be inacurate.

    Linux only

    .. rubric:: Available formatters

    * `{usage}`      — usage average of all cores
    * `{usage_cpu*}` — usage of one specific core. replace "*" by core number starting at 0
    * `{usage_all}`  — usage of all cores separate. usess natsort when available(relevant for more than 10 cores)

    """

    format = "{usage:02}%"
    format_all = "{core}:{usage:02}%"
    exclude_average = False
    interval = 1
    color = None
    settings = (("format", "format string."),
                ("format_all",
                 ("format string used for {usage_all} per core. "
                  "Available formaters are {core} and {usage}. ")),
                ("exclude_average", ("If True usage average of all cores will "
                                     "not be in format_all.")),
                ("color", "HTML color code #RRGGBB"))

    def init(self):
        self.prev_total = defaultdict(int)
        self.prev_busy = defaultdict(int)
        self.formatter = Formatter()

    def get_cpu_timings(self):
        """
        reads and parses /proc/stat
        returns dictionary with all available cores including global average
        """
        timings = {}
        with open('/proc/stat', 'r') as file_obj:
            for line in file_obj:
                if 'cpu' in line:
                    line = line.strip().split()
                    timings[line[0]] = [int(x) for x in line[1:]]

        return timings

    def calculate_usage(self, cpu, total, busy):
        """
        calculates usage
        """
        diff_total = total - self.prev_total[cpu]
        diff_busy = busy - self.prev_busy[cpu]

        self.prev_total[cpu] = total
        self.prev_busy[cpu] = busy

        if diff_total == 0:
            return 0
        else:
            return int(diff_busy / diff_total * 100)

    def gen_format_all(self, usage):
        """
        generates string for format all
        """
        format_string = " "
        core_strings = []
        for core, usage in usage.items():
            if core == 'usage_cpu' and self.exclude_average:
                continue
            elif core == 'usage':
                continue

            core = core.replace('usage_', '')
            string = self.formatter.format(format_string=self.format_all,
                                           core=core,
                                           usage=usage)
            core_strings.append(string)

        core_strings = sorted(core_strings)

        return format_string.join(core_strings)

    def get_usage(self):
        """
        parses /proc/stat and calcualtes total and busy time
        (more specific USER_HZ see man 5 proc for further informations )
        """
        usage = {}

        for cpu, timings in self.get_cpu_timings().items():
            cpu_total = sum(timings)
            del timings[3:5]
            cpu_busy = sum(timings)
            cpu_usage = self.calculate_usage(cpu, cpu_total, cpu_busy)

            usage['usage_' + cpu] = cpu_usage

        # for backward compatibility
        usage['usage'] = usage['usage_cpu']

        return usage

    def run(self):
        usage = self.get_usage()
        usage['usage_all'] = self.gen_format_all(usage)

        self.data = usage
        self.output = {
            "full_text": self.format.format_map(usage),
            "color": self.color
        }
示例#39
0
def strfdelta(tdelta: Union[datetime.timedelta, int, float, str],
              fmt='{D:02}d {H:02}h {M:02}m {S:02}s',
              inputtype='timedelta'):
    """
    Convert a ``datetime.timedelta`` object or a regular number to a custom-
    formatted string, just like the ``strftime()`` method does for
    ``datetime.datetime`` objects.

    The ``fmt`` argument allows custom formatting to be specified. Fields can
    include ``seconds``, ``minutes``, ``hours``, ``days``, and ``weeks``. Each
    field is optional.

    Some examples:

    .. code-block:: none

        '{D:02}d {H:02}h {M:02}m {S:02}s' --> '05d 08h 04m 02s' (default)
        '{W}w {D}d {H}:{M:02}:{S:02}'     --> '4w 5d 8:04:02'
        '{D:2}d {H:2}:{M:02}:{S:02}'      --> ' 5d  8:04:02'
        '{H}h {S}s'                       --> '72h 800s'

    The ``inputtype`` argument allows ``tdelta`` to be a regular number,
    instead of the default behaviour of treating it as a ``datetime.timedelta``
    object.  Valid ``inputtype`` strings:

    .. code-block:: none

        'timedelta',        # treats input as a datetime.timedelta
        's', 'seconds',
        'm', 'minutes',
        'h', 'hours',
        'd', 'days',
        'w', 'weeks'

    Modified from
    https://stackoverflow.com/questions/538666/python-format-timedelta-to-string
    """  # noqa

    # Convert tdelta to integer seconds.
    if inputtype == 'timedelta':
        remainder = int(tdelta.total_seconds())
    elif inputtype in ['s', 'seconds']:
        remainder = int(tdelta)
    elif inputtype in ['m', 'minutes']:
        remainder = int(tdelta) * 60
    elif inputtype in ['h', 'hours']:
        remainder = int(tdelta) * 3600
    elif inputtype in ['d', 'days']:
        remainder = int(tdelta) * 86400
    elif inputtype in ['w', 'weeks']:
        remainder = int(tdelta) * 604800
    else:
        raise ValueError(f"Bad inputtype: {inputtype}")

    f = Formatter()
    desired_fields = [field_tuple[1] for field_tuple in f.parse(fmt)]
    possible_fields = ('W', 'D', 'H', 'M', 'S')
    constants = {'W': 604800, 'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    values = {}
    for field in possible_fields:
        if field in desired_fields and field in constants:
            values[field], remainder = divmod(remainder, constants[field])
    return f.format(fmt, **values)
示例#40
0
        def write_output_file(document_tokens, mae_file_name):
            print("========== Write " + mae_file_name)
            with codecs.open(mae_file_name, WRITING_MODE,
                             CODEC) as file_handler:
                '''
                <?xml version="1.0" encoding="UTF-8" ?>
                <PartML>
                <TEXT><![CDATA[
                ...
                ]]></TEXT>
                <TAGS>
                <SELECTOR id="s1" start="278" end="282" text="fill" comment="" pos="VERB" />
                <SELECTOR id="s2" start="297" end="303" text="search" comment="" pos="ADJ" />
                ...
                <NOUN id="n1" start="287" end="291" text="void" comment="" />
                <NOUN id="n2" start="304" end="309" text="giant" comment="" />
                ...
                <QLINK id="q0" fromID="s1" fromText="fill" toID="n1" toText="void" comment="" qType="" qType2="" gramRel="" />
                <QLINK id="q1" fromID="n2" fromText="giant" toID="s2" toText="search" comment="" qType="" qType2="" gramRel="" />
                ...
                </TAGS>
                </PartML>
                '''
                text = ''
                length = 1
                relation_included = ['dobj', 'iobj', 'nsubjpass']

                verb_nouns = []
                verb_alphabet = Alphabet()
                noun_alphabet = Alphabet()
                for sentence_tokens in document_tokens:
                    for token_structure in sentence_tokens:
                        token = token_structure.token
                        token_structure.start = length
                        token_structure.end = length + len(token)
                        if token_structure.rel in relation_included:
                            # Add into alphabets
                            head_rel_index = int(token_structure.head) - 1
                            #                             print(sentence_tokens[head_rel_index])
                            #                             print(token_structure)
                            verb_alphabet.add(sentence_tokens[head_rel_index])
                            sentence_tokens[
                                head_rel_index].id = verb_alphabet.get_index(
                                    sentence_tokens[head_rel_index])
                            noun_alphabet.add(token_structure)
                            token_structure.id = noun_alphabet.get_index(
                                token_structure)

                            verb_nouns.append(
                                (sentence_tokens[head_rel_index].id,
                                 token_structure.id, token_structure.rel))
                        text += token + ' '
                        length += len(token) + 1
                    text += '\n'
                    length += 1
                file_handler.write('<?xml version="1.0" encoding="UTF-8" ?>')
                file_handler.write('\n')
                file_handler.write('<PartML>')
                file_handler.write('\n')
                file_handler.write('<TEXT><![CDATA[')
                file_handler.write('\n')
                file_handler.write(text)
                file_handler.write(']]></TEXT>')
                file_handler.write('\n')
                file_handler.write('<TAGS>')
                file_handler.write('\n')
                formatter = Formatter()
                for verb_structure_index in range(verb_alphabet.size()):
                    verb_structure = verb_alphabet.get_label(
                        verb_structure_index)
                    file_handler.write(
                        formatter.format(
                            '<SELECTOR id="s{0.id}" start="{0.start}" end="{0.end}" text="{0.token}" comment="" pos="{0.POS}" />',
                            verb_structure))
                    file_handler.write('\n')

                for noun_structure_index in range(noun_alphabet.size()):
                    noun_structure = noun_alphabet.get_label(
                        noun_structure_index)
                    file_handler.write(
                        formatter.format(
                            '<NOUN id="n{0.id}" start="{0.start}" end="{0.end}" text="{0.token}" comment="" />',
                            noun_structure))
                    file_handler.write('\n')

                for index in range(len(verb_nouns)):
                    verb_structure_index, noun_structure_index, rel = verb_nouns[
                        index]
                    verb_structure = verb_alphabet.get_label(
                        verb_structure_index)
                    noun_structure = noun_alphabet.get_label(
                        noun_structure_index)
                    file_handler.write(
                        formatter.format(
                            '<QLINK id="q{0}" fromID="s{1.id}" fromText="{1.token}" toID="n{2.id}" toText="{2.token}" comment="" qType="{3}" qType2="" gramRel="{4}" />',
                            index, verb_structure, noun_structure, 'FORMAL',
                            rel))
                    file_handler.write('\n')

                file_handler.write('</TAGS>')
                file_handler.write('\n')
                file_handler.write('</PartML>')
                file_handler.write('\n')
示例#41
0
import string

# string module constants
print(string.ascii_letters)
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.digits)
print(string.hexdigits)
print(string.whitespace)  # ' \t\n\r\x0b\x0c'
print(string.punctuation)

s = '  Welcome TO  \n\n JournalDev '
print(string.capwords(s))

from string import Formatter

formatter = Formatter()
print(formatter.format('{website}', website='JournalDev'))
print(formatter.format('{} {website}', 'Welcome to', website='JournalDev'))

print('{} {website}'.format('Welcome to', website='JournalDev'))

from string import Template

t = Template('$name is the $title of $company')
s = t.substitute(name='Pankaj', title='Founder', company='JournalDev.')
print(s)
示例#42
0
文件: utils.py 项目: hekejian/WaveSyn
 def format(self, format_string, *args, **kwargs):
     self.caller = sys._getframe(self.level)
     return Formatter.format(self, format_string, *args, **kwargs)
示例#43
0
def strfdelta(tdelta,
              fmt='{D:02}d {H:02}h {M:02}m {S:02}s',
              input_type='timedelta'):
    """
    Convert a datetime.timedelta object or a regular number to a custom-formatted string.

    This function works like the strftime() method works for datetime.datetime
    objects.

    The fmt argument allows custom formatting to be specified.  Fields can
    include seconds, minutes, hours, days, and weeks.  Each field is optional.

    Arguments:
        tdelta (datetime.timedelta, int): time delta object containing the duration or an integer
            to go with the input_type.
        fmt (str): Expected format of the time delta. place holders can only be one of the following.
            1. D to extract days from time delta
            2. H to extract hours from time delta
            3. M to extract months from time delta
            4. S to extract seconds from timedelta
        input_type (str):  The input_type argument allows tdelta to be a regular number instead of the
            default, which is a datetime.timedelta object.
            Valid input_type strings:
                1. 's', 'seconds',
                2. 'm', 'minutes',
                3. 'h', 'hours',
                4. 'd', 'days',
                5. 'w', 'weeks'
    Returns:
        (str): timedelta object interpolated into a string following the given format.

    Examples:
        '{D:02}d {H:02}h {M:02}m {S:02}s' --> '05d 08h 04m 02s' (default)
        '{W}w {D}d {H}:{M:02}:{S:02}'     --> '4w 5d 8:04:02'
        '{D:2}d {H:2}:{M:02}:{S:02}'      --> ' 5d  8:04:02'
        '{H}h {S}s'                       --> '72h 800s'
    """
    # Convert tdelta to integer seconds.
    if input_type == 'timedelta':
        remainder = int(tdelta.total_seconds())
    elif input_type in ['s', 'seconds']:
        remainder = int(tdelta)
    elif input_type in ['m', 'minutes']:
        remainder = int(tdelta) * 60
    elif input_type in ['h', 'hours']:
        remainder = int(tdelta) * 3600
    elif input_type in ['d', 'days']:
        remainder = int(tdelta) * 86400
    elif input_type in ['w', 'weeks']:
        remainder = int(tdelta) * 604800
    else:
        raise ValueError(
            'input_type is not valid. Valid input_type strings are: "timedelta", "s", "m", "h", "d", "w"'
        )

    f = Formatter()
    desired_fields = [field_tuple[1] for field_tuple in f.parse(fmt)]
    possible_fields = ('W', 'D', 'H', 'M', 'S')
    constants = {'W': 604800, 'D': 86400, 'H': 3600, 'M': 60, 'S': 1}
    values = {}

    for field in possible_fields:
        if field in desired_fields and field in constants:
            values[field], remainder = divmod(remainder, constants[field])

    return f.format(fmt, **values)