3
���h�# � @ s� d Z ddlZddlZddlmZ ddlZddlZddlZddlZddl m
Z
ddlZddl
mZ ddlmZ ddf dfedd�d d
�Zed�dd
�Zed�dd�Zeejddeeejejd�Zed�dd�ZG dd� d�ZdS )z
Module for scope operations
� N)�StringIO)�List)� Timestamp)�DeepChainMap�Scope)�level�returnc K s t | d ||||d�S )z.Ensure that we are grabbing the correct scope.� )�global_dict�
local_dict� resolvers�target)r )r r
r r r
�kwargs� r �?/tmp/pip-build-5_djhm0z/pandas/pandas/core/computation/scope.py�ensure_scope s r )r c C s. yt | �}W n tk
r$ | }Y nX t|�S )z�
Replace a number with its hexadecimal representation. Used to tag
temporary variables with their calling scope's id.
)�ord� TypeError�hex)�xZhexinr r r � _replacer! s
r c C s$ t jdt| ��}djdd� |D ��S )z,Return the padded hexadecimal id of ``obj``.z@P� c s s | ]}t |�V qd S )N)r )�.0r r r r � <genexpr>5 s z_raw_hex_id.<locals>.<genexpr>)�struct�pack�id�join)�obj�packedr r r �_raw_hex_id1 s r TF)r �datetime�True�False�list�tuple�infZInfc C s t � }tj| |d� |j� S )z�
Return a prettier version of obj.
Parameters
----------
obj : object
Object to pretty print
Returns
-------
str
Pretty print object repr
)�stream)r �pprint�getvalue)r �sior r r �_get_pretty_stringD s r+ c @ s� e Zd ZdZdddddgZddf dfdd �Zed
�dd�Zee d
�d
d��Z
ee d�dd�Zd!eed�dd�Ze
e d�dd�Zed�dd�Zed
�dd�Zeed
�dd��Zedd � �ZdS )"r a�
Object to hold scope, with a few bells to deal with some custom syntax
and contexts added by pandas.
Parameters
----------
level : int
global_dict : dict or None, optional, default None
local_dict : dict or Scope or None, optional, default None
resolvers : list-like or None, optional, default None
target : object
Attributes
----------
level : int
scope : DeepChainMap
target : object
temps : dict
r �scoper
r �tempsNc C s� |d | _ ttj� �| _|| _t|t�rT| jj|j� |jd k rH|j| _| j |j � t
j| j �}z>| jj|pp|j
j� �| _t|t�s�| jj|p�|jj� �| _W d ~X t|t�r�|t|jj�7 }t|� | _i | _d S )Nr )r r �_DEFAULT_GLOBALS�copyr, r
�
isinstancer �update�_update�sys� _getframe� new_child� f_globals�f_localsr% r �mapsr- )�selfr r
r r r
�framer r r �__init__n s$
zScope.__init__)r c C sD t t| jj� ��}t t| jj� ��}t| �j� d|� d|� d�}|S )Nz(scope=z, resolvers=�))r+ r$ r, �keysr �type�__name__)r9 Z
scope_keysZres_keysZunicode_strr r r �__repr__� s zScope.__repr__c C s t t| j��S )z�
Return whether we have any extra scope.
For example, DataFrames pass Their columns as resolvers during calls to
``DataFrame.eval()`` and ``DataFrame.query()``.
Returns
-------
hr : bool
)�bool�lenr )r9 r r r �
has_resolvers� s zScope.has_resolvers)�key�is_localc C s� y:|r| j | S | jr | j| S | r.| j s2t�| j | S tk
r� y
| j| S tk
r� } zddlm} |||�|�W Y dd}~X nX Y nX dS )a�
Resolve a variable name in a possibly local context.
Parameters
----------
key : str
A variable name
is_local : bool
Flag indicating whether the variable is local or not (prefixed with
the '@' symbol)
Returns
-------
value : object
The value of a particular variable
r )�UndefinedVariableErrorN)r, rC r �AssertionError�KeyErrorr- Zpandas.core.computation.opsrF )r9 rD rE �errrF r r r �resolve� s
z
Scope.resolve)�old_key�new_keyc C sR | j r| jj| jj }n| jj}|j| j� x |D ]}||kr2|||< |