Python常量工具类
1.定义常量类constant.py
# -*- coding: utf-8 -*
"""常量工具类 author: Jill usage:
from constant import _Const
const = _Const()
const.PI = 3.14
""" class _Const:
class ConstError(TypeError):
pass class ConstCaseError(ConstError):
pass def __setattr__(self, name, value):
if name in self.__dict__:
raise self.ConstError("Can't change const(%s) value" % name)
if not name.isupper():
raise self.ConstCaseError("Const name %s is not all uppercase" % name) self.__dict__[name] = value def __getitem__(self, key):
if key in self.__dict__:
return self.__dict__[key]
else:
raise self.ConstError("Can't return const %s, No Existing Key!" % key) def __delattr__(self, name):
if name in self.__dict__:
raise self.ConstError("Can't unbind const(%s)" % name)
raise NameError(name)
2.将常量放在一个模块中common_constant.py
# -*- coding: utf-8 -*
"""常用常量定义 author: Jill usage:
from common_constant import const
print(const.ROOT_PATH)
"""
from common.constant import _Const const = _Const() const.PI = 3.14 if __name__ == "__main__":
print(const.PI)
print(const['PI'])
3.在其他模块里使用test.py
from common_constant import const print(const.ROOT_PATH)
Python常量工具类的更多相关文章
- Python数据库工具类MySQLdb使用
MySQLdb模块用于连接mysql数据库. 基本操作 # -*- coding: utf-8 -*- #mysqldb import time, MySQLdb ...
- Java调用Python脚本工具类
[本文出自天外归云的博客园] 在网上查了很多方法都不成功,在google上搜到一篇文章,做了一些小修改,能够处理中文输出.提取一个运行python脚本的Java工具类如下: package com.a ...
- 一个python爬虫工具类
写了一个爬虫工具类. # -*- coding: utf-8 -*- # @Time : 2018/8/7 16:29 # @Author : cxa # @File : utils.py # @So ...
- Python Excel工具类封装, 给excel表头搞点颜色
封装Excel工具类 我们常用的excel工具类,读有xlrd,写有xlwt.有读有写,新一代库有pandas,openpyxl等等. 大家用法都差不多,今天博主就介绍新手最爱,我也爱的xlrd和xl ...
- Python配置工具类ConfigParser使用
ConfigParser模块定义了类ConfigParser,用于实现配置文件解释器.该模块ConfigParser在Python3中,已更名为configparser. 一,函数介绍 1.读取配置文 ...
- Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
Go/Python/Erlang编程语言对比分析及示例 本文主要是介绍Go,从语言对比分析的角度切入.之所以选择与Python.Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性, ...
- arcgis python脚本工具实例教程—栅格范围提取至多边形要素类
arcgis python脚本工具实例教程-栅格范围提取至多边形要素类 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 功能:提取栅格数据的范围, ...
- Python工具类(二)—— 操作时间相关
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = '操作时间的工具类' "" ...
- Python工具类(一)—— 操作Mysql数据库
如何调用直接看__main__函数里如何调用此工具类就阔以啦! # encoding=utf-8 import pymysql # 导入所有Mysql配置常量,请自行指定文件 from conf.se ...
随机推荐
- linux makefile (English)
############################################################################## Generic Makefile for ...
- Java第十次作业--多线程
一.学习要点 认真看书并查阅相关资料,掌握以下内容: 理解进程和线程的区别 掌握Java多线程的两种实现方式和区别 理解线程对象的生命周期 熟悉线程控制的基本方法 掌握Java线程的同步机制 理解多线 ...
- 发布网站的时候发现360极速浏览器ie7内核不兼容样式的问题
引言: 在Web应用的开发过程中,发现若干页面在360的浏览器上显示不正常,而在其他的浏览器上,皆为正常状态,问题出在哪里呢? 问题的提出: Web页面在360的浏览器上,显示不正确. 但是在Fir ...
- HDU2476 String painter
题意 String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 一张图解释java跨平台
- FastAdmin 浏览器 JS CSS 缓存如何更新?
由于代码修改,但文件名没有修改,因为浏览器对 JS 和 CSS 是缓存的,而且由于服务器无法控制客户端的缓存. 但是可以对 JS 和 CSS 的请求加上版本号,达到更新缓存的效果.
- git add -A、git add -u、git add .区别
git add各命令及缩写 git add各命令 缩写 git add --all git add -A git add --update git add -u git add . Git Versi ...
- windows server 2008 R2 无法启用"网络发现" 需要启动的服务
必须打开以下服务: 1.dnscache(简写.fdrespub(简写) 2.SSDP Discovery 3. UPnP Device Host 4. Computer Browser 5.Serv ...
- 一个最简的Thinkphp3.2 操作Mongodb的例子
看到Thinkphp网站上没有调用Mongodb的例子,就写一个一个最简的Thinkphp操作Mongodb的例子.欢迎讨论[前提]Thinkphp对Mongdb的支持依赖于PHP对Mongodb的支 ...
- 织梦dedecms模板制作时,循环递增autoindex使用方法整理
文章转载:http://www.maihui123.com/dedecms/2012051964.html 织梦dedecms模板制作时,我们需要每循环一次,变量加一,这是就需要使用到autoinde ...