Пример #1
0
 def test_new(self):
     php = PHP()
     dt = php.new('DateTime')
     ts = dt.getTimestamp()
     php.echo(ts)
     output = str(php)
     self.assertAlmostEquals(int(output), time(), delta=10)
Пример #2
0
	def test_new(self):
		php = PHP()
		dt = php.new('DateTime')
		ts = dt.getTimestamp()
		php.echo(ts)
		output = str(php)
		self.assertAlmostEquals(int(output), time(), delta=10)
Пример #3
0
def fuzzymatch(string1):
    #note:  fuzzymatch.php must be in php path, e.g.  /usr/lib/php/!!!
    #put in a cron job that runs every half hour for new entries?
    
    entities = Session.query(Entity)
    
    
    matches = []
    
    ##string1 = string1.decode('utf8')
    
    for entity in entities:
        php = PHP("require 'fuzzymatch.php';")
        #php = PHP()
        #print "testing " + entity.label.encode('utf8') + " against " + string1.encode('utf8') + "\n"
        
        code = '$string1 = utf8_decode("' + string1.encode('utf8') + '");'
        
        #code = code + "$string2 = '" + entity.label.encode('latin-1', 'replace') + "';"
        #code = code + "print $string1; print $string2;"
        #print code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");'
        code = code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");'
        code = code + """print fuzzy_match($string1, $string2, 2);"""
        
        verdict = php.get_raw(code)
        #print "verdict is " + verdict + "\n"
    
        if float(verdict)>=.5:
            #print entity.label + " is a match!\n"
            entity.matchvalue = verdict
            matches.append(entity)
    
    return matches
Пример #4
0
def fuzzymatch(string1):
    #note:  fuzzymatch.php must be in php path, e.g.  /usr/lib/php/!!!
    #put in a cron job that runs every half hour for new entries?

    entities = Session.query(Entity)

    matches = []

    ##string1 = string1.decode('utf8')

    for entity in entities:
        php = PHP("require 'fuzzymatch.php';")
        #php = PHP()
        #print "testing " + entity.label.encode('utf8') + " against " + string1.encode('utf8') + "\n"

        code = '$string1 = utf8_decode("' + string1.encode('utf8') + '");'

        #code = code + "$string2 = '" + entity.label.encode('latin-1', 'replace') + "';"
        #code = code + "print $string1; print $string2;"
        #print code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");'
        code = code + '$string2 = utf8_decode("' + entity.label.encode(
            'utf8') + '");'
        code = code + """print fuzzy_match($string1, $string2, 2);"""

        verdict = php.get_raw(code)
        #print "verdict is " + verdict + "\n"

        if float(verdict) >= .5:
            #print entity.label + " is a match!\n"
            entity.matchvalue = verdict
            matches.append(entity)

    return matches
Пример #5
0
def fuzzymatchtest(string1, string2):
    #note:  fuzzymatch.php must be in php path, e.g.  /usr/lib/php/!!!
    php = PHP("require 'fuzzymatch.php';")
    #php = PHP()

    code = "$string1 = '" + string1 + "';"
    code = code + "$string2 = '" + string2.encode('latin-1', 'replace') + "';"
    #code = code + "print $string1; print $string2;"
    code = code + """print fuzzy_match($string1, $string2, 2);"""

    return php.get_raw(code)
Пример #6
0
def fuzzymatchtest(string1, string2):
    #note:  fuzzymatch.php must be in php path, e.g.  /usr/lib/php/!!!
    php = PHP("require 'fuzzymatch.php';")
    #php = PHP()
    
    code = "$string1 = '" + string1 + "';"
    code = code + "$string2 = '" + string2.encode('latin-1', 'replace') + "';"
    #code = code + "print $string1; print $string2;"
    code = code + """print fuzzy_match($string1, $string2, 2);"""
    
    return php.get_raw(code)
Пример #7
0
 def test_dict(self):
     php = PHP()
     values = php.array_values(self.TEST_DICT)
     php.sort(values)
     php.echo(php.implode(',', values))
     expected = ','.join(sorted(self.TEST_DICT.itervalues()))
     self.assertEquals(str(php), expected)
Пример #8
0
 def test_dyn_getitem(self):
     php = PHP()
     array_params = range(self.TEST_RANGE)
     array = php.array(*array_params)
     php.shuffle(array)
     php.echo(array[array[0]])
     self.assertIn(int(str(php)), array_params)
Пример #9
0
	def test_dict(self):
		php = PHP()
		values = php.array_values(self.TEST_DICT)
		php.sort(values)
		php.echo(php.implode(',', values))
		expected = ','.join(sorted(self.TEST_DICT.itervalues()))
		self.assertEquals(str(php), expected)
Пример #10
0
def main():
	php = PHP()
	php.include('tcpdf/config/lang/eng.php')
	php.include('tcpdf/tcpdf.php')

	pdf = php.new('TCPDF', 'P', 'mm', 'A4', True, 'UTF-8')
	
	pdf.setFontSubsetting(False)
	pdf.setAuthor('VSzA')
	pdf.setCreator('PPCG')
	pdf.setTitle('TCPDF in Python!?')
	pdf.setPrintHeader(False)
	pdf.setMargins(10, 10)

	pdf.AddPage()
	pdf.SetFont('dejavusans', 'B', 12)
	pdf.Cell(0, 0, 'Hello Python')
	pdf.Ln()

	x = pdf.GetX()
	y = pdf.GetY()
	pdf.setXY(30, 30)
	pdf.Cell(0, 0, 'GOTOs are bad')
	pdf.setXY(x, y + 2.5)
	pdf.Cell(0, 0, 'I can has PHP variables')

	pdf.Output()
	pdf_data = str(php)

	with file('output.pdf', 'wb') as pdf_file:
		pdf_file.write(pdf_data)
	print '{0} bytes of PDF have been written'.format(len(pdf_data))
Пример #11
0
	def test_dyn_getitem(self):
		php = PHP()
		array_params = range(self.TEST_RANGE)
		array = php.array(*array_params)
		php.shuffle(array)
		php.echo(array[array[0]])
		self.assertIn(int(str(php)), array_params)
Пример #12
0
 def test_repr(self):
     php = PHP()
     self.assertEquals(repr(php), '<PHP object with 0 statement(s)>')
Пример #13
0
 def test_list(self):
     php = PHP()
     php.echo(php.count(range(self.TEST_RANGE)))
     output = str(php)
     self.assertEquals(output, str(self.TEST_RANGE))
Пример #14
0
 def test_number(self):
     php = PHP()
     php.var_dump(5)
     self.assertEquals(str(php), 'int(5)\n')
Пример #15
0
 def test_deep_compose(self):
     php = PHP()
     php.echo(php.strlen(php.strrev(self.TEST_STRING)))
     output = str(php)
     self.assertEquals(output, str(len(self.TEST_STRING)))
Пример #16
0
def fuzzymatchall(SEPEntrieslist):
    #takes outputs from addlist() and saves all fuzzy match IDs to SEPEntry.fuzzymatch with verdicts (percent of words matched)
    #now change so that it only updates ones that don't currently have a fuzzymatchlist
    
    #clear out fuzzymatch table--otherwise old fuzzies will accumulate, and nobody wants that
    delquery = Session.query(Fuzzymatch)
    delquery.delete()
    Session.flush()
    Session.commit()
    
    
    for SEPEntry in SEPEntrieslist:
            print "working on " + SEPEntry.title.encode('utf-8') + "\n"
            entities = Session.query(Entity)
            
            #exclude journals and nodes from fuzzy matching
            entities = entities.filter(Entity.typeID != 2)
            entities = entities.filter(Entity.typeID != 4)
            
            #reset fuzzymatches for that entry
            #SEPEntry.fuzzymatches = ""
    
            
            ##string1 = string1.decode('utf8')
            
            for entity in entities:
                php = PHP("set_include_path('/usr/lib/php/');")
                php = PHP("require 'fuzzymatch.php';")
                #php = PHP()
                #print "testing " + entity.label.encode('utf8') + " against " + string1.encode('utf8') + "\n"
                
                code = '$string1 = utf8_decode("' + SEPEntry.title.encode('utf8') + '");'
                
                #code = code + "$string2 = '" + entity.label.encode('latin-1', 'replace') + "';"
                #code = code + "print $string1; print $string2;"
                #print code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");'
                code = code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");'
                code = code + """print fuzzy_match($string1, $string2, 2);"""
                
                verdict = php.get_raw(code)
                #print "verdict is " + verdict + "\n"
                verdict = verdict.split(',')
            
                if float(verdict[0])>=.20:
                    #print entity.label + " is a match!\n"
                    #entity.matchvalue = verdict
                    #string = SEPEntry.fuzzymatches + "|" + str(entity.ID) + "," + verdict
                    
                    #if len(string) < 400:
                    #    SEPEntry.fuzzymatches = SEPEntry.fuzzymatches + "|" + str(entity.ID) + "," + verdict
                    #else:
                    #    print "sorry, too many matches!  Can't add " + str(entity.ID) + " to fuzzy matches; over 400 chars."
                    fmatch = Fuzzymatch(entity.ID)
                    fmatch.sep_dir = SEPEntry.sep_dir
                    fmatch.strength = verdict[0]
                    fmatch.edits = verdict[1]
                    
                    SEPEntry.fmatches.append(fmatch)
                    
                    
            Session.flush()
            Session.commit()
Пример #17
0
 def test_null(self):
     php = PHP()
     php.var_dump(None)
     self.assertEquals(str(php), 'NULL\n')
Пример #18
0
 def test_echo(self):
     php = PHP()
     php.echo(self.TEST_STRING)
     self.assertEquals(str(php), self.TEST_STRING)
Пример #19
0
 def test_error(self):
     php = PHP()
     php.nonexistant_method()
     with self.assertRaises(PhpError):
         str(php)
Пример #20
0
	def test_add(self):
		php = PHP()
		array = php.range(1, self.TEST_RANGE)
		php.echo(php.count(array) + self.TEST_VALUE)
		self.assertEquals(str(php), str(self.TEST_VALUE + self.TEST_RANGE))
Пример #21
0
	def test_echo(self):
		php = PHP()
		php.echo(self.TEST_STRING)
		self.assertEquals(str(php), self.TEST_STRING)
Пример #22
0
	def test_error(self):
		php = PHP()
		php.nonexistant_method()
		with self.assertRaises(PhpError):
			str(php)
Пример #23
0
	def test_null(self):
		php = PHP()
		php.var_dump(None)
		self.assertEquals(str(php), 'NULL\n')
Пример #24
0
	def test_number(self):
		php = PHP()
		php.var_dump(5)
		self.assertEquals(str(php), 'int(5)\n')
Пример #25
0
	def test_list(self):
		php = PHP()
		php.echo(php.count(range(self.TEST_RANGE)))
		output = str(php)
		self.assertEquals(output, str(self.TEST_RANGE))
Пример #26
0
	def test_deep_compose(self):
		php = PHP()
		php.echo(php.strlen(php.strrev(self.TEST_STRING)))
		output = str(php)
		self.assertEquals(output, str(len(self.TEST_STRING)))
Пример #27
0
 def test_getitem(self):
     php = PHP()
     array_params = range(self.TEST_RANGE)
     array = php.array(*array_params)
     php.echo(array[0])
     self.assertEquals(str(php), str(array_params[0]))
Пример #28
0
	def test_getitem(self):
		php = PHP()
		array_params = range(self.TEST_RANGE)
		array = php.array(*array_params)
		php.echo(array[0])
		self.assertEquals(str(php), str(array_params[0]))
Пример #29
0
 def test_add(self):
     php = PHP()
     array = php.range(1, self.TEST_RANGE)
     php.echo(php.count(array) + self.TEST_VALUE)
     self.assertEquals(str(php), str(self.TEST_VALUE + self.TEST_RANGE))
Пример #30
0
def fuzzymatchall(SEPEntrieslist):
    #takes outputs from addlist() and saves all fuzzy match IDs to SEPEntry.fuzzymatch with verdicts (percent of words matched)
    #now change so that it only updates ones that don't currently have a fuzzymatchlist

    #clear out fuzzymatch table--otherwise old fuzzies will accumulate, and nobody wants that
    delquery = Session.query(Fuzzymatch)
    delquery.delete()
    Session.flush()
    Session.commit()

    for SEPEntry in SEPEntrieslist:
        print "working on " + SEPEntry.title.encode('utf-8') + "\n"
        entities = Session.query(Entity)

        #exclude journals and nodes from fuzzy matching
        entities = entities.filter(Entity.typeID != 2)
        entities = entities.filter(Entity.typeID != 4)

        #reset fuzzymatches for that entry
        #SEPEntry.fuzzymatches = ""

        ##string1 = string1.decode('utf8')

        for entity in entities:
            php = PHP("set_include_path('/usr/lib/php/');")
            php = PHP("require 'fuzzymatch.php';")
            #php = PHP()
            #print "testing " + entity.label.encode('utf8') + " against " + string1.encode('utf8') + "\n"

            code = '$string1 = utf8_decode("' + SEPEntry.title.encode(
                'utf8') + '");'

            #code = code + "$string2 = '" + entity.label.encode('latin-1', 'replace') + "';"
            #code = code + "print $string1; print $string2;"
            #print code + '$string2 = utf8_decode("' + entity.label.encode('utf8') + '");'
            code = code + '$string2 = utf8_decode("' + entity.label.encode(
                'utf8') + '");'
            code = code + """print fuzzy_match($string1, $string2, 2);"""

            verdict = php.get_raw(code)
            #print "verdict is " + verdict + "\n"
            verdict = verdict.split(',')

            if float(verdict[0]) >= .20:
                #print entity.label + " is a match!\n"
                #entity.matchvalue = verdict
                #string = SEPEntry.fuzzymatches + "|" + str(entity.ID) + "," + verdict

                #if len(string) < 400:
                #    SEPEntry.fuzzymatches = SEPEntry.fuzzymatches + "|" + str(entity.ID) + "," + verdict
                #else:
                #    print "sorry, too many matches!  Can't add " + str(entity.ID) + " to fuzzy matches; over 400 chars."
                fmatch = Fuzzymatch(entity.ID)
                fmatch.sep_dir = SEPEntry.sep_dir
                fmatch.strength = verdict[0]
                fmatch.edits = verdict[1]

                SEPEntry.fmatches.append(fmatch)

        Session.flush()
        Session.commit()