Skip to main content

Common Python Library for server and client application.

Project description

Common Python Library

About

"gcommon" is a python utility library for server and client application development.

Installing

git clone https://github.com/liguopeng80/gcommon.git
pip install -e gcommon

Usage

Usage for server application

import asyncio

from gcommon.web import monkey_patch
monkey_patch.monkey_patch_flask()

from gcommon.aio.gserver import SimpleServer


class DemoServer(SimpleServer):
    SERVICE_NAME = 'demo'

    async def init_server(self):
        # parse configuration and init any dependencies to start service.
        # "init_server" can be async or not async.
        """do whatever you want"""
        import demo_service

        self.logger.debug("init database...")
        demo_service.init_database(self.config)

        self.logger.debug("init demo service...")
        demo_service.init_service()

    def start_server(self):
        # "start_server" can be async or not async.
        """do whatever you want"""
        from web.webapp import app

        host = self.config.get('service.web.host')
        port = self.config.get('service.web.port')
        auto_reload = self.config.get_bool('service.auto_reload')

        asyncio.create_task(app.run_task(host=host, port=port, use_reloader=auto_reload))
        self.logger.info("starting web service on %d...", port)


if __name__ == '__main__':
    DemoServer.start()

Usage for client application

from gcommon.utils.gglobal import Global
from gcommon.utils.gmain import init_main

# load configuration and init logger
Global.config = init_main()

Usage for just utils

from datetime import datetime
from gcommon.utils.gtime import DateUtil

last_monday = DateUtil.last_monday(datetime).now()

Configuration

For server and client applications, gcommon will load configuration from "deploy/default.yaml" by default.

# deploy/default.yaml
service:
  web:
    base_url: /v3
    host: 0.0.0.0
    port: 12580

Secret Configuration

You can also have a secret config file which will not be committed to git repository.

# deploy/secret.default.yaml
demo_postgre:
  server_address: "127.0.0.1"
  server_port: 5432

  username: "admin"
  password: "password"

How to load secret config:

from gcommon.utils.gglobal import Global

db_config = Global.config.get("secret.demo_postgre")
password = db_config.password

password = Global.config.get("secret.demo_postgre.password")

History

The library was named "common" in 2008 and then renamed to "gcommon" in 2010 to avoid name conflict. Its initial "g" comes from the author's name (Guo Peng Li).

It was implemented on Pytohn 2.6 and then upgraded to Python 2.7, while its all asynchronous functionalities were based on twisted.

On 2018, it was firstly migrated to Python 3 (3.6) but kept using twsited as its aysnc infrastructure until 2021.

At the middle of 2021, most async functionalities in gcommon were re-implemented with asycio and the library was published as MIT license.

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

gcommon-1.1.3.tar.gz (89.9 kB view details)

Uploaded Source

Built Distribution

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

gcommon-1.1.3-py3-none-any.whl (123.0 kB view details)

Uploaded Python 3

File details

Details for the file gcommon-1.1.3.tar.gz.

File metadata

  • Download URL: gcommon-1.1.3.tar.gz
  • Upload date:
  • Size: 89.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.5

File hashes

Hashes for gcommon-1.1.3.tar.gz
Algorithm Hash digest
SHA256 5584b39afaced74e0bb389ac17bb227c2892e81c817ffa16576456e7dbe73100
MD5 6f7d1abea7f3f42002e6896774001adc
BLAKE2b-256 aecde16398156ceb5d0911a1847dc93ced38af5419f7d5d378c490f7ca9bcce3

See more details on using hashes here.

File details

Details for the file gcommon-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: gcommon-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 123.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.5

File hashes

Hashes for gcommon-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 02d3fe3aca4fb22dfd15cbb14bb90d09b6b759e3d1b0d1720f00d2e7c3e30ec1
MD5 ca543f90e0caace9fc753deef01307b5
BLAKE2b-256 0135de384a40be8944f60244b5cb4ae86333af4798bb0cbc6dbe2444f6528207

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