因此,我有两个函数可以将python datetime.datetime()对象与毫秒之间相互转换。我无法弄清楚哪里出了问题。这是我正在使用的:
>>> import datetime
>>> def mil_to_date(mil):
"""date items from REST services are reported in milliseconds,
this function will convert milliseconds to datetime objects
Required:
mil -- time in milliseconds
"""
if mil == None:
return None
elif mil < 0:
return datetime.datetime.utcfromtimestamp(0) + datetime.timedelta(seconds=(mil/1000))
else:
return datetime.datetime.fromtimestamp(mil / 1000)
>>> def date_to_mil(date):
"""converts datetime.datetime() object to milliseconds
date -- datetime.datetime() object"""
if isinstance(date, datetime.datetime):
epoch = datetime.datetime.utcfromtimestamp(0)
return long((date - epoch).total_seconds() * 1000.0)
>>> mil = 1394462888000 …Run Code Online (Sandbox Code Playgroud) 我在应该是一个非常简单的脚本时遇到一些麻烦.我只是尝试使用Python pyodbc模块创建一个新的SQL Server数据库.我试图传入的"sqlcommand"参数在我在SQL Server 2012中执行时工作得很好,但它从这个python脚本失败了.不知道出了什么问题,谁有任何想法?
import pyodbc, os
def create_db(folder, db_name):
unc = r'\\arcsql\SDE\{0}'.format(folder)
if not os.path.exists(unc):
os.makedirs(unc)
full_name = os.path.join(r'E:\SDE', folder, db_name)
conn = pyodbc.connect("driver={SQL Server}; server=ArcSQL; database=master; Trusted_Connection=yes", automcommit=True)
cursor = conn.cursor()
sqlcommand = """USE [master]
GO
CREATE DATABASE [{0}] ON PRIMARY
( NAME = N'{0}', FILENAME = N'{1}.mdf', SIZE = 4MB , MAXSIZE = 10MB, FILEGROWTH = 1MB )
LOG ON
( NAME = N'{0}_log', FILENAME = N'{1}_log.ldf', SIZE = 4MB , MAXSIZE = 10MB, FILEGROWTH = …Run Code Online (Sandbox Code Playgroud) 在类中创建结果对象时,是否可以__slots__在此示例中使用?我以为我可以通过传入'__slots__'第三个参数的字典来使它工作type:
class GeocodeResult(object):
"""class to handle Reverse Geocode Result"""
__slots__ = ['geometry', 'response', 'spatialReference',
'candidates', 'locations', 'address', 'type', 'results']
def __init__(self, res_dict, geo_type):
RequestError(res_dict)
self.response = res_dict
self.type = 'esri_' + geo_type
self.spatialReference = None
self.candidates = []
self.locations = []
self.address = []
if 'spatialReference' in self.response:
self.spatialReference = self.response['spatialReference']
# more stuff
@property
def results(self):
results = []
for result in self.address + self.candidates + self.locations:
result['__slots__'] = ['address', 'score', 'location', 'attributes'] …Run Code Online (Sandbox Code Playgroud)