python 为MACE重塑黄金

为MACE重塑黄金

gistfile1.py
import csv
from sets import Set
import sys
golds = {}
items = Set()
def loadData(path):
  with open(path,'rU') as F:
		csvdr = csv.DictReader(F,delimiter='\t')
		for p  in csvdr:
			items.add(int(p['item']))
			#

def loadGolds(path):
	with open(path,'rU') as F:
		csvdr = csv.DictReader(F,delimiter=' ')
		for p  in csvdr:
			golds[int(p['item'])]=p['label']

def writeGolds(path):
	with open(path,'w') as F:
		for i in sorted(items):
			print i
			if i in golds:
				print "i in golds"
				F.write(golds[i])
				F.write('\n')
			else:
				F.write('\n')


def main():
	loadData('/Users/andi/Desktop/quality_platforms/obrobka_danych/big.in')
	print "ile itemow:",len(items)
	print items
	loadGolds('/Users/andi/Desktop/quality_platforms/obrobka_danych/golds.csv')
	print "ile goldow:",len(golds.items())
	print golds
	print sorted(golds.keys())
	writeGolds('/Users/andi/Desktop/quality_platforms/obrobka_danych/golds.out')


if __name__ == "__main__":
    main()

python 将数据从MTURK转换为MACE格式

将数据从MTURK转换为MACE格式

gistfile1.py
import csv
from sets import Set
import sys
results = {}
items = Set()#[]
workers = Set()#[]
def addItem(idict):
	#appending sets of workers and items
	if int(idict['worker']) not in workers:
		workers.add(int(idict['worker']))
	if int(idict['item']) not in items:	
		items.add(int(idict['item']))
	#construct dictionary	
	if int(idict['item']) not in results:
		results[int(idict['item'])]={int(idict['worker']):idict['label']}

	else:
		results[int(idict['item'])][int(idict['worker'])]=idict['label']

def loadData(path):
	with open(path,'rU') as F:
		csvdr = csv.DictReader(F,delimiter='\t')
		for p  in csvdr:
			addItem(p)


def writeData(path):
	with open(path,'w') as F: 
		s=""
		for i in items:
			for w in workers:
				if w in results[i].keys():
					foo=results[i][w]
				else:
					foo=""
				s+=str(foo)
				s+=","
			s+="\n"	
			#print s
			F.write(s)
			s=""

def prettyPrint():
	for kluczyk in results.keys():
		print 'item:',kluczyk
		for w in results[kluczyk].keys():
			print 'worker:',w,' label:',results[kluczyk][w]


def main():
	loadData('/Users/andi/Desktop/quality_platforms/obrobka_danych/big.in')
	print "ile workerow:",len(workers)
	print "ile itemow:", len(items)
	writeData('/Users/andi/Desktop/quality_platforms/obrobka_danych/bigSet.out')


if __name__ == "__main__":
    main()

python vert2intersection.py

vert2intersection.py
'''
BEGIN GPL LICENSE BLOCK
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
END GPL LICENCE BLOCK
'''
 
bl_info = {
    "name": "Vert tools : vert 2 intersection",
    "author": "zeffii",
    "version": (0, 0, 2),
    "blender": (2, 6, 8),
    "category": "Mesh",
    "location": "View3D > EditMode > (w) Specials",
    "wiki_url": "",
    "tracker_url": ""
}
 
import bmesh
import bpy
from mathutils import geometry

def add_vertex_to_intersection():

    obj = bpy.context.object
    me = obj.data
    bm = bmesh.from_edit_mesh(me)

    edges = [e for e in bm.edges if e.select]

    if len(edges) == 2:
        [[v1, v2], [v3, v4]] = [[v.co for v in e.verts] for e in edges]

        iv = geometry.intersect_line_line(v1, v2, v3, v4)
        iv = (iv[0] + iv[1]) / 2
        bm.verts.new(iv)
        bmesh.update_edit_mesh(me)

 
class Vert2Intersection(bpy.types.Operator):
 
    bl_idname = 'mesh.vertintersect'
    bl_label = bl_info['name']
    bl_options = {'REGISTER', 'UNDO'}
 
    @classmethod
    def poll(self, context):
        obj = context.active_object
        return obj != None and obj.type == 'MESH' and obj.mode == 'EDIT'
 
    def execute(self, context):
        add_vertex_to_intersection()
        return {'FINISHED'}
 
 
def menu_func(self, context):
    self.layout.operator(Vert2Intersection.bl_idname, text="Place vertex at Intersection")
 
def register():
    bpy.utils.register_module(__name__)
    bpy.types.VIEW3D_MT_edit_mesh_specials.append(menu_func)
 
def unregister():
    bpy.utils.unregister_module(__name__)
    bpy.types.VIEW3D_MT_edit_mesh_specials.remove(menu_func)
 
 
if __name__ == "__main__":
    register()

python ctrlp_glob.py

ctrlp_glob.py
import vim
import re
import os
import os.path
import fnmatch

custom_ignore = vim.eval('g:ctrlp_custom_ignore')
if custom_ignore['dir'][:2] == '\\v':
    custom_ignore['dir'] = custom_ignore['dir'][2 :]
if custom_ignore['file'][:2] == '\\v':
    custom_ignore['file'] = custom_ignore['file'][2 :]
if custom_ignore['link'][:2] == '\\v':
    custom_ignore['link'] = custom_ignore['link'][2 :]

fs_encoding = vim.eval('&termencoding')
progress_fn = vim.Function('ctrlp#progress')
max_depth = int(vim.eval('exists("b:ctrlp_max_depth") ? b:ctrlp_max_depth : g:ctrlp_max_depth'))
wildignore = vim.eval('&wildignore').split(',')

def glob(base_dir):
    start = base_dir.count(os.sep)
    files_path = []
    for root, dirs, files in os.walk(base_dir, topdown=True):
        files_path += filter((lambda x: re.match(custom_ignore["file"], x) == None), map((lambda x: os.path.join(os.path.normpath(root), x).decode(fs_encoding)), files))
        progress_fn(len(files_path), 1)
        for i in reversed(range(len(dirs))):
            depth = os.path.join(os.path.normpath(root), dirs[i]).count(os.sep)
            if depth - start >= max_depth or dirs[i][0] == '.' \
                    or re.match(custom_ignore["dir"], dirs[i]) != None:
                        del dirs[i]
    for ptn in wildignore:
        files_path = filter((lambda x: not fnmatch.fnmatch(x, ptn)), files_path)
    return files_path

files = glob(sys.argv[0].replace('\\ ', ' '))

python 将SQLAlchemy Model序列化为字典(用于JSON输出)并从字典属性更新Model。

将SQLAlchemy Model序列化为字典(用于JSON输出)并从字典属性更新Model。

api.py
import uuid
import wtforms_json
from sqlalchemy import not_
from sqlalchemy.dialects.postgresql import UUID
from wtforms import Form
from wtforms.fields import FormField, FieldList
from wtforms.validators import Length

from flask import current_app as app
from flask import request, json, jsonify, abort
from flask.ext.sqlalchemy import SQLAlchemy


db = SQLAlchemy(app)
wtforms_json.init()


class Model(db.Model):
    """Base SQLAlchemy Model for automatic serialization and
    deserialization of columns and nested relationships.

    Usage::

        >>> class User(Model):
        >>>     id = db.Column(db.Integer(), primary_key=True)
        >>>     email = db.Column(db.String(), index=True)
        >>>     name = db.Column(db.String())
        >>>     password = db.Column(db.String())
        >>>     posts = db.relationship('Post', backref='user', lazy='dynamic')
        >>>     ...
        >>>     default_fields = ['email', 'name']
        >>>     hidden_fields = ['password']
        >>>     readonly_fields = ['email', 'password']
        >>>
        >>> class Post(Model):
        >>>     id = db.Column(db.Integer(), primary_key=True)
        >>>     user_id = db.Column(db.String(), db.ForeignKey('user.id'), nullable=False)
        >>>     title = db.Column(db.String())
        >>>     ...
        >>>     default_fields = ['title']
        >>>     readonly_fields = ['user_id']
        >>>
        >>> model = User(email='john@localhost')
        >>> db.session.add(model)
        >>> db.session.commit()
        >>>
        >>> # update name and create a new post
        >>> validated_input = {'name': 'John', 'posts': [{'title':'My First Post'}]}
        >>> model.set_columns(**validated_input)
        >>> db.session.commit()
        >>>
        >>> print(model.to_dict(show=['password', 'posts']))
        >>> {u'email': u'john@localhost', u'posts': [{u'id': 1, u'title': u'My First Post'}], u'name': u'John', u'id': 1}
    """
    __abstract__ = True

    # Stores changes made to this model's attributes. Can be retrieved
    # with model.changes
    _changes = {}

    def __init__(self, **kwargs):
        kwargs['_force'] = True
        self._set_columns(**kwargs)

    def _set_columns(self, **kwargs):
        force = kwargs.get('_force')

        readonly = []
        if hasattr(self, 'readonly_fields'):
            readonly = self.readonly_fields
        if hasattr(self, 'hidden_fields'):
            readonly += self.hidden_fields

        readonly += [
            'id',
            'created',
            'updated',
            'modified',
            'created_at',
            'updated_at',
            'modified_at',
        ]

        changes = {}

        columns = self.__table__.columns.keys()
        relationships = self.__mapper__.relationships.keys()

        for key in columns:
            allowed = True if force or key not in readonly else False
            exists = True if key in kwargs else False
            if allowed and exists:
                val = getattr(self, key)
                if val != kwargs[key]:
                    changes[key] = {'old': val, 'new': kwargs[key]}
                    setattr(self, key, kwargs[key])

        for rel in relationships:
            allowed = True if force or rel not in readonly else False
            exists = True if rel in kwargs else False
            if allowed and exists:
                is_list = self.__mapper__.relationships[rel].uselist
                if is_list:
                    valid_ids = []
                    query = getattr(self, rel)
                    cls = self.__mapper__.relationships[rel].argument()
                    for item in kwargs[rel]:
                        if 'id' in item and query.filter_by(id=item['id']).limit(1).count() == 1:
                            obj = cls.query.filter_by(id=item['id']).first()
                            col_changes = obj.set_columns(**item)
                            if col_changes:
                                col_changes['id'] = str(item['id'])
                                if rel in changes:
                                    changes[rel].append(col_changes)
                                else:
                                    changes.update({rel: [col_changes]})
                            valid_ids.append(str(item['id']))
                        else:
                            col = cls()
                            col_changes = col.set_columns(**item)
                            query.append(col)
                            db.session.flush()
                            if col_changes:
                                col_changes['id'] = str(col.id)
                                if rel in changes:
                                    changes[rel].append(col_changes)
                                else:
                                    changes.update({rel: [col_changes]})
                            valid_ids.append(str(col.id))

                    # delete related rows that were not in kwargs[rel]
                    for item in query.filter(not_(cls.id.in_(valid_ids))).all():
                        col_changes = {
                            'id': str(item.id),
                            'deleted': True,
                        }
                        if rel in changes:
                            changes[rel].append(col_changes)
                        else:
                            changes.update({rel: [col_changes]})
                        db.session.delete(item)

                else:
                    val = getattr(self, rel)
                    if self.__mapper__.relationships[rel].query_class is not None:
                        if val is not None:
                            col_changes = val.set_columns(**kwargs[rel])
                            if col_changes:
                                changes.update({rel: col_changes})
                    else:
                        if val != kwargs[rel]:
                            setattr(self, rel, kwargs[rel])
                            changes[rel] = {'old': val, 'new': kwargs[rel]}

        return changes

    def set_columns(self, **kwargs):
        self._changes = self._set_columns(**kwargs)
        if 'modified' in self.__table__.columns:
            self.modified = datetime.utcnow()
        if 'updated' in self.__table__.columns:
            self.updated = datetime.utcnow()
        if 'modified_at' in self.__table__.columns:
            self.modified_at = datetime.utcnow()
        if 'updated_at' in self.__table__.columns:
            self.updated_at = datetime.utcnow()
        return self._changes

    @property
    def changes(self):
        return self._changes

    def reset_changes(self):
        self._changes = {}

    def to_dict(self, show=None, hide=None, path=None, show_all=None):
        """ Return a dictionary representation of this model.
        """

        if not show:
            show = []
        if not hide:
            hide = []
        hidden = []
        if hasattr(self, 'hidden_fields'):
            hidden = self.hidden_fields
        default = []
        if hasattr(self, 'default_fields'):
            default = self.default_fields

        ret_data = {}

        if not path:
            path = self.__tablename__.lower()
            def prepend_path(item):
                item = item.lower()
                if item.split('.', 1)[0] == path:
                    return item
                if len(item) == 0:
                    return item
                if item[0] != '.':
                    item = '.%s' % item
                item = '%s%s' % (path, item)
                return item
            show[:] = [prepend_path(x) for x in show]
            hide[:] = [prepend_path(x) for x in hide]

        columns = self.__table__.columns.keys()
        relationships = self.__mapper__.relationships.keys()
        properties = dir(self)

        for key in columns:
            check = '%s.%s' % (path, key)
            if check in hide or key in hidden:
                continue
            if show_all or key is 'id' or check in show or key in default:
                ret_data[key] = getattr(self, key)

        for key in relationships:
            check = '%s.%s' % (path, key)
            if check in hide or key in hidden:
                continue
            if show_all or check in show or key in default:
                hide.append(check)
                is_list = self.__mapper__.relationships[key].uselist
                if is_list:
                    ret_data[key] = []
                    for item in getattr(self, key):
                        ret_data[key].append(item.to_dict(
                            show=show,
                            hide=hide,
                            path=('%s.%s' % (path, key.lower())),
                            show_all=show_all,
                        ))
                else:
                    if self.__mapper__.relationships[key].query_class is not None:
                        ret_data[key] = getattr(self, key).to_dict(
                            show=show,
                            hide=hide,
                            path=('%s.%s' % (path, key.lower())),
                            show_all=show_all,
                        )
                    else:
                        ret_data[key] = getattr(self, key)

        for key in list(set(properties) - set(columns) - set(relationships)):
            if key.startswith('_'):
                continue
            check = '%s.%s' % (path, key)
            if check in hide or key in hidden:
                continue
            if show_all or check in show or key in default:
                val = getattr(self, key)
                try:
                    ret_data[key] = json.loads(json.dumps(val))
                except:
                    pass

        return ret_data


class User(Model):
    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    first_name = db.Column(db.String(120))
    last_name = db.Column(db.String(120))
    posts = db.relationship('Post', backref='user', lazy='dynamic')


class Post(Model):
    id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    user_id = db.Column(UUID(as_uuid=True), db.ForeignKey('user.id'), nullable=False)
    title = db.Column(db.String(200))
    text = db.Column(db.String())


class PostForm(Form):
    title = StringField(validators=[Length(max=200)])
    text = StringField()


class UserForm(Form):
    first_name = StringField(validators=[Length(max=120)])
    last_name = StringField(validators=[Length(max=120)])
    posts = FieldList(FormField(PostForm))


def requested_columns(request):
    show = request.args.get('show', None)
    if not show:
        return []
    return show.split(',')


@app.route('/users/<string:user_id>', methods=['GET'])
def read_user(user_id):

    # get user from database
    user = User.query.filter_by(id=user_id).first()
    if user is None:
        abort(404)

    # return user as json
    show = requested_columns(request)
    return jsonify(data=user.to_dict(show=show))


@app.route('/users/<string:user_id>', methods=['PUT'])
def update_user(user_id):

    # get user from database
    user = User.query.filter_by(id=user_id).first()
    if user is None:
        abort(404)

    input_data = request.get_json(force=True)
    if not isinstance(input_data, dict):
        return jsonify(error='Request data must be a JSON Object'), 400

    # validate json user input using WTForms-JSON
    form = UserForm.from_json(input_data)
    if not form.validate():
        return jsonify(errors=form.errors), 400

    # update user in database
    user.set_columns(**form.patch_data)
    db.session.commit()

    # return user as json
    show = requested_columns(request)
    return jsonify(data=user.to_dict(show=show))

python submodule_remove.py

submodule_remove.py
#!/usr/bin/env python
#coding:utf-8

import re
import sys
import os

t = sys.argv[1]
with open('.git/config', 'rb') as f:
    s = f.read()
    o = re.search(r'\[submodule\s+"' + t + '"][^[]+', s, re.S)
    p1 = o.start()
    p2 = o.end()
    s = s[:p1] + s[p2:]
with open('.git/config', 'wb') as f:
    f.write(s)
    
os.system('git rm --cached' + ' "' + t + '"')
os.system('rm -rf' + ' "' + t + '"')

python 从Excel工作簿中的所有电子表格创建CSV

从Excel工作簿中的所有电子表格创建CSV

gistfile1.py
# -*- coding: utf-8 -*-
import xlrd
import csv
from os import sys

def csv_from_excel(excel_file):
    workbook = xlrd.open_workbook(excel_file)
    all_worksheets = workbook.sheet_names()
    for worksheet_name in all_worksheets:
  	worksheet = workbook.sheet_by_name(worksheet_name)
		your_csv_file = open(''.join([worksheet_name,'.csv']), 'wb')
		wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)

		for rownum in xrange(worksheet.nrows):
			wr.writerow([unicode(entry).encode("utf-8") for entry in worksheet.row_values(rownum)])
		your_csv_file.close()



if __name__ == "__main__":
    csv_from_excel(sys.argv[1])

python folder.py

folder.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import re

import sys
import codecs

sys.stdout = codecs.getwriter('shift_jis')(sys.stdout)

def get_file_description(file_name):
    root, ext = os.path.splitext(file_name)
    ext = ext.lower()
    if ext == '.c':
        brief = re.compile('\s*/?\*\*\s+@brief\s+(\S*)\s*')
        for line in codecs.open(file_name, 'r', 'utf-8'):
            m = brief.match(line)
            if m:
                return m.groups()[0]
    return ''

def main():
    root_path = '../spx'
    message_len = 20
    root_path_len = len(root_path)+1
    paths = []
    for root, dirs, files in os.walk(root_path):
        if root_path == root:
            paths.append(([], files))
        else:
            paths.append((root[root_path_len:].split('\\'), files))
    dir_max_len = []
    file_max_len = 0
    for path in paths:
        for i, d in enumerate(path[0]):
            if i >= len(dir_max_len):
                dir_max_len.append(len(d))
            else:
                dir_max_len[i] = max(dir_max_len[i], len(d))
        if path[1]:
            file_max_len = max(file_max_len, max([len(x) for x in path[1]]))
    path_len = 0
    msg_line = '-'*message_len+'+'
    msg_space = ' '*message_len+'|'
    file_line = '-'*file_max_len+'+'
    file_space = ' '*file_max_len+'|'
    for path in paths:
        def make_line(dot1,sep1,dot2,sep2,half):
            if half < 0: half = 0
            return (sep1+ \
                        ''.join([dot1*l+sep1 for l in dir_max_len[0:half]])+ \
                        ''.join([dot2*l+sep2 for l in dir_max_len[half:]]))[0:-1]
        pre_path_len = path_len
        path_len = len(path[0])
        blank = min(pre_path_len, path_len-1)
        line1 = make_line(' ','+','-','+',blank)+'+'+file_line+msg_line
        line2 = ''
        for i, max_len in enumerate(dir_max_len):
            line2 += ' ' if i > path_len else '|'
            line2 += (path[0][i] if i == path_len-1 else '').ljust(max_len)
        line2 += '|'+file_space+msg_space
        line3 = make_line(' ','+',' ',' ',blank+1)+'+'+file_line+msg_line
        line4 = make_line(' ','|',' ',' ',path_len)
        print line1
        print line2
        for f in path[1]:
            print line3
            d = get_file_description(os.path.join(root_path, '\\'.join(path[0]), f))
            m = sum([ord(x) > 255 for x in d])
            print line4+'|'+f.ljust(file_max_len)+'|'+d.ljust(message_len-m)+'|'
    print make_line('-','+','-','+',0)+'+'+file_line+msg_line

if __name__ == '__main__':
    main()

python 一个简单的蟒蛇装饰。

一个简单的蟒蛇装饰。

Decorator.py
def logcall(f):
  def wrapper(*a, **opts):
    print('calling ' + f.__name__)
    f(*a, **opts)
    print('called ' + f.__name__)
  return wrapper


@logcall
def square(x):
  print 'result: ' + str(x * x)
  return x * x


square(2)

python PygLatin:CodeCademy上的一个Python作业

PygLatin:CodeCademy上的一个Python作业

PygLatin.py
# PygLatin Convertor
# by Blanboom
# 2013-9-13 Programmer's Day
# http://www.codecademy.com/zh/tracks/python

# Welcome message
print "Welcome to PygLatin convertor"

# The letters which should be added to the end of the word
pyg = 'ay'

# Input a word
original = raw_input('Enter a word:')

# Check if it is a real word
if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word[0]     # The first letter of the word
    
    # Check if the first letter is a vowel
    if 'a' == first or 'e' == first or 'i' == first or 'o' == first or 'u' == first:
        new_word = word + pyg
        print new_word
    else:
        word_len = len(word)
        new_word = word[1:word_len] + first + pyg
        print new_word

else:
    print 'empty'