Skip to main content

Sf Tools for Python3 Development

Project description

https://travis-ci.org/SF-Zhou/st.svg?branch=master https://app.wercker.com/status/601315e1243b000f0a38ff0c0d143921/s/master

A project that include my own (SF-Zhou) tools for Python3 development.

I hope it is useful for you, too. :D

install

pip3 install st

tools

  • singleton decorator

  • dynamic code runner

  • complex partial function decorator

  • serialize to & deserialize from ascii string

  • code running time measure

  • quicker foreach operator

singleton

import st


@st.singleton
class A:
    pass

assert A() == A()

runner

import st


a, b = 1, 2
ret = st.run('c = a + b', key='c', a=a, b=b)
assert ret == 1 + 2

partial function

import st


h = st.partial_back(int, 16)

a = h('A')
assert a == 10

func = st.partial_front(int, '11')
assert func(2) == 3
assert func(10) == 11
assert func(16) == 17

serialize & deserialize

>>> import st
>>> st.serialize([1, 2, 3])
'80035d7100284b014b024b03652e'
>>> st.deserialize('80035d7100284b014b024b03652e')
[1, 2, 3]

time point measure

import st
import time


st.set_time_point('start')
time.sleep(0.1)
assert 100 <= st.microsecond_from('start') <= 110

foreach

import st


objects = ['1', '2', '3']
assert st.foreach(int, objects) == [1, 2, 3]

The foreach operator can get the attribute of objects more quickly. It also can run the objects function with specific arguments.

import st


class A:
    def __init__(self, v):
        self.v = v

    def plus(self, p):
        return self.v + p

objects = [A(0), A(1), A(2)]
assert st.foreach('.v', objects) == [0, 1, 2]        # obj.v
assert st.foreach('#plus', objects, 1) == [1, 2, 3]  # obj.plus(1)

chain

chain(a, b, c)(*args, **kwargs) = a(b(c(*args, **kwargs))). In other word, it connect several function to a chain func.

import st


int_str_to_hex_str = st.chain(hex, int)
assert int_str_to_hex_str('0') == '0x0'
assert int_str_to_hex_str('1') == '0x1'
assert int_str_to_hex_str('10') == '0xa'
assert int_str_to_hex_str('16') == '0x10'
assert int_str_to_hex_str.__name__ == 'chain<hex, int>'

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

st-0.0.6.zip (7.3 kB view details)

Uploaded Source

File details

Details for the file st-0.0.6.zip.

File metadata

  • Download URL: st-0.0.6.zip
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for st-0.0.6.zip
Algorithm Hash digest
SHA256 3c338c33416235758554b39d2efa71300318c33aafc584419eb27512c05850be
MD5 566c3b7b79cdd981454c8e89536ab465
BLAKE2b-256 ea8ba49a6879a303edd611b6b1d9436dbddb6248e87c90abb091ea73160c50bb

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