Giter VIP home page Giter VIP logo

python-varname's Introduction

python-varname

Pypi Github PythonVers Travis building Codacy Codacy coverage

Dark magics about variable names in python

Installation

pip install python-varname

Features

  • Fetching variable names from inside the function/class call
  • Fetching variable names directly (added in v0.1.2)
  • A value wrapper to store the variable name that a value is assigned to (added in v0.1.1)
  • Detecting next immediate attribute name (added in v0.1.4)
  • Shortcut for collections.namedtuple (added in v0.1.6)
  • Injecting __varname__ to objects (added in v0.1.7)

Credits

Thanks goes to these awesome people/projects:


@alexmojaki

executing

Usage

Retrieving the variable names from inside a function call/class instantiation

  • From insdie a function call

    from varname import varname
    def function():
        return varname()
    
    func = function()
    # func == 'func'
  • varname calls being buried deeply

    def function():
        # I know that at which stack this will be called
        return varname(caller=3)
    
    def function1():
        return function()
    
    def function2():
        return function1()
    
    func = function2()
    # func == 'func'
  • Retrieving instance name of a class

    class Klass:
        def __init__(self):
            self.id = varname()
    
        def copy(self):
            # also able to fetch inside a member call
            return varname()
    
    k = Klass()
    # k.id == 'k'
    
    k2 = k.copy()
    # k2 == 'k2'
  • Some unusual use

    func = [function()]
    # func == ['func']
    
    func = [function(), function()]
    # func == ['func', 'func']
    
    func = function(), function()
    # func = ('func', 'func')
    
    func = func1 = function()
    # func == func1 == 'func'
    # a warning will be printed
    # since you may not want func1 to be 'func'
    
    x = func(y = func())
    # x == 'x'
    
    # get part of the name
    func_abc = function()[-3:]
    # func_abc == 'abc'
    
    # function alias supported now
    function2 = function
    func = function2()
    # func == 'func'
    
    # Since v0.1.3
    # We can ask varname to raise exceptions
    # if it fails to detect the variable name
    
    from varname import VarnameRetrievingError
    def get_name():
        try:
            # if raise_exc is False
            # "var_<index>" will be returned
            return varname(raise_exc=True)
        except VarnameRetrieveingError:
            return None
    
    a.b = get_name() # None

Value wrapper

from varname import Wrapper

foo = Wrapper(True)
# foo.name == 'foo'
# foo.value == True
bar = Wrapper(False)
# bar.name == 'bar'
# bar.value == False

def values_to_dict(*args):
    return {val.name: val.value for val in args}

mydict = values_to_dict(foo, bar)
# {'foo': True, 'bar': False}

Getting variable names directly

from varname import varname, nameof

a = 1
aname = nameof(a)
# aname == 'a

b = 2
aname, bname = nameof(a, b)
# aname == 'a', bname == 'b'

def func():
    return varname() + '_suffix'

f = func()
# f == 'f_suffix'
fname = nameof(f)
# fname == 'f'

Detecting next immediate attribute name

from varname import will
class AwesomeClass:
    def __init__(self):
        self.will = None

    def permit(self):
        self.will = will()
        if self.will == 'do':
            # let self handle do
            return self
        raise AttributeError('Should do something with AwesomeClass object')

    def do(self):
        if self.will != 'do':
            raise AttributeError("You don't have permission to do")
        return 'I am doing!'

awesome = AwesomeClass()
awesome.do() # AttributeError: You don't have permission to do
awesome.permit() # AttributeError: Should do something with AwesomeClass object
awesome.permit().do() == 'I am doing!'

Shortcut for collections.namedtuple

# instead of
from collections import namedtuple
Name = namedtuple('Name', ['first', 'last'])

# we can do:
from varname import namedtuple
Name = namedtuple(['first', 'last'])

Injecting __varname__

from varname import inject

class MyList(list):
    pass

a = inject(MyList())
b = inject(MyList())

a.__varname__ == 'a'
b.__varname__ == 'b'

a == b

# other methods not affected
a.append(1)
b.append(1)
a == b

Limitations

  • Working in ipython REPL but not in standard python console
  • You have to know at which stack the function/class will be called (caller's depth)
  • Not working with reticulate from R since it cuts stacks to the most recent one.
  • nameof cannot be used in statements in pytest (supported in v0.2.0)
    -a = 1
    +assert nameof(a) == 'a'
    -# Retrieving failure.
    -# The right way:
    -aname = nameof(a)
    -assert aname == 'a'

python-varname's People

Contributors

alexmojaki avatar pwwang avatar

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.