Code Monkey home page Code Monkey logo

sorcery's Issues

dict_of strange behavior

% ipython
Python 3.8.2 | packaged by conda-forge | (default, Apr 16 2020, 18:04:51)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: %doctest_mode
Exception reporting mode: Plain
Doctest mode is: ON
>>> import sorcery
>>> sorcery.__version__
'0.2.0'
>>> x, y, z = range(3)
>>> sorcery.dict_of(x, y, z)
{'code_obj': 0, 'user_global_ns': 1, 'user_ns': 2}
>>> sorcery.dict_of(x, y, z, w='WTF?')
{'code_obj': 0, 'user_global_ns': 1, 'user_ns': 2, 'w': 'WTF?'}
>>> dict(x=x, y=y, z=z)
{'x': 0, 'y': 1, 'z': 2}

Consider the walrus operator for assigned_names?

Hi Alex and sorcerers! Should the walrus operator be considered an assigned name for assigned_names?
I think so and this is an example with current and expected output:

from sorcery import spell, print_args

@spell
def lhs(frame_info) -> str:
    return frame_info.assigned_names(allow_one=True)[0]

test = [
    x := lhs(),
    y := lhs(),
]

print_args(x, y, test)

Current:

x =
('test',)

y =
('test',)

test =
[('test',), ('test',)]

Expected:

x =
('x',)

y =
('y',)

test =
[('x',), ('y',)]

What do you think?

FileNotFoundError when running interactively

from sorcery import dict_of
a=1
b=2
c = dict_of(a,b)
print(c)

If I save the script as mypytest.py and run it it prints {'a': 1, 'b': 2} as expected

But if I run it from python console, it complains

Type "help", "copyright", "credits" or "license" for more information.
>>> from sorcery import dict_of
>>> a=1
>>> b=2
>>> c = dict_of(a,b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/sorcery/core.py", line 331, in __call__
    call = FileInfo.for_frame(frame)._plain_call_at(frame, self)
  File "/usr/local/lib/python3.5/dist-packages/sorcery/core.py", line 44, in for_frame
    return file_info(frame.f_code.co_filename)
  File "/usr/local/lib/python3.5/dist-packages/sorcery/core.py", line 31, in __init__
    with tokenize.open(path) as f:
  File "/usr/lib/python3.5/tokenize.py", line 454, in open
    buffer = _builtin_open(filename, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '<stdin>'

can we use expressions

I want

a = 10
b = 20
dict_of(a,b,a < b)
if a < b:
    ....

to return

{
"a": 10,
"b": 20,
"a<b":True
}

Or can this be possible

a = 10
b = 20
dict_of(a,b,"a<10"=a < b)
or
dict_of(a,b,{"a<10":a < b})

if a < b:
   .........

currently i am doing

print({**dict_of(a,b),**{"a<b": a < b}})

Jupyter notebook error

Hi @alexmojaki love sorcery - thank you!

Just been trying to invoke dict_of inside Jupyter but get the below - any ideas? Thanks again!

from sorcery import dict_of
my_dict=dict_of(x,y,z)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)

[...]

.../lib/python3.9/site-packages/sorcery/core.py in __call__(self, *args, **kwargs)
    183 
    184         executing = Source.executing(frame)
--> 185         assert executing.node, "Failed to find call node"
    186         return self.at(FrameInfo(executing))(*args, **kwargs)
    187 

AssertionError: Failed to find call node

Support walrus operator assignments

...such as this:

if (some_key := unpack_keys(dict(some_key=42))):
    print(some_key)

currently this raises

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/sorcery/core.py", line 185, in __call__
    assert executing.node, "Failed to find call node"
AssertionError: Failed to find call node

while it would be magical if sorcery could handle this walrus assignment expression, too, wouldn't it?

dict_of doesn't work in the python shell

$ python
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 1
>>> y = 2
>>> from sorcery import dict_of
>>> dict_of(x, y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/david/PycharmProjects/sandbox/venv/lib/python3.6/site-packages/sorcery/core.py", line 177, in __call__
    while frame.f_code in self._excluded_codes or frame.f_code.co_filename.startswith('<'):
AttributeError: 'NoneType' object has no attribute 'f_code'

Jupyter Notebook support

Does sorcery have a chance of working inside Jupyter Notebook? I was really happy to find this package since I very often use the unpack_keys operation and dict_of when working with training flows in machine learning but ran into an error very quickly.

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-39-1de5f0c5f0a0> in <module>
      1 a = 'a'
      2 b = 'b'
----> 3 test = dict_of(a, b)
      4 a = unpack_keys(test)

~/.local/lib/python3.6/site-packages/sorcery/core.py in __call__(self, *args, **kwargs)
    329     def __call__(self, *args, **kwargs):
    330         frame = sys._getframe(1)
--> 331         call = FileInfo.for_frame(frame)._plain_call_at(frame, self)
    332         return self.at(FrameInfo(frame, call))(*args, **kwargs)
    333 

~/.local/lib/python3.6/site-packages/sorcery/core.py in for_frame(frame)
     42     @staticmethod
     43     def for_frame(frame) -> 'FileInfo':
---> 44         return file_info(frame.f_code.co_filename)
     45 
     46     @lru_cache()

~/.local/lib/python3.6/site-packages/sorcery/core.py in __init__(self, path)
     29 
     30     def __init__(self, path):
---> 31         with tokenize.open(path) as f:
     32             self.source = f.read()
     33         self.tree = ast.parse(self.source, filename=path)

/usr/lib/python3.6/tokenize.py in open(filename)
    450     detect_encoding().
    451     """
--> 452     buffer = _builtin_open(filename, 'rb')
    453     try:
    454         encoding, lines = detect_encoding(buffer.readline)

FileNotFoundError: [Errno 2] No such file or directory: '<ipython-input-39-1de5f0c5f0a0>'```

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.