示例#1
0
def test_defaults():
    """Test default conversions."""
    data = [
        ('1m', [
            C(1, 'meter', 100, 'centimeter', '[length]'),
            C(1, 'meter', 0.001, 'kilometer', '[length]')
        ]),
        ('100g', [
            C(100, 'gram', 0.1, 'kilogram', '[mass]'),
            C(100, 'gram', 100000, 'milligram', '[mass]')
        ]),
    ]

    wf = Workflow3()
    if 'default_units' not in wf.settings:
        wf.settings['default_units'] = {}

    wf.settings['default_units']['[length]'] = ['centimeter', 'kilometer']
    wf.settings['default_units']['[mass]'] = ['kilogram', 'milligram']

    c = convert.Converter(Defaults(wf))

    for t in data:
        i = c.parse(t[0])
        res = c.convert(i)
        assert len(res) == len(t[1])
        for j, r in enumerate(res):
            log.debug(r)
            verify_conversion(t[1][j], r)
def setup():
    if CONFIG["converter"] == None:
        CONFIG["converter"] = convert.Converter()

    if CONFIG["account"] == None:
        CONFIG["account"] = account.Account()

    if CONFIG["environment"] == None:
        CONFIG["environment"] = Environment(loader=FileSystemLoader(\
                                searchpath="./WebPages"))
示例#3
0
def test_conversion():
    """Test conversions."""
    data = [
        ('1km m', C(1, 'kilometer', 1000, 'meter', '[length]')),
    ]
    c = convert.Converter(None)
    for t in data:
        i = c.parse(t[0])
        res = c.convert(i)
        verify_conversion(t[1], res[0])
示例#4
0
def test_valid():
    """Test valid input."""
    data = [
        ('1.3 km', T(1.3, '[length]', 'kilometer', None)),
        ('1.3 km miles', T(1.3, '[length]', 'kilometer', 'mile')),
        ('5 m/s kph', T(5.0, '[length] / [time]', 'meter/second', 'kph')),
        ('21.3 m^2 acres', T(21.3, '[length] ** 2', u'meter²', 'acre')),
    ]
    c = convert.Converter(None)
    for t in data:
        i = c.parse(t[0])
        verify_parsed(t[1], i)
示例#5
0
def test_invalid():
    """Test invalid input."""
    queries = [
        'dave',  # doesn't start with a number
        '1.3',  # no unit
        '5 daves',  # invalid units
        '10 km m cm',  # too many units
    ]
    c = convert.Converter(None)
    for query in queries:
        with pytest.raises(ValueError):
            c.parse(query)
示例#6
0
def processTestSet():
	testPieces = {} #{composer name: piece object}

	currdir = os.path.dirname(__file__) #the absolute filepath to the directory containing main.py
	folderpath = "test_set" #the subdirectory containing our training data
	finalPath = os.path.join(currdir, folderpath) #concatenate the two paths for use

	for composer in os.listdir(finalPath): #test_set is full of folders corresponding to composers; inside is all pieces of theirs to train with
		testPieces[composer] = []
		if (composer != ".DS_Store"):
			nextPath = os.path.join(finalPath, composer) #the path to this composer folder
			if (os.path.isdir(nextPath)): #if this is a folder
				for testPiece in os.listdir(nextPath): #loop through all pieces by the corresponding composer
					if testPiece.endswith(".mxl"): #if the file is .mxl format (it should be)
						piecePath = os.path.join(nextPath, testPiece)
						C = convert.Converter(piecePath)
						C.convert() #convert the file
						toAdd = piece.Piece(C.getBass(), C.getChords())
						testPieces[composer].append(toAdd)
	return testPieces
    def convert(self, path):
        path = str(path)
        dbpath, idprove = path.split('|')
        outdir = os.path.dirname(dbpath)
        outdir = os.path.join(outdir, 'm4')
        if not os.path.exists(outdir):
            os.mkdir(outdir)
        self.outdir = outdir

        self.force = self.doForceData.isChecked()
        fi = self.doForceImages.currentIndex()

        if fi == 0:
            self.keep_img = True
            self.img = True
        elif fi == 1:
            self.img = False
            self.keep_img = True
        elif fi == 2:
            self.img = True
            self.keep_img = False
        self.pid = 'Converting to misura format: ' + idprove

        self.converter = convert.Converter(dbpath, self.outdir)
        self.outpath = self.converter.get_outpath(idprove,
                                                  img=self.img,
                                                  keep_img=self.keep_img)

        self.connect(registry.tasks, QtCore.SIGNAL('sig_done(QString)'),
                     self.done)
        run = widgets.RunMethod(self.converter.convert, frm=self.format)
        run.step = 100
        run.pid = self.pid
        QtCore.QThreadPool.globalInstance().start(run)
        self.progress_timer = QtCore.QTimer(self)
        self.progress_timer.setInterval(300)
        self.connect(self.progress_timer, QtCore.SIGNAL('timeout()'),
                     self.update_progress)
        self.progress_timer.start()
示例#8
0
def train(n):
	learners = {} #{composer name: NGLearner object}

	currdir = os.path.dirname(__file__) #the absolute filepath to the directory containing main.py
	folderpath = "mxl_files" #the subdirectory containing our training data
	finalPath = os.path.join(currdir, folderpath) #concatenate the two paths for use

	for composer in os.listdir(finalPath): #mxl_files is full of folders corresponding to composers; inside is all pieces of theirs to train with
		if (composer != ".DS_Store"): 
			print("checking composer ", composer)
			nextPath = os.path.join(finalPath, composer) #the path to this composer folder

			composerFB = NGramModel.N_Gram_Model(n, True) #the learner for figured bass
			composerChords = NGramModel.N_Gram_Model(n, False) #the learner for chords

			if (os.path.isdir(nextPath)): #if this is a folder
				print("in folder")
				for piece in os.listdir(nextPath): #loop through all pieces by the corresponding composer
				    print("checking piece", piece)
				    if piece.endswith(".mxl"): #if the file is .mxl format (it should be)
				    	piecePath = os.path.join(nextPath, piece)
				    	C = convert.Converter(piecePath)
				    	try:
				    		C.convert() #convert the file
				    	except music21.musicxml.xmlToM21.MusicXMLImportException:
				    		print("Error: %s could not be parsed.\n" % (piece))
				    		continue

				    	composerFB.train(C.getBass()) #train on this piece's figured bass for this composer
				    	composerChords.train(C.getChords()) #train on this piece's chords for this composer

			print("creating learner object for composer", composer)
			
			toAdd = NGLearner.learner(composerFB, composerChords) #create the learner object
			print("adding learner")
			learners[composer] = toAdd #add it to the learners dictionary
		
	return learners
示例#9
0
 def testCheckAccessOk(self, mock_access, mock_isfile):
     converter = convert.Converter([self.INPUT_PY])
     AssertThat(converter._Check()).IsTrue()
     AssertThat(mock_isfile).WasCalled().Once().With(self.INPUT_PY)
     AssertThat(mock_access).WasCalled().With(self.INPUT_PY, os.R_OK)
     AssertThat(mock_access).WasCalled().LastWith(self.INPUT_PY, os.W_OK)
示例#10
0
 def testCheckUnwritableInputFile(self, mock_access, mock_isfile):
     converter = convert.Converter([self.INPUT_PY])
     AssertThat(converter._Check()).IsFalse()
     AssertThat(mock_isfile).WasCalled().Once().With(self.INPUT_PY)
     AssertThat(mock_access).WasCalled().With(self.INPUT_PY, os.R_OK)
     AssertThat(mock_access).WasCalled().LastWith(self.INPUT_PY, os.W_OK)
示例#11
0
 def testCheckNonexistentInputFile(self, mock_isfile):
     converter = convert.Converter([self.INPUT_PY])
     AssertThat(converter._Check()).IsFalse()
     AssertThat(mock_isfile).WasCalled().Once().With(self.INPUT_PY)
示例#12
0
def main():
    print("Online Novel to PDF")

    title = input("Title of PDF: ")

    address = input("Address of first page: ")
    index = int(input("Incrementing index: "))
    #parse Address
    parts = address.split(str(index))
    prefix = parts[0]
    suffix = parts[1]

    titleKeyword = input("Keyword in Title \
    (To make sure only scraping what is needed): ")

    address = prefix + str(index) + suffix
    scraper = scrape.Scraper(address)

    tag = ''
    attrs = ''

    if input("Do you know which HTML container the content resides in? "
             ) == 'yes':
        tag = input("Element type: ")
        attrName = input("Element Atrribute Name: ")
        attrValue = input("Element Attribute Value: ")
        attrs = {attrName: attrValue}
    else:
        print(
            "You will be shown the text of each of the elements on the page. If \
        the text matches the content you are looking to scrape, type in yes. \
        If not, Just press enter.")
        elems = scraper.getAllElements()
        for i in range(2, len(elems)):
            content = elems[i].text
            if content != "":
                print(elems[i].text)
                if input("Is this correct?") == 'yes':
                    #Find element attributes
                    elem = elems[i]
                    tag = elem.name
                    attrs = elem.attrs
                    break

    if tag == '':
        print("No element selected. Exiting.")
        exit()

    pdf = convert.Converter()

    text = scraper.getText(tag, attrs)
    pdf.addText(text)
    print(text)
    index += 1
    address = prefix + str(index) + suffix
    scraper.goTo(address)

    while titleKeyword in scraper.getText('title'):
        text = scraper.getText(tag, attrs)
        pdf.addText(text)
        print(text)
        index += 1
        address = prefix + str(index) + suffix
        scraper.goTo(address)

    pdf.printPDF(title)
    print("done!")