Python time.html() Examples
The following are 25
code examples of time.html().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
time
, or try the search function
.
Example #1
Source File: Scheduler.py From EventGhost with GNU General Public License v2.0 | 6 votes |
def AddShortTaskAbsolute(self, startTime, func, *args, **kwargs): """ This does the same as :meth:`AddShortTask`, but the `startTime` parameter specifies an absolute time expressed in floating point seconds since the epoch. Take a look at the documentation of `Python's time module`_, for more information about this time format. Again a little example:: import time startTime = time.mktime((2007, 8, 15, 16, 53, 0, 0, 0, -1)) eg.scheduler.AddShortTaskAbsolute(startTime, eg.TriggerEvent, "MyEvent") This will trigger the event "Main.MyEvent" at 16:53:00 on 15 August 2007. If you run this code after this point of time, the :func:`eg.TriggerEvent` will be called immediately. .. _Python's time module: http://docs.python.org/lib/module-time.html """ try: self.lock.acquire() task = (startTime, func, args, kwargs) heappush(self.heap, task) self.event.set() finally: self.lock.release() return task
Example #2
Source File: timeHandler.py From BOSWatch with GNU General Public License v2.0 | 6 votes |
def curtime(timeStr="%d.%m.%Y %H:%M:%S", timestamp=""): """ Returns formated date and/or time see: https://docs.python.org/2/library/time.html#time.strftime @type format: string @param format: Python time Format-String @type timestamp: floating point number @param timestamp: time in seconds since the epoch @return: Formated Time and/or Date @exception: Exception if Error in format """ try: if timestamp == "": return time.strftime(timeStr) else: return time.strftime(timeStr, time.localtime(timestamp)) except: logging.warning("error in time-format-string") logging.debug("error in time-format-string", exc_info=True)
Example #3
Source File: datelib.py From google-apputils with Apache License 2.0 | 6 votes |
def DatetimeToUTCMicros(date): """Converts a datetime object to microseconds since the epoch in UTC. Args: date: A datetime to convert. Returns: The number of microseconds since the epoch, in UTC, represented by the input datetime. """ # Using this guide: http://wiki.python.org/moin/WorkingWithTime # And this conversion guide: http://docs.python.org/library/time.html # Turn the date parameter into a tuple (struct_time) that can then be # manipulated into a long value of seconds. During the conversion from # struct_time to long, the source date in UTC, and so it follows that the # correct transformation is calendar.timegm() micros = calendar.timegm(date.utctimetuple()) * _MICROSECONDS_PER_SECOND return micros + date.microsecond
Example #4
Source File: timestamp.py From python-mediawiki-utilities with MIT License | 6 votes |
def strptime(cls, string, format): """ Constructs a :class:`mw.Timestamp` from an explicitly formatted string. See `<https://docs.python.org/3/library/time.html#time.strftime>`_ for a discussion of formats descriptors. :Parameters: string : str A formatted timestamp format : str The format description :Returns: :class:`mw.Timestamp` """ return cls.from_time_struct(time.strptime(string, format))
Example #5
Source File: client.py From bosonnlp.py with MIT License | 6 votes |
def classify(self, contents): """BosonNLP `新闻分类接口 <http://docs.bosonnlp.com/classify.html>`_ 封装。 :param contents: 需要做分类的新闻文本或者文本序列。 :type contents: string or sequence of string :returns: 接口返回的结果列表。 :raises: :py:exc:`~bosonnlp.HTTPError` 如果 API 请求发生错误。 调用示例: >>> import os >>> nlp = BosonNLP(os.environ['BOSON_API_TOKEN']) >>> nlp.classify('俄否决安理会谴责叙军战机空袭阿勒颇平民') [5] >>> nlp.classify(['俄否决安理会谴责叙军战机空袭阿勒颇平民', ... '邓紫棋谈男友林宥嘉:我觉得我比他唱得好', ... 'Facebook收购印度初创公司']) [5, 4, 8] """ api_endpoint = '/classify/analysis' r = self._api_request('POST', api_endpoint, data=contents) return r.json()
Example #6
Source File: client.py From bosonnlp.py with MIT License | 6 votes |
def suggest(self, word, top_k=None): """BosonNLP `语义联想接口 <http://docs.bosonnlp.com/suggest.html>`_ 封装。 :param string word: 需要做语义联想的词。 :param int top_k: 默认为 10,最大值可设定为 100。返回的结果条数。 :returns: 接口返回的结果列表。 :raises: :py:exc:`~bosonnlp.HTTPError` 如果 API 请求发生错误。 调用示例: >>> import os >>> nlp = BosonNLP(os.environ['BOSON_API_TOKEN']) >>> nlp.suggest('北京', top_k=2) [[1.0, '北京/ns'], [0.7493540460397998, '上海/ns']] """ api_endpoint = '/suggest/analysis' params = {} if top_k is not None: params['top_k'] = top_k r = self._api_request('POST', api_endpoint, params=params, data=word) return r.json()
Example #7
Source File: common.py From pex with Apache License 2.0 | 5 votes |
def safe_sleep(seconds): """Ensure that the thread sleeps at a minimum the requested seconds. Until Python 3.5, there was no guarantee that time.sleep() would actually sleep the requested time. See https://docs.python.org/3/library/time.html#time.sleep.""" if sys.version_info[0:2] >= (3, 5): time.sleep(seconds) else: start_time = current_time = time.time() while current_time - start_time < seconds: remaining_time = seconds - (current_time - start_time) time.sleep(remaining_time) current_time = time.time()
Example #8
Source File: experiments.py From neptune-client with Apache License 2.0 | 5 votes |
def get_hardware_utilization(self): """Retrieve GPU, CPU and memory utilization data. Get hardware utilization metrics for entire experiment as a single `pandas.DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>`_ object. Returned DataFrame has following columns (assuming single GPU with 0 index): * `x_ram` - time (in milliseconds) from the experiment start, * `y_ram` - memory usage in GB, * `x_cpu` - time (in milliseconds) from the experiment start, * `y_cpu` - CPU utilization percentage (0-100), * `x_gpu_util_0` - time (in milliseconds) from the experiment start, * `y_gpu_util_0` - GPU utilization percentage (0-100), * `x_gpu_mem_0` - time (in milliseconds) from the experiment start, * `y_gpu_mem_0` - GPU memory usage in GB. | If more GPUs are available they have their separate columns with appropriate indices (0, 1, 2, ...), for example: `x_gpu_util_1`, `y_gpu_util_1`. | The returned DataFrame may contain ``NaN`` s if one of the metrics has more values than others. Returns: :obj:`pandas.DataFrame` - DataFrame containing the hardware utilization metrics. Examples: The following values denote that after 3 seconds, the experiment used 16.7 GB of RAM * `x_ram` = 3000 * `y_ram` = 16.7 Assuming that `experiment` is an instance of :class:`~neptune.experiments.Experiment`: .. code:: python3 hardware_df = experiment.get_hardware_utilization() """ metrics_csv = self._backend.get_metrics_csv(self) try: return pd.read_csv(metrics_csv) except EmptyDataError: return pd.DataFrame()
Example #9
Source File: time_utils.py From python-smime with Apache License 2.0 | 5 votes |
def timezone(temp_tz): """Sets the timezone, yields, then resets the timezone. Args: temp_tz: See https://docs.python.org/2/library/time.html#time.tzset """ original_tz = get_timezone_environ() set_timezone_environ(temp_tz) try: yield finally: set_timezone_environ(original_tz)
Example #10
Source File: timestamp.py From python-mediawiki-utilities with MIT License | 5 votes |
def strftime(self, format): """ Constructs a formatted string. See `<https://docs.python.org/3/library/time.html#time.strftime>`_ for a discussion of formats descriptors. :Parameters: format : str The format description :Returns: A formatted string """ return time.strftime(format, self.__time)
Example #11
Source File: token.py From asymmetric-jwt-auth with ISC License | 5 votes |
def sign(username, private_key, generate_nonce=None, iat=None, algorithm=DEFAULT_ALGORITHM): """ Create a signed JWT using the given username and RSA private key. :param username: Username (string) to authenticate as on the remote system. :param private_key: Private key to use to sign the JWT claim. :param generate_nonce: Optional. Callable to use to generate a new nonce. Defaults to `random.random <https://docs.python.org/3/library/random.html#random.random>`_. :param iat: Optional. Timestamp to include in the JWT claim. Defaults to `time.time <https://docs.python.org/3/library/time.html#time.time>`_. :param algorithm: Optional. Algorithm to use to sign the JWT claim. Default to ``RS512``. See `pyjwt.readthedocs.io <https://pyjwt.readthedocs.io/en/latest/algorithms.html>`_ for other possible algorithms. :return: JWT claim as a string. """ iat = iat if iat else time.time() if not generate_nonce: generate_nonce = lambda username, iat: random.random() # NOQA token_data = { 'username': username, 'time': iat, 'nonce': generate_nonce(username, iat), } token = jwt.encode(token_data, private_key, algorithm=algorithm) return token
Example #12
Source File: client.py From bosonnlp.py with MIT License | 5 votes |
def extract_keywords(self, text, top_k=None, segmented=False): """BosonNLP `关键词提取接口 <http://docs.bosonnlp.com/keywords.html>`_ 封装。 :param string text: 需要做关键词提取的文本。 :param int top_k: 默认为 100,返回的结果条数。 :param bool segmented: 默认为 :py:class:`False`,`text` 是否已进行了分词,如果为 :py:class:`True`,则不会再对内容进行分词处理。 :returns: 接口返回的结果列表。 :raises: :py:exc:`~bosonnlp.HTTPError` 如果 API 请求发生错误。 调用示例: >>> import os >>> nlp = BosonNLP(os.environ['BOSON_API_TOKEN']) >>> nlp.extract_keywords('病毒式媒体网站:让新闻迅速蔓延', top_k=2) [[0.8391345017584958, '病毒式'], [0.3802418301341705, '蔓延']] """ api_endpoint = '/keywords/analysis' params = {} if segmented: params['segmented'] = 1 if top_k is not None: params['top_k'] = top_k r = self._api_request('POST', api_endpoint, params=params, data=text) return r.json()
Example #13
Source File: client.py From bosonnlp.py with MIT License | 5 votes |
def convert_time(self, content, basetime=None): """BosonNLP `时间描述转换接口 <http://docs.bosonnlp.com/time.html>`_ 封装 :param content: 中文时间描述字符串 :type content: string :param basetime: 时间描述的基准时间,传入一个时间戳或datetime :type basetime: int or datetime.datetime :raises: :py:exc:`~bosonnlp.HTTPError` 如果 API 请求发生错误。 :returns: 接口返回的结果 调用示例: >>> import os >>> nlp = BosonNLP(os.environ['BOSON_API_TOKEN']) >>> _json_dumps(nlp.convert_time("2013年二月二十八日下午四点三十分二十九秒")) '{"timestamp": "2013-02-28 16:30:29", "type": "timestamp"}' >>> import datetime >>> _json_dumps(nlp.convert_time("今天晚上8点到明天下午3点", datetime.datetime(2015, 9, 1))) '{"timespan": ["2015-09-02 20:00:00", "2015-09-03 15:00:00"], "type": "timespan_0"}' """ api_endpoint = '/time/analysis' params = {'pattern': content} if basetime: if isinstance(basetime, datetime.datetime): basetime = int(time.mktime(basetime.timetuple())) params['basetime'] = basetime r = self._api_request('POST', api_endpoint, params=params) return r.json()
Example #14
Source File: client.py From bosonnlp.py with MIT License | 5 votes |
def sentiment(self, contents, model='general'): """BosonNLP `情感分析接口 <http://docs.bosonnlp.com/sentiment.html>`_ 封装。 :param contents: 需要做情感分析的文本或者文本序列。 :type contents: string or sequence of string :param model: 使用不同语料训练的模型,默认使用通用模型。 :type model: string :returns: 接口返回的结果列表。 :raises: :py:exc:`~bosonnlp.HTTPError` 如果 API 请求发生错误。 调用示例: >>> import os >>> nlp = BosonNLP(os.environ['BOSON_API_TOKEN']) >>> nlp.sentiment('这家味道还不错', model='food') [[0.9991737012037423, 0.0008262987962577828]] >>> nlp.sentiment(['这家味道还不错', '菜品太少了而且还不新鲜'], model='food') [[0.9991737012037423, 0.0008262987962577828], [9.940036427291687e-08, 0.9999999005996357]] """ api_endpoint = '/sentiment/analysis?' + model r = self._api_request('POST', api_endpoint, data=contents) return r.json()
Example #15
Source File: client.py From rele with Apache License 2.0 | 5 votes |
def consume(self, subscription_name, callback, scheduler): """Begin listening to topic from the SubscriberClient. :param subscription_name: str Subscription name :param callback: Function which act on a topic message :param scheduler: `Thread pool-based scheduler.<https://googleapis.dev/python/pubsub/latest/subscriber/api/scheduler.html?highlight=threadscheduler#google.cloud.pubsub_v1.subscriber.scheduler.ThreadScheduler>`_ # noqa :return: `Future <https://googleapis.github.io/google-cloud-python/latest/pubsub/subscriber/api/futures.html>`_ # noqa """ subscription_path = self._client.subscription_path( self._gc_project_id, subscription_name ) return self._client.subscribe( subscription_path, callback=callback, scheduler=scheduler )
Example #16
Source File: common.py From pex with Apache License 2.0 | 5 votes |
def finalize(self, source=None): """Rename `work_dir` to `target_dir` using `os.rename()`. :param str source: An optional source offset into the `work_dir`` to use for the atomic update of `target_dir`. By default the whole `work_dir` is used. If a race is lost and `target_dir` already exists, the `target_dir` dir is left unchanged and the `work_dir` directory will simply be removed. """ if self.is_finalized: return source = os.path.join(self._work_dir, source) if source else self._work_dir try: # Perform an atomic rename. # # Per the docs: https://docs.python.org/2.7/library/os.html#os.rename # # The operation may fail on some Unix flavors if src and dst are on different filesystems. # If successful, the renaming will be an atomic operation (this is a POSIX requirement). # # We have satisfied the single filesystem constraint by arranging the `work_dir` to be a # sibling of the `target_dir`. os.rename(source, self._target_dir) except OSError as e: if e.errno not in (errno.EEXIST, errno.ENOTEMPTY): raise e finally: self.cleanup()
Example #17
Source File: shipment.py From python-dhl with MIT License | 5 votes |
def get_dhl_formatted_shipment_time(self): """ Formats the shipment date and time in the DHL time format, including the UTC offset :return: formatted date time stamp """ self.ship_datetime = self.ship_datetime or datetime.now() if not self.utc_offset: # time lib https://docs.python.org/3/library/time.html#time.strftime self.utc_offset = time.strftime('%z') # just take the utc offset from the time lib self.utc_offset = self.utc_offset[:-2] + ':' + self.utc_offset[-2:] # insert : in +0100 to get +01:00 self.ship_datetime += timedelta(minutes=5) formatted_time = self.ship_datetime.strftime(self.dhl_datetime_format) return formatted_time + self.utc_offset
Example #18
Source File: client.py From bosonnlp.py with MIT License | 4 votes |
def tag(self, contents, space_mode=0, oov_level=3, t2s=0, special_char_conv=0): """BosonNLP `分词与词性标注 <http://docs.bosonnlp.com/tag.html>`_ 封装。 :param contents: 需要做分词与词性标注的文本或者文本序列。 :type contents: string or sequence of string :param space_mode: 空格保留选项 :type space_mode: int(整型), 0-3有效 :param oov_level: 枚举强度选项 :type oov_level: int(整型), 0-4有效 :param t2s: 繁简转换选项,繁转简或不转换 :type t2s: int(整型), 0-1有效 :param special_char_conv: 特殊字符转化选项,针对回车、Tab等特殊字符转化或者不转化 :type special_char_conv: int(整型), 0-1有效 :returns: 接口返回的结果列表。 :raises: :py:exc:`~bosonnlp.HTTPError` 如果 API 请求发生错误。 调用参数及返回值详细说明见:http://docs.bosonnlp.com/tag.html 调用示例: >>> import os >>> nlp = BosonNLP(os.environ['BOSON_API_TOKEN']) >>> result = nlp.tag('成都商报记者 姚永忠') >>> _json_dumps(result) '[{"tag": ["ns", "n", "n", "nr"], "word": ["成都", "商报", "记者", "姚永忠"]}]' >>> format_tag_result = lambda tagged: ' '.join('%s/%s' % x for x in zip(tagged['word'], tagged['tag'])) >>> result = nlp.tag("成都商报记者 姚永忠") >>> format_tag_result(result[0]) '成都/ns 商报/n 记者/n 姚永忠/nr' >>> result = nlp.tag("成都商报记者 姚永忠", space_mode=2) >>> format_tag_result(result[0]) '成都/ns 商报/n 记者/n /w 姚永忠/nr' >>> result = nlp.tag(['亚投行意向创始成员国确定为57个', '“流量贵”频被吐槽'], oov_level=0) >>> format_tag_result(result[0]) '亚/ns 投/v 行/n 意向/n 创始/vi 成员国/n 确定/v 为/v 57/m 个/q' >>> format_tag_result(result[1]) '“/wyz 流量/n 贵/a ”/wyy 频/d 被/pbei 吐槽/v' """ api_endpoint = '/tag/analysis' params = { 'space_mode': space_mode, 'oov_level': oov_level, 't2s': t2s, 'special_char_conv': special_char_conv, } r = self._api_request('POST', api_endpoint, params=params, data=contents) return r.json()
Example #19
Source File: client.py From bosonnlp.py with MIT License | 4 votes |
def summary(self, title, content, word_limit=0.3, not_exceed=False): """BosonNLP `新闻摘要 <http://docs.bosonnlp.com/summary.html>`_ 封装。 :param title: 需要做摘要的新闻标题。如果没有标题,请传空字符串。 :type title: unicode :param content: 需要做摘要的新闻正文。 :type content: unicode :param word_limit: 摘要字数限制。 当为 float 时,表示字数为原本的百分比,0.0-1.0有效; 当为 int 时,表示摘要字数。 .. note:: 传 1 默认为百分比。 :type word_limit: float or int :param not_exceed: 是否严格限制字数。 :type not_exceed: bool,默认为 False :returns: 摘要。 :raises: :py:exc:`~bosonnlp.HTTPError` 当API请求发生错误。 调用参数及返回值详细说明见: http://docs.bosonnlp.com/summary.html 调用示例: >>> import os >>> nlp = BosonNLP(os.environ['BOSON_API_TOKEN']) >>> content = ( '腾讯科技讯(刘亚澜)10月22日消息,前优酷土豆技术副总裁' '黄冬已于日前正式加盟芒果TV,出任CTO一职。' '资料显示,黄冬历任土豆网技术副总裁、优酷土豆集团产品' '技术副总裁等职务,曾主持设计、运营过优酷土豆多个' '大型高容量产品和系统。' '此番加入芒果TV或与芒果TV计划自主研发智能硬件OS有关。') >>> title = '前优酷土豆技术副总裁黄冬加盟芒果TV任CTO' >>> nlp.summary(title, content, 0.1) 腾讯科技讯(刘亚澜)10月22日消息,前优酷土豆技术副总裁黄冬已于日前正式加盟芒果TV,出任CTO一职。 """ api_endpoint = '/summary/analysis' not_exceed = int(not_exceed) data = { 'not_exceed': not_exceed, 'percentage': word_limit, 'title': title, 'content': content } r = self._api_request('POST', api_endpoint, data=data) return r.json()
Example #20
Source File: client.py From bosonnlp.py with MIT License | 4 votes |
def cluster(self, contents, task_id=None, alpha=None, beta=None, timeout=DEFAULT_TIMEOUT): """BosonNLP `文本聚类接口 <http://docs.bosonnlp.com/cluster.html>`_ 封装。 :param contents: 需要做文本聚类的文本序列或者 (_id, text) 序列或者 {'_id': _id, 'text': text} 序列。如果没有指定 _id,则自动 生成唯一的 _id。 :type contents: sequence of string or sequence of (_id, text) or sequence of {'_id': _id, 'text': text} :param string task_id: 唯一的 task_id,话题聚类任务的名字,可由字母和数字组成。 :param float alpha: 默认为 0.8,聚类最大 cluster 大小 :param float beta: 默认为 0.45,聚类平均 cluster 大小 :param float timeout: 默认为 1800 秒(30 分钟),等待文本聚类任务完成的秒数。 :returns: 接口返回的结果列表。 :raises: :py:exc:`~bosonnlp.HTTPError` - 如果 API 请求发生错误 :py:exc:`~bosonnlp.TaskError` - 任务出错。 :py:exc:`~bosonnlp.TimeoutError` - 如果文本聚类任务未能在 `timeout` 时间内完成。 调用示例: >>> import os >>> nlp = BosonNLP(os.environ['BOSON_API_TOKEN']) >>> nlp.cluster(['今天天气好', '今天天气好', '今天天气不错', '点点楼头细雨', ... '重重江外平湖', '当年戏马会东徐', '今日凄凉南浦']) [{'_id': 0, 'list': [0, 1], 'num': 2}] """ if not contents: return [] if isinstance(contents[0], string_types): contents = [{"_id": _id, "text": s} for _id, s in enumerate(contents)] cluster = None try: cluster = self.create_cluster_task(contents, task_id) cluster.analysis(alpha=alpha, beta=beta) cluster.wait_until_complete(timeout) result = cluster.result() return result finally: if cluster is not None: cluster.clear()
Example #21
Source File: client.py From bosonnlp.py with MIT License | 4 votes |
def comments(self, contents, task_id=None, alpha=None, beta=None, timeout=DEFAULT_TIMEOUT): """BosonNLP `典型意见接口 <http://docs.bosonnlp.com/comments.html>`_ 封装。 :param contents: 需要做典型意见的文本序列或者 (_id, text) 序列或者 {'_id': _id, 'text': text} 序列。如果没有指定 _id,则自动 生成唯一的 _id。 :type contents: sequence of string or sequence of (_id, text) or sequence of {'_id': _id, 'text': text} :param string task_id: 默认为 :py:class:`None`,表示自动生成一个 唯一的 task_id,典型意见任务的名字,可由字母和数字组成。 :param float alpha: 默认为 0.8,聚类最大 cluster 大小 :param float beta: 默认为 0.45,聚类平均 cluster 大小 :param float timeout: 默认为 1800 秒(30 分钟),等待典型意见任务完成的秒数。 :returns: 接口返回的结果列表。 :raises: :py:exc:`~bosonnlp.HTTPError` - 如果 API 请求发生错误 :py:exc:`~bosonnlp.TaskError` - 任务出错。 :py:exc:`~bosonnlp.TimeoutError` - 如果典型意见任务未能在 `timeout` 时间内完成。 调用示例: >>> import os >>> nlp = BosonNLP(os.environ['BOSON_API_TOKEN']) >>> nlp.comments(['今天天气好', '今天天气好', '今天天气不错', '点点楼头细雨', ... '重重江外平湖', '当年戏马会东徐', '今日凄凉南浦'] * 2) [{'_id': 0, 'list': [['点点楼头', 3], ['点点楼头', 10]], 'num': 2, 'opinion': '点点楼头'}, {'_id': 1, 'list': [['重重江外', 4], ['重重江外', 11]], 'num': 2, 'opinion': '重重江外'}, {'_id': 2, 'list': [['当年戏马', 5], ['当年戏马', 12]], 'num': 2, 'opinion': '当年戏马'}, {'_id': 3, 'list': [['今日凄凉', 6], ['今日凄凉', 13]], 'num': 2, 'opinion': '今日凄凉'}] """ if not contents: return [] if isinstance(contents[0], string_types): contents = [{"_id": _id, "text": s} for _id, s in enumerate(contents)] comments = None try: comments = self.create_comments_task(contents, task_id) comments.analysis(alpha=alpha, beta=beta) comments.wait_until_complete(timeout) result = comments.result() return result finally: if comments is not None: comments.clear()
Example #22
Source File: client.py From bosonnlp.py with MIT License | 4 votes |
def ner(self, contents, sensitivity=None, segmented=False, space_mode='3'): """BosonNLP `命名实体识别接口 <http://docs.bosonnlp.com/ner.html>`_ 封装。 :param contents: 需要做命名实体识别的文本或者文本序列。 :type contents: string or sequence of string :param sensitivity: 准确率与召回率之间的平衡, 设置成 1 能找到更多的实体,设置成 5 能以更高的精度寻找实体。 :type sensitivity: int 默认为 3 :param segmented: 输入是否为分词结果 :type segmented: boolean 默认为 False :param space_mode: 分词空格保留选项 :type space_mode: int(整型), 0-3有效,默认为 3 :returns: 接口返回的结果列表。 :raises: :py:exc:`~bosonnlp.HTTPError` 如果 API 请求发生错误。 调用示例: >>> import os >>> nlp = BosonNLP(os.environ['BOSON_API_TOKEN']) >>> nlp.ner('成都商报记者 姚永忠', sensitivity=2) [{'entity': [[0, 2, 'product_name'], [2, 3, 'job_title'], [3, 4, 'person_name']], 'tag': ['ns', 'n', 'n', 'nr'], 'word': ['成都', '商报', '记者', '姚永忠']}] >>> nlp.ner(['成都商报记者 姚永忠', '微软XP操作系统今日正式退休']) [{'entity': [[0, 2, 'product_name'], [2, 3, 'job_title'], [3, 4, 'person_name']], 'tag': ['ns', 'n', 'n', 'nr'], 'word': ['成都', '商报', '记者', '姚永忠']}, {'entity': [[0, 2, 'product_name'], [3, 4, 'time']], 'tag': ['nz', 'nx', 'nl', 't', 'ad', 'v'], 'word': ['微软', 'XP', '操作系统', '今日', '正式', '退休']}] """ api_endpoint = '/ner/analysis' params = {'space_mode': space_mode} if sensitivity is not None: params['sensitivity'] = sensitivity if segmented: params['segmented'] = True r = self._api_request('POST', api_endpoint, data=contents, params=params) return r.json()
Example #23
Source File: client.py From rele with Apache License 2.0 | 4 votes |
def publish(self, topic, data, blocking=False, timeout=None, **attrs): """Publishes message to Google PubSub topic. Usage:: publisher = Publisher() publisher.publish('topic_name', {'foo': 'bar'}) By default, this method is non-blocking, meaning that the method does not wait for the future to be returned. If you would like to wait for the future so you can track the message later, you can: Usage:: publisher = Publisher() future = publisher.publish('topic_name', {'foo': 'bar'}, blocking=True, timeout=10.0) # noqa However, it should be noted that using `blocking=True` may incur a significant performance hit. In addition, the method adds a timestamp `published_at` to the message attrs using `epoch floating point number <https://docs.python.org/3/library/time.html#time.time>`_. :param topic: string topic to publish the data. :param data: dict with the content of the message. :param blocking: boolean :param timeout: float, default None fallsback to :ref:`settings_publisher_timeout` :param attrs: additional string parameters to be published. :return: `Future <https://googleapis.github.io/google-cloud-python/latest/pubsub/subscriber/api/futures.html>`_ # noqa """ attrs["published_at"] = str(time.time()) run_middleware_hook("pre_publish", topic, data, attrs) payload = json.dumps(data, cls=self._encoder).encode("utf-8") topic_path = self._client.topic_path(self._gc_project_id, topic) future = self._client.publish(topic_path, payload, **attrs) if not blocking: return future future.result(timeout=timeout or self._timeout) run_middleware_hook("post_publish", topic) return future
Example #24
Source File: experiments.py From neptune-client with Apache License 2.0 | 4 votes |
def log_text(self, log_name, x, y=None, timestamp=None): """Log text data in Neptune | If a log with provided ``log_name`` does not exist, it is created automatically. | If log exists (determined by ``log_name``), then new value is appended to it. Args: log_name (:obj:`str`): The name of log, i.e. `mse`, `my_text_data`, `timing_info`. x (:obj:`double` or :obj:`str`): Depending, whether ``y`` parameter is passed: * ``y`` not passed: The value of the log (data-point). Must be ``str``. * ``y`` passed: Index of log entry being appended. Must be strictly increasing. y (:obj:`str`, optional, default is ``None``): The value of the log (data-point). timestamp (:obj:`time`, optional, default is ``None``): Timestamp to be associated with log entry. Must be Unix time. If ``None`` is passed, `time.time() <https://docs.python.org/3.6/library/time.html#time.time>`_ (Python 3.6 example) is invoked to obtain timestamp. Example: Assuming that `experiment` is an instance of :class:`~neptune.experiments.Experiment`: .. code:: python3 # common case, where log name and data are passed neptune.log_text('my_text_data', str(data_item)) # log_name, x and timestamp are passed neptune.log_text(log_name='logging_losses_as_text', x=str(val_loss), timestamp=1560430912) Note: For efficiency, logs are uploaded in batches via a queue. Hence, if you log a lot of data, you may experience slight delays in Neptune web application. Note: Passing ``x`` coordinate as NaN or +/-inf causes this log entry to be ignored. Warning is printed to ``stdout``. """ x, y = self._get_valid_x_y(x, y) if x is not None and is_nan_or_inf(x): x = None if not isinstance(y, six.string_types): raise InvalidChannelValue(expected_type='str', actual_type=type(y).__name__) if x is not None and is_nan_or_inf(x): _logger.warning( 'Invalid metric x-coordinate: %s for channel %s. ' 'Metrics with nan or +/-inf x-coordinates will not be sent to server', x, log_name) else: value = ChannelValue(x, dict(text_value=y), timestamp) self._channels_values_sender.send(log_name, ChannelType.TEXT.value, value)
Example #25
Source File: experiments.py From neptune-client with Apache License 2.0 | 4 votes |
def get_numeric_channels_values(self, *channel_names): """Retrieve values of specified metrics (numeric logs). The returned `pandas.DataFrame <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html>`_ contains 1 additional column `x` along with the requested metrics. Args: *channel_names (one or more :obj:`str`): comma-separated metric names. Returns: :obj:`pandas.DataFrame` - DataFrame containing values for the requested metrics. | The returned DataFrame may contain ``NaN`` s if one of the metrics has more values than others. Example: Invoking ``get_numeric_channels_values('loss', 'auc')`` returns DataFrame with columns `x`, `loss`, `auc`. Assuming that `experiment` is an instance of :class:`~neptune.experiments.Experiment`: .. code:: python3 batch_channels = experiment.get_numeric_channels_values('batch-1-loss', 'batch-2-metric') epoch_channels = experiment.get_numeric_channels_values('epoch-1-loss', 'epoch-2-metric') Note: It's good idea to get metrics with common temporal pattern (like iteration or batch/epoch number). Thanks to this each row of returned DataFrame has metrics from the same moment in experiment. For example, combine epoch metrics to one DataFrame and batch metrics to the other. """ channels_data = {} channels_by_name = self.get_channels() for channel_name in channel_names: channel_id = channels_by_name[channel_name].id try: channels_data[channel_name] = pd.read_csv( self._backend.get_channel_points_csv(self, channel_id), header=None, names=['x_{}'.format(channel_name), 'y_{}'.format(channel_name)], dtype=float ) except EmptyDataError: channels_data[channel_name] = pd.DataFrame( columns=['x_{}'.format(channel_name), 'y_{}'.format(channel_name)], dtype=float ) return align_channels_on_x(pd.concat(channels_data.values(), axis=1, sort=False))