def as_json(self): return { 'ID': self.ID, 'UpdatedInDB': datetime.isoformat(self.UpdatedInDB), 'InsertIntoDB': datetime.isoformat(self.InsertIntoDB), 'SendingDateTime': datetime.isoformat(self.SendingDateTime), 'SendBefore': time.isoformat(self.SendBefore), 'SendAfter': time.isoformat(self.SendAfter), 'Text': self.Text, 'DestinationNumber': self.DestinationNumber, 'Coding': self.Coding, 'UDH': self.UDH, 'Class': self.Class, 'TextDecoded': self.TextDecoded, 'MultiPart': self.MultiPart, 'RelativeValidity': self.RelativeValidity, 'SenderID': self.SenderID, 'SendingTimeOut': datetime.isoformat(self.SendingTimeOut), 'DeliveryReport': self.DeliveryReport, 'CreatorID': self.CreatorID, 'Retries': self.Retries, 'Priority': self.Priority, 'Status': self.Status, 'StatusCode': self.StatusCode }
def gen_row(start_time, day): gr = random.sample(groups, concurrent_lessons) pr = random.sample(professors.items(), concurrent_lessons) aud = random.sample(auditorium, concurrent_lessons) for i, (p, dis) in enumerate(pr): l = day, time.isoformat(start_time), p, random.choice(dis), aud[i], gr[i] print('\t'.join(map(str, l)))
def default(self, obj): if isinstance(obj, datetime): return datetime.isoformat(obj.replace(microsecond=0)) if isinstance(obj, time): return time.isoformat(obj.replace(microsecond=0)) if isinstance(obj, timedelta): return str(obj) # add support for other serialization formats here... return super(CustomJSONEncoder, self).default(obj)
def default(self, obj): if isinstance(obj,datetime): return datetime.isoformat(obj.replace(microsecond=0)) if isinstance(obj, time): return time.isoformat(obj.replace(microsecond=0)) if isinstance(obj, timedelta): return str(obj) #add support for other serialization formats here... return super(CustomJSONEncoder, self).default(obj)
def getsubtitles(inputfile, keyword, outputfile): subtitles = parse(inputfile) newsubs = [] clips = [] for subtitle in subtitles: subtext = subtitle.clean_up(remove_formatting=True, to_lowercase=True) if keyword in subtext: t, t2 = subtitle.start, subtitle.end newstart = time.isoformat(t) newend = time.isoformat(t2) clip = VideoFileClip("tmp.mkv").subclip(newstart, newend) clips.append(clip) newsubs.append('{} - {} > {}'.format(subtitle.start, subtitle.end, subtext)) final_clip = concatenate_videoclips(clips) final_clip.write_videofile(outputfile) for subtile in newsubs: print(subtile)
def update_reservation_status(reservation_id: int, status: int, time: datetime = None): body = {} body['status'] = status body['time'] = time.isoformat() if time else "" logging.info(f'BODY {body}') url = f'{Reservation.BASE_URL}/reservation/{reservation_id}/status' try: req = safe_put(url=url, json=body) logging.warning(f'PUT {req}') return req.json()['message'] except: return 'Reservation service not reachable'
def getsubtitles(): subtitles = parse('.\\subtitles\\Bohemian.Rhapsody.srt') newsubs = [] clips = [] for subtitle in subtitles: subtext = subtitle.clean_up(remove_formatting=True, to_lowercase=True) if 'freddie' in subtext: t, t2 = subtitle.start, subtitle.end newstart = time.isoformat(t) newend = time.isoformat(t2) clip = VideoFileClip("F:\\Bohemian.Rhapsody.mkv").subclip( newstart, newend) clips.append(clip) newsubs.append('{} - {} > {}'.format(subtitle.start, subtitle.end, subtext)) final_clip = concatenate_videoclips(clips) final_clip.write_videofile("freddie.mp4") for subtile in newsubs: print(subtile)
def iso_time(): return time.isoformat()
def editNode(outfile, infile, user, name, description, category, parent, attributes, color, aggregate, verbosity, no_comments, comments, **dummy): try: # Read and skip comments at start of CSV file. csvcomments = '' if infile: csvFile = file(infile, 'r') while True: line = csvFile.readline() if line[:1] == '#': csvcomments += line else: csvfieldnames = next(unicodecsv.reader([line])) break if not no_comments: logfilename = outfile.rsplit('.',1)[0] + '.log' if os.path.isfile(logfilename): incomments = open(logfilename, 'r').read() else: incomments = '' logfile = open(logfilename, 'w') logfile.write(comments) logfile.write(csvcomments) logfile.write(incomments) logfile.close() norm = NVivoNorm(outfile) norm.begin() datetimeNow = datetime.utcnow() if user: user = norm.con.execute(select([ norm.User.c.Id ]).where( norm.User.c.Name == bindparam('Name') ), { 'Name': user }).first() if user: userId = user['Id'] else: userId = uuid.uuid4() norm.con.execute(norm.User.insert(), { 'Id': userId, 'Name': user }) else: project = norm.con.execute(select([ norm.Project.c.ModifiedBy ])).first() if project: userId = project['ModifiedBy'] else: userId = uuid.uuid4() norm.con.execute(norm.User.insert(), { 'Id': userId, 'Name': "Default User" }) norm.con.execute(norm.Project.insert(), { 'Version': '0.2', 'Title': "Created by NVivotools http://barraqda.org/nvivotools/", 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) if infile: csvreader=unicodecsv.DictReader(csvFile, fieldnames=csvfieldnames) nodeRows = [] for row in csvreader: nodeRow = dict(row) nodeRow['Name'] = nodeRow.get('Name', name) nodeRow['Description'] = nodeRow.get('Description', description) nodeRow['Category'] = nodeRow.get('Category', category) nodeRow['Parent'] = nodeRow.get('Parent', parent) nodeRow['Aggregate'] = nodeRow.get('Aggregate', aggregate) nodeRow['Category'] = nodeRow.get('Category', category) nodeRow['Color'] = nodeRow.get('Color', color) nodeRows.append(nodeRow) colNames = csvfieldnames else: nodeRows = [{ 'Name': name, 'Description': description, 'Category': category, 'Color': color }] colNames = ['Name', 'Description', 'Category', 'Color'] # Fill in attributes from command-line if attributes: for attribute in attributes: attMatch = re.match("(?P<attname>[^:]+):(?P<attvalue>.+)?", attribute) if not parseattribute: raise RuntimeError("Incorrect attribute format " + attribute) colnames.append(attName) for nodeRow in nodeRows: attName = attMatch.group('attname') attValue = attMatch.group('attvalue') nodeRow[attName] = nodeRow.get(attName, attValue) nodeAttributes = {} for attributeName in colNames: # Skip reserved attribute names if attributeName in ['Name', 'Description', 'Category', 'Parent', 'Aggregate', 'Color']: continue # Determine whether attribute is already defined nodeattribute = norm.con.execute(select([ norm.NodeAttribute.c.Id, norm.NodeAttribute.c.Type, norm.NodeAttribute.c.Length ]).where( norm.NodeAttribute.c.Name == bindparam('Name') ), { 'Name': attributeName }).first() if nodeattribute: nodeAttributes[attributeName] = { 'Id': nodeattribute['Id'], 'Type': nodeattribute['Type'], 'Length': nodeattribute['Length'] } else: attributeId = uuid.uuid4() typeInteger = True typeDecimal = True typeDateTime = True typeDate = True typeTime = True typeBoolean = True attributeLength = 0 for nodeRow in nodeRows: attributeValue = nodeRow[attributeName] attributeLength = max(attributeLength, len(attributeValue)) try: int(attributeValue) except ValueError: typeInteger = False try: float(attributeValue) except ValueError: typeDecimal = False try: datetimeval = dateparser.parse(attributeValue, default=datetime.min) if datetimeval.hour or datetimeval.minute: typeDate = False # Assume date being min means taken from default, ie not specified in datetime if datetimeval.date() != datetime.min.date(): typeTime = False except ValueError: typeDateTime = False typeDate = False typeTime = False if not attributeValue.lower() in {'true', 'false'}: typeBoolean = False if typeInteger: attributeType = 'integer' elif typeDecimal: attributeType = 'decimal' elif typeBoolean: attributeType = 'boolean' elif typeDate: attributeType = 'date' elif typeTime: attributeType = 'time' elif typeDateTime: attributeType = 'datetime' else: attributeType = 'text' norm.con.execute(norm.NodeAttribute.insert(), { 'Id': attributeId, 'Name': attributeName, 'Description': "Created by NVivotools http://barraqda.org/nvivotools/", 'Type': attributeType, 'Length': attributeLength, 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) nodeAttributes[attributeName] = { 'Id': attributeId, 'Type': attributeType, 'Length': attributeLength, } rowNum = 0 nodesToInsert = [] nodeValuesToInsert = [] for nodeRow in nodeRows: rowNum += 1 categoryName = nodeRow.get('Category') categoryId = None if categoryName is not None: category = norm.con.execute(select([ norm.NodeCategory.c.Id ]).where( norm.NodeCategory.c.Name == bindparam('NodeCategory') ), { 'NodeCategory': categoryName }).first() if category is not None: categoryId = category['Id'] else: categoryId = uuid.uuid4() norm.con.execute(norm.NodeCategory.insert(), { 'Id': categoryId, 'Name': categoryName, 'Description': "Created by NVivotools http://barraqda.org/nvivotools/", 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) parentName = nodeRow.get('Parent') parentId = None if (parentName or '') != '': parentIdList = [nodeToInsert['Id'] for nodeToInsert in nodesToInsert if nodeToInsert['Name'] == parentName] if parentIdList: parentId = parentIdList[0] else: parent = norm.con.execute(select([ norm.Node.c.Id ]).where( norm.Node.c.Name == bindparam('Name') ), { 'Name': parentName }).first() if parent is not None: parentId = parent['Id'] else: parentId = uuid.uuid4() norm.con.execute(norm.Node.insert(), { 'Id': parentId, 'Name': parentName, 'Description': "Created by NVivotools http://barraqda.org/nvivotools/", 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) nodeName = nodeRow.get('Name') or str(rowNum) nodeDescription = nodeRow.get('Description') nodeAggregate = nodeRow.get('Aggregate') node = norm.con.execute(select([ norm.Node.c.Id ]).where( norm.Node.c.Name == bindparam('Name') ), { 'Name': nodeName }).first() nodeId = node['Id'] if node else uuid.uuid4() nodeValues = [] for attributeName, attributeNode in nodeAttributes.iteritems(): attributeId = attributeNode['Id'] attributeType = attributeNode['Type'] attributeLength = attributeNode['Length'] attributeValue = nodeRow[attributeName] if not attributeValue: continue if attributeType == 'text': if attributeLength and len(attributeValue) > attributeLength: raise RuntimeError("Value: " + attributeValue + " longer than attribute length") elif attributeType == 'integer': attributeValue = int(attributeValue) elif attributeType == 'decimal': attributeValue = float(attributeValue) elif attributeType == 'datetime': attributeValue = datetime.isoformat(dateparser.parse(attributeValue)) elif attributeType == 'date': attributeValue = date.isoformat(dateparser.parse(attributeValue)) elif attributeType == 'time': attributeValue = time.isoformat(dateparser.parse(attributeValue).time()) elif attributeType == 'boolean': attributeValue = str(bool(util.strtobool(attributeValue))) else: raise RuntimeError("Unknown attribute type: " + attributeType) nodeValues.append({ 'Node': nodeId, '_Node': nodeId, 'Attribute': attributeId, '_Attribute': attributeId, 'Value': attributeValue, 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) normNodeRow = { 'Id': nodeId, '_Id': nodeId, 'Name': nodeName, 'Description': nodeDescription, 'Category': categoryId, 'Parent': parentId, 'Aggregate': nodeAggregate, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow } normNodeRow['Color'] = nodeRow.get('Color') if node is None: # New node normNodeRow.update({ 'CreatedBy': userId, 'CreatedDate': datetimeNow, }) nodesToInsert.append(normNodeRow) nodeValuesToInsert += nodeValues else: norm.con.execute(norm.Node.update( norm.Node.c.Id == bindparam('_Id')), normNodeRow) for nodeValue in nodeValues: norm.con.execute(norm.NodeValue.delete(and_( norm.NodeValue.c.Node == bindparam('_Node'), norm.NodeValue.c.Attribute == bindparam('_Attribute'), )), nodeValues) nodeValuesToInsert += nodeValues if nodesToInsert: norm.con.execute(norm.Node.insert(), nodesToInsert) if nodeValuesToInsert: norm.con.execute(norm.NodeValue.insert(), nodeValuesToInsert) norm.commit() except: raise norm.rollback() finally: del norm
def editSource(outfile, infile, user, name, description, category, attributes, color, text, columns, exclude, textcolumns, encoding, verbosity, limit, no_comments, comments, **dummy): try: if not no_comments: logfilename = outfile.rsplit('.',1)[0] + '.log' if os.path.isfile(logfilename): incomments = open(logfilename, 'r').read() else: incomments = '' logfile = open(logfilename, 'w') logfile.write(comments) logfile.write(incomments) logfile.close() norm = NVivoNorm(outfile) norm.begin() datetimeNow = datetime.utcnow() if user: userRecord = norm.con.execute(select([ norm.User.c.Id ]).where( norm.User.c.Name == bindparam('Name') ), { 'Name': user }).first() if userRecord: userId = userRecord['Id'] else: userId = uuid.uuid4() norm.con.execute(norm.User.insert(), { 'Id': userId, 'Name': user }) else: project = norm.con.execute(select([ norm.Project.c.ModifiedBy ])).first() if project: userId = project['ModifiedBy'] else: userId = uuid.uuid4() norm.con.execute(norm.User.insert(), { 'Id': userId, 'Name': u"Default User" }) norm.con.execute(norm.Project.insert(), { 'Version': '0.2', 'Title': "Created by NVivotools http://barraqda.org/nvivotools/", 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) sourceRows = [] for infilepattern in infile: for infilename in glob.glob(infilepattern): if verbosity >= 2: print("Loading " + infilename, file=sys.stderr) infilebasename, infiletype = os.path.splitext(os.path.basename(infilename)) infiletype = infiletype[1:].upper() if infiletype == 'CSV': incomments = '' csvFile = file(infilename, 'rU') # Skip comments at start of CSV file. while True: line = csvFile.readline() if line[:1] == '#': incomments += line else: csvfieldnames = next(unicodecsv.reader([line])) break if not no_comments: logfile.write(incomments) csvreader=unicodecsv.DictReader(csvFile, fieldnames=csvfieldnames) for row in csvreader: sourceRow = dict(row) sourceRow['Name'] = sourceRow.get('Name', name) sourceRow['Description'] = sourceRow.get('Description', description or u"Created by NVivotools http://barraqda.org/nvivotools/") sourceRow['Category'] = sourceRow.get('Category', category) sourceRow['Color'] = sourceRow.get('Color', color) sourceRow['Text'] = sourceRow.get('Text', text) sourceRows.append(sourceRow) if limit and len(sourceRows) == limit: break else: sourceRows.append({ 'Name': name or infilebasename, 'Description': description or u"Created by NVivotools http://barraqda.org/nvivotools/", 'Category': category, 'Color': color, 'ObjectFile': infilename, }) if not infile: sourceRows = [{ 'Name': name, 'Description': description or u"Created by NVivotools http://barraqda.org/nvivotools/", 'Category': category, 'Color': color, 'Text': text }] # Fill in attributes if attributes: for attribute in attributes: attMatch = re.match("(?P<attname>[^:]+):(?P<attvalue>.+)?", attribute) if not parseattribute: raise RuntimeError("Incorrect attribute format " + attribute) colnames.append(attName) for sourceRow in sourceRows: attName = attMatch.group('attname') attValue = attMatch.group('attvalue') sourceRow[attName] = sourceRow.get(attName, attValue) sourceAttributes = {} sourceNodeId = {} for colName in sourceRows[0].keys(): # Does column define an attribute? if ((not columns or colName in columns) and colName and colName not in ['Name', 'Description', 'Category', 'Color', 'ObjectFile', 'Text'] + exclude + textcolumns): # Determine whether attribute is already defined sourceattribute = norm.con.execute(select([ norm.SourceAttribute.c.Id, norm.SourceAttribute.c.Type, norm.SourceAttribute.c.Length ]).where( norm.SourceAttribute.c.Name == bindparam('Name') ), { 'Name': colName }).first() if sourceattribute: sourceAttributes[colName] = { 'Id': sourceattribute['Id'], 'Type': sourceattribute['Type'], 'Length': sourceattribute['Length'] } else: attributeId = uuid.uuid4() typeInteger = True typeDecimal = True typeDateTime = True typeDate = True typeTime = True typeBoolean = True attributeLength = 0 for sourceRow in sourceRows: attributeValue = sourceRow[colName] attributeLength = max(attributeLength, len(attributeValue)) try: int(attributeValue) except ValueError: typeInteger = False try: float(attributeValue) except ValueError: typeDecimal = False try: datetimeval = dateparser.parse(attributeValue, default=datetime.min) if datetimeval.hour or datetimeval.minute: typeDate = False # Assume date being min means taken from default, ie not specified in datetime if datetimeval.date() != datetime.min.date(): typeTime = False except ValueError: typeDateTime = False typeDate = False typeTime = False if not attributeValue.lower() in {'true', 'false'}: typeBoolean = False if typeInteger: attributeType = 'integer' elif typeDecimal: attributeType = 'decimal' elif typeBoolean: attributeType = 'boolean' elif typeDate: attributeType = 'date' elif typeTime: attributeType = 'time' elif typeDateTime: attributeType = 'datetime' else: attributeType = 'text' norm.con.execute(norm.SourceAttribute.insert(), { 'Id': attributeId, 'Name': colName, 'Description': u"Created by NVivotools http://barraqda.org/nvivotools/", 'Type': unicode(attributeType), 'Length': attributeLength, 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) sourceAttributes[colName] = { 'Id': attributeId, 'Type': attributeType, 'Length': attributeLength, } # Does column define a node? elif colName in textcolumns and len(textcolumns) > 1: node = norm.con.execute(select([ norm.Node.c.Id ]).where( norm.Node.c.Name == bindparam('Name') ), { 'Name': colName }).first() if node: nodeId = node['Id'] else: nodeId = uuid.uuid4() norm.con.execute(norm.Node.insert(), { 'Id': nodeId, 'Name': unicode(colName), 'Description': u"Created by NVivotools http://barraqda.org/nvivotools/", 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) sourceNodeId[colName] = nodeId rowNum = 0 sourcesToInsert = [] sourceValuesToInsert = [] digits = len(str(len(sourceRows))) for sourceRow in sourceRows: rowNum += 1 categoryName = sourceRow.get('Category') categoryId = None if categoryName is not None: category = norm.con.execute(select([ norm.SourceCategory.c.Id ]).where( norm.SourceCategory.c.Name == bindparam('SourceCategory') ), { 'SourceCategory': categoryName }).first() if category is not None: categoryId = category['Id'] else: categoryId = uuid.uuid4() norm.con.execute(norm.SourceCategory.insert(), { 'Id': categoryId, 'Name': categoryName, 'Description': u"Created by NVivotools http://barraqda.org/nvivotools/", 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) sourceName = sourceRow.get('Name') or unicode(rowNum).zfill(digits) sourceDescription = sourceRow.get('Description') or u"Created by NVivotools http://barraqda.org/nvivotools/" source = norm.con.execute(select([ norm.Source.c.Id ]).where( norm.Source.c.Name == bindparam('Name') ), { 'Name': sourceName }).first() sourceId = source['Id'] if source else uuid.uuid4() sourceValues = [] for attributeName, attributeSource in sourceAttributes.iteritems(): attributeId = attributeSource['Id'] attributeType = attributeSource['Type'] attributeLength = attributeSource['Length'] attributeValue = sourceRow[attributeName] if attributeType == 'text': if attributeLength and len(attributeValue) > attributeLength: raise RuntimeError("Value: " + attributeValue + " longer than attribute length") elif attributeType == 'integer': attributeValue = int(attributeValue) elif attributeType == 'decimal': attributeValue = float(attributeValue) elif attributeType == 'datetime': attributeValue = datetime.isoformat(dateparser.parse(attributeValue)) elif attributeType == 'date': attributeValue = date.isoformat(dateparser.parse(attributeValue)) elif attributeType == 'time': attributeValue = time.isoformat(dateparser.parse(attributeValue).time()) elif attributeType == 'boolean': attributeValue = str(bool(util.strtobool(attributeValue))) else: raise RuntimeError("Unknown attribute type: " + attributeType) sourceValues.append({ 'Source': sourceId, '_Source': sourceId, 'Attribute': attributeId, '_Attribute': attributeId, 'Value': unicode(attributeValue), 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) normSourceRow = { 'Id': sourceId, '_Id': sourceId, 'Name': sourceName, 'Description': sourceDescription, 'Category': categoryId, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow } normSourceRow['Color'] = sourceRow.get('Color') ObjectFile = sourceRow.get('ObjectFile') if ObjectFile: dummy, ObjectFileExt = os.path.splitext(ObjectFile) ObjectFileExt = ObjectFileExt[1:].upper() normSourceRow['ObjectType'] = unicode(ObjectFileExt) if ObjectFileExt == 'TXT': # Detect file encoding if not specified if not encoding: raw = file(ObjectFile, 'rb').read(32) # at most 32 bytes are returned encoding = chardet.detect(raw)['encoding'] normSourceRow['Content'] = codecs.open(ObjectFile, 'r', encoding=encoding).read() if encoding != 'utf8': normSourceRow['Content'] = normSourceRow['Content'].encode('utf8') else: normSourceRow['Object'] = open(ObjectFile, 'rb').read() else: normSourceRow['ObjectType'] = u'TXT' normSourceRow['Content'] = sourceRow.get('Text') or u'' if normSourceRow.get('ObjectType') == u'TXT': for textColumn in textcolumns: normSourceText = sourceRow.get(textColumn) or u'' if normSourceText: normSourceText += u'\n' if normSourceRow['Content']: normSourceRow['Content'] += u'\n\n' # If more than one text column then make header in content and tag text if len(textcolumns) > 1: normSourceRow['Content'] += textColumn + u'\n\n' start = len(normSourceRow['Content']) + 1 end = start + len(normSourceText) - 1 nodeId = sourceNodeId[textColumn] taggingId = uuid.uuid4() norm.con.execute(norm.Tagging.insert(), { 'Id': taggingId, 'Source': sourceId, 'Node': nodeId, 'Fragment': unicode(start) + u':' + unicode(end), 'Memo': None, 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) normSourceRow['Content'] += normSourceText normSourceRow['Object'] = bytearray(normSourceRow['Content'], 'utf8') if source is None: # New source normSourceRow.update({ 'CreatedBy': userId, 'CreatedDate': datetimeNow, }) sourcesToInsert.append(normSourceRow) sourceValuesToInsert += sourceValues else: norm.con.execute(norm.Source.update( norm.Source.c.Id == bindparam('_Id')), normSourceRow) for sourceValue in sourceValues: norm.con.execute(norm.SourceValue.delete(and_( norm.SourceValue.c.Source == bindparam('_Source'), norm.SourceValue.c.Attribute == bindparam('_Attribute'), )), sourceValues) sourceValuesToInsert += sourceValues if sourcesToInsert: norm.con.execute(norm.Source.insert(), sourcesToInsert) if sourceValuesToInsert: norm.con.execute(norm.SourceValue.insert(), sourceValuesToInsert) norm.commit() except: raise norm.rollback() finally: del norm
def dump(self, value: time, ctx: Dumping) -> str: return time.isoformat(value)
def _format_time(time): from xoutil.string import cut_suffix return cut_suffix(time.isoformat(), ":00")
attributeType = sourceattribute['Type'] attributeLength = sourceattribute['Length'] if attributeType == 'Text': if len(attributeValue) > attributeLength: raise RuntimeError("Value: " + attributeValue + " longer than attribute length") elif attributeType == 'Integer': int(attributeValue) elif attributeType == 'Decimal': float(attributeValue) elif attributeType == 'Datetime': attributeValue = datetime.isoformat(dateparser.parse(attributeValue)) elif attributeType == 'Date': attributeValue = date.isoformat(dateparser.parse(attributeValue)) elif attributeType == 'Time': attributeValue = time.isoformat(dateparser.parse(attributeValue).time()) elif attributeType == 'Boolean': attributeValue = str(bool(util.strtobool(attributeValue))) else: raise RuntimeError("Unknown attribute type: " + attributeType) sourceValues += [{ 'Source': Id, '_Source': Id, 'Attribute': attributeId, '_Attribute': attributeId, 'Value': attributeValue, 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow
def editNode(outfile, infile, user, name, description, category, parent, attributes, color, aggregate, verbosity, no_comments, comments, **dummy): try: # Read and skip comments at start of CSV file. csvcomments = '' if infile: csvFile = file(infile, 'r') while True: line = csvFile.readline() if line[:1] == '#': csvcomments += line else: csvfieldnames = next(unicodecsv.reader([line])) break if not no_comments: logfilename = outfile.rsplit('.', 1)[0] + '.log' if os.path.isfile(logfilename): incomments = open(logfilename, 'r').read() else: incomments = '' logfile = open(logfilename, 'w') logfile.write(comments) logfile.write(csvcomments) logfile.write(incomments) logfile.close() norm = NVivoNorm(outfile) norm.begin() datetimeNow = datetime.utcnow() if user: user = norm.con.execute( select([norm.User.c.Id ]).where(norm.User.c.Name == bindparam('Name')), { 'Name': user }).first() if user: userId = user['Id'] else: userId = uuid.uuid4() norm.con.execute(norm.User.insert(), { 'Id': userId, 'Name': user }) else: project = norm.con.execute(select([norm.Project.c.ModifiedBy ])).first() if project: userId = project['ModifiedBy'] else: userId = uuid.uuid4() norm.con.execute(norm.User.insert(), { 'Id': userId, 'Name': "Default User" }) norm.con.execute( norm.Project.insert(), { 'Version': '0.2', 'Title': "Created by NVivotools http://barraqda.org/nvivotools/", 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) if infile: csvreader = unicodecsv.DictReader(csvFile, fieldnames=csvfieldnames) nodeRows = [] for row in csvreader: nodeRow = dict(row) nodeRow['Name'] = nodeRow.get('Name', name) nodeRow['Description'] = nodeRow.get('Description', description) nodeRow['Category'] = nodeRow.get('Category', category) nodeRow['Parent'] = nodeRow.get('Parent', parent) nodeRow['Aggregate'] = nodeRow.get('Aggregate', aggregate) nodeRow['Category'] = nodeRow.get('Category', category) nodeRow['Color'] = nodeRow.get('Color', color) nodeRows.append(nodeRow) colNames = csvfieldnames else: nodeRows = [{ 'Name': name, 'Description': description, 'Category': category, 'Color': color }] colNames = ['Name', 'Description', 'Category', 'Color'] # Fill in attributes from command-line if attributes: for attribute in attributes: attMatch = re.match("(?P<attname>[^:]+):(?P<attvalue>.+)?", attribute) if not parseattribute: raise RuntimeError("Incorrect attribute format " + attribute) colnames.append(attName) for nodeRow in nodeRows: attName = attMatch.group('attname') attValue = attMatch.group('attvalue') nodeRow[attName] = nodeRow.get(attName, attValue) nodeAttributes = {} for attributeName in colNames: # Skip reserved attribute names if attributeName in [ 'Name', 'Description', 'Category', 'Parent', 'Aggregate', 'Color' ]: continue # Determine whether attribute is already defined nodeattribute = norm.con.execute( select([ norm.NodeAttribute.c.Id, norm.NodeAttribute.c.Type, norm.NodeAttribute.c.Length ]).where(norm.NodeAttribute.c.Name == bindparam('Name')), { 'Name': attributeName }).first() if nodeattribute: nodeAttributes[attributeName] = { 'Id': nodeattribute['Id'], 'Type': nodeattribute['Type'], 'Length': nodeattribute['Length'] } else: attributeId = uuid.uuid4() typeInteger = True typeDecimal = True typeDateTime = True typeDate = True typeTime = True typeBoolean = True attributeLength = 0 for nodeRow in nodeRows: attributeValue = nodeRow[attributeName] attributeLength = max(attributeLength, len(attributeValue)) try: int(attributeValue) except ValueError: typeInteger = False try: float(attributeValue) except ValueError: typeDecimal = False try: datetimeval = dateparser.parse(attributeValue, default=datetime.min) if datetimeval.hour or datetimeval.minute: typeDate = False # Assume date being min means taken from default, ie not specified in datetime if datetimeval.date() != datetime.min.date(): typeTime = False except ValueError: typeDateTime = False typeDate = False typeTime = False if not attributeValue.lower() in {'true', 'false'}: typeBoolean = False if typeInteger: attributeType = 'integer' elif typeDecimal: attributeType = 'decimal' elif typeBoolean: attributeType = 'boolean' elif typeDate: attributeType = 'date' elif typeTime: attributeType = 'time' elif typeDateTime: attributeType = 'datetime' else: attributeType = 'text' norm.con.execute( norm.NodeAttribute.insert(), { 'Id': attributeId, 'Name': attributeName, 'Description': "Created by NVivotools http://barraqda.org/nvivotools/", 'Type': attributeType, 'Length': attributeLength, 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) nodeAttributes[attributeName] = { 'Id': attributeId, 'Type': attributeType, 'Length': attributeLength, } rowNum = 0 nodesToInsert = [] nodeValuesToInsert = [] for nodeRow in nodeRows: rowNum += 1 categoryName = nodeRow.get('Category') categoryId = None if categoryName is not None: category = norm.con.execute( select([norm.NodeCategory.c.Id]).where( norm.NodeCategory.c.Name == bindparam('NodeCategory')), { 'NodeCategory': categoryName }).first() if category is not None: categoryId = category['Id'] else: categoryId = uuid.uuid4() norm.con.execute( norm.NodeCategory.insert(), { 'Id': categoryId, 'Name': categoryName, 'Description': "Created by NVivotools http://barraqda.org/nvivotools/", 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) parentName = nodeRow.get('Parent') parentId = None if (parentName or '') != '': parentIdList = [ nodeToInsert['Id'] for nodeToInsert in nodesToInsert if nodeToInsert['Name'] == parentName ] if parentIdList: parentId = parentIdList[0] else: parent = norm.con.execute( select([ norm.Node.c.Id ]).where(norm.Node.c.Name == bindparam('Name')), { 'Name': parentName }).first() if parent is not None: parentId = parent['Id'] else: parentId = uuid.uuid4() norm.con.execute( norm.Node.insert(), { 'Id': parentId, 'Name': parentName, 'Description': "Created by NVivotools http://barraqda.org/nvivotools/", 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) nodeName = nodeRow.get('Name') or str(rowNum) nodeDescription = nodeRow.get('Description') nodeAggregate = nodeRow.get('Aggregate') node = norm.con.execute( select([norm.Node.c.Id ]).where(norm.Node.c.Name == bindparam('Name')), { 'Name': nodeName }).first() nodeId = node['Id'] if node else uuid.uuid4() nodeValues = [] for attributeName, attributeNode in nodeAttributes.iteritems(): attributeId = attributeNode['Id'] attributeType = attributeNode['Type'] attributeLength = attributeNode['Length'] attributeValue = nodeRow[attributeName] if not attributeValue: continue if attributeType == 'text': if attributeLength and len( attributeValue) > attributeLength: raise RuntimeError("Value: " + attributeValue + " longer than attribute length") elif attributeType == 'integer': attributeValue = int(attributeValue) elif attributeType == 'decimal': attributeValue = float(attributeValue) elif attributeType == 'datetime': attributeValue = datetime.isoformat( dateparser.parse(attributeValue)) elif attributeType == 'date': attributeValue = date.isoformat( dateparser.parse(attributeValue)) elif attributeType == 'time': attributeValue = time.isoformat( dateparser.parse(attributeValue).time()) elif attributeType == 'boolean': attributeValue = str(bool(util.strtobool(attributeValue))) else: raise RuntimeError("Unknown attribute type: " + attributeType) nodeValues.append({ 'Node': nodeId, '_Node': nodeId, 'Attribute': attributeId, '_Attribute': attributeId, 'Value': attributeValue, 'CreatedBy': userId, 'CreatedDate': datetimeNow, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow }) normNodeRow = { 'Id': nodeId, '_Id': nodeId, 'Name': nodeName, 'Description': nodeDescription, 'Category': categoryId, 'Parent': parentId, 'Aggregate': nodeAggregate, 'ModifiedBy': userId, 'ModifiedDate': datetimeNow } normNodeRow['Color'] = nodeRow.get('Color') if node is None: # New node normNodeRow.update({ 'CreatedBy': userId, 'CreatedDate': datetimeNow, }) nodesToInsert.append(normNodeRow) nodeValuesToInsert += nodeValues else: norm.con.execute( norm.Node.update(norm.Node.c.Id == bindparam('_Id')), normNodeRow) for nodeValue in nodeValues: norm.con.execute( norm.NodeValue.delete( and_( norm.NodeValue.c.Node == bindparam('_Node'), norm.NodeValue.c.Attribute == bindparam( '_Attribute'), )), nodeValues) nodeValuesToInsert += nodeValues if nodesToInsert: norm.con.execute(norm.Node.insert(), nodesToInsert) if nodeValuesToInsert: norm.con.execute(norm.NodeValue.insert(), nodeValuesToInsert) norm.commit() except: raise norm.rollback() finally: del norm
# 0 <= minute < 60 # 0 <= second < 60 # 0 <= microsecond < 1000000. print "Constructor" print time(hour=14, minute=2, second=34, microsecond=454, tzinfo=None) # Class Attributes print "\nClass Attributes" print time.min # The earliest representable time ie time(0, 0, 0, 0). print time.max # The latest representable time, time(23, 59, 59, 999999). print time.resolution # The smallest possible difference between non-equal time objects, # timedelta(microseconds=1). Note that arithmetic on time objects is not supported. # Instance attributes (read-only): print time.hour # In range(24). print time.minute # In range(60). print time.second # In range(60). print time.microsecond # In range(1000000). print time.tzinfo # If tzinfo is None, returns None, else returns the tzinfo object passed. # Instance Methods print "\nInstance Methods" now = datetime.now().time() print now.replace(hour=11, minute=12, second=13, microsecond=14, tzinfo=None) # Replace & return. print time.isoformat(now) # Return a string representing the time in ISO 8601 format, HH:MM:SS.mmmmmm. print time.__str__(now) # For a time t, str(t) is equivalent to t.isoformat(). print now.strftime("%d-%m-%Y") # Return a string representing the time, controlled by an explicit format string. print now.__format__("%d-%m-%Y") # Same as time.strftime(). print time.utcoffset(now) # If tzinfo is None, returns None, else returns self.tzinfo.utcoffset(None). print time.dst(now) # If tzinfo is None, returns None, else returns self.tzinfo.dst(None). print time.tzname(now) # If tzinfo is None, returns None, else returns self.tzinfo.tzname(None).
def test_time(self): time = self.ts.time() self.assertEqual(self.dump_and_load(time), time.isoformat())