Python 计算igraph中的共生系数

def assortativity(graph, degrees=None):
    if degrees is None: degrees = graph.degree()
    degrees_sq = [deg**2 for deg in degrees]

    m = float(graph.ecount())
    num1, num2, den1 = 0, 0, 0
    for source, target in graph.get_edgelist():
        num1 += degrees[source] * degrees[target]
        num2 += degrees[source] + degrees[target]
        den1 += degrees_sq[source] + degrees_sq[target]

    num1 /= m
    den1 /= 2*m
    num2 = (num2 / (2*m)) ** 2

    return (num1 - num2) / (den1 - num2)

Python 完整的FormEncode + htmlfill示例,包含表单和测试值

#!/usr/bin/python

import formencode
from formencode import validators
from formencode import htmlfill

class Valid(formencode.Schema):
    allo = validators.UnicodeString(not_empty=True)
    num = validators.Int()
    num2 = validators.Int(not_empty=True)
    pouet = validators.UnicodeString(not_empty=True)
    gna = validators.String(not_empty=True)


a = """
<form action="pouet" method="post">
  <input type="text" name="pouet" />
  <form:error name="pouet" />

  <input type="password" name="allo" />
  
  <input type="text" name="num" />
  <form:error name="num"  />

  <input type="text" name="num2" />
  <form:error name="num2"  />

  <textarea name="gna"></textarea>
  <form:iferror name="gna">Horrible horror message</form:iferror>
  <form:error name="gna"  />

  <textarea name="gna2"></textarea>
  <form:iferror name="gna2">Horrible horror message</form:iferror>
  <form:error name="gna2"  />


</form>

"""

inp = {'allo': 'salut', 'pouet': 's', 'num': '123', 'num2': "123", 'gna': ''}


ok = None
try:
    ok = Valid.to_python(inp)
except validators.Invalid, e:
    print e.value
    print htmlfill.render(a, {'gna2': 'salutataion'}, e.error_dict or {})
    

print ok

Python Python错误和异常

import sys
try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
except IOError, (errno, strerror):
    print "I/O error(%s): %s" % (errno, strerror)
except ValueError:
    print "Could not convert data to an integer."
except:
    print "Unexpected error:", sys.exc_info()[0]
    raise

Python htmlfill和Pylons集成,在控制器内部进行验证,使用i18n进行Mako模板示例

######################################################## development.ini / production.ini

[app:main]
...
lang = fr

##################################################################### project/lib/base.py

class BaseController(WSGIController):

    ### [...snip...]

    def __before__(self):

        ### [...snip...]

        formencode.api.set_stdtranslation(domain="FormEncode",
                                          languages=[config.get('lang', 'fr')])

################################################################## project/lib/helpers.py

from formencode import htmlfill
def p_error_formatter(error):  
    return '<p class="error">%s</p>' % htmlfill.html_quote(error) 

def ol_error_formatter(error):  
    return '<ol class="errors"><li>%s</li></ol>' % htmlfill.html_quote(error) 

def div_error_formatter(error):  
    return '<div class="error">%s</div>' % htmlfill.html_quote(error) 

def errorform(form_content, errors):
    """Return an HTMLFILLed form, according to those errors"""
    formats = {'default': p_error_formatter,
               'p': p_error_formatter,
               'ol': ol_error_formatter,
               'div': div_error_formatter}
    
    defs = errors.value if errors else {}
    errs = errors.unpack_errors() if errors else {}

    return htmlfill.render(form_content, defs, errs,
                           add_attributes={'+class': ' error'},
                           error_formatters=formats)

#
# NOTE: this will add a "class='error'" to each <input /> tag also.
##

############################################################# project/contorller/prefs.py
import formencode
from formencode import validators
from formencode import htmlfill

class UniqueUsername(formencode.FancyValidator):
    def _to_python(self, value, state):
        chk = model.User.query.filter_by(username=value).first()
        if chk:
            raise formencode.Invalid(
                "Ce nom d'usager existe déjà",
                value, state)
        return value


class PasswdValidate(formencode.Schema):
    username = formencode.All(validators.String(min=6,max=20),
                              validators.PlainText(),
                              UniqueUsername())
    passwd1 = validators.String(not_empty=True)
    passwd2 = validators.String(not_empty=True)

    chained_validators = [validators.FieldsMatch('passwd1', 'passwd2')]


### CONTROLLER

class PrefsController(BaseController):
    def passwd(self):

        return render("/prefs/passwd.html")

    def save_passwd(self):

        try:
            prm = PasswdValidate().to_python(request.params)
        except validators.Invalid, e:
            c.errors = e
            return render('/prefs/passwd.html')


##################################### project/templates/prefs/passwd.html (Mako template)
##
## NOTE tue usage of: buffered="True" .. required for h.errorform() to get the content
##
${h.errorform(form(), c.errors)}
<%def name="form()" buffered="True">
<form id="editform" action="${h.url_for(action='save_passwd')}" method="post">

  <ol>
    <li class="first"><label for="username">Username<em>*</em></label>
        ${h.text_field('username', size=15)}<br />
        <form:error name="username" format="ol" />
    </li>

    <li><label>Password:</label>
        <input type="password" name="passwd1" size="15" />
        <form:error name="passwd1" />
    </li>

    <li class="first"><label>Punch it again:</label>
        <input type="password" name="passwd2" size="15" />
        <form:error name="passwd2" />
    </li>
  </ol>
  <br />

  <input type="submit" value="Save" />

</form>
</%def>

################################################################# Add your own stylesheet

/* with stuff :) */

Python 加速Python的工具

If you need to speed up your Python program there are several possible approaches, with varying degree of effort needed, here is (probably not complete) overview:

   1. Rewrite your Python code by parallelizing or optimizing/replacing/tuning algorithm(s), e.g. using: 
          * Hadoop or Disco
                o Open source implementations of MapReduce 
          * Parallel Python 
          * Message Passing Interface (MPI)
                o Frequently used for numerical programming
          * Bulk Synchronous Parallel (BSP) 
          * RPyC
                o RPC for Distributed/Parallel Programming
          * Twisted
                o Network libraries for Distributed/Parallel Programming
          * Profiling Tools
          * Threading or Microthreads (Stackless) 
   2. Use a tool that can speed up your code more or less unchanged
          * Psyco
                o Just in time compiler, note: this is probably the easiest approach to try
          * Pyrex
                o Write and compile Python with a flavor of C data structures
          * Cython 
          * PyJs 
                o Compile (large subset of) Python to Javascript, note: probably more interesting for client development (ajax) than server side.
          * Rpython
                o Compile (large subset of) Python to native code, note: part of PyPy project
          * PyStream
          * GPULib
          * Shedskin 
                o Compile (large subset of) Python to C++, some benchmarks
   3. Replace (parts of) your Python code with another language
          * Simplified Wrapper and Interface Generator (SWIG) 
                o Use C/C++ from Python
          * Fortran to Python Interface Generator (F2PY) 
                o Use Fortran from Python
          * llvm-py 
                o Code and Compile to Assembler for running on Low-Level Virtual Machine
          * CorePy 
                o Write Assembly Code in Python
          * Weave 
          * PyInline 
          * Boost.Python
          * Cinpy 
                o C code blended with Python (using ctypes) and runtime compilation with tcc

Python 在Python中将数字从负基础系统转换为十进制

def negabase(number, base):
    """ Calculates the decimal value of a number
        in a given negative base """
    hbase = 1
    sign = 1
    result = 0
    while number>0:
        digit = number % 10
        result += sign*hbase*digit
        number = int(number / 10)
        sign = -1*sign
        hbase *= base
    return result

if __name__ == '__main__':
    # See http://en.wikipedia.org/wiki/Negative_base
    print negabase(12243, 10) # 8163

Python GSU登录

'''
Syntax 1:  "python gsulogin.py <username> <password> [remove old login]"

Example 1:  "python gsulogin.py js87235 mypassword"
And then if you see that you have too many active users:
Example 1b: "python gsulogin.py js87235 mypassword yes"

###########################################################################

Syntax 2:  "python gsulogin.py logout <user_key>
NOTE:  Your user_key will be printed out when you use this script to login

Example 2:  "python gsulogin.py logout 141.165.34.45_CUX614P30KDGNBAT"

'''

import sys, getpass, urllib, re, time

separator = "-----------------------------"
print(separator + 
       "\n   Georgia Southern Network Login\n" + 
       separator)

if len(sys.argv) >= 3:
    #if logging out, just send logout url
    if (sys.argv[1] == "logout"):
        test = urllib.urlopen('https://smrtsrv2.georgiasouthern.edu/auth/perfigo_logout.jsp',
                            'user_key=' + sys.argv[2] )
        print test.read()
        sys.exit()
        
    username = sys.argv[1]
    password = sys.argv[2]
    if len(sys.argv) > 3:
        removeOld = '1'
        print "removing old..."
    else:
        removeOld = '0'
else:
    username = raw_input("Username: ")
    password = getpass.getpass()
    sometext = raw_input("Enter 'y' if you want to remove old login: ");
    if (sometext == 'Y'):
        removeOld = '1'
    else:
        removeOld = '0'

#put it inside a function so multiple tries can be done
#for autorunning, when system is just started up and not connected to internet yet
tries = 0;
def tryLogin(tries):
    tries = tries + 1
    if (tries > 4):
        return 0
        
    try:

        print "\nGetting userip from test run:"
        test = urllib.urlopen('https://smrtsrv2.georgiasouthern.edu/auth/perfigo_cm_validate.jsp')
        jane = test.read()
        pattern = re.compile('userip[^1-9]*([1-9.]*)')
        userip = pattern.search(jane).groups()[0]
        print separator + "\nYour CAA IP: " + userip + "\n" + separator
        
        print "\nAttempting to login..." 
        data = urllib.urlopen('https://smrtsrv2.georgiasouthern.edu/auth/perfigo_cm_validate.jsp',
                               'reqFrom=perfigo_login.jsp' +
                               '&provider=Student' + 
                               '&username=' + username +
                               '&password=' + password +
                               '&remove_old=' + removeOld +
                               '&userip=' + userip +
                               '&uri=http://www.google.com/' +
                               '&cm=ws32vklm')
        print separator
        bob = data.read()
        if (re.search("Too many users", bob)):
            print "Too many users on account, use the syntax: \n\n   python login.py [user] [pass] [anything you want]\n\nto attempt to remove an old user account.\n"
        elif (re.search("Invalid Clean Access Server", bob)):
            print "Invalid Clean Access Server (wtf is this?)"
        elif (re.search("Invalid username or password", bob)):
            print "Invalid username or password"
        elif (re.search("successfully logged on the network", bob)):
            print "You have been successfully logged into network."
            patter = re.compile('userkey[^1-9]*([^"]*)')
            print separator + "\nYour user_key is:  " + patter.search(bob).groups()[0] + "\n"+separator
        else:
            print bob + "\n\n ERROR, unknown response, printed out the whole HTML above.  Have fun reading through it."
            
    except IOError, e:
        print "IOError:", e, "\n", seperator
        print "Waiting 5 seconds and trying again..."
        time.sleep(5)
        tryLogin(tries)

tryLogin(tries)

Python 简单的csv转储脚本

SELECTQ="SELECT * FROM category"
FILENAME="dump.csv"

import MySQLdb
import csv

db = MySQLdb.connect(host="localhost", user="root", passwd="", db="sakila")
dump_writer = csv.writer(open(FILENAME,'w'), delimiter=',',quotechar="'")
cursor = db.cursor()
cursor.execute(SELECTQ)
result = cursor.fetchall()
for record in result:
    dump_writer.writerow(record)
    
db.close()

Python Tumblr标签列表生成器

import cgi

import md5

import time

import datetime

import math

from xml.dom import minidom

import urllib

import sys



from google.appengine.api import users

from google.appengine.ext import webapp

from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.ext import db

from google.appengine.api import urlfetch

from django.utils import simplejson as json





# Database Classes

class TumblrCache(db.Model):

    cache_id = db.StringProperty(required=True)

    content = db.StringProperty(multiline=True)

    date = db.DateTimeProperty(auto_now_add=True)

    

class Flush(webapp.RequestHandler):

  def get(self):

      self.response.headers['Content-Type'] = 'text/plain'

      w = self.response.out

      

      query = TumblrCache.all()

      for q in query:

          q.delete()

      

      w.write('FLUSHED!')

      



class MainPage(webapp.RequestHandler):

  def get(self):

      self.response.headers['Content-Type'] = 'text/plain'



class TagCloud(webapp.RequestHandler):

  def get(self):

      self.response.headers['Content-Type'] = 'text/plain'

      w = self.response.out

      # Thu thap cac bien can thiet

      tumblr_api = cgi.escape(self.request.get('url').encode('utf-8')) + '/api/read'

      tumblr_api = 'http://im.doquangtu.net/api/read'

      cache_id = md5.new(tumblr_api).hexdigest()

      # Neu da co cache, thi tra lai cache

      try:

          cache = TumblrCache.gql("WHERE cache_id = :1 LIMIT 1", cache_id)[0]

      except:

          cache = TumblrCache(cache_id=cache_id)

          

      content = cache.content

      if content == None:

          content = ''

      

      d = (((cache.date.toordinal()-719163)*24+cache.date.hour)*60+cache.date.minute)*60+cache.date.second

      d = time.time() - d

           

      if d > 24*60*60 or len(content) < 5:

          # Da qua 24h cache trong he thong

            form_fields = {

              "filter": "text"

            }

            

            loop = True

            start = 0

            taglist = {}

            



        

            try:

                while loop == True:

                    form_fields['start'] = int(start) * 20

                    form_data = urllib.urlencode(form_fields)

                    result = urlfetch.fetch(tumblr_api + '?filter=text&start=' + str(form_fields['start']))

                    result = result.content #.encode('utf-8')



                    xdom = minidom.parseString(result)

                    try:

                        posts = xdom.firstChild.getElementsByTagName("posts")[0].getElementsByTagName("post")

                        for post in posts:

                            post_id = post.getAttribute("id")

                            tags = post.getElementsByTagName("tag")

                            try:

                                for tag in tags:

                                    # Duyet qua tags

                                    t = "" . join(t.nodeValue for t in tag.childNodes if t.nodeType == t.TEXT_NODE)

                                    t = t.strip()

                                    #w.write(t)

                                    if t not in taglist:

                                        taglist[t] = 1

                                    else:

                                        taglist[t] = taglist[t] + 1

                            except:

                                # Loi & khong tim thay tags nao

                                i = 0

                    except:

                        # Loi & khong tim thay posts nao

                        i = 0

                    

                    # kiem tra xem co thoat vong lap hay chua

                    try:

                        posts = xdom.firstChild.getElementsByTagName("posts")[0].getAttribute("total")

                        posts = int(math.ceil(float(posts) / float(20)) - 1)

                        if start + 1 > posts:

                            loop = False

                        else:

                            start = start + 1

                            loop = True

                    except:

                        loop = False

            except:

                loop = False

                

            # Viet tags

            txt = json.JSONEncoder().encode( taglist )

            

            cache.content = txt

            cache.date = datetime.datetime.today()

            cache.put()            

            

            w.write(taglist)

      else:

          # Chua qua 24h cache trong he thong

          # Tra ve cache data 

          content = cache.content

          w.write( "var myJSONObject = " + content + ";" )



application = webapp.WSGIApplication(

                                     [('/', MainPage),

                                      ('/tagcloud', TagCloud),

                                      ('/flush', Flush)],

                                     debug=True)







def main():

  run_wsgi_app(application)



if __name__ == "__main__":

  main()

Python 人工C“¡E“³ã??®ã,½ãƒ¼ã,¹ã€€ã,¢ハ«ã,'リã,ºãƒã,³ãƒ³ãƒ†ã,¹ãƒå<‰那张·ä¼šã??«的ΔΣ|

#!-*- coding:utf-8 -*-
import sys
import os
import string
import urllib
import urllib2
import math
import htmlentitydefs
import re
from django.shortcuts import render_to_response
from google.appengine.api import users
from django.http import HttpResponseRedirect
from BeautifulSoup import BeautifulSoup
from google.appengine.api import urlfetch
from base64 import b64encode


#初期ページ
def Mainpage(request):
    return render_to_response('input.html',)


#質問をもらって回答を出すまでのメソッド
def getAnswer(request):
    search_text = ''
    from_user = 'username'
    try:
        search_text=request.POST['query']
        search_text=search_text.encode('utf-8')
    except:pass
    if search_text == '':
        search_text,from_user = GetresponseTwitter()
        #print &gt;&gt;sys.stderr,'search_text=%s'%search_text
        search_text = search_text.encode('utf-8')
    to_ = ''
    from_ = ''
    
    #質問文をそのまま検索にかける
    if search_text!='' :
        url = 'http://pcod.no-ip.org/yats/search?query=%s&amp;rss' % (search_text)
        xml_search2=urlfetch.fetch(url).content
        entry = BeautifulSoup(xml_search2)('entry')
        words=[]
        titledict=dict()
        answer = ''
        for en in entry:
            #print &gt;&gt;sys.stderr,en('summary')[0]
            words=en('summary')[0](text = True)[0].split(' ')
            update_before = en('updated')[0](text = True)[0]
            #print &gt;&gt;sys.stderr,status_id
            try:
                regexp = re.compile('username')
                if regexp.search(en('summary')[0](text = True)[0])== None:#tam_botの発言は無視
                    from_=words[0].split('@')[1]
                    to_=words[2].split('@')[1]
                    
                    url='http://pcod.no-ip.org/yats/search?query=user:'+to_+'%20%40'+from_+'%20&amp;lang=ja&amp;rss'
                    #print &gt;&gt;sys.stderr,url
                    xml_search=urlfetch.fetch(url).content
                    title1to2 = BeautifulSoup(xml_search)('entry')
                    for x in title1to2:
                        #titledict[x('updated')[0](text = True)[0]]=x('summary')[0].contents[0]
                        update_after=x('updated')[0](text = True)[0]
                        if  update_after &gt; update_before:
                            #print &gt;&gt;sys.stderr,'update_before=%s update_after=%s'%(update_before,update_after)
                            tmp_answer=x('summary')[0](text = True)[0].split(' ')[3]
                            regexp1 = re.compile('@')
                            if regexp1.search(tmp_answer)==None and tmp_answer!='':
                                print &gt;&gt;sys.stderr,'answer=%s'%tmp_answer
                                answer = tmp_answer
                                break#一個でもみつかればbreak
                    if tmp_answer != '':
                        break
            except:pass
        #print &gt;&gt;sys.stderr,'answer=%s'%answer
        
        
        if answer != '':#検索結果があった場合
            post = '@%s %s'%(from_user,answer)
        else:#検索結果がなかった場合
            #############文章だけで回答が出せない場合#############
            question_word=GetKeitaiso_mecapi(search_text).encode('utf-8')
            q = {'query':question_word,'lang' : 'ja'}            
            url = 'http://pcod.no-ip.org/yats/search?rss&amp;' + urllib.urlencode(q)
            xml=urlfetch.fetch(url).content
            #print &gt;&gt;sys.stderr,xml
            entry = BeautifulSoup(xml)('entry')
            for en in entry:
                tmp_answer=en('summary')[0](text = True)[0].split(' : ')[1].rstrip()
                regexp = re.compile('tam_bot')
                if regexp.search(en('summary')[0](text = True)[0])== None:#tam_botの発言は無視
                    regexp1 = re.compile('@')
                    if regexp1.search(tmp_answer)==None and tmp_answer!='' and search_text.rstrip()!=tmp_answer.rstrip().encode('utf-8'):
                        answer = tmp_answer
                        #print &gt;&gt;sys.stderr,'tmp_answer=%s'%tmp_answer
                        break
            post = '@%s %s'%(from_user,answer)
            
            #########最後までわからない場合##############
            if answer == '':
                post = u'@%s %s'%(from_user,unicode(search_text,'utf-8'))
    
    #print &gt;&gt;sys.stderr,'post=%s'%post
    Twitter(post)        
    return render_to_response('outanswer.html',{'search_text':search_text,'post':post})

    
#質問文章を形態素解析かけて単語を取り出す
def GetKeitaiso_mecapi(question):
    question_word=''
    word_rank=dict()
    i=0
    q={'format':'xml','sentence':question,'response':'','filter':'uniq'}
    url='http://mimitako.net/api/mecapi.cgi?'+ urllib.urlencode(q)
    xml_question=urlfetch.fetch(url).content
    #print &gt;&gt;sys.stderr,BeautifulSoup(xml_question)('mecabresult')
    ngword=[u'何',u'誰']
    for mlist in BeautifulSoup(xml_question)('mecabresult'):
        for m in mlist('word'):
            term=m('feature')[0].contents[0].split(',')
            word=m('surface')[0]
            word=tr(u'!&quot;#$%&amp;()=-^~\|[]{}@:`*;+,.&lt;&gt;/\?_。、・', u'                                   ',word)
            word=string.replace(word, ' ', '')
            #print &gt;&gt;sys.stderr,'%s %s %s %s %s %s %s'%(term[0],term[1],term[2],term[3],term[4],term[5],word)
            if (term[0]==u'名詞' or term[0]==u'動詞' or term[0]==u'形容詞') and word != '' and word not in ngword:
                cntwd = int(countWord(word))
                #print &gt;&gt;sys.stderr,'%s %s %s'%(term[0],word,cntwd)
                if i&lt;3 and cntwd&lt;100000000:
                    question_word = u'%s %s'%(question_word,word)
                    print &gt;&gt;sys.stderr,'%s %s %s'%(term[0],word,cntwd)
                    i=i+1
    return question_word

#Twitterへのpostメソッド
def Twitter(post):
    #basic auth
    username='username'
    password='password'
    url = 'http://twitter.com/statuses/update.json'
    payload = urllib.urlencode({&quot;status&quot;: post.encode(&quot;utf-8&quot;)})
    base64string =b64encode(&quot;%s:%s&quot; % (username, password))
    headers = {&quot;Authorization&quot;: &quot;Basic %s&quot; % base64string}
    json_result = urlfetch.fetch(url, payload=payload, method=urlfetch.POST, headers=headers).content
    return 1


#Twitterからあるユーザの直近の質問をとってくる
def GetresponseTwitter():
    #basic auth
    username='username'
    password='password'

    url = 'http://twitter.com/statuses/replies.xml'
    base64string =b64encode(&quot;%s:%s&quot; % (username, password))
    headers = {&quot;Authorization&quot;: &quot;Basic %s&quot; % base64string}
    xml_result=urlfetch.fetch(url, payload='', method=urlfetch.POST, headers=headers).content
    result_comment=htmlentity2unicode(BeautifulSoup(xml_result)('text')[0].contents[0].split(' ')[1])
    from_user=BeautifulSoup(xml_result)('screen_name')[0].contents[0]
    return result_comment,from_user


#googleの検索結果数を見る
def countWord(word):
    q = {#'q':'\&quot;'+question+'\&quot;',
        'q':word.encode('utf-8'),
        'v' : 1.0,
        'hl' : 'ja',
        'rsz': 'small'
    }
    url=u'http://ajax.googleapis.com/ajax/services/search/web?'+ urllib.urlencode(q)
    json_result=urlfetch.fetch(url).content
    json_result = string.replace(json_result, 'null', '&quot;null&quot;')
    try:
        dict_result=eval('dict('+json_result+')')        
        return dict_result['responseData']['cursor']['estimatedResultCount']
    except:
        return 0


def tr(pattern, repl, string):
    m = dict(zip(pattern, repl))
    return ''.join((m.get(c,c) for c in string))



#数値文字参照→文字へ
def htmlentity2unicode(text):
    # 正規表現のコンパイル
    reference_regex = re.compile(u'&amp;(#x?[0-9a-f]+|[a-z]+);', re.IGNORECASE)
    num16_regex = re.compile(u'#x\d+', re.IGNORECASE)
    num10_regex = re.compile(u'#\d+', re.IGNORECASE)
    
    result = u''
    i = 0
    while True:
        # 実体参照 or 文字参照を見つける
        match = reference_regex.search(text, i)
        if match is None:
            result += text[i:]
            break
        
        result += text[i:match.start()]
        i = match.end()
        name = match.group(1)
        
        # 実体参照
        if name in htmlentitydefs.name2codepoint.keys():
            result += unichr(htmlentitydefs.name2codepoint[name])
        # 文字参照
        elif num16_regex.match(name):
            # 16進数
            result += unichr(int(u'0'+name[1:], 16))
        elif num10_regex.match(name):
            # 10進数
            result += unichr(int(name[1:]))
        
    return result