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 ...
随机推荐
- stm32 定时器TIM时钟步骤
1)TIM3 时钟使能 . RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIMx, ENABLE); //时钟使能 2) 初始化定时器参数,设置自动重装值, 分频系数, ...
- hdu2073-2078
hdu2073 数学 #include<stdio.h> #include<math.h> double len(double x){ )*(x+)); } int main( ...
- pandas Timestamp的用法
(Timestamp('2018-08-01 00:00:00'), <class 'pandas._libs.tslibs.timestamps.Timestamp'>) 注意这里面的T ...
- 直接new一个对象出来
- System.out.println()详解 和 HttpServletRequest 和 XMLHttpRequest
System是一个类,位于java.lang这个包里面.out是这个System类的一个PrintStream类型静态属性.println()是这个静态属性out所属类PrintStream的方法. ...
- C# 线程会合实例
有这样一个题目:四个线程t1,t2,t3,t4,向4个文件中写入数据,要求:t1只能写入“1”,t2只能写入“2”,t3只能写入“3”,t4只能写入“4”,对4个文件A,B,C,D写入如下内容: A: ...
- PHP 中的对象传递
<?php class A { public $age = 0; public $username = ""; public $obj = null; } $a = new ...
- Spring Cloud Netflix项目进入维护模式
任何项目都有其生命周期,Spring Could Netflix也不例外,官宣已进入维护模式,如果在新项目开始考虑技术选型时要考虑到这点风险,并考虑绕道的可能性. 原创: itmuch IT牧场 这 ...
- win10禁用自动更新服务
win10禁用自动更新服务 按Win+R,打开运行,输入"services.msc"打开服务: 找到"Windows Update",选择属性,修改为禁用即可: ...
- java 线程转换图