All Projects → kinegratii → borax

kinegratii / borax

Licence: MIT license
📓 Python3工具集合库——中国农历/中文数字/设计模式/树形结构

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to borax

LunarCalendar
A lunar calendar converter in Python, including 24 solar terms and a number of solar holidays and lunar holidays, mainly from China.
Stars: ✭ 34 (-40.35%)
Mutual labels:  lunar-calendar, chinese-festivals
Python Scripts
It contains all the Python Programs, whether it's a GUI, basic, Data Structures, etc. It's a collection of some great Python scripts from basic to advance levels for automating some monotonous tasks.
Stars: ✭ 23 (-59.65%)
Mutual labels:  datastructures
Data-Structures-and-Algorithms--A-Comprehensive-Guide
Data Structures & Algorithms - A Comprehensive Guide
Stars: ✭ 15 (-73.68%)
Mutual labels:  datastructures
csa-misc-utils
Miscellaneous samples, documents, how-tos, utilities, scripts, and other CSA tidbits
Stars: ✭ 79 (+38.6%)
Mutual labels:  utils
peds
Type safe persistent/immutable data structures for Go
Stars: ✭ 57 (+0%)
Mutual labels:  datastructures
utils.js
Fast, small and purely functional utility library
Stars: ✭ 132 (+131.58%)
Mutual labels:  utils
treebitmap
Fast IP lookup table for IPv4/IPv6 prefixes
Stars: ✭ 81 (+42.11%)
Mutual labels:  datastructures
cmn-utils
公共函数&请求封装
Stars: ✭ 43 (-24.56%)
Mutual labels:  utils
timestampy
🕒 Bunch of utilities useful when working with UNIX timestamps
Stars: ✭ 21 (-63.16%)
Mutual labels:  utils
py-eth-sig-utils
Collection of python functions to generate hashes for signing on Ethereum
Stars: ✭ 18 (-68.42%)
Mutual labels:  utils
Y-BP
YFE Team 前端最佳实践
Stars: ✭ 28 (-50.88%)
Mutual labels:  utils
monica-fork
🧗 您的私人社交关系管家。Monica with Lunar Calendar support and more security features.
Stars: ✭ 11 (-80.7%)
Mutual labels:  lunar-calendar
Joy-of-Computing-using-Python-NPTEL
Joy of computing using Python NPTEL Course Assignment Solutions
Stars: ✭ 22 (-61.4%)
Mutual labels:  datastructures
Programacion-Competitiva
Material de programación competitiva elaborado por el Grupo de Programación Competitiva UNI (GPC-UNI)
Stars: ✭ 23 (-59.65%)
Mutual labels:  datastructures
useful
🇨🇭 A collection of useful functions for working in Elixir
Stars: ✭ 21 (-63.16%)
Mutual labels:  utils
dd
This package will add the dd and dump helpers to your Phalcon application.
Stars: ✭ 17 (-70.18%)
Mutual labels:  utils
AlgorithmSet
LeetCode 算法练习集合 ~ 每天一道算法题
Stars: ✭ 19 (-66.67%)
Mutual labels:  datastructures
autils
Awesome frontend utils library
Stars: ✭ 17 (-70.18%)
Mutual labels:  utils
aws-sdk-extra
The AWS SDK + a handful of extra convenience methods.
Stars: ✭ 18 (-68.42%)
Mutual labels:  utils
android-library
Android开发基础库
Stars: ✭ 14 (-75.44%)
Mutual labels:  utils

Borax - python3工具库 - 中国农历/中文数字/设计模式/树形结构

PyPI PyPI - Python Version Python package Codecov GitHub license borax

概述 (Overview)

github https://github.com/kinegratii/borax

gitee https://gitee.com/kinegratii/borax

Borax 是一个Python3工具集合库。包括了以下几个话题:

话题(Topics) 内容
Borax.LunarDate 1900-2100年的中国农历日期库
Borax.Festivals 实现常见节日(公历、农历、星期、节气)的工具库
Borax.Choices 声明式的选项类。适用于Django.models.choices 定义。
Borax.Datasets 记录型数据操作库,包括连结(Join)、列选择(fetch)等
Borax.DataStuctures 树形结构,json数据
Borax.Numbers 数字库。包括中文数字、百分数等。
Borax.Patterns 设计模式。包括单例模式、代理对象、延迟对象。

安装 (Installation)

从 v3.5.1开始,安装包文件格式为 borax-3.5.1-py3-none-any.whl(移除py2标识)以区别于之前的 borax-3.5.0-py2.py3-none-any.whl

Borax 要求 Python3.6+ ,可以通过 pip 安装 :

$ pip install borax

使用示例 (Usage)

Borax.LunarDate: 中国农历日期

一个支持1900-2100年的农历日期工具库。

本模块的数据和算法参考自项目 jjonline/calendar.js

创建日期,日期推算

from datetime import timedelta
from borax.calendars import LunarDate

# 获取今天的农历日期(农历2018年七月初一)
print(LunarDate.today()) # LunarDate(2018, 7, 1, 0)

# 将公历日期转化为农历日期
ld = LunarDate.from_solar_date(2018, 8, 11)
print(ld) # LunarDate(2018, 7, 1, 0)

# 日期推算,返回10天后的农历日期
print(ld.after(10)) # LunarDate(2018, 7, 11, 0)

# 可以直接与 datetime.timedelta 直接相加减
print(ld + timedelta(days=10)) # LunarDate(2018, 7, 11, 0)

格式化字符串

today = LunarDate.today()
print(today.strftime('%Y年%L%M月%D')) # '二〇一八年六月廿六'
print(today.strftime('今天的干支表示法为:%G')) # '今天的干支表示法为:戊戌年庚申月辛未日'

Borax.Festival: 国内外节日

创建春节(每年正月初一)对应的节日对象

from borax.calendars.festivals2 import LunarFestival

festival = LunarFestival(month=1, day=1)
print(festival.description) # '农历每年正月初一'

# 下一次春节的具体日期以及距离天数
print(festival.countdown()) # (273, <GeneralDate:2022-02-01(二〇二二年正月初一)>)

# 接下来5个春节的日期 ['2022-02-01(二〇二二年正月初一)', '2023-01-22(二〇二三年正月初一)', '2024-02-10(二〇二四年正月初一)', '2025-01-29(二〇二五年正月初一)', '2026-02-17(二〇二六年正月初一)']
print([str(wd) for wd in festival.list_days(start_date=date.today(), count=5)])

Borax.FestivalLibrary:内置节日库

基本使用示例

from datetime import date
from borax.calendars.festivals2 import FestivalLibrary, WrappedDate

library = FestivalLibrary.load_builtin()

# 2020年国庆节和中秋节是同一天
names = library.get_festival_names(date(2020, 10, 1))
print(names) # ['国庆节', '中秋节']

# 2021年七夕
festival = library.get_festival('七夕')
print(festival.description) # '农历每年七月初七'
print(WrappedDate(festival.at(year=2021))) # '2021-08-14(二〇二一年七月初七)'

计算节日及其距离今天(2021年5月4日)的天数

from borax.calendars.festivals2 import FestivalLibrary

library = FestivalLibrary.load_builtin()
for nday, gd_list in library.iter_festival_countdown():
    for gd in gd_list:
        print('{:>3d} {} {}'.format(nday, gd.name, gd))

输出结果

  0 青年节 2021-05-04(二〇二一年三月廿三)
  5 母亲节 2021-05-09(二〇二一年三月廿八)
  8 护士节 2021-05-12(二〇二一年四月初一)
 28 儿童节 2021-06-01(二〇二一年四月廿一)
<...>
336 清明 2022-04-05(二〇二二年三月初五) 
362 劳动节 2022-05-01(二〇二二年四月初一)

Borax.Numbers: 中文数字处理

不同形式的中文数字

from borax.numbers import ChineseNumbers

# 小写、计量
print(ChineseNumbers.measure_number(204)) # '二百零四'
# 小写、编号
print(ChineseNumbers.order_number(204)) # '二百〇四'
# 大写、计量
print(ChineseNumbers.measure_number(204, upper=True)) # '贰佰零肆'
# 大写、编号
print(ChineseNumbers.order_number(204, upper=True)) # '贰佰〇肆'

财务金额

import decimal

from borax.numbers import FinanceNumbers

decimal.getcontext().prec = 2

print(FinanceNumbers.to_capital_str(100000000)) # '壹亿元整'
print(FinanceNumbers.to_capital_str(4578442.23)) # '肆佰伍拾柒万捌仟肆佰肆拾贰元贰角叁分'
print(FinanceNumbers.to_capital_str(107000.53)) # '壹拾万柒仟元伍角叁分'
print(FinanceNumbers.to_capital_str(decimal.Decimal(4.50))) # '肆元伍角零分'

更多模块功能参见文档。

文档 (Document)

文档由 docsify 构建。

网址
github https://kinegratii.github.io/borax
gitee https://kinegratii.gitee.io/borax

开发特性和规范 (Development Features)

开源协议 (License)

The MIT License (MIT)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].