Skip to main content

suger is a sugar. use python in @decorator/@Annotation like other languages~

Project description

suger

version = 0.2.2

Introduce 介绍

suger is a sugar ~

Feature:

  1. python @decorator = Java @annotation / C# [Required(ErrorMessage = "{0} is required")]
  2. python Stream = Java Stream / C# LinQ

How to use 如何使用

install 安装依赖

pip install suger

Stream 流式计算 / from v0.2.0

from suger.stream.Stream import Stream

    def test_demo(self):
        data = [1, 2, 3, 3, 4, 5]
        result = Stream(data)\
            .filter(lambda x: x % 2 == 0) \
            .sort(reverse=True) \
            .map(lambda x: x * 2) \
            .toSet()
        self.assertEqual(result, {8, 4})

decorator 装饰器/注解

@string | str

@string
class MockData:
    def __init__(self, age):
        self.age = age


data = MockData(18)

# Output = "MockData(age=18)"
print(data)

@csv | CSV

@csv
class MockData:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.emptyTips = ''




class Test(TestCase):
    def test_csv(self):
        data = MockData(name='neko', age=18)

        # csv 输出文本
        print(data.csv_str())
        # csv 的格式
        print(data.csv_format())

# 示例中,有个字段为空
# neko,18,
# name,age,emptyTips

工具

FileUtils 文件工具

from suger.common import FileUtils

# 递归扫描, png 格式
fileArray = FileUtils.scanDir('C:/Users/suger/Documents/WeChat Files', 'png')
print(fileArray)

ObjectUtils 对象工具

from suger.common import ObjectUtils

# true
ObjectUtils.isNull(None)

#  true
data = {}
ObjectUtils.isNotNull(data)

SSH

from unittest import TestCase

from suger.terminal import SSH


class TestSSH(TestCase):
    def test_connect(self):
        ssh = SSH(host='localhost', password='root')
        ssh.connect()
        output, err = ssh.execute_command('ls .')
        print(output)

Data Operator 数据操作

XML

from unittest import TestCase

from suger.data_operator import XmlUtils, ElementTree

class TestXmlUtils(TestCase):
    def test_find_element(self):

        # 读取 XML 文件
        xml = XmlUtils('example.xml')

        # 查询节点
        node = xml.find_element('.//book[@id="123"]')
        print(node.text)

        # 修改节点值
        xml.set_element_value('.//book[@id="123"]/name', 'New Book Title')

        # 添加节点
        new_element = ElementTree.Element('book', {'id': '456'})
        sub_element1 = ElementTree.SubElement(new_element, 'name')
        sub_element1.text = 'New Book'
        sub_element2 = ElementTree.SubElement(new_element, 'author')
        sub_element2.text = 'New Author'
        xml.add_element('.//books', new_element)

        # 删除节点
        xml.remove_element('.//book[@id="123"]')

        # 写入文件
        xml.write_file('new_example.xml')

Excel

from unittest import TestCase

from suger.data_operator.ExcelUtils import ExcelUtils


class TestExcelUtils(TestCase):

    def test_serialize(self):
        # 读取 Excel 文件
        workbook = ExcelUtils.load_workbook("example.xlsx")

        # 获取指定名称的 sheet 对象
        sheet = ExcelUtils.get_sheet_by_name(workbook, "Sheet1")

        # 将 sheet 序列化为一个列表
        data = ExcelUtils.serialize(sheet, skip_rows=1)

        # 对列表进行操作

        # 反序列化列表到指定的 sheet 对象
        ExcelUtils.deserialize(sheet, data, skip_rows=1)

        # 保存 Excel 文件
        ExcelUtils.save_workbook(workbook, "example.xlsx")

my project init

git init

 git remote add github https://github.com/SolarisNeko/neko233-python-suger.git
 git remote add origin https://gitee.com/SolarisNeko/neko233-python-suger.git

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

suger-0.2.2.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

suger-0.2.2-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file suger-0.2.2.tar.gz.

File metadata

  • Download URL: suger-0.2.2.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for suger-0.2.2.tar.gz
Algorithm Hash digest
SHA256 ac19694cd21a12974d59f22c71967d2c662b0a87118c869f516ccf82fd58e30a
MD5 c31fdb941562a57ce8bad9ca86d4589a
BLAKE2b-256 2849df9316b4f5cd503ce77ec88e3178859a184270250af322c8c1d2152b3cc5

See more details on using hashes here.

File details

Details for the file suger-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: suger-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for suger-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b6b5d4b12dcab723dafb1ada4af40306a53601c987d821ca595566aa3b5107bd
MD5 8c9fdec592ffba9f2754ca56a3ce2ae2
BLAKE2b-256 d46672416c1c12faaecd7b7eb6e36e15398227a28e07912ef880b7691f27e6e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page