示例#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
文件: test_php.py 项目: dnet/ppcg
	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
文件: test_php.py 项目: dnet/ppcg
	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
文件: test_php.py 项目: dnet/ppcg
	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
文件: test_php.py 项目: dnet/ppcg
	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
文件: test_php.py 项目: dnet/ppcg
	def test_echo(self):
		php = PHP()
		php.echo(self.TEST_STRING)
		self.assertEquals(str(php), self.TEST_STRING)
示例#22
0
文件: test_php.py 项目: dnet/ppcg
	def test_error(self):
		php = PHP()
		php.nonexistant_method()
		with self.assertRaises(PhpError):
			str(php)
示例#23
0
文件: test_php.py 项目: dnet/ppcg
	def test_null(self):
		php = PHP()
		php.var_dump(None)
		self.assertEquals(str(php), 'NULL\n')
示例#24
0
文件: test_php.py 项目: dnet/ppcg
	def test_number(self):
		php = PHP()
		php.var_dump(5)
		self.assertEquals(str(php), 'int(5)\n')
示例#25
0
文件: test_php.py 项目: dnet/ppcg
	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
文件: test_php.py 项目: dnet/ppcg
	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
文件: test_php.py 项目: dnet/ppcg
	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()