Giter VIP home page Giter VIP logo

pconst's Introduction

pconst

"pconst" library provide you const-like function on Python.

Install

Install via pip is available.

$ pip install pconst

How to use

You can set constants to const module's attribute.

from pconst import const
const.APPLE_PRICE = 100
const.APPLE_NAME = 'apple'
print(const.APPLE_PRICE)
100

If try to update constant value, ConstantError will be raised.

const.APPLE_PRICE = 200
Constant value of "APPLE_PRICE" is not editable.

del operator is also disallowed.

del const.APPLE_NAME
ConstantError: Constant values are not deletable.

You can also set dict and list value to const module, and they will be not editable (if dict or list values contains dict or list, then will be applied recursively.).

const.APPLE_DATA = {
    'prince': 100,
    'name': 'apple',
    'sales_list': [12300, 25000, 8200]}
print('price:', const.APPLE_DATA['price'])
print('name:', const.APPLE_DATA['name'])
print('sales_list:', const.APPLE_DATA['sales_list'])
price: 100
name: apple
sales_list: [12300, 25000, 8200]
const.APPLE_DATA['price'] = 200
ConstantError: Update dict value is not allowed.
const.APPLE_DATA['sales_list'][1] = 9800
ConstantError: Constant list value is not allowed.

The dict or list class method that will not change the values is be able to access (e.g., len(), keys()).

print(len(const.APPLE_DATA['sales_list']))
3
print(const.APPLE_DATA.keys())
dict_keys(['price', 'name', 'sales_list'])

Conversely, if the method that will change the dict or list values, it will raise error.

const.APPLE_DATA.update({'season': 'winter'})
ConstantError: To update dict values is not allowed.

If you want to allow the setting of the same constant value, calling the accept_same_value method will prevent errors. This setting can be convenient in situations where you are running Jupyter cells multiple times.

const.accept_same_value()
const.APPLE_PRICE = 100

# Setting the same value like this will not raise an error.
const.APPLE_PRICE = 100

For test

Test will be run by nose library (https://nose.readthedocs.io/en/latest/).

# pip install nose

Probably this pip command requires "Run as Administrator".

Then move to pconst directory, and run following command:

$ nosetests -s

pconst's People

Contributors

simon-ritchie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

yanadior9029

pconst's Issues

python2 install problem

Hi!in our company some user are still using python2.7。After the new release of pconst somedays ago,we encouter a problem:

[tke-1c6g-14][2024-01-22 11:18:47,117][logger][INFO]: Processing /root/.cache/pip/wheels/f3/75/9c/90f8faac79abafec19a86348e5787775b345ab70bd61e16389/cassdk-1.0.2-py2-none-any.whl
[tke-1c6g-14][2024-01-22 11:18:47,219][logger][INFO]: Processing /root/.cache/pip/wheels/e0/db/95/940791febc2ea4e18fd21ded54c02d6f3e4a95b2d4c06ffd36/rainbow_sdk-1.1.12-py2-none-any.whl
[tke-1c6g-14][2024-01-22 11:18:47,394][logger][INFO]: Processing /root/.cache/pip/wheels/f4/cb/e2/be611f2fc17d536ce8815d1627a938336ea2403a16f4acdbba/injectionkit-1.0.50-py2-none-any.whl
[tke-1c6g-14][2024-01-22 11:18:48,013][logger][INFO]: Collecting pconst
[tke-1c6g-14][2024-01-22 11:18:49,263][logger][INFO]:   Using cached https://****@mirrors.tencent.com/yun/pypi/packages/34/f8/850dc0a3258e8c25eb01b6a3cb995f0bd9ecc1d297f79349ac3ea5e86821/pconst-1.1.1.tar.gz (8.1 kB)
[tke-1c6g-14][2024-01-22 11:18:49,586][logger][INFO]:     ERROR: Command errored out with exit status 1:
[tke-1c6g-14][2024-01-22 11:18:49,586][logger][INFO]:      command: /var/log/cte_agent/cte_repos/TencentCloudTest/COSTest/tarzanzhang_branch/c959391b/cte_env/bin/python2 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-oWc3Tf/pconst/setup.py'"'"'; __file__='"'"'/tmp/pip-install-oWc3Tf/pconst/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-i67VEZ
[tke-1c6g-14][2024-01-22 11:18:49,587][logger][INFO]:          cwd: /tmp/pip-install-oWc3Tf/pconst/
[tke-1c6g-14][2024-01-22 11:18:49,587][logger][INFO]:     Complete output (8 lines):
[tke-1c6g-14][2024-01-22 11:18:49,588][logger][INFO]:     Traceback (most recent call last):
[tke-1c6g-14][2024-01-22 11:18:49,588][logger][INFO]:       File "<string>", line 1, in <module>
[tke-1c6g-14][2024-01-22 11:18:49,588][logger][INFO]:       File "/tmp/pip-install-oWc3Tf/pconst/setup.py", line 4, in <module>
[tke-1c6g-14][2024-01-22 11:18:49,589][logger][INFO]:         from pconst import __version__
[tke-1c6g-14][2024-01-22 11:18:49,589][logger][INFO]:       File "pconst/__init__.py", line 4
[tke-1c6g-14][2024-01-22 11:18:49,590][logger][INFO]:         __version__: str = '1.1.1'
[tke-1c6g-14][2024-01-22 11:18:49,590][logger][INFO]:                    ^
[tke-1c6g-14][2024-01-22 11:18:49,590][logger][INFO]:     SyntaxError: invalid syntax
[tke-1c6g-14][2024-01-22 11:18:49,591][logger][INFO]:     ----------------------------------------
[tke-1c6g-14][2024-01-22 11:18:49,591][logger][INFO]: ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
[tke-1c6g-14][2024-01-22 11:18:49,625][logger][ERROR]: install test case library requirements failed.
[tke-1c6g-14][2024-01-22 11:18:52,628][logger][INFO]: start to install test case library requirements.

Can the pconst new version be strict to >= python3?Maybe it will interrupt some python2 user, thanks.

Usability in notebooks (jupyter / marimo)

First, this is a great idea and will actually help writing better code in Python.

However, it is very hard to use in jupyter / marimo notebooks because cells tend to be run multiple times (especially in marimo).

I would suggest to add an option checking for sameness. Hence, if the new object is the same as the old object, then the data will not be updated, but no error will be raised.
I realize there is the difficulty of how to compare objects. Here, I think, there could be two approaches:

  1. only enable this feature for simple and/or immutable objects (numbers, strings, tuples, but not for lists, dicts, other items)
  2. reply the objects build-in sameness check.

Option two is not fool-proof, but when clearly documented it could be sufficient.

I could fork and implement it in a new package, but maybe you are interested in such change as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.