Generic dictionary

2016-11-20 Thread Thorsten Kampe
[Crossposted to tutor and general mailing list]

Hi,

I'd like to extend the dictionary class by creating a class that acts 
like a dictionary if the class is instantiated with a dictionary and 
acts like a "dictitem" ([(key1, value1), (key2, value2), ...]) if 
instantiated with a list (that is dictitem).

The code (see extract at bottom) works well but it contains a lot of 
"if this is a dictionary then do as a dictionary already does" 
boilerplate code". How can I "inherit"(?)/"subclass"(?)/derive from 
dict so I don't have to write the code for the dictionary case?

Thorsten

```
class GenericDict:
"""
a GenericDict is a dictionary or a list of tuples (when the keys
are not hashable)
"""
def __init__(inst, generic_dict):
inst._generic = generic_dict

def __getitem__(inst, key):
if isinstance(inst._generic, dict):
return inst._generic[key]
else:
return inst.values()[inst.keys().index(key)]

def values(inst):
if isinstance(inst._generic, dict):
return inst._generic.values()
else:
try:
return list(zip(*inst._generic))[1]
except IndexError:  # empty GenericDict
return ()

def keys(inst):
if isinstance(inst._generic, dict):
return inst._generic.keys()
else:
try:
return list(zip(*inst._generic))[0]
except IndexError:  # empty GenericDict
return ()
```

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Peter Otten
Thorsten Kampe wrote:

> [Crossposted to tutor and general mailing list]
> 
> Hi,
> 
> I'd like to extend the dictionary class by creating a class that acts
> like a dictionary if the class is instantiated with a dictionary and
> acts like a "dictitem" ([(key1, value1), (key2, value2), ...]) if
> instantiated with a list (that is dictitem).
> 
> The code (see extract at bottom) works well but it contains a lot of
> "if this is a dictionary then do as a dictionary already does"
> boilerplate code". How can I "inherit"(?)/"subclass"(?)/derive from
> dict so I don't have to write the code for the dictionary case?

Hm.

def GenericDict(dict_or_items):
if isinstance(dict_or_items, dict):
return dict(dict_or_items)
else:
return SimpleGenericDictWithOnlyTheFalseBranchesImplemented(
dict_or_items
)

> 
> Thorsten
> 
> ```
> class GenericDict:
> """
> a GenericDict is a dictionary or a list of tuples (when the keys
> are not hashable)
> """
> def __init__(inst, generic_dict):
> inst._generic = generic_dict
> 
> def __getitem__(inst, key):
> if isinstance(inst._generic, dict):
> return inst._generic[key]
> else:
> return inst.values()[inst.keys().index(key)]

It's not obvious why you'd ever want that. What's the use case?
If you have unhashable keys consider bisect...
 
> def values(inst):
> if isinstance(inst._generic, dict):
> return inst._generic.values()
> else:
> try:
> return list(zip(*inst._generic))[1]
> except IndexError:  # empty GenericDict
> return ()
> 
> def keys(inst):
> if isinstance(inst._generic, dict):
> return inst._generic.keys()
> else:
> try:
> return list(zip(*inst._generic))[0]
> except IndexError:  # empty GenericDict
> return ()
> ```

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Couldn't load SIP module.

2016-11-20 Thread Xristos Xristoou
Τη Σάββατο, 19 Νοεμβρίου 2016 - 10:45:16 μ.μ. UTC+2, ο χρήστης Xristos Xristoou 
έγραψε:
> hello
> i using python 2.7.12 on ubuntu 16.04 and i have that error with SIP :
> 
> Couldn't load SIP module.
> 
> 
> Traceback (most recent call last):
>   File "", line 1, in 
> ImportError: /usr/local/lib/python2.7/site-packages/sip.so: undefined symbol: 
> PyUnicodeUCS2_AsUTF8String
> 
> ('Qt version:', '4.8.7')
> ('SIP version:', '4.17')
> ('PyQt version:', '4.11.4')
> 
> any idea how to fix that ?

how to delete complete python with all packages on ubuntu ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Steve D'Aprano
On Sun, 20 Nov 2016 08:27 pm, Thorsten Kampe wrote:

> I'd like to extend the dictionary class by creating a class that acts
> like a dictionary if the class is instantiated with a dictionary and
> acts like a "dictitem" ([(key1, value1), (key2, value2), ...]) if
> instantiated with a list (that is dictitem).

I'm not sure exactly what you are trying to accomplish here. You want a
single class that sometimes behaves like a dict, and sometimes behaves like
a list? Not just any list, but specifically a list of tuples of two items.

Frankly, that sounds... weird. Generally speaking, classes don't behave
differently according to how they are created. Consider:

float(123)  # int argument

float("123")  # str argument


The object you get back is the same, regardless of whether the class was
instantiated from an int or a str. That's a general principle of Object
Oriented Programming: an object's behaviour should depend on *what it is*,
not where it came from.


> The code (see extract at bottom) works well but it contains a lot of
> "if this is a dictionary then do as a dictionary already does"
> boilerplate code". How can I "inherit"(?)/"subclass"(?)/derive from
> dict so I don't have to write the code for the dictionary case?

class MyDict(dict):
pass


inherits from dict. But that won't do what you want, since a dict doesn't
behave anything like a list of tuples. You'll have to over-ride
*everything*, filling your code with horrible stuff like this:


class MyDict(dict):
def __init__(self, *args, **kwargs):
super(MyDict, self).__init__(*args, **kwargs)
# Decide whether to act like a list or a dict.
if args and isinstance(args[0], dict):
self._act_like_dict = True
else:
self._act_like_dict = False

def __getitem__(self, key_or_index):
if self._act_like_dict:
# treat like a key
super(MyDict, self).__getitem__(key_or_index)
 else:
# treat like an index
tmp = list(self.values())
return tmp[key_or_index]

def update(self, *args, **kwargs):
if self._act_like_dict:
super(MyDict, self).update(*args, **kwargs)
 else:
raise TypeError('list doesn't support update')

def sort(self, *args, **kwargs):
if self._act_like_dict:
raise TypeError('dict doesn't support sort')
 else:
# somehow re-implement list sorting for something 
# which is like a dict
...



Etc. It would be a monstrous pain in the posterior to write, and confusing
to use. Are you sure you want this? The whole concept of a class that
behaves differently depending on how you create it feels to me like that
movie "The Fly".

I think that a better strategy would be to reconsider. Why not just write
your code to accept *either* a dict, or a list of tuples? Then you don't
have to invent some monstrous hybrid class that is like the product of an
awful teleportation accident.






-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Anny Mous
On Sun, 20 Nov 2016 08:43 pm, Peter Otten wrote:

> Thorsten Kampe wrote:
> 
>> [Crossposted to tutor and general mailing list]
>> 
>> Hi,
>> 
>> I'd like to extend the dictionary class by creating a class that acts
>> like a dictionary if the class is instantiated with a dictionary and
>> acts like a "dictitem" ([(key1, value1), (key2, value2), ...]) if
>> instantiated with a list (that is dictitem).
[...]
> def GenericDict(dict_or_items):
> if isinstance(dict_or_items, dict):
> return dict(dict_or_items)
> else:
> return SimpleGenericDictWithOnlyTheFalseBranchesImplemented(
> dict_or_items
> )


Personally, I'd go even simpler:

dict(dict_of_items)

will return a dict regardless of whether you start with another dict or a
list or tuples. And then you just work on it as if it were a dict (which is
exactly what it actually is). And if you want to turn it back into a list
of (key, value) pairs, then you call:

list(d.items())  # Python 3

d.items()  # Python 2



-- 
Steve


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Steve D'Aprano
Further thoughts come to mind, after looking more closely at your code.

On Sun, 20 Nov 2016 08:27 pm, Thorsten Kampe wrote:

> class GenericDict:
> """
> a GenericDict is a dictionary or a list of tuples (when the keys
> are not hashable)
> """
> def __init__(inst, generic_dict):
> inst._generic = generic_dict
> 
> def __getitem__(inst, key):
> if isinstance(inst._generic, dict):
> return inst._generic[key]
> else:
> return inst.values()[inst.keys().index(key)]

Your code seems to be different from your description. Rather than
*behaving* like a list of (key, item) pairs, you seem to actually have
something which always behaves like a dict, but has a different internal
storage. That's less disturbing than what I thought you wanted at first.

There's probably a "Design Pattern" name for this sort of thing: a
consistent front end, with a different back end for storage. But I guess it
is overkill for here, and besides, using a list of (key, item) is not going
to be an efficient back end storage. Oh, it will be fine if you have ten or
a hundred items, but if you have a million, it will be painful.

Better to just use a dict, always, and simply convert any list of tuples to
a dict:

py> dict([('a', 1), ('b', 2), ('c', 3)])
{'c': 3, 'a': 1, 'b': 2}


> def values(inst):
> if isinstance(inst._generic, dict):
> return inst._generic.values()
> else:
> try:
> return list(zip(*inst._generic))[1]
> except IndexError:  # empty GenericDict
> return ()

That's... weird. Sometimes your instance will return a list, and sometimes a
tuple. So if you write code expecting a list, it will suddenly and
unexpectedly break when the instance is empty.

A better way of writing this is:

def values(inst):
if isinstance(inst._generic, dict):
return inst._generic.values()
else:
return [t[1] for t in inst._generic]

(But even better is not to write it at all, and just use the dict, like I
suggested above :-)





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: help on "from deen import *" vs. "import deen"

2016-11-20 Thread Bev in TX

From the Python 3.5.2 docs:

6.15. Evaluation order
Python evaluates expressions from left to right. Notice that while 
evaluating an assignment, the right-hand side is evaluated before the 
left-hand side.


Thus, spam = eggs = cheese = obj is equivalent to:

spam = (eggs = (cheese = obj))

which is also equivalent to:

cheese = obj
eggs = cheese
spam = eggs

These all are now references to obj.

In both of your examples, everything also ends up as references to obj. 
The only difference is the order of evaluation.


Bev in TX

On 11/19/16 12:58 AM, Chris Angelico wrote:

On Sat, Nov 19, 2016 at 3:34 PM, Steve D'Aprano
 wrote:

What happens if you do this?

spam = eggs = cheese = obj

Is that different from:

spam = obj
eggs = obj
cheese = obj


or from this?

spam = obj
eggs = spam
cheese = eggs
...
These aren't silly questions.


Indeed, they are not silly. It surprised me (as a C programmer) that
the assignments happen left-to-right, rather than right-to-left (in C,
cheese would be set first, then eggs, then spam). These are questions
filled with subtleties.

ChrisA


--
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Thorsten Kampe
* Peter Otten (Sun, 20 Nov 2016 10:43:01 +0100)
> 
> Thorsten Kampe wrote:
> > 
> > I'd like to extend the dictionary class by creating a class that acts
> > like a dictionary if the class is instantiated with a dictionary and
> > acts like a "dictitem" ([(key1, value1), (key2, value2), ...]) if
> > instantiated with a list (that is dictitem).
> > 
> > The code (see extract at bottom) works well but it contains a lot of
> > "if this is a dictionary then do as a dictionary already does"
> > boilerplate code". How can I "inherit"(?)/"subclass"(?)/derive from
> > dict so I don't have to write the code for the dictionary case?
> 
> Hm.
> 
> def GenericDict(dict_or_items):
> if isinstance(dict_or_items, dict):
> return dict(dict_or_items)
> else:
> return SimpleGenericDictWithOnlyTheFalseBranchesImplemented(
> dict_or_items
> )

That would be a kind of factory function for the class?
 
> > ```
> > class GenericDict:
> > """
> > a GenericDict is a dictionary or a list of tuples (when the keys
> > are not hashable)
> > """
> > def __init__(inst, generic_dict):
> > inst._generic = generic_dict
> > 
> > def __getitem__(inst, key):
> > if isinstance(inst._generic, dict):
> > return inst._generic[key]
> > else:
> > return inst.values()[inst.keys().index(key)]
> 
> It's not obvious why you'd ever want that. What's the use case?

The use case is an Equivalence class which creates a {invariant(x): 
[x, y, z] ...} dictionary. Since the Equivalence class should accept 
all kinds of key functions, I have to make sure that lists (which are 
not hashable), etc. can be handled as keys.

Treating a list of tuples as a substitute for a dictionary is a well 
established idiom (think list(dict.items()) and dict()).

The GenericDict class allows the Equivalence class to be agnostic 
regarding the underlying data structure. It tries to create a 
dictionary and if that fails uses a dictitem. All method calls are 
the same because that's handled by GenericDict.

Thorsten

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Thorsten Kampe
* Anny Mous (Sun, 20 Nov 2016 21:46:25 +1100)
> 
> On Sun, 20 Nov 2016 08:43 pm, Peter Otten wrote:
> 
> > Thorsten Kampe wrote:
> > 
> >> [Crossposted to tutor and general mailing list]
> >> 
> >> Hi,
> >> 
> >> I'd like to extend the dictionary class by creating a class that acts
> >> like a dictionary if the class is instantiated with a dictionary and
> >> acts like a "dictitem" ([(key1, value1), (key2, value2), ...]) if
> >> instantiated with a list (that is dictitem).
> [...]
> > def GenericDict(dict_or_items):
> > if isinstance(dict_or_items, dict):
> > return dict(dict_or_items)
> > else:
> > return SimpleGenericDictWithOnlyTheFalseBranchesImplemented(
> > dict_or_items
> > )
> 
> 
> Personally, I'd go even simpler:
> 
> dict(dict_of_items)
> 
> will return a dict regardless of whether you start with another dict or a
> list or tuples.

The whole point of my posting was non hashable keys (like lists):

```
>>> dictitem
[([1], '11'), ([2], '22'), ([4], '33'), ([3], '44')]
>>> dict(dictitem)
-
TypeError   Traceback (most recent call last)
 in ()
> 1 dict(dictitem)

TypeError: unhashable type: 'list'
```

Thorsten

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: help on "from deen import *" vs. "import deen"

2016-11-20 Thread Tristan B. Kildaire
On Tue, 15 Nov 2016 22:16:07 +, Erik wrote:

> On 15/11/16 14:43, Michael Torrie wrote:
>> As you've been told several times, if you "import deen" then you can
>> place a new object into the deen namespace using something like:
>>
>> deen.foo=bar
>>
>> Importing everything from an imported module into the current module's
>> namespace is not the best idea
> 
> But "from foo import *" is not importing "everything". It's the opposite
> - it's importing everything _that the module wants to explicitly expose_
> (i.e., a subset of everything ;)).
> 
> It *used* to be everything in the module's namespace, but then the
> possibly misnamed "__all__" magic variable made it a subset of whatever
> the module's author wanted to expose.
> 
> However, "import foo" _does_ import "everything", and also gives the
> importer the power to re-bind names within that module's namespace too
> (which is the crux of the original question). This is not usually a good
> thing unless the importing module is incestuous with the imported
> module.
> 
> So this brings up another point - not sure if it has been made before:
> should a module have a way of *not* allowing an importer to re-bind its
> local bindings?
> 
> For example, something like a "__bindable__" variable such that all
> names not included may not be bound by any other module? And/or
> something like an "__export__" variable that says what gets exposed to a
> simple "import foo" (sometimes one might want to "import foo" and
> sometimes "from foo import *" for namespace reasons, but why should
> those two statements import different things)?
> 
> E.

>From deen import * imports all the things in deen but accessable with no 
`deen.`

import deen imports all of deen but accessible via `deen.thing`
-- 
https://mail.python.org/mailman/listinfo/python-list


Guido? Where are you?

2016-11-20 Thread Tristan B. Kildaire
Is Guido active on this newsgroup. Sorry for the off-topic ness.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Thorsten Kampe
* Steve D'Aprano (Sun, 20 Nov 2016 21:10:08 +1100)
> 
> On Sun, 20 Nov 2016 08:27 pm, Thorsten Kampe wrote:
> 
> > I'd like to extend the dictionary class by creating a class that acts
> > like a dictionary if the class is instantiated with a dictionary and
> > acts like a "dictitem" ([(key1, value1), (key2, value2), ...]) if
> > instantiated with a list (that is dictitem).
> 
> I'm not sure exactly what you are trying to accomplish here. You want a
> single class that sometimes behaves like a dict, and sometimes behaves like
> a list? Not just any list, but specifically a list of tuples of two items.

Treating a list of tuples as a substitute for a dictionary is a well 
established idiom (think list(dict.items()) and dict()).
 
> Frankly, that sounds... weird. Generally speaking, classes don't behave
> differently according to how they are created.

I think, you misinterpreted my question. It's not about behaving 
differently but the opposite: a streamlined interface for the caller 
so the caller does not have to worry whether it uses a dict or a 
dictitem.

See my response to Peter:
The use case is an Equivalence class which creates a {invariant(x): 
[x, y, z] ...} dictionary. Since the Equivalence class should accept 
all kinds of key functions, I have to make sure that lists (which are 
not hashable), etc. can be handled as keys.

The GenericDict class allows the Equivalence class to be agnostic 
regarding the underlying data structure. It tries to create a 
dictionary and if that fails uses a dictitem. All method calls are 
the same because that's handled by GenericDict.

Thorsten

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Chris Angelico
On Sun, Nov 20, 2016 at 11:19 PM, Thorsten Kampe
 wrote:
> The whole point of my posting was non hashable keys (like lists):
>
> ```
 dictitem
> [([1], '11'), ([2], '22'), ([4], '33'), ([3], '44')]
 dict(dictitem)
> -
> TypeError   Traceback (most recent call last)
>  in ()
> > 1 dict(dictitem)
>
> TypeError: unhashable type: 'list'
> ```

I see. So you want to be able to have something that looks and feels
like a dictionary, but uses a different way of looking things up.
Makes reasonable sense, on the surface.

Before you go down that route, I strongly recommend reading up on
exactly *why* a dictionary has the requirement of hashability, and
what the consequences are of trying to look things up using lists as
keys. You can easily experiment with it like this:

class HashableList(list):
def __hash__(self):
return hash(tuple(self))

Use those in your dict, rather than vanilla lists. Then you can mess
around with the consequences of mutable dict keys.

Alternatively, switch from using lists to using tuples. They're
hashable and immutable, thus avoiding the problems. If what you're
trying to do is use multi-part dict keys, a tuple is far and away the
best solution.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Guido? Where are you?

2016-11-20 Thread Ned Batchelder
On Sunday, November 20, 2016 at 7:24:43 AM UTC-5, Tristan B. Kildaire wrote:
> Is Guido active on this newsgroup. Sorry for the off-topic ness.

He is definitely not active on this list. The most recent message from
him here (not cross-posted) seems to be in September 2014.

Maybe there's something the rest of us can help you with?

--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: help on "from deen import *" vs. "import deen"

2016-11-20 Thread Chris Angelico
On Sun, Nov 20, 2016 at 10:47 PM, Bev in TX  wrote:
> From the Python 3.5.2 docs:
>
> 6.15. Evaluation order
> Python evaluates expressions from left to right. Notice that while
> evaluating an assignment, the right-hand side is evaluated before the
> left-hand side.
>
> Thus, spam = eggs = cheese = obj is equivalent to:
>
> spam = (eggs = (cheese = obj))

Except that that's not how it's parsed. Assignment in Python isn't an
operator. You cannot run the parenthesized version:

>>> spam = (eggs = (cheese = obj))
  File "", line 1
spam = (eggs = (cheese = obj))
 ^
SyntaxError: invalid syntax

Chained assignment is a special piece of syntax.

https://docs.python.org/3/reference/simple_stmts.html#assignment-statements

You're not evaluating one assignment and then another; it's a single
assignment statement that has a target_list. Scroll down a little in
that page and you'll see an example that specifically points this out.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Thorsten Kampe
* Steve D'Aprano (Sun, 20 Nov 2016 22:40:19 +1100)
> 
> Further thoughts come to mind, after looking more closely at your code.
> 
> On Sun, 20 Nov 2016 08:27 pm, Thorsten Kampe wrote:
> 
> > def values(inst):
> > if isinstance(inst._generic, dict):
> > return inst._generic.values()
> > else:
> > try:
> > return list(zip(*inst._generic))[1]
> > except IndexError:  # empty GenericDict
> > return ()
> 
> That's... weird. Sometimes your instance will return a list, and sometimes a
> tuple. So if you write code expecting a list, it will suddenly and
> unexpectedly break when the instance is empty.
> 
> A better way of writing this is:
> 
> def values(inst):
> if isinstance(inst._generic, dict):
> return inst._generic.values()
> else:
> return [t[1] for t in inst._generic]

You are right, I modified it to
`return [value for key, value in inst._generic]`
...which is more more intelligible than my original try/except/list
(zip)

Thanks, Thorsten

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: help on "from deen import *" vs. "import deen"

2016-11-20 Thread Ned Batchelder
On Sunday, November 20, 2016 at 6:47:35 AM UTC-5, Bev in TX wrote:
> From the Python 3.5.2 docs:
> 
> 6.15. Evaluation order
> Python evaluates expressions from left to right. Notice that while 
> evaluating an assignment, the right-hand side is evaluated before the 
> left-hand side.
> 
> Thus, spam = eggs = cheese = obj is equivalent to:
> 
> spam = (eggs = (cheese = obj))
> 
> which is also equivalent to:
> 
> cheese = obj
> eggs = cheese
> spam = eggs

This is not right.  Python will evaluate the right-hand side, and then
assign that value to all of the names, from left to right.  So the end
result is equivalent to:

spam = obj
eggs = obj
cheese = obj

The reason the doc quote above doesn't apply here is that assignment
is not an expression, but a statement of its own, described in 6.2:

"An assignment statement evaluates the expression list (remember
that this can be a single expression or a comma-separated list,
the latter yielding a tuple) and assigns the single resulting object
to each of the target lists, from left to right."

You can see this by disassembling some examples:

>>> import dis
>>> def f():
... a = b = c = expr()
...
>>> dis.dis(f)
2   0 LOAD_GLOBAL  0 (expr)
2 CALL_FUNCTION0
4 DUP_TOP
6 STORE_FAST   0 (a)
8 DUP_TOP
10 STORE_FAST   1 (b)
12 STORE_FAST   2 (c)
14 LOAD_CONST   0 (None)
16 RETURN_VALUE
>>> def g():
... d[a()] = d[b()] = d[c()] = expr()
...
>>> dis.dis(g)
2   0 LOAD_GLOBAL  0 (expr)
2 CALL_FUNCTION0
4 DUP_TOP
6 LOAD_GLOBAL  1 (d)
8 LOAD_GLOBAL  2 (a)
10 CALL_FUNCTION0
12 STORE_SUBSCR
14 DUP_TOP
16 LOAD_GLOBAL  1 (d)
18 LOAD_GLOBAL  3 (b)
20 CALL_FUNCTION0
22 STORE_SUBSCR
24 LOAD_GLOBAL  1 (d)
26 LOAD_GLOBAL  4 (c)
28 CALL_FUNCTION0
30 STORE_SUBSCR
32 LOAD_CONST   0 (None)
34 RETURN_VALUE

Of course, it hardly ever matters, because how often does anyone
use multiple assignment, and even rarer, where the targets are
computed, and even rarer, where the order of their assignment
matters?

--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Thorsten Kampe
* Chris Angelico (Sun, 20 Nov 2016 23:35:52 +1100)
> I see. So you want to be able to have something that looks and 
> feels
> like a dictionary, but uses a different way of looking things up.
> Makes reasonable sense, on the surface.
> 
> Before you go down that route, I strongly recommend reading up on
> exactly *why* a dictionary has the requirement of hashability, and
> what the consequences are of trying to look things up using lists as
> keys. You can easily experiment with it like this:
> 
> class HashableList(list):
> def __hash__(self):
> return hash(tuple(self))
> 
> Use those in your dict, rather than vanilla lists. Then you can mess
> around with the consequences of mutable dict keys.
> 
> Alternatively, switch from using lists to using tuples. They're
> hashable and immutable, thus avoiding the problems. If what you're
> trying to do is use multi-part dict keys, a tuple is far and away the
> best solution.

Chris, it's up to the consumer to decide which key/keyfunc to use. 
The lists as keys (that is first element in an tuple) are created on 
the fly and not used for anything. They are mutable but not mutated.

Mutating the list would be like mutating a list you're iterating 
through: just don't do it.

Thorsten

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Generic dictionary

2016-11-20 Thread Chris Angelico
On Mon, Nov 21, 2016 at 12:12 AM, Thorsten Kampe
 wrote:
> Chris, it's up to the consumer to decide which key/keyfunc to use.
> The lists as keys (that is first element in an tuple) are created on
> the fly and not used for anything. They are mutable but not mutated.
>
> Mutating the list would be like mutating a list you're iterating
> through: just don't do it.

Then use tuples. Job done! :)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: help on "from deen import *" vs. "import deen"

2016-11-20 Thread Bev in TX

Thanks to ChrisA and Ned for that clarification.

Bev in TX

--
https://mail.python.org/mailman/listinfo/python-list


Re: help on "from deen import *" vs. "import deen"

2016-11-20 Thread jfong
Tristan B. Kildaire at 2016/11/20 8:23:37PM wrote:
> From deen import * imports all the things in deen but accessable with no 
> `deen.`

These "accessible" objects become read-only even if it's mutable. For immutable 
objects, you can't even create a new one in the deen's namespace.

> import deen imports all of deen but accessible via `deen.thing` 

:-)

--Jach
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Numpy slow at vector cross product?

2016-11-20 Thread BartC

On 20/11/2016 20:46, DFS wrote:

import sys, time, numpy as np
loops=int(sys.argv[1])

x=np.array([1,2,3])
y=np.array([4,5,6])
start=time.clock()
for i in range(loops):
np.cross(x,y)
print "Numpy, %s loops: %.2g seconds" %(loops,time.clock()-start)

x=[1,2,3]
y=[4,5,6]
z=[0,0,0]
start=time.clock()
for i in range(loops):
z[0]=x[1]*y[2]-x[2]*y[1];
z[1]=x[2]*y[0]-x[0]*y[2];
z[2]=x[0]*y[1]-x[1]*y[0];
print "Calc, %s loops: %.2g seconds" %(loops,time.clock()-start)


I don't know why numpy is slow, but I can confirm similar results.

In fact if the workings are put into a function, then the difference is 
even more marked, with the normal calculation being 50% faster


Maybe numpy has extra overheads, and the arrays being operated on are 
very small, but even so, 30 times slower than CPython? (2.5 to 0.083 
seconds.)


--
Bartc




--
https://mail.python.org/mailman/listinfo/python-list


Re: Numpy slow at vector cross product?

2016-11-20 Thread Steve D'Aprano
On Mon, 21 Nov 2016 07:46 am, DFS wrote:

> import sys, time, numpy as np
> loops=int(sys.argv[1])
> 
> x=np.array([1,2,3])
> y=np.array([4,5,6])
> start=time.clock()
> for i in range(loops):
>  np.cross(x,y)
> print "Numpy, %s loops: %.2g seconds" %(loops,time.clock()-start)

[...]
> $ python vector_cross.py
> Numpy, 10 loops: 2.5 seconds
> Calc,  10 loops: 0.13 seconds
> 
> 
> Did I do something wrong, or is numpy slow at this?

I can confirm similar results.

However, your code is not a great way of timing code. Timing code is *very*
difficult, and can be effected by many things, such as external processes,
CPU caches, even the function you use for getting the time. Much of the
time you are timing here will be in creating the range(loops) list,
especially if loops is big.

The best way to time small snippets of code is to use the timeit module.
Open a terminal or shell (*not* the Python interactive interpreter, the
operating system's shell: you should expect a $ or % prompt) and run timeit
from that. Copy and paste the following two commands into your shell
prompt:


python2.7 -m timeit --repeat 5 -s "import numpy as np" \
-s "x = np.array([1, 2, 3])" -s "y = np.array([4, 5, 6])" \
-- "np.cross(x, y)"


python2.7 -m timeit --repeat 5 -s "x = [1, 2, 3]" \
-s "y = [4, 5, 6]" -s "z = [0, 0, 0]" \
-- "z[0] = x[1]*y[2] - x[2]*y[1]; z[1] = x[2]*y[0] - \
x[0]*y[2]; z[2] = x[0]*y[1] - x[1]*y[0]"


The results I get are:

1 loops, best of 5: 30 usec per loop

100 loops, best of 5: 1.23 usec per loop


So on my machine, np.cross() is about 25 times slower than multiplying by
hand.





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


TemplateError

2016-11-20 Thread iivri . andre
THIS error is constantly showing up when I run my python script 
 eloiim:build iivri.andre$ python run.py 
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2016-11-21 01:15:26,561] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1988, in 
wsgi_app
response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1641, in 
full_dispatch_request
rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1544, in 
handle_user_exception
reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1639, in 
full_dispatch_request
rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1625, in 
dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
  File "run.py", line 10, in IsSelf
return render_template('index.html', author=author, name=name)
  File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 133, 
in render_template
return 
_render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 
851, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
  File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 
812, in get_template
return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 
774, in _load_template
cache_key = self.loader.get_source(self, name)[1]
  File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 57, 
in get_source
return self._get_source_fast(environment, template)
  File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 85, 
in _get_source_fast
raise TemplateNotFound(template)
TemplateNotFound: index.html

THIS is the code 

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
# THE @ is a decorator that is used to 'augment' function definitions
# Flask uses 'route()' to indicate that if the browser requests the address '/' 
(the default | home address), then our app shoud 'route' that request to this 
'IsSelf' function
def IsSelf() :
author = "raphael James "
name = "Jaden iivii"
return render_template('index.html', author=author, name=name)

if __name__ == "__main__" :
app.run()


How do I fix this? 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TemplateError

2016-11-20 Thread Chris Angelico
On Mon, Nov 21, 2016 at 5:22 PM,   wrote:
> TemplateNotFound: index.html
>
> return render_template('index.html', author=author, name=name)

The render_template function looks for a directory called "templates"
and a file in that of the given name. So you'll need to have
"templates/index.html" available. Possibly you have index.html in the
wrong place?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Clean way to return error codes

2016-11-20 Thread Steven D'Aprano
I have a script that can be broken up into four subtasks. If any of those 
subtasks fail, I wish to exit with a different exit code and error.

Assume that the script is going to be run by system administrators who know no 
Python and are terrified of tracebacks, and that I'm logging the full traceback 
elsewhere (not shown).

I have something like this:


try:
begin()
except BeginError:
print("error in begin")
sys.exit(3)

try:
cur = get_cur()
except FooError:
print("failed to get cur")
sys.exit(17)

try:
result = process(cur)
print(result)
except FooError, BarError:
print("error in processing")
sys.exit(12)

try:
cleanup()
except BazError:
print("cleanup failed")
sys.exit(8)



It's not awful, but I don't really like the look of all those try...except 
blocks. Is there something cleaner I can do, or do I just have to suck it up?




-- 
Steven
299792.458 km/s — not just a good idea, it’s the law!

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TemplateError

2016-11-20 Thread Vincent Vande Vyvre

Le 21/11/2016 à 07:22, [email protected] a écrit :

THIS error is constantly showing up when I run my python script
  eloiim:build iivri.andre$ python run.py
  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2016-11-21 01:15:26,561] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1988, in 
wsgi_app
 response = self.full_dispatch_request()
   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1641, in 
full_dispatch_request
 rv = self.handle_user_exception(e)
   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1544, in 
handle_user_exception
 reraise(exc_type, exc_value, tb)
   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1639, in 
full_dispatch_request
 rv = self.dispatch_request()
   File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1625, in 
dispatch_request
 return self.view_functions[rule.endpoint](**req.view_args)
   File "run.py", line 10, in IsSelf
 return render_template('index.html', author=author, name=name)
   File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 133, 
in render_template
 return 
_render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
   File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 
851, in get_or_select_template
 return self.get_template(template_name_or_list, parent, globals)
   File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 
812, in get_template
 return self._load_template(name, self.make_globals(globals))
   File "/usr/local/lib/python2.7/site-packages/jinja2/environment.py", line 
774, in _load_template
 cache_key = self.loader.get_source(self, name)[1]
   File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 57, 
in get_source
 return self._get_source_fast(environment, template)
   File "/usr/local/lib/python2.7/site-packages/flask/templating.py", line 85, 
in _get_source_fast
 raise TemplateNotFound(template)
TemplateNotFound: index.html

THIS is the code

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
# THE @ is a decorator that is used to 'augment' function definitions
# Flask uses 'route()' to indicate that if the browser requests the address '/' 
(the default | home address), then our app shoud 'route' that request to this 
'IsSelf' function
def IsSelf() :
 author = "raphael James "
 name = "Jaden iivii"
 return render_template('index.html', author=author, name=name)

if __name__ == "__main__" :
 app.run()


How do I fix this?


Flask don't know where are your templates.

I use this:

WEBDIR = os.path.dirname(os.path.abspath('__files__'))
PAGESDIR = os.path.join(os.path.dirname(WEBDIR), 'somewhere/templates')
STATICDIR = os.path.join(os.path.dirname(WEBDIR), 'somewhere/static')
...

app = Flask(__name__, template_folder=PAGESDIR, static_folder=STATICDIR)
...

--
Vincent V.V.
Oqapy  . python3-exiv2 
 . Qarte 
 . PaQager 

--
https://mail.python.org/mailman/listinfo/python-list


Re: Clean way to return error codes

2016-11-20 Thread Jussi Piitulainen
Steven D'Aprano writes:

> I have a script that can be broken up into four subtasks. If any of
> those subtasks fail, I wish to exit with a different exit code and
> error.
>
> Assume that the script is going to be run by system administrators who
> know no Python and are terrified of tracebacks, and that I'm logging
> the full traceback elsewhere (not shown).
>
> I have something like this:
>
>
> try:
> begin()
> except BeginError:
> print("error in begin")
> sys.exit(3)
>
> try:
> cur = get_cur()
> except FooError:
> print("failed to get cur")
> sys.exit(17)
>
> try:
> result = process(cur)
> print(result)
> except FooError, BarError:
> print("error in processing")
> sys.exit(12)
>
> try:
> cleanup()
> except BazError:
> print("cleanup failed")
> sys.exit(8)
>
>
>
> It's not awful, but I don't really like the look of all those
> try...except blocks. Is there something cleaner I can do, or do I just
> have to suck it up?

Have the exception objects carry the message and the exit code?

try:
begin()
cur = get_cur()
result = process(cur)
print(result)
cleanup()
except (BeginError, FooError, BarError, BazError) as exn:
print("Steven's script:", message(exn))
sys.exit(code(exn))
-- 
https://mail.python.org/mailman/listinfo/python-list