Giter VIP home page Giter VIP logo

token-utils's Introduction

token-utils

This project consists of a single module which is extracted from the ideas package. Its purpose is to simplify manipulations of tokens from Python's tokenize module. One of its features is that, unlike Python's version, the following is always guaranteed:

from token_utils import tokenize, untokenize

source = "Arbitrary Python code here"

assert source == untokenize(tokenize(source))

See ideas documentation for more information

token-utils's People

Contributors

aroberge avatar

Stargazers

Gregory M. Kapfhammer avatar Paul m. p. Peny avatar Christian Hammond avatar Jordan Kalebu avatar

Watchers

 avatar James Cloos avatar  avatar

token-utils's Issues

Replacing a group of tokens

What is the right way to replace a group of tokens with a different set of tokens? It feels like I must be missing something. There doesn't seem to be an easy way of constructing a list of new tokens and some of the obvious ways of doing this produce odd results. For instance this transform:

def transform_source(source, **_):
    tokens = token_utils.tokenize(source)
    for ii in range(2, 4):
        del tokens[ii]

    tokens.insert(2, '"Some Text"')
    return token_utils.untokenize(tokens)

on this source:

x = Some Text

produces this transformed source:

x ="Some Text" Some Text

What is the right way to do this? Is there a reliable way?

Add a function to identify brace groups

Consider the following example where we'd like to replace

x!
(x + 1)!

by

factorial(x)!
factorial(x + 1)!

Currently, the way to do this would be to process the string until we encounter a !, and then start backtracking workout out the logic.

Suppose we had access to groups of braces, perhaps something like:

braces = {"end_index": "begin_index"}

For example, the second case above would be

braces = {4:0}

when encoutering a ! at position index, we could simply do something like the following:

if tokens[index-1] == ")":
    begin_index = braces[index-1]
    tokens[begin_index].string = "factorial("

untokenize returns all tokens up to a given one

The following code fragment in useful context:

t = tokens[4]
print(token_utils.untokenize([t]))

will print t.tokens[0:4] for some reason.

It is unclear to me precisely how the thing works, as it can untokenize the array tokens, while passing a list containing only one token from tokens will untokenize all elements of tokens up to that point. When iterating over tokens, untokenize seems to emit nothing if given tokens[n] where n hasn't been iterated yet…or something.

I have no idea how this thing behaves. It's doing nothing logical.

Create separate repository for demos

The idea would be to have a few tests, run with the development version in this repository, and a separate repository, using an installed version (from pypi), that could include many extended demos - in particular, an alternate version of sympy's parsing.

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.