Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94797

Re: Logical Query JSON

Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'exercise': 0.03; '(python': 0.05; 'json': 0.05; 'indices': 0.07; '(defined': 0.09; 'attribute.': 0.09; 'dict': 0.09; 'expectation': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'reference)': 0.09; 'typeerror:': 0.09; 'example:': 0.10; 'python': 0.10; 'syntax': 0.13; 'appropriate': 0.14; '"b"': 0.16; '"general': 0.16; '"not"': 0.16; '"or"': 0.16; "'b'": 0.16; '(more': 0.16; '(unlike': 0.16; '...)': 0.16; 'etc...': 0.16; 'integers,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:JSON': 0.16; 'subject:Query': 0.16; 'syntaxerror:': 0.16; 'this)': 0.16; 'string': 0.17; 'attribute': 0.18; 'solution.': 0.18; 'variable': 0.18; 'language': 0.19; 'extension': 0.20; 'not,': 0.22; '"",': 0.22; 'keyerror:': 0.22; 'keys': 0.22; 'logical': 0.22; 'trying': 0.22; 'bit': 0.23; "python's": 0.23; 'import': 0.24; '(most': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; '(such': 0.27; 'ibm': 0.27; 'thorough': 0.27; 'correct': 0.28; "skip:' 10": 0.28; 'values': 0.28; 'looks': 0.29; 'operators': 0.29; 'str': 0.29; 'thus,': 0.29; 'tutorial': 0.29; 'print': 0.30; 'e.g.': 0.30; 'etc.)': 0.32; 'url:python': 0.33; 'oracle': 0.33; 'traceback': 0.33; "skip:' 20": 0.34; 'file': 0.34; 'list': 0.34; 'could': 0.35; 'quite': 0.35; 'something': 0.35; 'but': 0.36; 'list,': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; '(2)': 0.37; 'charset:us- ascii': 0.37; '(1)': 0.38; 'wrong': 0.38; 'means': 0.39; 'data': 0.39; 'does': 0.39; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'some': 0.40; 'your': 0.60; 'leading': 0.61; 'skip:u 10': 0.61; 'provide': 0.61; 'email addr:gmail.com': 0.62; 'making': 0.62; 'more': 0.63; 'between': 0.65; 'received:217': 0.66; 'results.': 0.67; 'applying': 0.70; 'special': 0.73; 'age': 0.84; '(search': 0.84; 'dict.': 0.84; 'subject:Logical': 0.84; 'mean.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From dieter <dieter@handshake.de>
Subject Re: Logical Query JSON
Date Fri, 31 Jul 2015 08:07:23 +0200
References <cd8de52a-b0cf-45f8-8d07-9867ff4f6aa4@googlegroups.com> <mpdh10$108$2@dont-email.me> <36c251da-3dc0-47ee-8831-c6ae5d092aa1@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
X-Gmane-NNTP-Posting-Host pd9e08f91.dip0.t-ipconnect.de
User-Agent Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux)
Cancel-Lock sha1:d/pKrRuEb6lg395L8VTIgf81USQ=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1104.1438322864.3674.python-list@python.org> (permalink)
Lines 118
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1438322864 news.xs4all.nl 2905 [2001:888:2000:d::a6]:60343
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!news.stben.net!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:94797

Show key headers only | View raw


subhabrata.banerji@gmail.com writes:
> ...
> I am trying to quote some of my exercises below, and my objective.

A general remark. Python errror messages are quite good (unlike e.g.
many Microsoft or Oracle error messages). You can almost always trust
them.

Thus, if you get a "SyntaxError", something with your Python syntax
is wrong (Python is *very* sensitive to leading spaces, take case
to get the correct indentation).

An "AttributeError" means that you used the attribute access syntax
("obj.attr") for something which was not an attribute.

Etc...

> (1) Exercise with objectpath: 
>>>> from objectpath import *
>>>> tree=Tree({"a":1})
>>>> tree.execute("$.a")
> 1
>>>> $
> {
>   "a":1,
>   "b":{
>     "c":[1,2,3]
>   }
> }
> SyntaxError: invalid syntax

This looks a bit strange: "$" is not a Python name - thus,
it could give a "SyntaxError" - but then, you should not see the
dict like representation between the "$" and the "SyntaxError".
Anyway, the dict like representation looks out of place - where does
it come from?

>>>> x1={"a":1,"b":{"c":[1,2,3]}}
>>>> x1.b
>
> Traceback (most recent call last):
>   File "<pyshell#46>", line 1, in <module>
>     x1.b
> AttributeError: 'dict' object has no attribute 'b'

"x1" is a dict and access to its keys is via subscription syntax
("mapping[key]"), not the attribute access syntax ("obj.attr").

>>>> x1."b"
> SyntaxError: invalid syntax

The "attr" in the attribute access syntax "obj.attr" must be
a Python name, not an expression (such as the string '"b"').

The correct access to "b" would be 'x["b"]'.


> (2) Exercise from IBM Example:
>
>>>> x1={"or":[{"age":4},{"name":"Joe"}]}
>>>> x2=x1
>>>> print x2
> {'or': [{'age': 4}, {'name': 'Joe'}]}
>>>> x1['age']
>
> Traceback (most recent call last):
>   File "<pyshell#27>", line 1, in <module>
>     x1['age']
> KeyError: 'age'

"x1" is a dict with a single key "or". The value for "or"
is a list with two dicts. The get the dict for "age",
you would use 'x1["or"][0]'; to get the age value
'x1["or"][0]["age"]'.

>>>> x1['or']
> [{'age': 4}, {'name': 'Joe'}]
>>>> x1['or']['age']
>
> Traceback (most recent call last):
>   File "<pyshell#29>", line 1, in <module>
>     x1['or']['age']
> TypeError: list indices must be integers, not str

'x1["or"]' is a list, not a dict.
>>>> x1['or'][0]
> {'age': 4}
>>>> x1['or'][1]
> {'name': 'Joe'}

You are making progress....

>
> My expectation is:
>
> If I do AND, NOT, OR with two or more JSON values like
> {"age":4}, {"name":"Joe"}, ...etc. then I should get recirprocal
> results.
> Considering each one as Python variable and applying logical Python
> operation helps, but I am looking a smarter solution. 

You might need to read through the Python tutorial - in order
to get a thorough understanding of

 *  attribute versus subscription access,
 *  Python's simple data types (such as "dict"s, "list"s, etc.)
    and how they are represented when printed
 *  what the various error messages (such as "SyntaxError",
    "AttributeError", "KeyError", "TypeError", ...) mean.

Keep in mind that Python is a (more or less) "general purpose" language
which does not know about "jsonquery". It has "and", "or" and "not"
operators (defined in the language reference) *BUT* these are not
the operators you are looking for.
You will need a special "jsonquery" extension (search
"http://pypi.python.org" to find out whether there is something like this)
which would provide appropriate support.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Logical Query JSON subhabrata.banerji@gmail.com - 2015-07-30 06:32 -0700
  Re: Logical Query JSON Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-30 15:48 +0000
    Re: Logical Query JSON subhabrata.banerji@gmail.com - 2015-07-30 09:25 -0700
      Re: Logical Query JSON dieter <dieter@handshake.de> - 2015-07-31 08:07 +0200
        Re: Logical Query JSON Denis McMahon <denismfmcmahon@gmail.com> - 2015-07-31 12:36 +0000

csiph-web