def __init__(self): cur_dir = os.path.dirname(__file__) cur_dir_list = cur_dir.split('/') cur_dir_list.pop() data_dir = '/'.join(cur_dir_list) nom_db_dir = data_dir + r'/data/database/nom.db' sec_db_dir = data_dir + r'/data/database/sec.db' dia_db_dir = data_dir + r'/data/database/dia.db' self.nom_db = pydblite.Base(nom_db_dir) self.sec_db = pydblite.Base(sec_db_dir) self.dia_db = pydblite.Base(dia_db_dir) if self.nom_db.exists(): self.nom_db.open() if self.sec_db.exists(): self.sec_db.open() if self.dia_db.exists(): self.dia_db.open()
def create_db(self, dbDir): db = pydblite.Base(dbDir) if db.exists(): db.open() else: db.create('file', 'tag', 'question', 'answer') return db
def insert_to_db(db_name, data_dir): # Init database db = pydblite.Base(db_name) if db.exists(): os.remove(db_name) db.create('question', 'answer') # Read the yml files data_file_list = os.listdir(data_dir) for data_file in data_file_list: # Check the extension if os.path.splitext(data_file)[1] != '.yml': continue f = open(data_dir + data_file, 'r') readStr = f.read() readStr = re.sub(r'\n +\n', '\n\n', readStr) data = readStr.split('\n\n') # Insert the data into database try: for d in data: dd = yaml.load(d) print('\n[%s]\n%s' % (data_file, d)) if not dd: continue for q in dd['que']: ans = dd['ans'] if type(ans) == bool: print('\n[Error] Bool value\n[%s]\n%s' % (data_file, d)) sys.exit(1) db.insert(question=q, answer=ans) except: raise db.create_index('question') print(db_name, ' data insert OK!')
def __init__(self): current_dir = os.path.dirname(__file__) current_dir_list = current_dir.split('/') current_dir_list.pop() data_dir = '' for dir in current_dir_list: data_dir += dir + '/' nom_db_1_dir = data_dir + 'data/db/nom_db_1' sec_db_1_dir = data_dir + 'data/db/sec_db_1' dia_db_1_dir = data_dir + 'data/db/dia_db_1' self.nom_db_1 = pydblite.Base(nom_db_1_dir) self.sec_db_1 = pydblite.Base(sec_db_1_dir) self.dia_db_1 = pydblite.Base(dia_db_1_dir) if self.nom_db_1.exists(): self.nom_db_1.open() if self.sec_db_1.exists(): self.sec_db_1.open() if self.dia_db_1.exists(): self.dia_db_1.open()
def insert_to_db(db_name, data_dir): # Init database db = pydblite.Base(db_name) if db.exists(): os.remove(db_name) db.create('question', 'answer') # Read the yml files data_file_list = os.listdir(data_dir) for data_file in data_file_list: # Check the extension if os.path.splitext(data_file)[1] != '.yml': continue print('Reading %s and insert data...' % data_file) f = open(data_dir + data_file, 'r') data = f.read().split('\n\n') # Insert the data into database try: for d in data: dd = yaml.load(d) if not dd: continue for q in dd['question']: db.insert(question=q, answer=dd['answer']) #print('question = ', q, 'answer = ', dd['answer']) except: raise db.create_index('question') print(db_name, ' data insert OK!')
def open_db(self, dbDir): db = pydblite.Base(dbDir) if db.exists(): db.open() return db else: print('[Error] No database found') sys.exit(1)
def __init__(self, filepath, targets): self.path = filepath self.targets = targets self.db = pydblite.Base(self.path) if not self.db.exists(): self.db.create('permissions', 'slug', 'created', 'due', 'priority', 'status', 'projects', 'labels', 'title', "details") else: self.db.open()
def genKeys(num: int, effecct: float): dataPath = Data('forest').getfo() db = pydblite.Base(f'{dataPath}/keys.db') if db.exists(): db.open() else: db.create('key', 'effect') db.create_index('uuid') db.commit() for i in range(0, num): key = uuid.uuid4() db.insert(key=key, effect=effecct) db.commit()
def open_db(self, dbDir): db = pydblite.Base(dbDir) if db.exists(): db.open() return db
# -*- coding: utf-8 -*- import traceback import requests import web from web import BadRequest, InternalError, HTTPError import os, sys, logging import config, types from model import Data from threading import Timer import time import pydblite os.chdir(os.path.dirname(sys.argv[0])) db = pydblite.Base('users.pdl') db.create('ips', 'cfg', mode="open") web.config.debug = False _log = logging.getLogger(__name__) urls = ( '/', 'index', '/starttest', 'starttest', '/stoptest', 'stoptest', '/additem', 'additem', '/viewitem/(.*)', 'viewitem', '/delitem', 'delitem', '/singlepost', 'singlepost', '/freshpage', 'freshpage' ) stop_code = ['4010', '5010', '100', '201','200'] t = None
from core.extern.data import Data from core.application import App from core.event import ContactMessageRecvEvent, GroupMessageRecvEvent from core.entity.group import PermissionType from util.crontab import Crontab from module.session import Session, SessionLib MODULE_NAME = 'forest' data = Data(MODULE_NAME) config = Config(MODULE_NAME) sessions = SessionLib(MODULE_NAME) db = pydblite.Base(f'{data.getfo()}/{MODULE_NAME}.db') userdb = pydblite.Base(f'{data.getfo()}/user.db') crontab = Crontab() settings = { 'enabled_groups': [], 'max_time': 2 * 24 * 60 * 60, # 单位s 'min_time': 1 * 1 * 30 * 60, 'admin': 0 } def hourToSec(hour: float) -> int: return int(hour * 3600)
def from_csv(csvfile, out=None, fieldnames=None, fmtparams=None, conv_func={}, empty_to_none=[]): """Conversion from CSV to PyDbLite csvfile : name of the CSV file in the file system out : path for the new PyDbLite base in the file system fieldnames : list of field names. If set to None, the field names must be present in the first line of the CSV file fmtparams : the format parameters for the CSV file, as described in the csv module of the standard distribution conv_func is a dictionary mapping a field name to the function used to convert the string read in the CSV to the appropriate Python type. For instance if field "age" must be converted to an integer : conv_func["age"] = int empty_to_none is a list of the fields such that when the value read in the CSV file is the empty string, the field value is set to None """ import csv import time import datetime if out is None: out = os.path.splitext(csvfile)[0] + ".pdl" if fieldnames is None: # read field names in the first line of CSV file reader = csv.reader(open(csvfile)) fieldnames = reader.next() reader = csv.DictReader(open(csvfile), fieldnames, fmtparams) reader.next() # skip first line db = pydblite.Base(out) conv_func.update({"__id__": int}) auto_id = "__id__" not in fieldnames fieldnames = [f for f in fieldnames if f not in ("__id__")] kw = {"mode": "override"} db.create(*fieldnames, **kw) print(db.fields) next_id = 0 records = {} while True: try: record = reader.next() except StopIteration: break if auto_id: record["__id__"] = next_id next_id += 1 # replace empty strings by None for field in empty_to_none: if not record[field]: record[field] = None # type conversion for field in conv_func: if not isinstance(conv_func[field], (tuple, list)): record[field] = conv_func[field](record[field]) else: # date or datetime date_class, date_fmt = conv_func[field] if not record[field]: record[field] = None else: time_tuple = time.strptime(record[field], date_fmt) if date_class is datetime.date: time_tuple = time_tuple[:3] record[field] = date_class(*time_tuple) records[record["__id__"]] = record db.records = records db.commit() print(len(db)) return db
date_class, date_fmt = conv_func[field] if not record[field]: record[field] = None else: time_tuple = time.strptime(record[field], date_fmt) if date_class is datetime.date: time_tuple = time_tuple[:3] record[field] = date_class(*time_tuple) records[record["__id__"]] = record db.records = records db.commit() print(len(db)) return db if __name__ == "__main__": os.chdir(os.path.join(os.getcwd(), 'test')) pdl = pydblite.Base("test.pdl").open() csvfile = to_csv(pdl) db = from_csv(csvfile, out="test_copy.pdl") ok = nok = 0 for r1 in pdl: try: r2 = db[r1["__id__"]] ok += 1 except: nok += 1 print(ok, nok) print(r1, r2)