def load_table(table, filename, con, delimiter=',', format=None, encoding='utf-8', delete=True): import csv from uliweb.utils.date import to_date, to_datetime table = reflect_table(con, table.name) if delete: do_(table.delete()) if not os.path.exists(filename): log.info("The table [%s] data is not existed." % table.name) return f = fin = open(filename, 'rb') try: first_line = f.readline() fields = first_line[1:].strip().split() n = 1 if format: fin = csv.reader(f, delimiter=delimiter) for line in fin: try: n += 1 if not format: line = eval(line.strip()) record = dict(zip(fields, line)) params = {} for c in table.c: if c.name in record: if not format: params[c.name] = record[c.name] else: if record[c.name] == 'NULL': params[c.name] = None else: if isinstance(c.type, String): params[c.name] = unicode( record[c.name], encoding) elif isinstance(c.type, Date): params[c.name] = to_date( to_datetime(record[c.name])) elif isinstance(c.type, DateTime): params[c.name] = to_datetime( record[c.name]) else: params[c.name] = record[c.name] ins = table.insert().values(**params) do_(ins) except: log.error('Error: Line %d' % n) raise finally: f.close()
def load_table(table, filename, con, delimiter=',', format=None, encoding='utf-8', delete=True): import csv from uliweb.utils.date import to_date, to_datetime table = reflect_table(con, table.name) if delete: do_(table.delete()) if not os.path.exists(filename): log.info("The table [%s] data is not existed." % table.name) return f = fin = open(filename, 'rb') try: first_line = f.readline() fields = first_line[1:].strip().split() n = 1 if format: fin = csv.reader(f, delimiter=delimiter) for line in fin: try: n += 1 if not format: line = eval(line.strip()) record = dict(zip(fields, line)) params = {} for c in table.c: if c.name in record: if not format: params[c.name] = record[c.name] else: if record[c.name] == 'NULL': params[c.name] = None else: if isinstance(c.type, String): params[c.name] = unicode(record[c.name], encoding) elif isinstance(c.type, Date): params[c.name] = to_date(to_datetime(record[c.name])) elif isinstance(c.type, DateTime): params[c.name] = to_datetime(record[c.name]) else: params[c.name] = record[c.name] ins = table.insert().values(**params) do_(ins) except: log.error('Error: Line %d' % n) raise finally: f.close()
def _conver(): nonlocal v if v and col.type.python_type == datetime: _v = v v = to_datetime(v, tzinfo=getattr(request, "tzinfo", None)) if v == None: raise UliwebError("'%s' cannot convert to datetime" % (_v))
def _get_tutorial_chapters(self, tutorial_id): from uliweb.utils import date User = get_model('user') query = self.model_chapters.filter((self.model_chapters.c.tutorial == tutorial_id) & (self.model_chapters.c.deleted==False)) query.values('id', 'title', 'hits', 'parent', 'render', 'order', 'chars_count', 'comments_count', 'modified_date', 'modified_user') query.order_by(self.model_chapters.c.parent, self.model_chapters.c.order) for row in query: d = Storage(dict(row)) d['modified_date'] = date.to_datetime(d.modified_date) d['modified_user'] = User.get(d.modified_user) yield d
def _get_tutorial_chapters(self, tutorial_id): from uliweb.utils import date User = get_model('user') query = self.model_chapters.filter( (self.model_chapters.c.tutorial == tutorial_id) & (self.model_chapters.c.deleted == False)) query.values('id', 'title', 'hits', 'parent', 'render', 'order', 'chars_count', 'comments_count', 'modified_date', 'modified_user') query.order_by(self.model_chapters.c.parent, self.model_chapters.c.order) for row in query: d = Storage(dict(row)) d['modified_date'] = date.to_datetime(d.modified_date) d['modified_user'] = User.get(d.modified_user) yield d
def _make_data(self): from uliweb.utils import date return { 'str':'string', 'password':'******', # 'hidden':'hidden', 'int':10, 'float':1.0, 'bool':True, 'list':[u'中', u'文'], 'select1':'F', 'select2':['F', 'M'], 'radios1':'F', 'radios2':'M', 'checkboxes1':['F'], 'checkboxes2':['F', 'M'], 'date':date.to_date('2010-10-12'), 'datetime':date.to_datetime('2010-10-12 13:23:45'), 'time':date.to_time('13:23:45'), 'desc':'<p>abc</p><p>cde</p>', }
def load_table(table, filename, con, delimiter=',', format=None, encoding='utf-8', delete=True, bulk=100, engine_name=None): import csv from uliweb.utils.date import to_date, to_datetime if not os.path.exists(filename): return "Skipped (data not found)" table = reflect_table(con, table.name) if delete: do_(table.delete(), engine_name) b = time() bulk = max(1, bulk) f = fin = open(filename, 'rb') try: first_line = f.readline() if first_line.startswith('#'): first_line = first_line[1:] n = 0 count = 0 if format: fields = first_line.strip().split(delimiter) fin = csv.reader(f, delimiter=delimiter) else: fields = first_line.strip().split() buf = [] for line in fin: try: n += 1 count += 1 if not format: line = eval(line.strip()) record = dict(zip(fields, line)) params = {} for c in table.c: if c.name in record: if not format: params[c.name] = record[c.name] else: if record[c.name] == 'NULL': params[c.name] = None else: if isinstance(c.type, String): params[c.name] = unicode( record[c.name], encoding) elif isinstance(c.type, Date): params[c.name] = to_date( to_datetime(record[c.name])) elif isinstance(c.type, DateTime): params[c.name] = to_datetime( record[c.name]) else: params[c.name] = record[c.name] buf.append(params) if count >= bulk: do_(table.insert(), engine_name, args=buf) count = 0 buf = [] except: log.error('Error: Line %d' % n) raise if buf: do_(table.insert(), engine_name, args=buf) return 'OK (%d/%lfs)' % (n, time() - b) finally: f.close()
def timesince(d, now=None, pos=True, flag=False): """ pos means calculate which direction, pos = True, now - d, pos = False, d - now flag means return value type, True will return since, message and Flase return message >>> d = datetime.datetime(2009, 10, 1, 12, 23, 19) >>> timesince(d, d, True) >>> now = datetime.datetime(2009, 10, 1, 12, 24, 19) >>> timesince(d, now, True) u'1 minute ago' >>> now = datetime.datetime(2009, 10, 1, 12, 24, 30) >>> timesince(d, now, True) u'1 minute ago' >>> now = datetime.datetime(2009, 9, 28, 12, 24, 30) >>> timesince(d, now, True) u'2 days, 23 hours later' >>> now = datetime.datetime(2009, 10, 3, 12, 24, 30) >>> timesince(d, now, True) u'2 days ago' """ if not d: if flag: return 0, '' else: return '' chunks = ( (60 * 60 * 24 * 365, lambda n: ungettext('year', 'years', n)), (60 * 60 * 24 * 30, lambda n: ungettext('month', 'months', n)), (60 * 60 * 24 * 7, lambda n : ungettext('week', 'weeks', n)), (60 * 60 * 24, lambda n : ungettext('day', 'days', n)), (60 * 60, lambda n: ungettext('hour', 'hours', n)), (60, lambda n: ungettext('minute', 'minutes', n)) ) if not now: now = date.now() else: now = date.to_datetime(now) d = date.to_datetime(d) delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) oldsince = since = delta.days * 24 * 60 * 60 + delta.seconds suffix = '' if pos: if since >= 0: suffix = ugettext(' ago') elif since < 0: suffix = ugettext(' later') since *= -1 for i, (seconds, name) in enumerate(chunks): count = since // seconds if count != 0: break s = ('%(number)d %(type)s') % {'number': count, 'type': name(count)} if i + 1 < len(chunks): # Now get the second item seconds2, name2 = chunks[i + 1] count2 = (since - (seconds * count)) // seconds2 if count2 != 0: s += (', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)} #if flag==True, then return twe elements (since, message) if flag: return oldsince, s + suffix else: return s + suffix
def load_table(table, filename, con, delimiter=',', format=None, encoding='utf-8', delete=True, bulk=100, engine_name=None): import csv from uliweb.utils.date import to_date, to_datetime if not os.path.exists(filename): return "Skipped (data not found)" table = reflect_table(con, table.name) if delete: table.drop(con) table.create(con) # do_(table.drop(), engine_name) # do_(table.create(), engine_name) b = time() bulk = max(1, bulk) f = fin = open(filename, 'rb') try: first_line = f.readline() if first_line.startswith('#'): first_line = first_line[1:] n = 0 count = 0 if format: fields = first_line.strip().split(delimiter) fin = csv.reader(f, delimiter=delimiter) else: fields = first_line.strip().split() buf = [] for line in fin: try: n += 1 count += 1 if not format: line = eval(line.strip()) record = dict(zip(fields, line)) params = {} for c in table.c: if c.name in record: if not format: params[c.name] = record[c.name] else: if record[c.name] == 'NULL': params[c.name] = None else: if isinstance(c.type, String): params[c.name] = unicode(record[c.name], encoding) elif isinstance(c.type, Date): params[c.name] = to_date(to_datetime(record[c.name])) elif isinstance(c.type, DateTime): params[c.name] = to_datetime(record[c.name]) else: params[c.name] = record[c.name] buf.append(params) if count >= bulk: do_(table.insert(), engine_name, args=buf) count = 0 buf = [] except: log.error('Error: Line %d of %s' % (n, filename)) raise if buf: do_(table.insert(), engine_name, args=buf) return 'OK (%d/%lfs)' % (n, time()-b) finally: f.close()
def validate(self, data, all_data=None): return bool(date.to_datetime(data))
def timesince(d, now=None, pos=True, flag=False): """ pos means calculate which direction, pos = True, now - d, pos = False, d - now flag means return value type, True will return since, message and Flase return message >>> d = datetime.datetime(2009, 10, 1, 12, 23, 19) >>> timesince(d, d, True) >>> now = datetime.datetime(2009, 10, 1, 12, 24, 19) >>> timesince(d, now, True) u'1 minute ago' >>> now = datetime.datetime(2009, 10, 1, 12, 24, 30) >>> timesince(d, now, True) u'1 minute ago' >>> now = datetime.datetime(2009, 9, 28, 12, 24, 30) >>> timesince(d, now, True) u'2 days, 23 hours later' >>> now = datetime.datetime(2009, 10, 3, 12, 24, 30) >>> timesince(d, now, True) u'2 days ago' """ if not d: if flag: return 0, '' else: return '' chunks = ((60 * 60 * 24 * 365, lambda n: ungettext('year', 'years', n)), (60 * 60 * 24 * 30, lambda n: ungettext('month', 'months', n)), (60 * 60 * 24 * 7, lambda n: ungettext('week', 'weeks', n)), (60 * 60 * 24, lambda n: ungettext('day', 'days', n)), (60 * 60, lambda n: ungettext('hour', 'hours', n)), (60, lambda n: ungettext('minute', 'minutes', n))) if not now: now = date.now() else: now = date.to_datetime(now) d = date.to_datetime(d) delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) oldsince = since = delta.days * 24 * 60 * 60 + delta.seconds suffix = '' if pos: if since >= 0: suffix = ugettext(' ago') elif since < 0: suffix = ugettext(' later') since *= -1 for i, (seconds, name) in enumerate(chunks): count = since // seconds if count != 0: break s = ('%(number)d %(type)s') % {'number': count, 'type': name(count)} if i + 1 < len(chunks): # Now get the second item seconds2, name2 = chunks[i + 1] count2 = (since - (seconds * count)) // seconds2 if count2 != 0: s += (', %(number)d %(type)s') % { 'number': count2, 'type': name2(count2) } #if flag==True, then return twe elements (since, message) if flag: return oldsince, s + suffix else: return s + suffix
def validate(self, data, all_data=None): return bool(date.to_datetime(data))