程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java 獲得以後函數名的完成代碼

java 獲得以後函數名的完成代碼

編輯:關於JAVA

java 獲得以後函數名的完成代碼。本站提示廣大學習愛好者:(java 獲得以後函數名的完成代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java 獲得以後函數名的完成代碼正文


空話不多說,直接上代碼

import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Java完成相似C/C++中的__FILE__、__FUNC__、__LINE__等,重要用於日記等功效中。
*
* @version 1.0 
*
*/
public abstract class CommonFunction {

/**
* 打印日記時獲得以後的法式文件名、行號、辦法名 輸入格局為:[FileName | LineNumber | MethodName]
*
* @return
*/
public static String getFileLineMethod() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
StringBuffer toStringBuffer = new StringBuffer("[").append(
traceElement.getFileName()).append(" | ").append(
traceElement.getLineNumber()).append(" | ").append(
traceElement.getMethodName()).append("]");
return toStringBuffer.toString();
}

// 以後文件名
public static String _FILE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getFileName();
}

// 以後辦法名
public static String _FUNC_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getMethodName();
}

// 以後行號
public static int _LINE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getLineNumber();
}

// 以後時光
public static String _TIME_() {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return sdf.format(now);
}


p>

這裡用的是html全頁面抓取 所以對那些feed沒有全文輸入的站點來講, 這個app 可以去把它要隱蔽的文字抓來
在加載的時刻會花許多時光由於法式會主動到一個沒有全文輸入的頁面上抓取一切的文章列表,作者信息,更新時光,和文章全文。。所以翻開的時刻請耐煩。。。下一步會參加數據存儲部門,如許就會快了。。

技巧預備
 
前端:

1. CSS 在信仰簡略之上的准繩上 twitter的bootstrap.css知足了我年夜多半的請求 小我超愛好它的 Grid System
2. Javascript上, 固然選用了jQuery 自從我開端在我的第一個小項目上用了jQuery 後 我就愛上了它   誰人靜態的目次體系就是用jQuery疾速生成的
   為了合營bootstrap.css, bootstrap-dropdown.js 也用到了

辦事器:

這個app有兩個版本:
     一個跑在我的Apache上, 然則由於我的收集是ADSL, 所以ip一向會變根本上只是我在我的所謂的局域網內自測用的。。這個版本是純Django的
     另外一個跑在Google App Engine上 地址是 http://l2zstory.appspot.com 在把Django 設置裝備擺設到GAE的時刻我花了許多工夫才把框架搭起來

概況請見: Using Django with Google App Engine GAE: l2Z Story Setup-Step 1           http://blog.sina.com.cn/s/blog_6266e57b01011mjk.html

後台:

重要說話是Python--不說明, 自從熟悉Python後就沒有分開它

重要用到的module是

1. BeautifulSoup.py 用於html 的解析--不說明
2. feedparser.py 用於對feed xml的解析--網上有許多人說GAE不支撐feedparser..這裡你們獲得謎底了。。可以。。這裡我也是花了良久才弄明確究竟是怎樣回事。。總之簡略講就是: 可以用!然則feedparser.py這個文件必需放到跟app.yaml統一個目次中 否則會湧現網上世人說的弗成以import feedparser的情形

數據庫:
Google Datastore: 鄙人一步中, 這個法式會每隔30分鐘醒來 一一檢查各個站點有無更新並抓取更新後的文章並存入Google 的Datastore中

App 的設置裝備擺設

遵守Google的規矩, 設置裝備擺設文件app.yaml 以下:
這裡重要是界說了一些static directory--css 和 javascript的地點地


application: l2zstory
version: 1
runtime: python
api_version: 1


handlers:
  
- url: /images
  static_dir: l2zstory/templates/template2/images
- url: /css
  static_dir: l2zstory/templates/template2/css
- url: /js
  static_dir: l2zstory/templates/template2/js 
- url: /js
  static_dir: l2zstory/templates/template2/js
- url: /.*
  script: main.py

URL的設置裝備擺設


這裡采取的是Django 裡的正則表達式


from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()


urlpatterns = patterns('',
    # Example:
    # (r'^l2zstory/', include('l2zstory.foo.urls')),


    # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
    # to INSTALLED_APPS to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),


    # Uncomment the next line to enable the admin:
    # (r'^admin/(.*)', admin.site.root),
    (r'^$','l2zstory.stories.views.L2ZStory'),
    (r'^YukiLife/','l2zstory.stories.views.YukiLife'),
     (r'^ZLife_Sina/','l2zstory.stories.views.ZLife_Sina'),
     (r'^ZLife/','l2zstory.stories.views.ZLife')
)

Views的細節


對Django比擬熟習的人應當會從url的設置裝備擺設中看到view的名字了 我只把L2ZStory的這個view貼出來由於其他的在view裡的架構至多是差不多的

#from BeautifulSoup import BeautifulSoup
from PyUtils import getAboutPage
from PyUtils import getPostInfos

def L2ZStory(request):
    url="feed://l2zstory.wordpress.com/feed/"
    about_url="http://l2zstory.wordpress.com/about/"
    blog_type="wordpress"
    htmlpages={}
    aboutContent=getAboutPage(about_url,blog_type)
    if aboutContent=="Not Found":
        aboutContent="We use this to tell those past stories..."
    htmlpages['about']={}
    htmlpages['about']['content']=aboutContent
    htmlpages['about']['title']="About This Story"
    htmlpages['about']['url']=about_url
    PostInfos=getPostInfos(url,blog_type,order_desc=True)
    return render_to_response('l2zstory.html',
{'PostInfos':PostInfos,
'htmlpages':htmlpages
})

這裡重要是構建一個dictionary of dictionary  htmlpages 和一個list of dictionary PostInfos
htmlpages 重要是存貯站點的 About, Contact US 之類的頁面
PostInfos 會存貯一切文章的 內容, 作者, 宣布時光 之類的

這外面最主要的是PyUtils。。這是這個app的焦點

PyUtils的細節

我把一些我以為比擬主要的細節加深了 並加了評論


import feedparser
import urllib2
import re
from BeautifulSoup import BeautifulSoup
header={
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:8.0.1) Gecko/20100101 Firefox/8.0.1',
}

#用來誘騙網站的後台。。象新浪這類的網站對我們這類的app非常不友愛。。。願望它們可以多象被牆失落的wordpress學一學。。


timeoutMsg="""
The Robot cannot connect to the desired page due to either of these reasons:
1. Great Fire Wall
2. The Blog Site has block connections made by Robots.
"""

def getPageContent(url,blog_type):
    try:
        req=urllib2.Request(url,None,header)
        response=urllib2.urlopen(req)
        html=response.read()
        html=BeautifulSoup(html).prettify()
        soup=BeautifulSoup(html)
        Content=""
        if blog_type=="wordpress":
            try:
                for Sharesection in soup.findAll('div',{'class':'sharedaddy sd-like-enabled sd-sharing-enabled'}):
                    Sharesection.extract()
                for item in soup.findAll('div',{'class':'post-content'}):
                    Content+=unicode(item)
            except:
                Content="No Post Content Found"
        elif blog_type=="sina":
            try:
                for item in soup.findAll('div',{'class':'articalContent  '}):
                    Content+=unicode(item)
            except:
                Content="No Post Content Found"


        #關於分歧的網站類型 運用分歧的過濾器


    except:
        Content=timeoutMsg
    return removeStyle(Content)


def removeStyle(Content):
    #add this to remove all the img tag : (<img +(\w=\".*\")>)|(</img>)|(src=\".*\")|
    patn=re.compile(r"(align=\".*\")|(id=\".*\")|(class=\"*\")|(style=\".*\")|(</font>)|(<font.*\">)|(<embed +(\w*=\".*\")>)|(</embed>)")
    replacepatn=""


    Content=re.sub(patn,replacepatn,Content)
    #應用正則表達式把抓取的內容中那些格局統統去失落 如許獲得的文字比擬純潔
    return Content
  
def getPostInfos(url,blog_type,order_desc=False):
    feeds=feedparser.parse(url)
    PostInfos=[]
    if order_desc:
        items=feeds.entries[::-1]
    else:
        items=feeds.entries
    Cnt=0
    for  item in items:
        PostInfo={}
        PostInfo['title']=item.title
        PostInfo['author']=item.author
        PostInfo['date']=item.date
        PostInfo['link']=item.link
      
        if blog_type=="wordpress":
            Cnt+=1
            if Cnt<=8:
                PostInfo['description']=getPageContent(item.link,blog_type)
            else:
                PostInfo['description']=removeStyle(item.description)
        elif blog_type=="sina":
            PostInfo['description']=removeStyle(item.description)
          
      
        PostInfos.append(PostInfo)
      
    return PostInfos

template 的概覽

在簡略之上的准繩的鼓舞下, 一切的站點都同一應用一個template 這個template 只接收兩個變量--前文中提到的htmlpages 和 PostInfos
主要的片段是:

<div class="page-header">
                              <a href="{{htmlpages.about.url}}" name="{{htmlpages.about.title}}"><h3>{{htmlpages.about.title}}</h3></a>
                            
                         </div>
                         <p>
                              {{htmlpages.about.content}}
                         </p>
                         {%for item in PostInfos%}
                         <div class="page-header">
                              <a href="{{item.link}}" name="{{item.title}}"><h3>{{item.title}}</h3></a>
                            
                         </div>
                         <p><i>author: {{item.author}}    date: {{item.date}}</i></p>
                         <p>{{item.description}}</p>
                         {%endfor%}
                    </div>

總結

一句話, 我愛逝世Python了
兩句話, 我愛逝世Python了,我愛逝世Django了
三句話, 我愛逝世Python了,我愛逝世Django了,我愛逝世jQuery了。。。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved