当前位置: 首页>>代码示例>>Python>>正文


Python TinyDB.contains方法代码示例

本文整理汇总了Python中tinydb.TinyDB.contains方法的典型用法代码示例。如果您正苦于以下问题:Python TinyDB.contains方法的具体用法?Python TinyDB.contains怎么用?Python TinyDB.contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tinydb.TinyDB的用法示例。


在下文中一共展示了TinyDB.contains方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TinyDBStore

# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import contains [as 别名]
class TinyDBStore(object):
    def __init__(self):
        self.drafts_db = TinyDB('event_drafts.json')
        self.events_db = TinyDB('events.json')

    # Drafts
    def contains_draft(self, user_id):
        return self.drafts_db.contains(Query().user_id == user_id)

    def new_draft(self, user_id):
        if self.contains_draft(user_id):
            self.drafts_db.remove(Query().user_id == user_id)

        self.drafts_db.insert({
            'user_id': user_id,
            'current_field': 0,
            'event': {}
        })

    def update_draft(self, user_id, event, current_field):
        self.drafts_db.update({
            'user_id': user_id,
            'current_field': current_field,
            'event': event
        }, Query().user_id == user_id)

    def get_draft(self, user_id):
        return self.drafts_db.get(Query().user_id == user_id)

    def remove_draft(self, user_id):
        self.drafts_db.remove(Query().user_id == user_id)

    # Events
    def insert_event(self, event):
        event_id = self.events_db.insert(event)
        event['id'] = event_id
        return event

    def update_event(self, event):
        self.events_db.update(event, eids=[event.eid])

    def remove_event(self, event):
        self.events_db.remove(eids=[event['id']])

    def get_events(self, user_id, name=None):
        if name:
            return self.events_db.search((Query().user_id == user_id) & (Query().name.test(lambda v: name in v)))
        return self.events_db.search(Query().user_id == user_id)

    def get_event(self, event_id):
        return self.events_db.get(eid=int(event_id))
开发者ID:lukaville,项目名称:create-event-bot,代码行数:53,代码来源:store.py

示例2: ListCache

# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import contains [as 别名]
class ListCache(object):

    DB_FILE = 'listing_db.json'
    DB_TTL = timedelta(hours=12)

    def __init__(self):
        self.db = TinyDB(os.path.join(os.path.join(os.getcwd(), os.path.dirname(__file__)), ListCache.DB_FILE))

    @property
    def db(self):
        return self._db

    @db.setter
    def db(self, db):
        self._db = db

    def listing_in_cache(self, listing):
        lquery = Query()
        return self.db.contains(lquery.hsh == listing.hsh)

    def retrieve_listing(self, listing):
        lquery = Query()
        list_dict = self.db.get(lquery.hsh == listing.hsh)
        return Listing.from_dict(list_dict)

    def insert_listing(self, listing):
        if self.listing_in_cache(listing):
            self.update_listing(listing)
        else:
            list_dict = listing.as_dict()
            list_dict['last_updated'] = datetime.now().isoformat()
            list_dict['hsh'] = listing.hsh
            self.db.insert(list_dict)

    def remove_listing(self, listing):
        lquery = Query()
        self.db.remove(lquery.hsh == listing.hsh)

    def update_listing(self, listing):
        lquery = Query()
        if self.listing_in_cache(listing):
            self.remove_listing(listing)
        self.insert_listing(listing)

    def remove_old_listings(self):
        list_ar = self.db.all()
        for listing in list_ar:
            if datetime.strptime(listing['last_updated'], '%Y-%m-%dT%H:%M:%S.%f') < datetime.now() - ListCache.DB_TTL:
                self.remove_listing(Listing.from_dict(listing))
开发者ID:althor880,项目名称:househunt,代码行数:51,代码来源:househunt.py

示例3: DataStore

# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import contains [as 别名]
class DataStore(object):
	def __init__(self, store_path):
		self.store_path = os.path.join(store_path,"META")
		try:
			os.makedirs(self.store_path)
		except OSError as exc:
			if exc.errno != errno.EEXIST:
				raise

		self.db = TinyDB(os.path.join(self.store_path,"__meta__.json"))

	def add_file(self, file_path):
		file_path = self._get_internal_path(file_path)
		self._add_to_db(file_path)

	def remove_file(self,file_path):
		file_path = self._get_internal_path(file_path)
		self._remove_from_db(file_path)
		
	def list_all(self):
		return self._list_all_db()
		
	def _init_file_list(self):
		with open(self.store_path,"r") as f:
			for line in tqdm(f):
				self.file_list.append(f)

	def _init_db(self):
		self.db = TinyDB(self.store_path)
		
	def _add_to_db(self,file_path):
		if not self.db.contains(where('file_path')== file_path):
			self.db.insert({'file_path':file_path})

	def _remove_from_db(self,file_path):
		self.db.remove(where('file_path') == file_path)
	
	def _list_all_db(self):
		return [rec['file_path'] for rec in self.db.all()]

	def _get_internal_path(self, path):
		return get_internal_path(path)
开发者ID:nithishdivakar,项目名称:syncro,代码行数:44,代码来源:data_store.py

示例4: scrap

# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import contains [as 别名]
def scrap(url):
    db = TinyDB(DB_FILENAME)
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    # Remove promoted section
    soup.select_one('div.view.top-listings').extract()
    # Remove reply window
    soup.select_one('li.result.reply').extract()
    # Find all results
    results = []
    for li in soup.select('li.result'):
        results.append(_parse_result(li))
    # Skip those that are already there - single result found will break out
    # of the loop
    valid = []
    for result in results:
        if db.contains(_prepare_query(result)):
            break
        db.insert(_to_dict(result))
        valid.append(result)
    db.close()
    return valid
开发者ID:modrzew,项目名称:gumtree-scraper,代码行数:24,代码来源:scrap.py

示例5: filter

# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import contains [as 别名]
flist = filter(lambda file: os.path.isfile(os.path.join(config['share_dir'], 
                                                        file)), 
               os.listdir(config['share_dir']))
files = {byte_to_hex(md5((os.path.join(config['share_dir'], fname)))): fname 
         for fname in flist if fname != os.path.basename(config['database'])}

# remove records of which the file does not exist in share directory
for file in db.all():
    if file['md5'] in files:
        db.update({'name': files[file['md5']]}, File.md5==file['md5'])
    else:
        db.remove(File.md5==file['md5'])

# move files in share directory to upload directory if not recorded
for file in files:
    if not db.contains(File.md5==file):
        os.rename(os.path.join(config['share_dir'], files[file]),
                  os.path.join(config['upload_dir'], files[file]))

# record files in upload directory and move to share direcory
def upload(fname=None):
    if fname:
        if os.path.isfile(os.path.join(config['upload_dir'], fname)):
            file = {}
            fmd5 = md5(os.path.join(config['upload_dir'], fname))
            file['md5'] = byte_to_hex(fmd5)
            
            if db.contains(File.md5==file['md5']):
                os.remove(os.path.join(config['upload_dir'], fname))
                return db.get(File.md5==file['md5'])['route']
            
开发者ID:tangym,项目名称:Sharet,代码行数:32,代码来源:Sharet.py

示例6: __init__

# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import contains [as 别名]
class JobDB:
    """Keeps a database of jobs, with a MD5 hash that encodes the function
    name, version, and all arguments to the function.
    """
    def __init__(self, path):
        self.db = TinyDB(path)
        self.lock = Lock()

    def get_result_or_attach(self, key, prov, running):
        job = Query()
        with self.lock:
            rec = self.db.get(job.prov == prov)

            if 'result' in rec:
                return 'retrieved', rec['key'], rec['result']

            job_running = rec['key'] in running
            wf_running = rec['link'] in running.workflows

            if job_running or wf_running:
                self.db.update(attach_job(key), job.prov == prov)
                return 'attached', rec['key'], None

            print("WARNING: unfinished job in database. Removing it and "
                  " rerunning.", file=sys.stderr)
            self.db.remove(eids=[rec.eid])
            return 'broken', None, None

    def job_exists(self, prov):
        job = Query()
        with self.lock:
            return self.db.contains(job.prov == prov)

    def store_result(self, key, result):
        job = Query()
        with self.lock:
            if not self.db.contains(job.key == key):
                return

        self.add_time_stamp(key, 'done')
        with self.lock:
            self.db.update(
                    {'result': result, 'link': None},
                    job.key == key)
            rec = self.db.get(job.key == key)
            return rec['attached']

    def new_job(self, key, prov, job_msg):
        with self.lock:
            self.db.insert({
                'key': key,
                'attached': [],
                'prov': prov,
                'link': None,
                'time': {'schedule': time_stamp()},
                'version': job_msg['data']['hints'].get('version'),
                'function': job_msg['data']['function'],
                'arguments': job_msg['data']['arguments']
            })

        return key, prov

    def add_link(self, key, ppn):
        job = Query()
        with self.lock:
            self.db.update({'link': ppn}, job.key == key)

    def get_linked_jobs(self, ppn):
        job = Query()
        with self.lock:
            rec = self.db.search(job.link == ppn)
            return [r['key'] for r in rec]

    def add_time_stamp(self, key, name):
        def update(r):
            r['time'][name] = time_stamp()

        job = Query()
        with self.lock:
            self.db.update(
                update,
                job.key == key)
开发者ID:NLeSC,项目名称:noodles,代码行数:84,代码来源:prov.py

示例7: walk_and_write_to_db

# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import contains [as 别名]
def walk_and_write_to_db(db: TinyDB):
    """Retrieves paths of all files from server and saves new to database."""
    paths = get_filepaths_on_server()
    for path in paths:
        if not db.contains(where('path') == path):
            db.insert({'path': path, 'datetime': datetime.now()})
开发者ID:honza-kasik,项目名称:check-for-files,代码行数:8,代码来源:check_for_files.py

示例8: IconObjectDB

# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import contains [as 别名]
class IconObjectDB(object):
    """The Class for inserting objects in the icon_object.json db using tinydb"""

    def __init__(self):
        super(IconObjectDB, self).__init__()
        try:
            self.db = TinyDB(
                os.path.abspath(
                    os.path.join(
                        os.path.dirname(__file__),
                        "..",
                        "..",
                        "..",
                        "EI",
                        "icons",
                        "icon_objects.json",
                    )
                )
            )
        except Exception as err:
            raise ValueError("Database is locked")

    def insert_object(self, obj, override=False):
        if not self.db.contains(where("name") == obj["name"]):
            print("Inserting: " + str(obj['name']))
            self.db.insert(obj)
        else:
            if self.warn_user(override):
                print("Overwriting: " + str(obj['name']))
                self.db.update(obj, where("name") == obj["name"])
            else:
                return False

    def get_values(self, value):
        obj_list = list()
        for obj in self.get_all():
            if value in obj:
                obj_list.append(obj[value])
        return obj_list

    def get_obj_by_mtl(self, mtl):
        if self.db.contains(where("mtl") == str(mtl)):
            return self.db.search(where("mtl") == str(mtl))[0]

    def get_obj_by_brush(self, brush):
        if self.db.contains(where("brush") == str(brush)):
            return self.db.search(where("brush") == str(brush))[0]

    def get_obj_by_brush_and_mtl(self, brush, mtl):
        if self.db.contains(
            (where("brush") == str(brush)) & (where("mtl") == str(mtl))
        ):
            return self.db.search(
                (where("brush") == str(brush)) & (where("mtl") == str(mtl))
            )[0]

    def get_objs_by_brush(self, brush):
        if self.db.contains(where("brush") == str(brush)):
            return self.db.search(where("brush") == str(brush))

    def get_obj_by_name(self, name):
        if self.db.contains(where("name") == name):
            return self.db.search(where("name") == name)[0]

    def get_all(self):
        return self.db.search(lambda x: True)

    @staticmethod
    def warn_user(override):
        return override
开发者ID:csprance,项目名称:CE_Python,代码行数:72,代码来源:__init__.py

示例9: GCodeRenderPlugin

# 需要导入模块: from tinydb import TinyDB [as 别名]
# 或者: from tinydb.TinyDB import contains [as 别名]
class GCodeRenderPlugin(octoprint.plugin.StartupPlugin, 
                        octoprint.plugin.SettingsPlugin,
                        octoprint.plugin.EventHandlerPlugin,
                        octoprint.plugin.BlueprintPlugin
):
    def initialize(self):
        # Because we use last modified, make sure we only get integers
        os.stat_float_times(False)

        # The actual render jobs
        self.renderJobs = Queue.Queue()

        # Prepare loks for render queue and database accessget_settings_defaults
        self.dbLock = threading.Lock()

        self.preview_extension = "png"

        # Initialize tinydb
        self._prepareDatabase()

        # Cleanup the database and previews folder
        self.cleanup()

        # Begin watching for render jobs
        self._start_render_thread()

        # Fill the queue with any jobs we may have missed
        self._updateAllPreviews()

    def _prepareDatabase(self):
        self.dbLock.acquire()
        self.previews_database_path = os.path.join(self.get_plugin_data_folder(), "previews.json")
        self.previews_database = TinyDB(self.previews_database_path)
        self._previews_query = Query() # underscore for blueprintapi compatability
        self.dbLock.release()
    
    def _updateAllPreviews(self, subFolder = None):
        """
        Reads the entire preview database, checks if there are any outdated previews (last modified of preview
        is before last modified of gcode file) and updates these.
        """ 
        current_folder = self._settings.global_get_basefolder('uploads')

        if subFolder:
            current_folder = os.path.join(current_folder, subFolder)

        self._logger.debug('Scanning folder {0} for render jobs'.format(current_folder))

        for entry in os.listdir(current_folder):
            entry_path = os.path.join(current_folder, entry)
            entry_rel_path = entry

            if subFolder:
                entry_rel_path = subFolder + '/' + entry

            if os.path.isfile(entry_path):
                file_type = octoprint.filemanager.get_file_type(entry_rel_path)
                if(file_type):
                    if file_type[0] is "machinecode":
                        self._updatePreview(entry_path, entry_rel_path)
            else:
                self._updateAllPreviews(entry_rel_path)
   
    def _updatePreview(self, path, filename):
        """
        Checks if the preview is up to date with the gcode file (based on last modified) and re-renders if neceserry.
        """
        self.dbLock.acquire()
        db_entry = self.previews_database.get(self._previews_query.path == path)
        self.dbLock.release()

        
        modtime = os.path.getmtime(path)
        if db_entry is None or db_entry["modtime"] != modtime or not os.path.exists(db_entry["previewPath"]):
            self.render_gcode(path, filename, modtime)

    def cleanup(self):
        """
        Loop through database, remove items not found in upload or preview folder
        """
        self.dbLock.acquire()
        db_entries = self.previews_database.all()
        for db_entry in db_entries:
            if not os.path.exists(db_entry["previewPath"]) or not os.path.exists(db_entry["path"]):
                self.previews_database.remove(eids=[db_entry.eid])
                self._logger.debug("Removed from preview database: %s" % db_entry["filename"])
        

        #Loop through images, remove items not found in db
        image_folder = self._get_image_folder()
        for entry in os.listdir(image_folder):
            entry_path = os.path.join(image_folder, entry)

            if entry_path.endswith(self.preview_extension) and \
                not self.previews_database.contains(self._previews_query.previewPath == entry_path):
                try:
                    os.remove(entry_path)
                    self._logger.debug("Removed preview %s" % entry_path)
                except Exception:
                    self._logger.debug("Could not remove preview %s" % entry_path)
#.........这里部分代码省略.........
开发者ID:ErikHeidstra,项目名称:OctoPrint-gcodeRender,代码行数:103,代码来源:gcoderenderplugin.py


注:本文中的tinydb.TinyDB.contains方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。