當前位置: 首頁>>代碼示例>>Python>>正文


Python DB.fetchone方法代碼示例

本文整理匯總了Python中db.DB.fetchone方法的典型用法代碼示例。如果您正苦於以下問題:Python DB.fetchone方法的具體用法?Python DB.fetchone怎麽用?Python DB.fetchone使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在db.DB的用法示例。


在下文中一共展示了DB.fetchone方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: count_players

# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import fetchone [as 別名]
def count_players():
    """
    Returns the number of players currently registered.
    """

    cursor = DB().execute('SELECT COUNT(*) FROM "players"')["cursor"]
    total = cursor.fetchone()[0]
    cursor.close()

    return total
開發者ID:obelesk411,項目名稱:udacity_tournament_project,代碼行數:12,代碼來源:tournament.py

示例2: LastFM

# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import fetchone [as 別名]
class LastFM(Worker):
  _title = 'lastfm'
  _key = None
  _lfm = None
  # wait this amount of seconds between lookups
  _lookup_threshold = 10
  _last_lookup = None
  _history_filename = None
  _artist_history = {}
  _track_history = {}
  _db = None

  def __init__(self, config, no_cache=False):
    Worker.__init__(self)
    self._key = config.get('LastFM', 'key', None)
    self._lfm = Api(self._key, no_cache=no_cache)
    self._db = DB(config.get('LastFM', 'path',
        join(config.configdir(), 'echonest.db')))
    self._db.execute(
        u"CREATE TABLE IF NOT EXISTS artist_lookup "\
        "(artist text, timestamp integer, "\
        "PRIMARY KEY(artist))")
    self._db.execute(
        u"CREATE TABLE IF NOT EXISTS track_lookup "\
        "(artist text, title text, timestamp integer, "\
        "PRIMARY KEY(artist, title))")
    self._db.commit()

  def get_artist_timestamp(self, artist):
    self._db.execute(
        u"SELECT timestamp FROM artist_lookup "\
        "WHERE artist=?", (artist,))
    row = self._db.fetchone()
    if row: return row[0]
    return 0

  def set_artist_timestamp(self, artist, timestamp):
    self._db.execute(
        u"REPLACE INTO artist_lookup (artist, timestamp) "\
        "VALUES (?, ?)", (artist, timestamp))
    self._db.commit()

  def get_track_timestamp(self, artist, title):
    self._db.execute(
        u"SELECT timestamp FROM track_lookup "\
        "WHERE artist=? and title=?", (artist, title))
    row = self._db.fetchone()
    if row: return row[0]
    return 0

  def set_track_timestamp(self, artist, title, timestamp):
    self._db.execute(
        u"REPLACE INTO track_lookup (artist, title, timestamp) "\
        "VALUES (?, ?, ?)", (artist, title, timestamp))
    self._db.commit()

  def _delay(self):
    if self._last_lookup:
      last_lookup = (datetime.utcnow() - self._last_lookup)
      if last_lookup.seconds < self._lookup_threshold:
        self._logger.debug(u"sleeping %ss" % (
            self._lookup_threshold - last_lookup.seconds))
        sleep(self._lookup_threshold - last_lookup.seconds)
    self._last_lookup = datetime.utcnow()

  def _track(self, artist, title):
    self._delay()
    result = self._lfm.search_track(track=title, artist=artist)
    # for some tracks (e.g. 'after all' from 'the peacocks' lastfm doesn't
    # return the correct track as first result (for 'peacocks' it works?!))
    # so let's try to find the correct track
    for t in result:
      self._logger.info(u'{} {} vs {} {}'.format(artist, title,
          t.artist.name.lower() ,t.name.lower()))
      if t.artist.name.lower() == artist and t.name.lower() == title:
        return t
    if len(result) == 0:
      return None
    return result[0] # return the first

  def _artist(self, artist):
    self._delay()
    result = self._lfm.search_artist(artist=artist)
    for a in result:
      if a.name.lower() == artist:
        return a
    if len(result) == 0:
      return None
    return result[0]

  def similar_tracks(self, callback, artist, title, threshold):
    self.queue_callback(self._similar_tracks, callback, artist, title,
        threshold)

  def similar_tracks_low(self, callback, artist, title, threshold):
    self.queue_callback_low(self._similar_tracks, callback, artist, title,
        threshold)

  def _similar_tracks(self, callback, artist, title, threshold):
    """
#.........這裏部分代碼省略.........
開發者ID:pscn,項目名稱:ads,代碼行數:103,代碼來源:lookup_lastfm.py

示例3: EchoNest

# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import fetchone [as 別名]
class EchoNest(Worker):
  _title = 'echonest'
  _key = None
  # wait this amount of seconds between lookups
  _lookup_threshold = 10
  _last_lookup = None
  _history_filename = None
  _artist_history = {}
  _track_history = {}
  _db = None

  def __init__(self, config, no_cache=False):
    Worker.__init__(self)
    self._key = config.get('EchoNest', 'key', None)
    en_config.ECHO_NEST_API_KEY = self._key
    self._db = DB(config.get('EchoNest', 'path',
        join(config.configdir(), 'echonest.db')))
    self._db.execute(
        u"CREATE TABLE IF NOT EXISTS artist_lookup "\
        "(artist text, timestamp integer, "\
        "PRIMARY KEY(artist))")
    self._db.execute(
        u"CREATE TABLE IF NOT EXISTS track_lookup "\
        "(artist text, title text, timestamp integer, "\
        "PRIMARY KEY(artist, title))")
    self._db.commit()

  def get_artist_timestamp(self, artist):
    self._db.execute(
        u"SELECT timestamp FROM artist_lookup "\
        "WHERE artist=?", (artist,))
    row = self._db.fetchone()
    if row: return row[0]
    return 0

  def set_artist_timestamp(self, artist, timestamp):
    self._db.execute(
        u"REPLACE INTO artist_lookup (artist, timestamp) "\
        "VALUES (?, ?)", (artist, timestamp))
    self._db.commit()

  def get_track_timestamp(self, artist, title):
    self._db.execute(
        u"SELECT timestamp FROM track_lookup "\
        "WHERE artist=? and title=?", (artist, title))
    row = self._db.fetchone()
    if row: return row[0]
    return 0

  def set_track_timestamp(self, artist, title, timestamp):
    self._db.execute(
        u"REPLACE INTO track_lookup (artist, title, timestamp) "\
        "VALUES (?, ?, ?)", (artist, title, timestamp))
    self._db.commit()

  def _delay(self):
    if self._last_lookup:
      last_lookup = (datetime.utcnow() - self._last_lookup)
      if last_lookup.seconds < self._lookup_threshold:
        self._logger.debug(u"sleeping %ss" % (
            self._lookup_threshold - last_lookup.seconds))
        sleep(self._lookup_threshold - last_lookup.seconds)
    self._last_lookup = datetime.utcnow()

  def similar_tracks(self, callback, artist, title, threshold):
    self.queue_callback(self._similar_tracks, callback, artist, title,
        threshold)

  def similar_tracks_low(self, callback, artist, title, threshold):
    self.queue_callback_low(self._similar_tracks, callback, artist, title,
        threshold)

  def _similar_tracks(self, callback, artist, title, threshold):
    timestamp = now()
    diff = timestamp - self.get_track_timestamp(artist, title)
    if diff < threshold:
      self._logger.debug(u"similar_tracks[%s-%s]: looked up %d seconds ago" %
          (artist, title, diff))
      return
    self.set_track_timestamp(artist, title, timestamp)
    try:
      self._logger.debug(u"similar_tracks[%s-%s]: lookup" % (artist, title))
      self._delay()
      a = en_song.search(title=title, artist=artist)
      try:
        p = en_playlist.static(type='song-radio', song_id=a[0].id, results=100)
        i = 100.0
        self._logger.info(u"similar_tracks[%s-%s]: %d result(s)" % (artist,
            title, len(p)))
        for song in p:
          callback(artist, title, song.artist_name.lower(), song.title.lower(),
              i / 100.0, self._title)
          i -= 1.0
      except IndexError:
        self._logger.info(u"similar_tracks[%s-%s]: no result" % (artist, title))
        return
    except Exception, e:
      self._logger.error(e)
      self._logger.info(u"similar_tracks[%s-%s]: no result" % (artist, title))
      return
開發者ID:pscn,項目名稱:ads,代碼行數:102,代碼來源:lookup_echonest.py


注:本文中的db.DB.fetchone方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。