Beispiel #1
0
def checkPreConditions(nextPeriod, driversFileName, vehiclesFileName,
                       servicesFileName, reservationsFileName):
    """Checks the preconditions.
    Requires:
    The same as update (ommitted here to avoid redudancy)
    Ensures:
    returns bool value False if some of the conditions are not met
    and True otherwise
    """

    headerDrivers = FileUtil(driversFileName).getHeader()
    headerVehicles = FileUtil(vehiclesFileName).getHeader()
    headerServices = FileUtil(servicesFileName).getHeader()
    headerReservations = FileUtil(reservationsFileName).getHeader()

    previousPeriod = Time().getPreviousPeriod(nextPeriod)
    # Changes the format of the period to the one in the header of files
    nextPeriodOther = nextPeriod[0:2] + ":00 - " + nextPeriod[2:4] + ":00"
    previousPeriodOther = previousPeriod[0:2] + ":00 - " + previousPeriod[
        2:4] + ":00"

    # NextPeriod is a str from the set 0911, 1113, ..., 1921
    if nextPeriod not in ['0911', '1113', '1315', '1517', '1719', '1921']:
        return False

    # The files whose names are driversFileName, vehiclesFileName, servicesFileName and reservationsFileName
    # concern the same company and the same day;
    elif not (headerDrivers[INDEXCompany:INDEXDate + 1] ==
              headerVehicles[INDEXCompany:INDEXDate + 1] ==
              headerServices[INDEXCompany:INDEXDate + 1] ==
              headerReservations[INDEXCompany:INDEXDate + 1]):
        return False

    # The file whose name is reservationsFileName concerns the period indicated by nextPeriod
    elif headerReservations[INDEXPeriod].strip() != nextPeriodOther:
        return False

    # The files whose names are driversFileName, vehiclesFileName, servicesFileName concern the period
    # immediately preceding the period indicated by nextPeriod;

    elif not (headerDrivers[INDEXPeriod].strip() ==
              headerVehicles[INDEXPeriod].strip() ==
              headerServices[INDEXPeriod].strip() == previousPeriodOther):
        return False

    # The file name reservationsFileName ends (before the .txt extension) with
    # the string nextPeriod;
    elif reservationsFileName[-8:-4] != nextPeriod:
        return False

    # The file names driversFileName, vehiclesFileName and servicesFileName
    # end (before their .txt extension) with the string representing
    # the period immediately preceding the one indicated by nextPeriod,
    # from the set 0709, 0911, ..., 1719;
    elif not (driversFileName[-8:-4] == vehiclesFileName[-8:-4] ==
              servicesFileName[-8:-4] == previousPeriod):
        return False

    else:
        return True
Beispiel #2
0
    def __init__(self, file_name=None):
        """Creates a DriversDict composed by Driver objects,
        from a file with a list of drivers.

        Requires: If given, file_name is str with the name of a .txt file containing
        a list of drivers organized as in the examples provided in
        the general specification (omitted here for the sake of readability).

        Ensures:
        if file_name is given:
            a DriversDict, composed by objects of class Driver that correspond to the drivers listed
            in file with name file_name.
        if file_name is none:
            a empty DriversList."""

        UserDict.__init__(self)

        if file_name is not None:
            inFile = FileUtil(file_name)
            for line in inFile.getContent():
                driverData = line.rstrip().split(", ")
                driverName = driverData.pop(DriversDict.INDEXDriverName)
                driverEntryTime, driverAccumTime = driverData
                driverEntryTime = Time(driverEntryTime)
                driverAccumTime = Time(driverAccumTime)
                newDriver = Driver(driverName, driverEntryTime,
                                   driverAccumTime)

                self[driverName] = newDriver
    def __init__(self, file_name=None):
        """Creates a VehicleDict composed by vehicles objects,
        from a file with a list of vehicles.

        Requires: If given, file_name is str with the name of a .txt file containing
        a list of vehicles organized as in the examples provided in
        the general specification (omitted here for the sake of readability).
        Ensures:
        if file_name is given:
            a VehiclesDict, composed by objects of class Vehicle that correspond to the vehicles listed
            in file with name file_name.
        if file_name is none:
            a empty VehiclesList.
        """

        UserDict.__init__(self)

        inFile = FileUtil(file_name)
        for line in inFile.getContent():
            vehicleData = line.rstrip().split(", ")
            vehiclePlate = vehicleData.pop(VehiclesDict.INDEXVehiclePlate)
            vehicleModel, vehicleAutonomy, vehicleKms = vehicleData
            newVehicle = Vehicle(vehiclePlate, vehicleModel, vehicleAutonomy,
                                 vehicleKms)

            self[vehiclePlate] = newVehicle
Beispiel #4
0
 def loadImage(self, path):
     imgs = []
     cvHelp = CvHelp()
     fileUtil = FileUtil()
     (files, counts, dirs) = fileUtil.getSamplesLabelsDirnames("number")
     for file in files:
         img = cv2.resize(cvHelp.openGray(file), (IMAGE_SIZE, IMAGE_SIZE))
         imgs.append(img)
     return imgs, counts, dirs
Beispiel #5
0
    def testTrainCNN(self):
        samples = np.load('samples.npy')
        labels = np.load('label.npy')
        imgs = []

        fileUtil = FileUtil()
        (files, counts, dirs) = getSamplesLabelsDirnames("number")
        for file in files:
            imgs.append(self.openZero(file))

        return model
Beispiel #6
0
 def loadImage(self, path):
     imgs = []
     cvHelp = CvHelp()
     fileUtil = FileUtil()
     (files, counts, dirs) = fileUtil.getSamplesLabelsDirnames(path)
     for file in files:
         img = cv2.resize(cvHelp.open(file), (img_rows, img_cols))
         imgs.append(img)
     imgs = np.array(imgs)
     counts = np.array(counts)
     counts -= counts.min()
     dirs = np.array(dirs)
     return imgs, counts, dirs
Beispiel #7
0
    def __init__(self, file_name=None):
        """Creates a ServicesList composed by Services objects,
        from a file with a list of services.

        Requires: If given, file_name is str with the name of a .txt file containing
        a list of services organized as in the examples provided in
        the general specification (omitted here for the sake of readability).
        Ensures:
        if file_name is given:
            a ServiceList, composed by objects of class Service that correspond to the services listed
            in file with name file_name. In this ServiceList, drivers terminating their services earlier
            have priority over the ones terminating later; lexicographic order of drivers's names
            decides eventual ties in each case above.
        if file_name is none:
            a empty ServiceList.
        """

        # creates empty ServicesList
        UserList.__init__(self)

        # if file_name is given, self is populated with Services corresponding to the
        # services on the file file_name
        if file_name is not None:
            inFile = FileUtil(file_name)

            for line in inFile.getContent():
                servData = line.rstrip().split(", ")
                servDriver = servData[ServicesList.INDEXDriverName]
                servPlate = servData[ServicesList.INDEXVehiclePlate]
                servClient = servData[ServicesList.INDEXClientName]
                servDeparTime = Time(servData[ServicesList.INDEXDepartureHour])
                servArrivalTime = Time(servData[ServicesList.INDEXArrivalHour])
                servCircuit = servData[ServicesList.INDEXCircuitId]
                servCircuitKms = servData[ServicesList.INDEXCircuitKms]
                servDriverStatus = servData[ServicesList.INDEXDriverStatus]
                newService = Service(servDriver, servPlate, servClient, servDeparTime, servArrivalTime, \
                                     servCircuit, servCircuitKms, servDriverStatus)
                self.append(newService)
Beispiel #8
0
from bs4 import BeautifulSoup
from FileUtil import *
from UrlUtil import *
from client import *

# url = 'http://www.newyx.net/zq/wuxiadanji/'
# host = 'www_newyx_net'
url = ''
host = ''
if __name__ == '__main__':
    url = sys.argv[1]
    host = sys.argv[2]

if not url == '':
    mq = lg_mq_client("127.0.0.1")
    f = FileUtil(host)
    f.initPath()  # 初始化目录结构
    u = Util(url)
    urlinfo = UrlUtil(url)
    soup = u.getSoup()
    info = u.getInfo(soup)
    main_md5 = u.getMd5(urlinfo.host)
    minor_md5 = u.getMd5(urlinfo.path)
    f.saveInfo(main_md5 + "_" + minor_md5, info)

    #过滤顶部菜单
    u.filterColumn(soup)

    a_lsit = u.filterHtmlByA(soup, urlinfo)
    img_list = u.filterHtmlByImg(soup, urlinfo, f)
Beispiel #9
0
 def generateDLink(self, DATASET_PATH, dataSet):
     userSet = self.ul.AllUser()
     Outsequence = []
     Insequence = []
     for user in userSet:
         for num in range(0, self.ul.getUser(user).getODegree()):
             Outsequence.append(user)
         for num in range(0, self.ul.getUser(user).getIDegree()):
             Insequence.append(user)
     print "generating directedgraph "
     loop1 = set()
     loop2 = set()
     while Outsequence and Insequence:
         # 若所有出度节点均已被选为ID1,处理死循环
         if loop1 | set(Outsequence) == loop1:
             print "step into endless loop "
             self.newdealD(Outsequence, Insequence)
             break
         # 从出度节点集选一个用户做起始节点vi
         ID1 = random.choice(Outsequence)
         node1 = self.ul.getUser(ID1)
         if node1.firstCandidateSorted:
             ftSample = FtSample()
             # 用ftsample依概率选择vj
             seqNum = ftSample.getPrizeIndex(node1.firstCandidateSorted,
                                             node1.weight)
             IDn = node1.firstCandidateSorted[seqNum]
             while not IDn in Insequence:
                 node1.firstCandidateSorted.remove(IDn)  #候选集中去除IDn
                 if not node1.firstCandidateSorted:  #ID1候选集为空后,令IDn=None,ID1将在下一次被选中时进入if node1.firstCandidateSorted同级的else中
                     IDn = None
                     break
                 else:
                     # 重新用ftsample依概率选择vj
                     IDn = node1.firstCandidateSorted[
                         ftSample.getPrizeIndex(node1.firstCandidateSorted,
                                                node1.weight)]
             if not IDn == None:
                 edge = ID1 + " " + IDn
                 node1.Prob_AdjList.append(IDn)
                 Outsequence.remove(ID1)
                 Insequence.remove(IDn)
                 node1.firstCandidateSorted.remove(IDn)
                 self.edges.append(edge)
         else:
             loop1.add(ID1)
             # 选择IDn
             IDn = random.choice(Insequence)
             loop2.add(IDn)
             # 若ID1与IDn是同一个点,或者ID1与IDn已经练成边,重新选择IDn
             while ID1 == IDn or IDn in node1.Prob_AdjList:
                 IDn = random.choice(Insequence)
                 loop2.add(IDn)
                 if loop2 | set(Insequence) == loop2:
                     IDn = None
                     break
             if not IDn == None:
                 edge = ID1 + " " + IDn
                 loop1.remove(ID1)
                 node1.Prob_AdjList.append(IDn)
                 Outsequence.remove(ID1)
                 Insequence.remove(IDn)
                 # 加入边集
                 self.edges.append(edge)
             loop2.clear()
     # print "edges,",self.edges
     print "sum of edges:" + str(self.edges.__len__())
     futil = FileUtil()
     futil.writeTextFile(DATASET_PATH + dataSet + ".edges", self.edges)
Beispiel #10
0
 def generateUDLink(self, DATASET_PATH, dataSet):
     # 全体用户集
     userSet = self.ul.AllUser()
     sequence = []
     if self.sumLength % 2 == 1:
         print "after perturbing ,sum of degree is odd,deal..."
         for i in userSet:
             if self.ul.getUser(i).o_degree > 1:
                 self.ul.getUser(
                     i).o_degree = self.ul.getUser(i).getODegree() - 1
                 break
     # 按照出度倍将用户加入sequence中
     for user in userSet:
         for num in range(0, self.ul.getUser(user).getODegree()):
             sequence.append(user)
     print "generating undirecedgraph"
     # 可能死循环的外层循环试过的点
     loop1 = set()
     # 可能死循环的内层循环试过的点
     loop2 = set()
     while sequence:
         if loop1 | set(sequence) == loop1:
             print "step into endless loop"
             self.newdealUD(sequence)
             break
         # 选择ID1
         ID1 = random.choice(sequence)
         node1 = self.ul.getUser(ID1)
         if node1.firstCandidateSorted:
             # 从候选集中选择IDn
             ftSample = FtSample()
             seqNum = ftSample.getPrizeIndex(node1.firstCandidateSorted,
                                             node1.weight)
             IDn = node1.firstCandidateSorted[seqNum]
             # 若IDn不在sequence中,重新选择IDn
             while not (IDn in sequence):
                 node1.firstCandidateSorted.remove(IDn)
                 if not node1.firstCandidateSorted:
                     IDn = None
                     break
                 else:
                     IDn = node1.firstCandidateSorted[
                         ftSample.getPrizeIndex(node1.firstCandidateSorted,
                                                node1.weight)]
             noden = self.ul.getUser(IDn)
             if not IDn == None:
                 edge = ID1 + " " + IDn
                 node1.Prob_AdjList.append(IDn)
                 noden.Prob_AdjList.append(ID1)
                 sequence.remove(ID1)
                 sequence.remove(IDn)
                 node1.firstCandidateSorted.remove(IDn)
                 noden.firstCandidateSorted.remove(ID1)
                 self.edges.append(edge)
         else:
             loop1.add(ID1)
             IDn = random.choice(sequence)
             noden = self.ul.getUser(IDn)
             loop2.add(IDn)
             # 如果ID1是IDn,或者IDn已经和ID1连成边,则重新选点
             while (ID1 == IDn) or (IDn in node1.Prob_AdjList) or (
                     ID1 in noden.Prob_AdjList):
                 IDn = random.choice(sequence)
                 noden = self.ul.getUser(IDn)
                 loop2.add(IDn)
                 if loop2 | set(sequence) == loop2:
                     IDn = None
                     break
             if not IDn == None:
                 edge = ID1 + " " + IDn
                 loop1.remove(ID1)
                 node1.Prob_AdjList.append(IDn)
                 noden.Prob_AdjList.append(ID1)
                 sequence.remove(ID1)
                 sequence.remove(IDn)
                 self.edges.append(edge)
             #     不论是否生成边,都需要清空loop2
             loop2.clear()
         # print "edges",self.edges
     # print "剩余sequence", sequence
     # #后处理
     # # 根据edge生成gra判断连通性
     # gra = nx.Graph()
     # tuedge=[]
     # for i in self.edges:
     #     ea = i.split()
     #     a = (ea[0],ea[1])
     #     tuedge.append(a)
     # gra.add_edges_from(tuedge)#处理后边的顺序会乱,不按生成顺序
     # # el=list(gra.edges())
     # # print gra.edges()
     # # print "edges",self.edges
     # print "进行后处理的边",tuedge
     #
     # # tuedge.reverse()
     # # print tuedge
     # if (nx.is_connected(gra)):
     #     print "connected graph"
     # else:
     #     print "unconnected graph,begin postprocessing"
     #     self.edges=self.postProcessing(tuedge)
     print "sum of edges:", self.edges.__len__()
     futil = FileUtil()
     futil.writeTextFile(DATASET_PATH + dataSet + ".edges", self.edges)
Beispiel #11
0
        parserDict.pop('lineno')
        parserDict.pop('offset')
        parserDict.pop('cdata_elem')
        parserDict.pop('rawdata')
        parserDict.pop('_HTMLParser__starttag_text')
        parserDict.pop('index')
        # parserDict['buyNumStart'] = "100.00"
        # parserDict['buyNumEnd'] = "0.00"
    except Exception, e:
        log.log(e.message)

    # 当前最新数据
    parserJson = json.dumps(parserDict)

    # 本地最近数据
    fileUtil = FileUtil.FileUtil()
    newStock = fileUtil.readNewStock()
    jsonToDict = json.loads(newStock)
    myNewParser = MyHTMLParser()
    myNewParser.__dict__ = jsonToDict

    # 两次时间比较,如果不相同,则下单
    if myNewParser.buyTime != htmlParser.buyTime:
        log.log(
            '================================================================================================================'
        )
        mOrderUtill = myOrder.MyOrder()
        var = var + 1
        # 开始新的交易,卖出还是买入
        if htmlParser.buyNumStart > htmlParser.buyNumEnd:  # 卖出
            # 计算卖出%比,取出目前此股票持仓,按百分比计算卖出的数目,卖出
Beispiel #12
0
import urllib
from UrlUtil import *
from Util import *
from FileUtil import *
import HTMLParser
import logging
import logging.config

logging.config.fileConfig("../config/logging.conf")
logger = logging.getLogger()
# url = 'http://img.newyx.net/tj/201702/28/798fac1681.jpg'
# host = 'www_newyx_net'

url = ''
host = ''
if __name__ == '__main__':
    url = sys.argv[1]
    host = sys.argv[2]

if not url == '':
    u = UrlUtil(url)
    util = Util(url)
    file = FileUtil(host)
    main_md5 = util.getMd5(u.host)
    fall_md5 = util.getMd5(u.path_suffix)
    dir = file.image
    urllib.urlretrieve(url, file.image + util.converUrl(url))
    logger.info('success=>' + url)
else:
    logger.error('err=>没有图片路径' + url)
Beispiel #13
0
 def log(self, msg):
     futil = FileUtil()
     futil.writeToFile("log.txt", msg, True, True)
Beispiel #14
0
def update(nextPeriod, driversFileName, vehiclesFileName, servicesFileName,
           reservationsFileName):
    """Obtains the planning for a period of activity.
    Requires:
    nextPeriod is a str from the set 0911, 1113, ..., 1921 indicating the
    2 hour period to be planned;
    driversFileName is a str with the name of a .txt file containing a list
    of drivers organized as in the examples provided;
    vehiclesFileName is a str with the name of a .txt file containing a list
    of vehicles organized as in the examples provided;
    servicesFileName is a str with the name of a .txt file containing a list
    of services organized as in the examples provided;
    reservationsFileName is a str with the name of a .txt file containing
    a list of reserved services organized as in the examples provided;
    the files whose names are driversFileName, vehiclesFileName,
    servicesFileName and reservationsFileName concern the same company and
    the same day;
    the file whose name is reservationsFileName concerns the period
    indicated by nextPeriod;
    the files whose names are driversFileName, vehiclesFileName,
    servicesFileName concern the period immediately preceding the period
    indicated by nextPeriod;
    the file name reservationsFileName ends (before the .txt extension) with
    the string nextPeriod;
    the file names driversFileName, vehiclesFileName and servicesFileName
    end (before their .txt extension) with the string representing
    the period immediately preceding the one indicated by nextPeriod,
    from the set 0709, 0911, ..., 1719;
    Ensures:
    writing of .txt file containing the updated list of services for
    the period nextPeriod according to the requirements in the general
    specifications provided (omitted here for the sake of readability);
    the name of that file is outputXXYY.txt where XXYY represents
    the nextPeriod.
    """

    if checkPreConditions(nextPeriod, driversFileName, vehiclesFileName,
                          servicesFileName, reservationsFileName):

        file_name = "output" + nextPeriod

        header = FileUtil(servicesFileName).createNewHeader(nextPeriod)

        drivers = DriversDict(driversFileName)
        vehicles = VehiclesDict(vehiclesFileName)
        services = ServicesList(servicesFileName)
        reservations = ReservationsList(reservationsFileName)

        # 1st period
        if nextPeriod == "0911" and ("0911" in reservationsFileName):
            tempServices = ServicesList()
            tempServices.emptyServices(drivers, vehicles)
            waiting4services = DetailedServicesList(drivers, vehicles,
                                                    tempServices)
        else:
            waiting4services = DetailedServicesList(drivers, vehicles,
                                                    services)

        new_services = waiting4services.updateServices(reservations)

        new_services.writeServicesFile(file_name, header)

    else:
        raise IOError('File names and/or headers not consistent.')