使python导入更有条理? [英] Making python imports more structured?

查看:89
本文介绍了使python导入更有条理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码工作但看起来很乱,所以这可能是一个代码审查问题,我没有研究足够的pythons约定,知道如何构建和组织我的文件的开头更pythonic。我基本上只是粘贴在进口中,所以它们可能是重复的,不再需要或错误订购。你能告诉我如何构建我的导入的任何建议吗?或者我可以留下这样的代码来专注于我自己的功能吗?

The code works but looks messy so this might be a code review question where I didn't study enough of pythons conventions to know how to structure and organize the beginning of my file more pythonic. I basically just pasted in imports so they could be duplicates, not needed anymore or wrongly ordered. Can you advice anything how to structure my imports or can I leave code like this to focus on my own functions?

文件1:

from __future__ import with_statement
import logging
import os
from google.appengine.api.users import is_current_user_admin, UserNotFoundError
import time
import cgi
import geo.geotypes
import main
import captcha
from google.appengine import api
from google.appengine.runtime import DeadlineExceededError
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.blobstore import BlobInfo
from google.appengine.ext.db import djangoforms
from django import forms
from django.core.exceptions import ValidationError
from django.utils import translation
from datetime import datetime, timedelta
os.environ['DJANGO_SETTINGS_MODULE'] = 'conf.settings'
from django.conf import settings
from django.template import RequestContext
from util import I18NHandler
import util
from google.appengine.api import urlfetch, taskqueue
from django.template.defaultfilters import register
from django.utils import simplejson as json
from functools import wraps
from google.appengine.api import urlfetch, taskqueue, users, images
from google.appengine.ext import db, webapp, search, blobstore
from google.appengine.ext.webapp import util, template
from google.appengine.runtime import DeadlineExceededError
from random import randrange
import Cookie
import base64
import cgi
import conf
import datetime
import hashlib
import hmac
import logging
import time
import traceback
import urllib
import twitter_oauth_handler
from twitter_oauth_handler import OAuthClient
from geo.geomodel import GeoModel
from django.utils.translation import gettext_lazy as _
webapp.template.register_template_library('common.templatefilters')

文件2(有几条指令在这里,我不明白):

File 2 (there's are several instructions here I don't understand):

from __future__ import with_statement
                # -*- coding: utf-8 -*-
import facebookconf
import os, wsgiref.handlers
os.environ[u'DJANGO_SETTINGS_MODULE'] = u'conf'
import util
import time
import logging
import urllib
import wsgiref.handlers
import appengine_admin
import cgi
import captcha
import re
import hashlib
import string
import hmac
import twitter_oauth_handler
from twitter_oauth_handler import OAuthClient
os.environ['DJANGO_SETTINGS_MODULE'] = 'conf.settings'
from geo.geomodel import GeoModel
from google.appengine.dist import use_library
from google.appengine.ext import blobstore, webapp, db, search
# template import must be run before other Django modules imports
from google.appengine.ext.webapp import blobstore_handlers, util, template
from google.appengine.ext.blobstore import BlobInfo
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import files, images, mail, memcache, users
from django.conf import settings
# Force Django reload
settings._target = None
from util import I18NHandler, FacebookBaseHandler
from google.appengine.ext.db import djangoforms
from django.utils import translation
from django.utils import simplejson as json
from django.contrib.formtools.preview import FormPreview
from random import choice
from urllib import quote
from google.appengine.api.users import is_current_user_admin, UserNotFoundError
from google.appengine.api import urlfetch
import random
import datetime
from datetime import timedelta
from django.utils.translation import gettext_lazy as _
from django.template import defaultfilters

我怎么知道导入的时间是自函数移动或重新启动后不再使用感动?为什么我不能在一个地方为多个文件指定相同的导入,我必须在两个文件中指定相同的导入?我可以想象将处理导入移动到一个单独的文件,即 imports.yaml ,以指定该目录中所有python文件的导入或同样。

How do I know when an import is no longer used since the function was moved or removed? Why can't I specify the same import for multiple files at one place and I must spec the same import in both files? I can imagine moveing handling imports to a separate file i.e. imports.yaml to specify imports for all python files in that directory or likewise.

推荐答案

一旦您使用pylint识别重复和未使用的导入,并根据PEP8组织它们,如其他答案所示,您可以通过更改导入包的方式进一步清理它。

Once you've used pylint to identify duplicate and unused imports, and organized them according to PEP8 as the other answers suggest, you can further clean it up by changing the way you import packages.

而不是

from google.appengine.api import urlfetch, taskqueue, users, images

你可以只需从google.appengine import api

you could just do

then you would need to put "api.urlfetch", "api.taskqueue", etc. wherever you use those.

这不是正确的方式,它只是另一种方式。你必须选择你喜欢的那个。

This isn't the "right" way to do it, it's just another way. You'll have to choose which one you prefer.

另请注意,您可以使用谷歌的别名:

Also note that you can use aliases:

from google.appengine import api as gaeapi

现在你要放gaeapi.urlfetch。如果你需要从多个包中导入名为api的模块,这很有用。

now you would put "gaeapi.urlfetch". This is useful if you need to import modules called "api" from multiple packages.

另外,回答你的问题为什么我不能为多个包指定相同的导入文件在一个地方,我必须在两个文件中指定相同的导入?,如果你在多个文件中导入相同的包,这可能表明这些文件密切相关,应该合并到一个文件中。与C ++或Java不同,每个类都是自己的文件,pythonic方法是使每个模块(文件)尽可能自包含,这通常意味着它们包含多个类和函数。

Also, to answer your question "Why can't I specify the same import for multiple files at one place and I must spec the same import in both files?", if you're importing the same packages in multiple files, that could indicate those files are closely related, and should be merged into a single file. Unlike C++ or Java, where every class is its own file, the pythonic way is to make each module (file) as self-contained as possible, which usually means they contain multiple classes and functions.

这篇关于使python导入更有条理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆