Example #1
0
def main():
    """
    Builds the dictionaries and the overcomplete word embeddings
    Usage:
        python preprocessing.py -input <original_embs_file> -output <overcomp_file> -factor <factor_overcomplete>
                                -bin <binary_file>
        
    <original_embs_file>: the original word embeddings is used to learn denoising
    <overcomp_file>: the file name of overcomplete word embeddings
    <factor_overcomplete>: a factor of overcomplete embeddings length (=factor * length of original word embeddings)
    <binary_file>: 1 for binary; 0 for text
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('-input', type=str)
    parser.add_argument('-output', type=str)
    parser.add_argument('-factor', type=int, default=10)
    parser.add_argument('-bin', type=int, default=1)
    args = parser.parse_args()
    
    vocab, vecs = read_file(args.input, binary=args.bin)
    dict_comp = learn_dict(vecs.T, factor=1)
    dict_overcomp = learn_dict(vecs.T, factor=args.factor)
    dim_over = args.factor * len(vecs[0])
    vecs_overcomp = overcomplete_embs(vecs.T, dim_over)
    np.save(args.input + '.dict_comp', dict_comp)
    np.save(args.input + '.dict_overcomp', dict_overcomp)
    save_file(args.output, vocab, vecs_overcomp.T, binary=args.bin)
    print 'Preprocessing done!'
Example #2
0
def finalize():
    """\
    Code generator finalization function.
    """
    # write the code for every toplevel object
    for obj in xrc_objects.itervalues():
        obj.write(output_file, 1)
    output_file.write('</resource>\n')
    #output_file.close()
    # store the contents to file
    common.save_file(output_file_name, output_file.getvalue(), 'codegen')
Example #3
0
def finalize():
    """\
    Code generator finalization function.
    """
    # write the code for every toplevel object
    for obj in xrc_objects.itervalues():
        obj.write(output_file, 1)
    output_file.write('</resource>\n')
    #output_file.close()
    # store the contents to file
    common.save_file(output_file_name, output_file.getvalue(), 'codegen')
Example #4
0
def _gen_logic(logic_lib_dir, t, fields):
    className = '%sLogic' % (formatTableName(t))

    li = []
    li.append('<?php')
    li.append('class %s {' % (className))

    li.append('}')
    li.append('?>')

    lfile = '%s%s.inc.gen' % (logic_lib_dir, className)
    save_file(lfile, li)
Example #5
0
 def _save_app(self, filename):
     try:
         from cStringIO import StringIO
         buffer = StringIO()
         common.app_tree.write(buffer)
         common.save_file(filename,
                          buffer.getvalue(), 'wxg')
     except (IOError, OSError), msg:
         common.app_tree.app.saved = False
         fn = filename
         wx.MessageBox(_("Error saving app:\n%s") % msg, _("Error"),
                      wx.OK|wx.CENTRE|wx.ICON_ERROR)
Example #6
0
def _gen_controller(control_dir, t, fields):
    className = '%sController' % (formatTableName(t))

    li = []
    li.append('<?php')
    li.append('class %s {' % (className))

    li.append('}')
    li.append('?>')

    lfile = '%s%s.inc.gen' % (control_dir, className)
    save_file(lfile, li)
Example #7
0
 def _save_app(self, filename):
     try:
         from cStringIO import StringIO
         buffer = StringIO()
         common.app_tree.write(buffer)
         common.save_file(filename, buffer.getvalue(), 'wxg')
     except (IOError, OSError), msg:
         common.app_tree.app.saved = False
         fn = filename
         wx.MessageBox(
             _("Error saving app:\n%s") % msg, _("Error"),
             wx.OK | wx.CENTRE | wx.ICON_ERROR)
Example #8
0
def get_sentences(dir):
    parent_dir = os.path.abspath(os.path.dirname(
        os.getcwd()))  #C:\Users\jee_s\Desktop\助研\中文语料处理
    orgDir = os.path.join(parent_dir, "摘要文件")
    toDir = os.path.join(parent_dir, dir)
    common.mkdir(toDir)
    all_dirs, all_files, all_names = common.getdir(orgDir)
    for i in all_files[1:]:  #所有摘要文件
        print(i)
        filename = os.path.basename(i)
        # print(os.path.splitext(i))
        sents = splitSentence(i)
        common.save_file(sents, os.path.join(toDir, filename))
Example #9
0
def gen_parts(parts_dir):
    # header,footer,sidebar
    tables = load_tables()
    for t in tables:
        className = '%sPage' % (formatTableName(t))
        fields = load_fields(t)
        li = []
        li.append('<?php')

        li.append('?>')

        lfile = '%s%s.inc.gen' % (parts_dir, className)
        save_file(lfile, li)
Example #10
0
def _gen_model(dir, t, fields):
    className = formatTableName(t)

    li = []
    li.append('<?php')
    li.append('class %s {' % (className))
    for f in fields:
        li.append('var $%s;' % (f.Field))

    # li.append('var %s;' % (','.join('$%s' % (f.Field)  for f in fields)  ) ) #$id, $first_name, $last_name, $email
    li.append('}')
    li.append('?>')

    lfile = '%s%s.inc.gen' % (dir, className)
    save_file(lfile, li)
Example #11
0
def finalize():
    """\
    Writer ``finalization'' function: flushes buffers, closes open files, ...
    """
    if previous_source is not None:
        # insert all the new custom classes inside the old file
        tag = "#<%swxGlade insert new_classes>" % nonce
        if previous_source.new_classes:
            code = "".join(previous_source.new_classes)
        else:
            code = ""
        previous_source.content = previous_source.content.replace(tag, code)
        tag = "#<%swxGlade extra_modules>\n" % nonce
        code = "".join(_current_extra_modules.keys())
        previous_source.content = previous_source.content.replace(tag, code)
        # now remove all the remaining <123415wxGlade ...> tags from the
        # source: this may happen if we're not generating multiple files,
        # and one of the container class names is changed
        import re

        tags = re.findall("(#<%swxGlade replace ([a-zA-Z_]\w*) +\w+>)" % nonce, previous_source.content)
        for tag in tags:
            indent = previous_source.spaces.get(tag[1], "\t")
            comment = "%s# content of this block not found: " "did you rename this class?\n" % indent
            previous_source.content = previous_source.content.replace(tag[0], comment)
        tags = re.findall("#<%swxGlade event_handlers \w+>" % nonce, previous_source.content)
        for tag in tags:
            previous_source.content = previous_source.content.replace(tag, "")

        # write the new file contents to disk
        common.save_file(previous_source.name, previous_source.content, "codegen")

    elif not multiple_files:
        global output_file
        em = "".join(_current_extra_modules.keys())
        content = output_file.getvalue().replace("#<%swxGlade extra_modules>\n" % nonce, em)
        output_file.close()
        try:
            common.save_file(output_file_name, content, "codegen")
            # make the file executable
            if _app_added:
                os.chmod(output_file_name, 0755)
        except IOError, e:
            raise XmlParsingError(str(e))
        except OSError:
            pass  # this isn't necessary a bad error
Example #12
0
def main():
    """
    Noise filtering from word embeddings
    Usage:
        python filter_noise_embs.py -input <original_embs_file> -output <denoising_embs_file> -bin <binary_file>
                                    -over <over_complete_embs_file> -iter <iteration> -bsize <batch_size>
    <original_embs_file>: the original word embeddings is used to learn denoising
    <denoising_embs_file>: the output name file of word denoising embeddings
    <over_complete_embs_file>: the overcomple word embeddings is used to learn overcomplete word denoising embeddings
    <binary_file>: 1 for binary; 0 for text
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('-input', type=str)
    parser.add_argument('-output', type=str)
    parser.add_argument('-over',
                        action='store',
                        default=False,
                        dest='file_over')
    parser.add_argument('-iter', type=int)
    parser.add_argument('-bsize', type=int)
    parser.add_argument('-bin', type=int, default=1)

    args = parser.parse_args()

    vocab, vecs_in = read_file(args.input, binary=args.bin)
    if args.file_over is False:
        vecs_dict = np.load(args.input + '.dict_comp.npy')
        Q, S = initialize_parameters(vecs_dict)
        model = DeEmbs(vecs_in=vecs_in,
                       batch_size=args.bsize,
                       epochs=args.iter,
                       Q=Q,
                       S=S)
    else:
        vecs_dict = np.load(args.input + '.dict_overcomp.npy')
        Q, S = initialize_parameters(vecs_dict)
        vc, vecs_over = read_file(args.file_over, binary=args.bin)
        assert vocab == vc
        model = DeEmbs(vecs_in=vecs_in,
                       vecs_over=vecs_over,
                       batch_size=args.bsize,
                       epochs=args.iter,
                       Q=Q,
                       S=S)
    vecs_out = model.fit()
    save_file(args.output, vocab, vecs_out, binary=args.bin)
Example #13
0
 def _save_app(self, filename):
     try:
         obuffer = compat.StringIO()
         common.app_tree.write(obuffer)
         common.save_file(filename, obuffer.getvalue(), 'wxg')
     except EnvironmentError as inst:
         common.app_tree.app.saved = False
         bugdialog.ShowEnvironmentError(_('Saving this project failed'),
                                        inst)
     except Exception as inst:
         common.app_tree.app.saved = False
         fn = os.path.basename(filename).encode('ascii', 'replace')
         bugdialog.Show(_('Save File "%s"') % fn, inst)
     else:
         common.app_tree.app.saved = True
         common.remove_autosaved()
         if config.preferences.autosave and self.autosave_timer is not None:
             self.autosave_timer.Start()
         self.user_message(_("Saved %s") % os.path.basename(filename))
Example #14
0
def gen_config(dir):
    li = []
    li.append('<?php')
    li.append('class Config {')
    li.append(
        "static $dbRinfo = ['dsn'=>'mysql:host=%s;dbname=%s;port=3306;charset=utf8', "
        % (host, db))
    li.append("'username' => '%s','password' => '%s' ];" % (user, pw))

    li.append(
        "static $dbWinfo = ['dsn'=>'mysql:host=%s;dbname=%s;port=3306;charset=utf8', "
        % (host, db))
    li.append("'username' => '%s','password' => '%s' ];" % (user, pw))

    li.append('}')
    li.append('?>')

    lfile = '%sconfig.inc.gen' % (dir)
    save_file(lfile, li)
Example #15
0
def gen_basic(dir):
    li = []
    li.append('<?php')
    li.append('require_once(ROOT_DIR."/private/dao/BaseDAO.inc");')
    li.append(
        'require_once(ROOT_DIR."/private/controller/BaseController.inc");')

    tables = load_tables()
    for t in tables:
        className = formatTableName(t)
        li.append('require_once(ROOT_DIR."/private/model/%s.inc");' %
                  (className))
        li.append('require_once(ROOT_DIR."/private/dao/%s.inc");' %
                  ('%sDAO' % (className)))
        li.append('require_once(ROOT_DIR."/private/logic/%s.inc");' %
                  ('%sLogic' % (className)))
        li.append('')
    li.append('?>')

    lfile = '%sbase.inc.gen' % (dir)
    save_file(lfile, li)
Example #16
0
def to_internal_csv(source_folder, dest_folder):
    data = read_internal(source_folder)

    csv_string = 'filename,width,height,class,xmin,ymin,xmax,ymax'

    for img_data in data:
        img = common.get_image_data('/'.join(
            [source_folder, 'images', img_data['filename']]))

        for markup in img_data['markups']:
            markup_string = ','.join([
                'images/' + img['filename'],
                str(img['width']),
                str(img['height']), markup["label"],
                str(markup['x']),
                str(markup['y']),
                str(markup['x1']),
                str(markup['y1'])
            ])
            csv_string += '\n' + markup_string

    common.save_file(csv_string, dest_folder + '/markup.csv')
    common.copy_folder(source_folder + '/images', dest_folder + '/images')
Example #17
0
import jieba.posseg as pseg
import common
import os

def pos_tag(file):
	data,stens=[],[]
	f=open(file,'r')
	lines=f.readlines()
	for line in lines:
		# if len(line)>1:
		line = line.strip('\n') #去除末尾换行符
		stens=[]
		for word,pos in pseg.cut(line):
			stens.append(word+'_'+pos)
		# stens=[word+'_'+pos for word,pos in pseg.cut(line)]
		data.append(' '.join(stens))
	f.close()
	return data

parent_dir = os.path.abspath(os.path.dirname(os.getcwd()))#C:\Users\jee_s\Desktop\助研\中文语料处理
src_dir = os.path.join(parent_dir,"分句语料库")
dst_dir = os.path.join(parent_dir,"附码语料库")
common.mkdir(dst_dir)
_,files,_ = common.getdir(src_dir)
for file in files:
	print(file)
	filename = os.path.basename(file)
	data = pos_tag(file)
	common.save_file(data,os.path.join(dst_dir,filename))
Example #18
0
def _gen_dao(dir, t, fields):
    className = '%sDAO' % (formatTableName(t))

    li = []
    li.append('<?php')
    li.append('class %s extends BaseDAO {' % (className))

    li.append(
        'function save(&$vo) { if ($vo->pk_id == 0) { $this->insert($vo); } else { $this->update($vo); } }'
    )

    li.append('function get($pk_id) {')

    li.append('$sql = "select %s from %s where pk_id=:pk_id";' %
              (','.join([f.Field for f in fields]), t))
    li.append('$p = $this->dbR->prepare($sql);')
    li.append('$p->execute(array(":pk_id"=>$pk_id));')
    li.append('$vo = new %s();' % (formatTableName(t)))
    li.append('$this->getFromResult($vo,$p) ; ')
    li.append('return $vo;')
    li.append('}')

    li.append('function delete(&$vo) {')
    li.append('$sql = "update %s set status=1 where pk_id=:pk_id ";' % (t))
    li.append('$p = $this->dbR->prepare($sql);')
    li.append('$p->execute(array(":pk_id"=>$vo->pk_id));')
    li.append('$p->rowCount(); ')
    li.append('}')

    li.append('#-- private functions')
    li.append('function getFromResult(&$vo, $result) {')
    li.append('if(($r = $result->fetch(PDO::FETCH_ASSOC)) !== false){')  #len?
    for f in fields:
        li.append('$vo->%s = $r["%s"];' % (f.Field, f.Field))
    li.append('}}')

    li.append('function update(&$vo) {')
    # fields1 =  ','.join(['%s=:%s'%(f.Field,f.Field)  for f in fields if f.Field not in ['pk_id','create_date']])
    # fields1 = fields1.replace(':create_date','now()').replace(':last_update','now()')
    # fields2 =  ','.join(['  ' %(f.Field,f.Field)  for f in fields if f.Field not in ['pk_id','create_date','last_update']])

    li.append('$fields = array();')
    li.append('$arr = array(":pk_id"=>$vo->pk_id);')
    for f in fields:
        if f.Field not in ['pk_id', 'create_date', 'last_update']:
            li.append(
                'if(!is_null($vo->%s)){$arr[":%s"] = $vo->%s;$fields[]="%s=:%s";  }'
                % (f.Field, f.Field, f.Field, f.Field, f.Field))

    if 'last_update' in [f.Field for f in fields]:
        li.append('$fields[]="last_update=now()";')

    li.append(
        '$sql = "update %s set ". join(",",$fields) ." where pk_id=:pk_id";' %
        (t))
    li.append('$p = $this->dbW->prepare($sql);')
    li.append('$p->execute($arr);')
    li.append('$p->rowCount();  ')

    li.append('}')

    li.append('function insert(&$vo) {')
    fields3 = ','.join([f.Field for f in fields if f.Field != 'pk_id'])
    fields4 = ','.join(
        [':%s' % (f.Field) for f in fields if f.Field != 'pk_id'])
    fields4 = fields4.replace(':create_date', 'now()').replace(
        ':last_update', 'now()').replace(':status', '0')
    fields5 = ','.join([
        '":%s"=>$vo->%s' % (f.Field, f.Field) for f in fields
        if f.Field not in ['pk_id', 'create_date', 'last_update', 'status']
    ])
    li.append('$sql = "insert into %s (%s) values(%s)";' %
              (t, fields3, fields4))
    li.append('$p = $this->dbW->prepare($sql);')
    li.append('$p->execute(array(%s));' % fields5)
    li.append('$vo->pk_id = $this->dbW->lastinsertid();  ')
    li.append('}')

    li.append('}')
    li.append('?>')

    lfile = '%s%s.inc.gen' % (dir, className)
    save_file(lfile, li)
Example #19
0
def add_class(code_obj):
    """\
    Generates the code for a custom class.
    """
    global _current_extra_modules
    if not multiple_files:
        # in this case, previous_source is the SourceFileContent instance
        # that keeps info about the single file to generate
        prev_src = previous_source
    else:
        # let's see if the file to generate exists, and in this case
        # create a SourceFileContent instance
        filename = os.path.join(out_dir, code_obj.klass + ".pm")  # MODULE!!
        if not os.path.exists(filename):
            prev_src = None
        else:
            prev_src = SourceFileContent(filename)
        _current_extra_modules = {}

    if classes.has_key(code_obj.klass) and classes[code_obj.klass].done:
        return  # the code has already been generated

    try:
        builder = obj_builders[code_obj.base]
    except KeyError:
        print code_obj
        raise  # this is an error, let the exception be raised

    if prev_src is not None and prev_src.classes.has_key(code_obj.klass):
        is_new = False
    else:
        # this class wasn't in the previous version of the source (if any)
        is_new = True
        mods = getattr(builder, "extra_modules", [])
        if mods:
            for m in mods:
                _current_extra_modules[m] = 1

    buffer = []
    write = buffer.append

    if not classes.has_key(code_obj.klass):
        # if the class body was empty, create an empty ClassLines
        classes[code_obj.klass] = ClassLines()

    new_signature = getattr(builder, "new_signature", [])

    if _use_gettext and not is_new:
        classes[code_obj.klass].dependencies["use Wx::Locale gettext => '_T';\n"] = 1

    if is_new:
        write("package %s;\n\n" % code_obj.klass)
        write("use Wx qw[:everything];\nuse base qw(%s);\nuse strict;\n\n" % code_obj.base.replace("wx", "Wx::", 1))

        if _use_gettext:
            if multiple_files:
                classes[code_obj.klass].dependencies["use Wx::Locale gettext => '_T';\n"] = 1
            else:
                write("use Wx::Locale gettext => '_T';\n")

        if multiple_files:
            # write the module dependecies for this class (package)
            write("# begin wxGlade: ::dependencies\n")
            for module in classes[code_obj.klass].dependencies:
                write(module)
            write("# end wxGlade\n")
            write("\n")

        write("sub new {\n")

        write("\tmy( $self, %s ) = @_;\n" % ", ".join(new_signature))

        if new_signature:
            for k in new_signature:
                if new_defaults.has_key(k):
                    write(new_defaults[k])
        else:
            new_signature = ["@_[1 .. $#_]"]  # shift(@_)->SUPER::new(@_);
            print code_obj.klass + " did not declare new_defaults "

    # constructor (new) begin tag
    write("# begin wxGlade: %s::new\n\n" % code_obj.klass)
    prop = code_obj.properties
    style = prop.get("style", None)
    if style:
        write("\t$style = %s \n\t\tunless defined $style;\n\n" % style)

    # constructor (new)
    write("\t$self = $self->SUPER::new( %s );\n" % ", ".join(new_signature))

    init_lines = classes[code_obj.klass].init

    parents_init = classes[code_obj.klass].parents_init
    parents_init.reverse()

    for l in parents_init:
        write("\t" + l)
    for l in init_lines:
        write("\t" + l)

    # now check if there are extra lines to add to the init method
    if hasattr(builder, "get_init_code"):
        for l in builder.get_init_code(code_obj):
            write("\t" + l)

    write("\n\t$self->__set_properties();\n")
    write("\t$self->__do_layout();\n\n")

    event_handlers = classes[code_obj.klass].event_handlers
    if hasattr(builder, "get_events"):
        for id, event, handler in builder.get_events(code_obj):
            event_handlers.append((id, event, handler))
    for win_id, event, handler in event_handlers:
        if win_id.startswith("#"):
            win_id = "$self->{" + win_id[8:] + "}->GetId"
        write("\tWx::Event::%s($self, %s, \\&%s);\n" % (event, win_id, handler))
    if event_handlers:
        write("\n")

    write("# end wxGlade\n")

    if is_new:
        write("\treturn $self;\n\n")
        write("}\n\n")

    if prev_src is not None and not is_new:
        # replace the lines inside the ::new wxGlade block with the new ones
        tag = "#<%swxGlade replace %s %s>" % (nonce, code_obj.klass, "new")
        if prev_src.content.find(tag) < 0:
            # no __init__ tag found, issue a warning and do nothing
            print >> sys.stderr, "WARNING: wxGlade ::new block not found," " constructor code NOT generated"
        else:
            prev_src.content = prev_src.content.replace(tag, "".join(buffer))
        buffer = []
        write = buffer.append

    # __set_properties
    obj_p = getattr(builder, "get_properties_code", generate_common_properties)(code_obj)
    obj_p.extend(classes[code_obj.klass].props)
    write_body = len(obj_p)

    if is_new:
        write("\nsub __set_properties {\n\tmy $self = shift;\n\n")

    write("# begin wxGlade: %s::__set_properties\n\n" % code_obj.klass)

    if not write_body:
        write("\treturn;\n")
    else:
        for l in obj_p:
            write("\t" + l)

    write("\n# end wxGlade\n")

    if is_new:
        write("}\n")

    if prev_src is not None and not is_new:
        # replace the lines inside the __set_properties wxGlade block
        # with the new ones
        tag = "#<%swxGlade replace %s %s>" % (nonce, code_obj.klass, "__set_properties")
        if prev_src.content.find(tag) < 0:
            # no __set_properties tag found, issue a warning and do nothing
            print >> sys.stderr, "WARNING: wxGlade __set_properties block " "not found, __set_properties code NOT generated"
        else:
            prev_src.content = prev_src.content.replace(tag, "".join(buffer))
        buffer = []
        write = buffer.append

    # __do_layout
    if is_new:
        write("\nsub __do_layout {\n\tmy $self = shift;\n\n")
    layout_lines = classes[code_obj.klass].layout
    sizers_init_lines = classes[code_obj.klass].sizers_init

    # check if there are extra layout lines to add
    if hasattr(builder, "get_layout_code"):
        extra_layout_lines = builder.get_layout_code(code_obj)
    else:
        extra_layout_lines = []

    write("# begin wxGlade: %s::__do_layout\n\n" % code_obj.klass)

    if layout_lines or sizers_init_lines or extra_layout_lines:
        sizers_init_lines.reverse()
        for l in sizers_init_lines:
            write("\t" + l)
        for l in layout_lines:
            write("\t" + l)
        for l in extra_layout_lines:
            write("\t" + l)
    else:
        write("\treturn;\n")

    write("\n# end wxGlade\n")

    if is_new:
        write("}\n")

    if prev_src is not None and not is_new:
        # replace the lines inside the __do_layout wxGlade block
        # with the new ones
        tag = "#<%swxGlade replace %s %s>" % (nonce, code_obj.klass, "__do_layout")
        if prev_src.content.find(tag) < 0:
            # no __do_layout tag found, issue a warning and do nothing
            print >> sys.stderr, "WARNING: wxGlade __do_layout block " "not found, __do_layout code NOT generated"
        else:
            prev_src.content = prev_src.content.replace(tag, "".join(buffer))

    if prev_src is not None and not is_new:
        already_there = prev_src.event_handlers.get(code_obj.klass, {})
        buf = []
        for name, event, handler in event_handlers:
            if handler not in already_there:
                buf.append(
                    "\nsub %s {\n"
                    "\tmy ($self, $event) = @_;\n"
                    "# wxGlade: %s::%s <event_handler>\n\n"
                    '\twarn "Event handler (%s) not implemented";\n'
                    "\t$event->Skip;\n\n# end wxGlade\n}\n\n" % (handler, code_obj.klass, handler, handler)
                )
                already_there[handler] = 1

        tag = "#<%swxGlade event_handlers %s>" % (nonce, code_obj.klass)
        if prev_src.content.find(tag) < 0:
            # no event_handlers tag found, issue a warning and do nothing
            print >> sys.stderr, "WARNING: wxGlade event_handlers block " "not found, event_handlers code NOT generated"
        else:
            prev_src.content = prev_src.content.replace(tag, "".join(buf))
        del buf
    else:
        already_there = {}
        for name, event, handler in event_handlers:
            if handler not in already_there:
                write(
                    "\nsub %s {\n"
                    "\tmy ($self, $event) = @_;\n"
                    "# wxGlade: %s::%s <event_handler>\n\n"
                    '\twarn "Event handler (%s) not implemented";\n'
                    "\t$event->Skip;\n\n# end wxGlade\n}\n\n" % (handler, code_obj.klass, handler, handler)
                )
                already_there[handler] = 1

    # the code has been generated
    classes[code_obj.klass].done = True

    write("\n# end of class %s\n\n1;\n\n" % code_obj.klass)

    if not multiple_files and prev_src is not None:
        # if this is a new class, add its code to the new_classes list of the
        # SourceFileContent instance
        if is_new:
            prev_src.new_classes.append("".join(buffer))
        return

    if multiple_files:
        # return # not implemented yet -- crazyinsomniac
        if prev_src is not None:
            tag = "#<%swxGlade insert new_classes>" % nonce
            prev_src.content = prev_src.content.replace(tag, "")

            # insert the extra modules
            tag = "#<%swxGlade extra_modules>\n" % nonce
            code = "".join(_current_extra_modules.keys())
            prev_src.content = prev_src.content.replace(tag, code)

            # insert the module dependencies of this class
            extra_modules = classes[code_obj.klass].dependencies.keys()
            deps = ["# begin wxGlade: ::dependencies\n"] + extra_modules + ["# end wxGlade\n"]
            tag = "#<%swxGlade replace %s dependencies>" % (nonce, code_obj.klass)
            prev_src.content = prev_src.content.replace(tag, "".join(deps))

            try:
                # store the new file contents to disk
                common.save_file(filename, prev_src.content, "codegen")
            except:
                raise IOError("pl_codegen.add_class: %s, %s, %s" % (out_dir, prev_src.name, code_obj.klass))
            return

        # create the new source file
        filename = code_obj.klass.replace("::", os.sep) + ".pm"  # MODULE!!
        filename = os.path.join(out_dir, filename)
        out = cStringIO.StringIO()
        write = out.write
        # write the common lines
        for line in header_lines:
            write(line)

        # write the class body
        for line in buffer:
            write(line)
        try:
            dirname = os.path.dirname(filename)  # create Foo in Foo::Bar,
            # Foo/Bar.pm
            if not os.path.exists(dirname):
                os.makedirs(dirname)
            # store the contents to filename
            common.save_file(filename, out.getvalue(), "codegen")
        except:
            import traceback

            traceback.print_exc()
        out.close()
    else:  # not multiple_files
        # write the class body onto the single source file
        for dep in classes[code_obj.klass].dependencies:
            _current_extra_modules[dep] = 1
        write = output_file.write
        for line in buffer:
            write(line)
Example #20
0
def add_app(app_attrs, top_win_class):
    """\
    Generates the code for a wxApp instance.
    If the file to write into already exists, this function does nothing.
    """
    global _app_added
    _app_added = True

    name = app_attrs.get("name")
    if not name:
        name = "app"

    if not multiple_files:
        prev_src = previous_source
    else:
        filename = os.path.join(out_dir, name + ".pl")
        if not os.path.exists(filename):
            prev_src = None
        elif _overwrite:
            prev_src = None
        else:
            # prev_src doesn't need to be a SourceFileContent instance in this
            # case, as we do nothing if it is not None
            prev_src = 1

    if prev_src is not None:
        return  # do nothing if the file existed

    klass = app_attrs.get("class")
    top_win = app_attrs.get("top_window")
    if not top_win:
        return  # do nothing if there is no top window
    lines = []
    append = lines.append

    if klass:
        append("package %s;\n" % klass)
        append("\nuse base qw(Wx::App);\nuse strict;\n\n")
        if multiple_files:
            # import the top window module
            append("use %s;\n\n" % top_win_class)
        append("sub OnInit {\n\tmy( $self ) = shift;\n\n")
    else:
        append("1;\n\npackage main;\n")
        if multiple_files:
            # import the top window module
            append("\nuse %s;\n\n" % top_win_class)
        append("\nunless(caller){\n")
        if _use_gettext:
            append('\tmy $local = Wx::Locale->new("English", "en", "en");' + " # replace with ??\n")
            append('\t$local->AddCatalog("%s");' % name + " # replace with the appropriate catalog name\n\n")
        # and now,  basically fake wxPySimpleApp
        append("\tlocal *Wx::App::OnInit = sub{1};\n")
        append("\tmy $%s = Wx::App->new();\n" % name)

    append("\tWx::InitAllImageHandlers();\n\n")  # we add this to avoid troubles
    append("\tmy $%s = %s->new();\n\n" % (top_win, top_win_class))

    if klass:
        append("\t$self->SetTopWindow($%s);\n" % top_win)
        append("\t$%s->Show(1);\n\n" % top_win)
        append("\treturn 1;\n}\n")
        append("# end of class %s\n\n" % klass)
        append("package main;\n\nunless(caller){\n")

        if _use_gettext:
            append('\tmy $local = Wx::Locale->new("English", "en", "en");' + " # replace with ??\n")
            append('\t$local->AddCatalog("%s");' % name + " # replace with the appropriate catalog name\n\n")
        append("\tmy $%s = %s->new();\n" % (name, klass))
    else:
        append("\t$%s->SetTopWindow($%s);\n" % (name, top_win))
        append("\t$%s->Show(1);\n" % top_win)
    append("\t$%s->MainLoop();\n}\n" % name)

    if multiple_files:  # not read yet
        filename = os.path.join(out_dir, name + ".pl")
        out = cStringIO.StringIO()
        write = out.write
        write("#!/usr/bin/perl -w -- \n")
        # write the common lines
        for line in header_lines:
            write(line)
        # write the wxApp code
        for line in lines:
            write(line)
        try:
            common.save_file(filename, out.getvalue(), "codegen")
        except:
            import traceback

            traceback.print_wexc()
        # make the file executable
        try:
            os.chmod(filename, 0755)
        except OSError:
            pass  # this is not a bad error
        out.close()
    else:
        write = output_file.write
        for line in lines:
            write(line)
Example #21
0
homedir = os.path.join(cdir, '..')
sys.path.append(homedir)
import main
import weixin
from common import save_file, make_qrcode

# 初始化数据库
script_file = 'script.sql'
script = "\
create database if not exists {0};\
create user '{1}'@'{2}' identified by '{3}';\
grant all on {0}.* to '{1}'@'{2}';".format(main.app.config['DB_SCHEMA'],
                                           main.app.config['DB_USER'],
                                           main.app.config['DB_HOST'],
                                           main.app.config['DB_PASS'])
save_file(script_file, script)
print("entry mysql root user password:"******"sed -i '/AUTH2_SHORT_URL/c\AUTH2_SHORT_URL = \"{}\"\' {}/{}".format(
    url['short_url'], homedir, '/conf/config.py'))
Example #22
0
label_dict = {
    '0': 'A',
    '1': 'B',
    '2': 'C',
    '3': 'D',
    '4': 'E',
}
if __name__ == '__main__':
    results_file = os.path.join(root_dirs, 'results', 'weight_rel.csv')
    gold_file = os.path.join(root_dirs, 'csqa_data', 'conceptnet',
                             'weight_rel', 'test_data.json')

    results = read_to_array(results_file)
    golds = load_json(gold_file)
    print(results[:2])
    print(golds[:2])
    assert len(results) == len(golds)
    length = len(results)
    print(results[0])
    print(golds[0])

    tests = []
    for i in range(length):
        assert int(results[i][0]) == golds[i]['initial_id']
        tests.append([golds[i]['id'], label_dict[results[i][-1]]])
    print(tests[0])
    print(len(tests))
    save_file(tests, os.path.join(root_dirs, 'test_results',
                                  'predictions.csv'))
Example #23
0
input_divide_screen = "input_divide_screen"

for folder in dirs:
    #folder_name = folder
    print(folder)
    files = os.listdir(path + "/" + folder)

    for im in files:
        img = path + "/" + folder + "/" + im
        img = cv2.imread(img)

        height, width, channels = img.shape
        print(height, width, channels)

        title = img[0:500, 0:width]
        save_file(cropped, title)

        rate = height // 5

        menu = img[height - rate:height, 0:width]
        save_file(cropped, menu)

        if folder == input_line:

            line3 = img[(3 * height) // 7:(4 * height) // 7, 0:width]
            save_file(cropped, line3)

            line4 = img[(4 * height) // 7:(5 * height) // 7, 0:width]
            save_file(cropped, line4)

            line5 = img[(5 * height) // 7:(6 * height) // 7, 0:width]