Re: Sorting dictionary by datetime value

2014-02-08 Thread Frank Millman

"Chris Angelico"  wrote in message 
news:captjjmqdusdfc1elbu6lf5-up__lae-63ii0uuvaggnem9u...@mail.gmail.com...
> On Sat, Feb 8, 2014 at 6:06 PM, Igor Korot  wrote:
> sorted(a.items(), key=a.get)
>> [('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100)), ('3', 
>> datetime.datetim
>> e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.datetime(2012, 12, 28, 
>> 12, 17,
>>  29, 100))]

That seemed like a neat trick, so I thought I would try to understand it a 
bit better in case I could use it some day.

I am using python3. I don't know if that makes a difference, but I cannot 
get it to work.

>>> d = {1: 'abc', 2: 'xyz', 3: 'pqr'}
>>> sorted(d.items(), key=d.get)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unorderable types: NoneType() < NoneType()
>>>

I know that python3 is stricter regarding ordering of non-comparable types, 
but I don't see where None is coming from.

I have python 2.7.3 on another machine. Here are the results -

>>> d = {1: 'abc', 2: 'xyz', 3: 'pqr'}
>>> sorted(d.items(), key=d.get)
[(1, 'abc'), (2, 'xyz'), (3, 'pqr')]

It did not crash, but it did not sort.

Then I changed the keys to strings, to match Igor's example -

>>> d = {'1': 'abc', '2': 'xyz', '3': 'pqr'}
>>> sorted(d.items(), key=d.get)
[('1', 'abc'), ('3', 'pqr'), ('2', 'xyz')]

It works - now I am even more confused.

Any hints will be appreciated.

Frank Millman



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


Re: Sorting dictionary by datetime value

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 6:53 PM, Igor Korot  wrote:
> Chris,
>
> On Fri, Feb 7, 2014 at 11:09 PM, Chris Angelico  wrote:
>> On Sat, Feb 8, 2014 at 6:06 PM, Igor Korot  wrote:
>> sorted(a.items(), key=a.get)
>>> [('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100)), ('3', 
>>> datetime.datetim
>>> e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.datetime(2012, 12, 28, 
>>> 12, 17,
>>>  29, 100))]
>>
>>>
>>> However, trying to do the same thing from the script does not sort the
>>> dictionary:
>>>
>>> sorted(my_dict.items(), key=my_dict.get, reverse=False)
>>> for key, value in my_dict.items():
>>>  print value, key
>>>
>>> the dictionary prints with unsorted items.
>>
>> The sorted() function returns a sorted list. You're then going back to
>> the original dictionary. Instead, just iterate over the sorted items:
>>
>> items = sorted(my_dict.items(), key=my_dict.get, reverse=False)
>> for key, value in items:
>> print value, key
>
> Still does not work. It prints:
>
> ===
> DATE TIME - EVENT
> 058f63666438&0  - Instance ID
> Original values
> 2013-11-15 15:42:27.000499 User Datetime
> 2013-07-14 16:42:18.000637 Property Keys
> 2013-11-15 15:42:17.000938 Volume Device
> 2013-07-14 16:42:22.000276 Last Modify Reg Times 1
> Sorted values
> 2013-11-15 15:42:27.000499 User Datetime
> 2013-07-14 16:42:18.000637 Property Keys
> 2013-11-15 15:42:17.000938 Volume Device
> 2013-07-14 16:42:22.000276 Last Modify Reg Times 1
>
> Code is as follows:
>
> sorted_items = sorted(my_dict.items(), key=my_dict.get, reverse=False)
> print row[19], " - Instance ID"
> print "Original values"
> for key, value in my_dict.items():
>  print value, key
> print "Sorted values"
> for key, value in sorted_items:
>  print value, key
>
> Thank you.

Assuming you sent that privately only by mistake - hope you don't mind
me responding on-list.

The problem here is actually your key function. my_dict.items()
returns a series of two-item tuples, none of which exists in your
dictionary; so you're actually sorting [None, None, None], which isn't
very useful.

Try this:

sorted_items = sorted(my_dict.keys(), key=my_dict.get)
for key in sorted_items:
print my_dict[key], key

Note that reverse=False is the default, so you don't need to specify that.

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


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread Asaf Las
On Saturday, February 8, 2014 9:41:53 AM UTC+2, cstru...@gmail.com wrote:
> I am writing a couple of class methods to build up several 
> lines of html.  Some of the lines are conditional and most need 
> variables inserted in them.  Searching the web has given me a 
> few ideas.  Each has its pro's and cons.
> 
> The best I have come up with is:
> 
> def output_header_js(self, jquery=True, theme=None):
> if self.static_path is None :
> return None
> 
> if jquery is True:
> output = '" output += 'src="/%s/jquery/jqueryui.js">'% static
> output += '" output += 'src="/%s/jquery/jquery.js">'% static
> 
> if theme is not None:
> output += ' theme
> 
> output += 'rel="stylesheet" type="text/css" />'
> 
> output += '" output += 'src="/%s/jtable/jquery.jtable.js">' % "static"
> 
> I realize that a lot of the above looks repetitive but it is 
> designed to eliminate boilerplate HTML.
> 

note, due to strings are immutable - for every line in sum operation 
above you produce new object and throw out older one. you can write 
one string spanned at multiple lines in very clear form. 


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


Re: Sorting dictionary by datetime value

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 7:03 PM, Frank Millman  wrote:
> I am using python3. I don't know if that makes a difference, but I cannot
> get it to work.
>
 d = {1: 'abc', 2: 'xyz', 3: 'pqr'}
 sorted(d.items(), key=d.get)
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: unorderable types: NoneType() < NoneType()


You probably hadn't seen my subsequent post yet, in which I explain
what's going on here.

In Python 2, "None > None" is simply False. (So is "None < None",
incidentally.) Py3 makes that an error. But in all your examples,
you're effectively trying to sort the list [None, None, None], which
is never going to be useful. What you can do, though, is either sort
items using itemgetter to sort by the second element of the tuple, or
sort keys using dict.get.

Using 3.4.0b2:

>>> d = {1: 'abc', 2: 'xyz', 3: 'pqr'}
>>> sorted(d.keys(), key=d.get)
[1, 3, 2]

You don't get the values that way, but you get the keys in their
correct order, so you can iterate over that and fetch the
corresponding values. Alternatively, this is a bit more verbose, but
works on items():

>>> import operator
>>> sorted(d.items(), key=operator.itemgetter(1))
[(1, 'abc'), (3, 'pqr'), (2, 'xyz')]

operator.itemgetter(1) returns a callable that, when passed some
object, returns that_object[1].

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


Re: Sorting dictionary by datetime value

2014-02-08 Thread Frank Millman

"Frank Millman"  wrote in message 
news:ld4ocf$9rg$1...@ger.gmane.org...
>
> "Chris Angelico"  wrote in message 
> news:captjjmqdusdfc1elbu6lf5-up__lae-63ii0uuvaggnem9u...@mail.gmail.com...
>> On Sat, Feb 8, 2014 at 6:06 PM, Igor Korot  wrote:
>> sorted(a.items(), key=a.get)
>>> [('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100)), ('3', 
>>> datetime.datetim
>>> e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.datetime(2012, 12, 28, 
>>> 12, 17,
>>>  29, 100))]
>
> That seemed like a neat trick, so I thought I would try to understand it a 
> bit better in case I could use it some day.
>
> I am using python3. I don't know if that makes a difference, but I cannot 
> get it to work.
>
 d = {1: 'abc', 2: 'xyz', 3: 'pqr'}
 sorted(d.items(), key=d.get)
> Traceback (most recent call last):
>  File "", line 1, in 
> TypeError: unorderable types: NoneType() < NoneType()

>
> I know that python3 is stricter regarding ordering of non-comparable 
> types, but I don't see where None is coming from.
>
> I have python 2.7.3 on another machine. Here are the results -
>
 d = {1: 'abc', 2: 'xyz', 3: 'pqr'}
 sorted(d.items(), key=d.get)
> [(1, 'abc'), (2, 'xyz'), (3, 'pqr')]
>
> It did not crash, but it did not sort.
>
> Then I changed the keys to strings, to match Igor's example -
>
 d = {'1': 'abc', '2': 'xyz', '3': 'pqr'}
 sorted(d.items(), key=d.get)
> [('1', 'abc'), ('3', 'pqr'), ('2', 'xyz')]
>
> It works - now I am even more confused.
>

As Chris replied in another post -

> The problem here is actually your key function. my_dict.items()
> returns a series of two-item tuples, none of which exists in your
> dictionary; so you're actually sorting [None, None, None], which isn't
> very useful.
>
> Try this:
>
> sorted_items = sorted(my_dict.keys(), key=my_dict.get)
> for key in sorted_items:
>print my_dict[key], key
>

Without realising it, you have answered all my questions - thanks very much.

Frank



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


Re: Possible bug with stability of mimetypes.guess_* function output

2014-02-08 Thread Asaf Las
On Saturday, February 8, 2014 9:51:48 AM UTC+2, Peter Otten wrote:
> 
> At least the mimetypes already defined in the module could easily produce 
> the same guessed extension consistently.

imho one workaround for OP could be to supply own map file in init() thus 
ensure unambiguous mapping across every platform and distribution. guess 
some libraries already doing that. or write wrapper and process all_guesses
to eliminate ambiguity up to needed requirement.
that is in case if bug request will be rejected. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Fwd: Sorting dictionary by datetime value

2014-02-08 Thread Igor Korot
-- Forwarded message --
From: Igor Korot 
Date: Sat, Feb 8, 2014 at 12:25 AM
Subject: Re: Sorting dictionary by datetime value
To: Chris Angelico 


Chris,


On Fri, Feb 7, 2014 at 11:58 PM, Chris Angelico  wrote:

> On Sat, Feb 8, 2014 at 6:53 PM, Igor Korot  wrote:
> > Chris,
> >
> > On Fri, Feb 7, 2014 at 11:09 PM, Chris Angelico 
> wrote:
> >> On Sat, Feb 8, 2014 at 6:06 PM, Igor Korot  wrote:
> >> sorted(a.items(), key=a.get)
> >>> [('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100)), ('3',
> datetime.datetim
> >>> e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.datetime(2012, 12,
> 28, 12, 17,
> >>>  29, 100))]
> >>
> >>>
> >>> However, trying to do the same thing from the script does not sort the
> >>> dictionary:
> >>>
> >>> sorted(my_dict.items(), key=my_dict.get, reverse=False)
> >>> for key, value in my_dict.items():
> >>>  print value, key
> >>>
> >>> the dictionary prints with unsorted items.
> >>
> >> The sorted() function returns a sorted list. You're then going back to
> >> the original dictionary. Instead, just iterate over the sorted items:
> >>
> >> items = sorted(my_dict.items(), key=my_dict.get, reverse=False)
> >> for key, value in items:
> >> print value, key
> >
> > Still does not work. It prints:
> >
> > ===
> > DATE TIME - EVENT
> > 058f63666438&0  - Instance ID
> > Original values
> > 2013-11-15 15:42:27.000499 User Datetime
> > 2013-07-14 16:42:18.000637 Property Keys
> > 2013-11-15 15:42:17.000938 Volume Device
> > 2013-07-14 16:42:22.000276 Last Modify Reg Times 1
> > Sorted values
> > 2013-11-15 15:42:27.000499 User Datetime
> > 2013-07-14 16:42:18.000637 Property Keys
> > 2013-11-15 15:42:17.000938 Volume Device
> > 2013-07-14 16:42:22.000276 Last Modify Reg Times 1
> >
> > Code is as follows:
> >
> > sorted_items = sorted(my_dict.items(), key=my_dict.get, reverse=False)
> > print row[19], " - Instance ID"
> > print "Original values"
> > for key, value in my_dict.items():
> >  print value, key
> > print "Sorted values"
> > for key, value in sorted_items:
> >  print value, key
> >
> > Thank you.
>
> Assuming you sent that privately only by mistake - hope you don't mind
> me responding on-list.
>
> The problem here is actually your key function. my_dict.items()
> returns a series of two-item tuples, none of which exists in your
> dictionary; so you're actually sorting [None, None, None], which isn't
> very useful.
>
> Try this:
>
> sorted_items = sorted(my_dict.keys(), key=my_dict.get)
> for key in sorted_items:
> print my_dict[key], key
>

This code fail.
sorted_item is a list of tuples. And so iterating the list in the for loop
I will get a tuple.
It probably should be:

for key[1] in sorted_items:

Let me try that.

Thank you.


>
> Note that reverse=False is the default, so you don't need to specify that.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sorting dictionary by datetime value

2014-02-08 Thread Peter Otten
Frank Millman wrote:

> 
> "Chris Angelico"  wrote in message
> news:captjjmqdusdfc1elbu6lf5-up__lae-63ii0uuvaggnem9u...@mail.gmail.com...
>> On Sat, Feb 8, 2014 at 6:06 PM, Igor Korot  wrote:
>> sorted(a.items(), key=a.get)
>>> [('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100)), ('3',
>>> datetime.datetim
>>> e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.datetime(2012, 12, 28,
>>> 12, 17,
>>>  29, 100))]
> 
> That seemed like a neat trick, so I thought I would try to understand it a
> bit better in case I could use it some day.
> 
> I am using python3. I don't know if that makes a difference, but I cannot
> get it to work.
> 
 d = {1: 'abc', 2: 'xyz', 3: 'pqr'}
 sorted(d.items(), key=d.get)
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: unorderable types: NoneType() < NoneType()

> 
> I know that python3 is stricter regarding ordering of non-comparable
> types, but I don't see where None is coming from.
> 
> I have python 2.7.3 on another machine. Here are the results -
> 
 d = {1: 'abc', 2: 'xyz', 3: 'pqr'}
 sorted(d.items(), key=d.get)
> [(1, 'abc'), (2, 'xyz'), (3, 'pqr')]
> 
> It did not crash, but it did not sort.
> 
> Then I changed the keys to strings, to match Igor's example -
> 
 d = {'1': 'abc', '2': 'xyz', '3': 'pqr'}
 sorted(d.items(), key=d.get)
> [('1', 'abc'), ('3', 'pqr'), ('2', 'xyz')]
> 
> It works - now I am even more confused.
> 
> Any hints will be appreciated.

Chris has already explained it. Here you can watch the key calculation at 
work:

>>> d = {'1': 'abc', '2': 'xyz', '3': 'pqr'}
>>> def sortkey(value):
... key = d.get(value)
... print "value:", value, "sort-key:", key
... return key
... 
>>> sorted(d.items(), key=sortkey)
value: ('1', 'abc') sort-key: None
value: ('3', 'pqr') sort-key: None
value: ('2', 'xyz') sort-key: None
[('1', 'abc'), ('3', 'pqr'), ('2', 'xyz')]

Can you change the dict to make d.get return non-None values? 

The OP was probably trying to mimic sorted(d, key=d.get) which sorts the 
dict keys by the associated dict values:

>>> sorted(d, key=sortkey)
value: 1 sort-key: abc
value: 3 sort-key: pqr
value: 2 sort-key: xyz
['1', '3', '2']


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


Re: Sorting dictionary by datetime value

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 7:25 PM, Igor Korot  wrote:
>> Try this:
>>
>> sorted_items = sorted(my_dict.keys(), key=my_dict.get)
>> for key in sorted_items:
>> print my_dict[key], key
>
>
> This code fail.
> sorted_item is a list of tuples. And so iterating the list in the for loop I
> will get a tuple.
> It probably should be:
>
> for key[1] in sorted_items:
>
> Let me try that.
>

Actually, it's a list of keys - notice that I changed my_dict.items()
into my_dict.keys()?

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


Re: Sorting dictionary by datetime value

2014-02-08 Thread Igor Korot
Thank you. That worked.
And no, I didn't notice that change. :(


On Sat, Feb 8, 2014 at 12:29 AM, Chris Angelico  wrote:

> On Sat, Feb 8, 2014 at 7:25 PM, Igor Korot  wrote:
> >> Try this:
> >>
> >> sorted_items = sorted(my_dict.keys(), key=my_dict.get)
> >> for key in sorted_items:
> >> print my_dict[key], key
> >
> >
> > This code fail.
> > sorted_item is a list of tuples. And so iterating the list in the for
> loop I
> > will get a tuple.
> > It probably should be:
> >
> > for key[1] in sorted_items:
> >
> > Let me try that.
> >
>
> Actually, it's a list of keys - notice that I changed my_dict.items()
> into my_dict.keys()?
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Possible bug with stability of mimetypes.guess_* function output

2014-02-08 Thread Peter Otten
Asaf Las wrote:

> On Saturday, February 8, 2014 9:51:48 AM UTC+2, Peter Otten wrote:
>> 
>> At least the mimetypes already defined in the module could easily produce
>> the same guessed extension consistently.
> 
> imho one workaround for OP could be to supply own map file in init() thus
> ensure unambiguous mapping across every platform and distribution. guess
> some libraries already doing that. or write wrapper and process
> all_guesses to eliminate ambiguity up to needed requirement.
> that is in case if bug request will be rejected.

You also have to set mimetypes.types_map and mimetypes.common_types to an 
empty dict (or an OrderedDict).


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


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread Rustom Mody
On Saturday, February 8, 2014 1:11:53 PM UTC+5:30, cstru...@gmail.com wrote:
> I am writing a couple of class methods to build up several lines of html.  
> Some of the lines are conditional and most need variables inserted in them.  
> Searching the web has given me a few ideas.  Each has its pro's and cons.

For creating html the method of choice is a template engine -- cheetah, mako 
and a dozen others

You can of course roll your own (poor mans version) template engine
For that look up 
1. triple quoted strings
2. format operator
-- 
https://mail.python.org/mailman/listinfo/python-list


datetime formatting output

2014-02-08 Thread Igor Korot
Hi, ALL,
I am reading data from the DB (mySQL) where the datetime field is stored as:

2012-12-12 23:59:59.099

When I retrieve this date I am successfully see under debugger the
dateteime object with (2012, 12, 12, 23, 59, 59, 099)

However as you can see from my previous post this date shows up incorrectly
as:

2012-12-12 23:59:59.99

Notice 3 extra 0's in the milliseconds field.

How do I display this datetime object properly?

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


Re: datetime formatting output

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 7:40 PM, Igor Korot  wrote:
> I am reading data from the DB (mySQL) where the datetime field is stored as:
>
> 2012-12-12 23:59:59.099
>
> When I retrieve this date I am successfully see under debugger the dateteime
> object with (2012, 12, 12, 23, 59, 59, 099)
>
> However as you can see from my previous post this date shows up incorrectly
> as:
>
> 2012-12-12 23:59:59.99
>
> Notice 3 extra 0's in the milliseconds field.

It's not a milliseconds field, that's why :) The real question is: Why
is the datetime you're getting from MySQL putting milliseconds into
the microseconds field? Possibly if you show your code for generating
those datetime objects, that would help.

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


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread Peter Otten
cstrutto...@gmail.com wrote:

> I am writing a couple of class methods to build up several lines of html. 
> Some of the lines are conditional and most need variables inserted in
> them.  Searching the web has given me a few ideas.  Each has its pro's and
> cons.
> 
> The best I have come up with is:
> 
> 
> def output_header_js(self, jquery=True, theme=None):
> if self.static_path is None :
> return None
> 
> if jquery is True:
> output = '" output += 'src="/%s/jquery/jqueryui.js">'% static
> output += '" output += 'src="/%s/jquery/jquery.js">'% static
> 
> if theme is not None:
> output += ' theme output += 'rel="stylesheet" type="text/css" />'
> 
> output += '" output += 'src="/%s/jtable/jquery.jtable.js">' % "static"
> 
> 
> I realize that a lot of the above looks repetitive but it is designed to
> eliminate boilerplate HTML.

I sometimes use a variation of the above

def output_header(...):
if jquery:
yield """src=..."""
if theme is not None:
yield """ I have another method that will build some javascript that looks like
> this:
> 
> $('#StudentTableContainer').jtable({
> title: 'The Student List',
> paging: true, //Enable paging
> pageSize: 10, //Set page size (default: 10)
> sorting: true, //Enable sorting
> defaultSorting: 'Name ASC', //Set default sorting
> actions: {
> listAction: '/Demo/StudentList',
> deleteAction: '/Demo/DeleteStudent',
> updateAction: '/Demo/UpdateStudent',
> createAction: '/Demo/CreateStudent'
> },
> fields: {
> StudentId: {
> key: true,
> create: false,
> edit: false,
> list: false
> },
> Name: {
> title: 'Name',
> width: '23%'
> },
> EmailAddress: {
> title: 'Email address',
> list: false
> },
> ...
> 
> Almost every line in this code will require variable insertion or if
> statements.
> 
> Any thoughts on how to improve this?  Thanks in advance.  Chris

Again, you can build this with Python proper

"... title: {title} ...".format(title="The Student List", ...)

but a template language will make sure that your data is escaped properly, 
think

"... title: {title} ...".format(title="The Student's List", ...)


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


Why use _mysql module and not use MySQLdb directly?

2014-02-08 Thread Sam
I am writing my first python script to access MySQL database. With reference to 
http://mysql-python.sourceforge.net/MySQLdb.html#connection-objects

Why is it advisable to use _mysql and not MySQLdb module directly?
-- 
https://mail.python.org/mailman/listinfo/python-list


What is the recommended python module for SQL database access?

2014-02-08 Thread Sam
Is MySQLdb the recommended python module for SQL database access? Are there 
other modules? What I want in a module is to be able to write readable and 
maintainable code.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why use _mysql module and not use MySQLdb directly?

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 7:52 PM, Sam  wrote:
> I am writing my first python script to access MySQL database. With reference 
> to http://mysql-python.sourceforge.net/MySQLdb.html#connection-objects
>
> Why is it advisable to use _mysql and not MySQLdb module directly?

Other way around. It's advisable to ignore _mysql, which is a fairly
direct representation of the MySQL C API, and use MySQLdb instead.
When you use MySQLdb, you can fairly easily switch over to another
database engine (like PostgreSQL, which for most purposes is superior
to MySQL anyway), without changing most of your code. The only reason
to use _mysql would be if you need your code to be really similar to
other MySQL code in some other language - maybe you're using Python to
prototype a C application, and want to keep everything as close as you
can. Normally, use the higher level module.

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


Re: What is the recommended python module for SQL database access?

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 7:55 PM, Sam  wrote:
> Is MySQLdb the recommended python module for SQL database access? Are there 
> other modules? What I want in a module is to be able to write readable and 
> maintainable code.
>

As long as you use some module that speaks the Python Database API
(PEP 249, if a quick Google search has given me the right number), use
whatever talks to the database back-end you want - all your code will
be basically the same. Are you starting a completely new project and
creating its database? Go with SQLite or PostgreSQL (the former if
your needs are simple, the latter if you want a full-featured database
engine); both are open source and excellent. Are you connecting to an
existing database? Use that database engine, obviously :)

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


Fwd: datetime formatting output

2014-02-08 Thread Igor Korot
-- Forwarded message --
From: Igor Korot 
Date: Sat, Feb 8, 2014 at 1:06 AM
Subject: Re: datetime formatting output
To: Chris Angelico 


Chris,


On Sat, Feb 8, 2014 at 12:45 AM, Chris Angelico  wrote:

> On Sat, Feb 8, 2014 at 7:40 PM, Igor Korot  wrote:
> > I am reading data from the DB (mySQL) where the datetime field is stored
> as:
> >
> > 2012-12-12 23:59:59.099
> >
> > When I retrieve this date I am successfully see under debugger the
> dateteime
> > object with (2012, 12, 12, 23, 59, 59, 099)
> >
> > However as you can see from my previous post this date shows up
> incorrectly
> > as:
> >
> > 2012-12-12 23:59:59.99
> >
> > Notice 3 extra 0's in the milliseconds field.
>
> It's not a milliseconds field, that's why :) The real question is: Why
> is the datetime you're getting from MySQL putting milliseconds into
> the microseconds field? Possibly if you show your code for generating
> those datetime objects, that would help.
>

Nothing fancy, really. ;-)

import MySQLdb as mdb
self.conn = mdb.connect()
self.cur = self.conn.cursor()
self.cur.execute("SELECT * FROM mytable")
db_results = self.cur.fetchall()
for row in db_results:
 my_dict = {}
 #Fill in my_dict
 if row[3] is not None:
  my_dict["Install"] = row[3]

That's all.

P.S.: Maybe its a problem with the datetime module which formats the
datetime incorrectly?


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


Re: datetime formatting output

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 8:06 PM, Igor Korot  wrote:
> P.S.: Maybe its a problem with the datetime module which formats the
> datetime incorrectly?

No, I'm pretty sure datetime.datetime really is meant to be working
with microseconds. I'm not very familiar with MySQLdb, haven't used it
in years; it could be a bug there, or it could be something different
again.

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


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread cstrutton11
On Saturday, February 8, 2014 3:35:34 AM UTC-5, Rustom Mody wrote:
> On Saturday, February 8, 2014 1:11:53 PM UTC+5:30, cstru...@gmail.com wrote:
> 
> > I am writing a couple of class methods to build up several lines of html.  
> > Some of the lines are conditional and most need variables inserted in them. 
> >  Searching the web has given me a few ideas.  Each has its pro's and cons.
> 
> 
> 
> For creating html the method of choice is a template engine -- cheetah, mako 
> 
> and a dozen others
> 
> 
> 
> You can of course roll your own (poor mans version) template engine
> 
> For that look up 
> 
> 1. triple quoted strings
> 
> 2. format operator

I am using this with a template engine.  This method is going into a pyramid 
view class which will be rendered with chameleon or any other template engine.  
Actually it is going into a base class to be inherited into a view class.  The 
idea is to setup all the parameters for the HTML when the view class is created 
and reuse all that info for the initial page as well as all the ajax calls.

I didn't realize I could use formatting with triple quoted strings.  I will 
look into that.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: datetime formatting output

2014-02-08 Thread Igor Korot
Thank you Chris.


On Sat, Feb 8, 2014 at 1:10 AM, Chris Angelico  wrote:

> On Sat, Feb 8, 2014 at 8:06 PM, Igor Korot  wrote:
> > P.S.: Maybe its a problem with the datetime module which formats the
> > datetime incorrectly?
>
> No, I'm pretty sure datetime.datetime really is meant to be working
> with microseconds. I'm not very familiar with MySQLdb, haven't used it
> in years; it could be a bug there, or it could be something different
> again.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread cstrutton11
On Saturday, February 8, 2014 3:13:54 AM UTC-5, Asaf Las wrote:
 
> 
> note, due to strings are immutable - for every line in sum operation 
> 
> above you produce new object and throw out older one. you can write 
> 
> one string spanned at multiple lines in very clear form. 
> 

I get what your saying here about immutable strings.  Is there anyway 
efficiently build large strings with lots of conditional inclusions, repetative 
sections built dynamically by looping (see the field section above)etc.  Is 
there a mutable string class?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread cstrutton11
On Saturday, February 8, 2014 3:13:54 AM UTC-5, Asaf Las wrote:

> 
> note, due to strings are immutable - for every line in sum operation 
> 
> above you produce new object and throw out older one. you can write 
> 
> one string spanned at multiple lines in very clear form. 
> 
> /Asaf

I think I going to rewrite this to build up a list of strings and then run a 
join on them at the end.  Each section can be conditionally built up with 
variable insertions as required.  This should be more efficient and will scale 
nicely as required.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using virtualenv to bypass sudoer issues

2014-02-08 Thread Glenn Hutchings

On 06/02/14 17:32, Jean-Michel Pichavant wrote:

> Assuming I have a debian workstation for which I don't have any sudo
> rights, in order to be able to install / remove python packages, should
> I be using virtualenv ? Is it a suited solution ?

It depends on whether you need to share the installation with anyone 
else.  If not, you could also install packages using:


python setup.py install --user

This will install in your home directory, in the '.local' subdirectory. 
 And to run any scripts that get installed, add ~/.local/bin to your PATH.


Glenn

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


Re: Possible bug with stability of mimetypes.guess_* function output

2014-02-08 Thread Asaf Las
On Saturday, February 8, 2014 10:39:06 AM UTC+2, Peter Otten wrote:
> Asaf Las wrote:
> > On Saturday, February 8, 2014 9:51:48 AM UTC+2, Peter Otten wrote:
> >> At least the mimetypes already defined in the module could easily produce
> >> the same guessed extension consistently.
> > imho one workaround for OP could be to supply own map file in init() thus
> > ensure unambiguous mapping across every platform and distribution. guess
> > some libraries already doing that. or write wrapper and process
> > all_guesses to eliminate ambiguity up to needed requirement.
> > that is in case if bug request will be rejected.
> 
> You also have to set mimetypes.types_map and mimetypes.common_types to an 
> empty dict (or an OrderedDict).

Hmmm, yes. then the quickest workaround is to get all guesses list then
sort it and use the one at index 0.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread Steven D'Aprano
On Sat, 08 Feb 2014 01:56:46 -0800, cstrutton11 wrote:

> On Saturday, February 8, 2014 3:13:54 AM UTC-5, Asaf Las wrote:
>> note, due to strings are immutable - for every line in sum operation
>> above you produce new object and throw out older one. you can write
>> one string spanned at multiple lines in very clear form.
>> 
> I get what your saying here about immutable strings.  Is there anyway
> efficiently build large strings with lots of conditional inclusions,
> repetative sections built dynamically by looping (see the field section
> above)etc. 

Yes. Build up all the substrings individually, storing them in a list. 
Then, when you are ready to actually use the string, assemble it in one 
go.

substrings = []
for x in whatever():
if condition():
substrings.append("something")

process("".join(substrings))


> Is there a mutable string class?

No. Well, actually there is, but it's a toy, and operates under the hood 
by creating new immutable strings, so there's no point using it. It may 
even have been removed from more recent versions of Python.



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


Re: how to reduce bugs due to incorrect indentation

2014-02-08 Thread Jurko Gospodnetić

  Hi,

On 7.2.2014. 2:20, msus...@gmail.com wrote:

Based on the responses I arrived to the conclusion that there
is no better solution than trying to be careful and have good
testing suites.

It would be possible to disable the Tab key completely
...[snipped]...
Maybe a coloring of the background based on tab position
...[snipped]...
I also considered
...[snipped]...


  YMMV, but for me, just reading through this fun thread took more
time then ever debugging issues caused by bad Python code
indentation. :-D

  So, my suggestion would be to just ignore the problem and deal
with any resulting issues as they occur.

  Clean coding & development practices, some of which have been
mentioned earlier in this thread and are useful for many other
reasons as well, will additionally reduce the chance of such
errors causing any non-trivial issues.

  Best regards,
Jurko Gospodnetić

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


Re: Why use _mysql module and not use MySQLdb directly?

2014-02-08 Thread Asaf Las
On Saturday, February 8, 2014 10:52:36 AM UTC+2, Sam wrote:
> I am writing my first python script to access MySQL database. 
> With reference to 
> http://mysql-python.sourceforge.net/MySQLdb.html#connection-objects
> Why is it advisable to use _mysql and not MySQLdb module directly?

I used this one from Oracle and it was OK for simple test case and 
supports from 2.6 till 3.3:
http://dev.mysql.com/doc/connector-python/en/index.html
https://pypi.python.org/pypi/mysql-connector-python/1.1.5


yet there is page to bunch of others but i have never tried them:
https://wiki.python.org/moin/MySQL

Are there hidden issues about Oracle provided connector?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why use _mysql module and not use MySQLdb directly?

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 10:09 PM, Asaf Las  wrote:
> I used this one from Oracle and it was OK for simple test case and
> supports from 2.6 till 3.3:
> http://dev.mysql.com/doc/connector-python/en/index.html
> https://pypi.python.org/pypi/mysql-connector-python/1.1.5
>
>
> yet there is page to bunch of others but i have never tried them:
> https://wiki.python.org/moin/MySQL
>
> Are there hidden issues about Oracle provided connector?

I don't know. The first thing I'd look for is compatibility with the
Python Database API. I flipped through the docs without finding
anything obvious either direction; it seems to be similar, at least,
but it's not declaring that it complies, which I would have thought
would be an important boast.

Also check for platform availability. If one package is available on
Linux, Mac, Windows, and myriad others, and the other is available on
only a few platforms, that's a mark in favour of the first. But I
suspect that won't be an issue with most of what you'll find.

My suspicion, without any proof, is that it's going to come down to a
matter of taste, or maybe some tangential features. The core will most
likely work just fine with pretty much any module you choose to use.

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


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread Asaf Las
On Saturday, February 8, 2014 11:56:46 AM UTC+2, cstru...@gmail.com wrote:
> On Saturday, February 8, 2014 3:13:54 AM UTC-5, Asaf Las wrote:
> 
>  
> 
> > 
> 
> > note, due to strings are immutable - for every line in sum operation 
> 
> > 
> 
> > above you produce new object and throw out older one. you can write 
> 
> > 
> 
> > one string spanned at multiple lines in very clear form. 
> 
> > 
> 
> 
> 
> I get what your saying here about immutable strings.  
> Is there anyway efficiently build large strings with 
> lots of conditional inclusions, repetitive sections 
> built dynamically by looping (see the field section above)etc.  
> Is there a mutable string class?

Check this approach if it suits you:

str_t= '' \
''.format('bella', 'donna')

print(str_t)

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


Re: Why use _mysql module and not use MySQLdb directly?

2014-02-08 Thread Asaf Las
On Saturday, February 8, 2014 1:25:15 PM UTC+2, Chris Angelico wrote:
> On Sat, Feb 8, 2014 at 10:09 PM, Asaf Las  wrote:
> 
> > I used this one from Oracle and it was OK for simple test case and
> > supports from 2.6 till 3.3:
> > http://dev.mysql.com/doc/connector-python/en/index.html
> > https://pypi.python.org/pypi/mysql-connector-python/1.1.5
> > yet there is page to bunch of others but i have never tried them:
> > https://wiki.python.org/moin/MySQL
> 
> > Are there hidden issues about Oracle provided connector?
> 
> I don't know. The first thing I'd look for is compatibility with the
> Python Database API. I flipped through the docs without finding
> anything obvious either direction; it seems to be similar, at least,
> but it's not declaring that it complies, which I would have thought
> would be an important boast.
> Also check for platform availability. If one package is available on
> Linux, Mac, Windows, and myriad others, and the other is available on
> only a few platforms, that's a mark in favour of the first. But I
> suspect that won't be an issue with most of what you'll find.
> 
> My suspicion, without any proof, is that it's going to come down to a
> matter of taste, or maybe some tangential features. The core will most
> likely work just fine with pretty much any module you choose to use.
> ChrisA

Hi Chris 
The doc says 
https://pypi.python.org/pypi/mysql-connector-python/1.1.5

MySQL driver written in Python which does not depend on MySQL C 
client libraries and implements the DB API v2.0 specification (PEP-249).

it is pure one and confirms to PEP. though of course i can't say for sure
any side impact.

/Asaf

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


Re: Why use _mysql module and not use MySQLdb directly?

2014-02-08 Thread Chris Angelico
On Sat, Feb 8, 2014 at 10:32 PM, Asaf Las  wrote:
> Hi Chris
> The doc says
> https://pypi.python.org/pypi/mysql-connector-python/1.1.5
>
> MySQL driver written in Python which does not depend on MySQL C
> client libraries and implements the DB API v2.0 specification (PEP-249).

Ah. And that links to dev.mysql.com, so it's presumably the same
thing... would be nice if they'd say that on their own site. That's
what I was looking for, anyhow. Confirms the suspicion.

There may well be performance differences between pure-Python
implementations and ones that go via C, but having used a
pure-high-level-language implementation of PostgreSQL's wire protocol
(granted, that was Pike, which is a somewhat higher performance
language than Python, but same difference), I can assure you of what
ought to be obvious anyway: that performance is dominated by the
server's throughput and thus (usually) by disk speed. So it's going to
be pretty much the same with all of them; look for ancillary features
that might make your life easier, otherwise pick whichever you like.

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


Re: Why use _mysql module and not use MySQLdb directly?

2014-02-08 Thread Asaf Las
On Saturday, February 8, 2014 1:42:30 PM UTC+2, Chris Angelico wrote:
> On Sat, Feb 8, 2014 at 10:32 PM, Asaf Las  wrote:
> 
> > Hi Chris
> > The doc says
> > https://pypi.python.org/pypi/mysql-connector-python/1.1.5
> > MySQL driver written in Python which does not depend on MySQL C
> > client libraries and implements the DB API v2.0 specification (PEP-249).
> 
> Ah. And that links to dev.mysql.com, so it's presumably the same
> thing... would be nice if they'd say that on their own site. That's
> what I was looking for, anyhow. Confirms the suspicion.
> 
> There may well be performance differences between pure-Python
> implementations and ones that go via C, but having used a
> pure-high-level-language implementation of PostgreSQL's wire protocol
> (granted, that was Pike, which is a somewhat higher performance
> language than Python, but same difference), I can assure you of what
> ought to be obvious anyway: that performance is dominated by the
> server's throughput and thus (usually) by disk speed. So it's going to
> be pretty much the same with all of them; look for ancillary features
> that might make your life easier, otherwise pick whichever you like.
> 
> ChrisA

Hmmm, they say :
http://dev.mysql.com/doc/connector-python/en/connector-python-introduction.html

"MySQL Connector/Python enables Python programs to access MySQL databases, 
using an API that is compliant with the Python DB API version 2.0. It 
is written in pure Python and does not have any dependencies except for 
the Python Standard Library..."

i guess with Oracle connector there could be one advantage - they will 
try to be most compliant to their product in every aspect as they would 
raiser promote their DB instead of discouraging from it.

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


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread Roy Smith
In article <3157d511-48d1-4e4d-be4c-2c461fc17...@googlegroups.com>,
 Rustom Mody  wrote:

> On Saturday, February 8, 2014 1:11:53 PM UTC+5:30, cstru...@gmail.com wrote:
> > I am writing a couple of class methods to build up several lines of html.  
> > Some of the lines are conditional and most need variables inserted in them. 
> >  Searching the web has given me a few ideas.  Each has its pro's and cons.
> 
> For creating html the method of choice is a template engine -- cheetah, mako 
> and a dozen others

Absolutely agree.  No need to reinvent a wheel which has already been 
invented in so many shapes, sizes, and colors.

We use http://jinja.pocoo.org/, but like Rustom said, there are many to 
pick from.  Any of them is likely to be a better solution than what you 
would roll yourself.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread Rustom Mody
On Saturday, February 8, 2014 4:58:03 PM UTC+5:30, Asaf Las wrote:
> Check this approach if it suits you:

> str_t= '' \
> ''.format('bella', 'donna')

> print(str_t)


Many people prefer this

>>> str_t= ( '' 
... ''
... )

>>> str_t
''

Which is to say use the fact that adjacent string constants get automatically
concatenated. You avoid the ugly \ at EOL though you then need an enclosing 
paren.

However this is still C programmer style
Triple quotes are better
And templating engine is still better

And for more heavy duty use of format, it may be better to use
named-formats + a dict

>>> dc={'pth':'bella', 'file':'donna'}
>>> formatstr='>> type="text/javascript>> type="text/javascriptsrc="/%(file)s/jquery/jquery.js">'

>>> formatstr % dc
''
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding size of Variable

2014-02-08 Thread Mark Lawrence

On 08/02/2014 02:48, Steven D'Aprano wrote:

On Thu, 06 Feb 2014 05:51:54 -0800, wxjmfauth wrote:


Sorry, I'm only pointing you may lose memory when working with short
strings as it was explained. I really, very really, do not see what is
absurd or obsure in:


sys.getsizeof('abc' + 'EURO')

46

sys.getsizeof(('abc' + 'EURO').encode('utf-32'))

37



Why do you care about NINE bytes? The least amount of memory in any PC
that I know about is 5 bytes, more than fifty million times more.
And you are whinging about wasting nine bytes?

If you care about that lousy nine bytes, Python is not the language for
you. Go and program in C, where you can spent ten or twenty times longer
programming, but save nine bytes in every string.

Nobody cares about your memory "benchmark" except you. Python is not
designed to save memory, Python is designed to use as much memory as
needed to give the programmer an easier job. In C, I can store a single
integer in a single byte. In Python, horror upon horrors, it takes 14
bytes!!!

py> sys.getsizeof(1)
14

We consider it A GOOD THING that Python spends memory for programmer
convenience and safety. Python looks for memory optimizations when it can
save large amounts of memory, not utterly trivial amounts. So in a Python
wide build, a ten-thousand block character string requires a little bit
more than 40KB. In Python 3.3, that can be reduced to only 10KB for a
purely Latin-1 string, or 20K for a string without any astral characters.
That's the sort of memory savings that are worthwhile, reducing memory
usage by 75%.

Could Python save memory by using UTF-8? Yes. But it would cost
complexity and time, strings would be even slower than they are now. That
is not a trade-off that the core developers have chosen to make, and I
agree with them.





This is a C +1 to save memory when compared against this Python +1 :)

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread Mark Lawrence

On 08/02/2014 10:11, cstrutto...@gmail.com wrote:

On Saturday, February 8, 2014 3:13:54 AM UTC-5, Asaf Las wrote:



note, due to strings are immutable - for every line in sum operation

above you produce new object and throw out older one. you can write

one string spanned at multiple lines in very clear form.

/Asaf


I think I going to rewrite this to build up a list of strings and then run a 
join on them at the end.  Each section can be conditionally built up with 
variable insertions as required.  This should be more efficient and will scale 
nicely as required.



An alternative is to use io.Stringio which is available in Python 2.7 
and 3.x.


Also would you please read and action this 
https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing the 
double line spacing above, thanks.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


What does """ means in python?

2014-02-08 Thread Sam
For string, one uses "" to represent string. Below is a code fragment that uses 
""" instead.

cursor.execute("""SELECT name, phone_number 
  FROM coworkers 
  WHERE name=%s 
  AND clue > %s 
  LIMIT 5""",
   (name, clue_threshold))

What does """ means in python?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What does """ means in python?

2014-02-08 Thread Roy Smith
In article <72a7dd52-7619-4520-991e-20db7ce55...@googlegroups.com>,
 Sam  wrote:

> For string, one uses "" to represent string. Below is a code fragment that 
> uses """ instead.
> 
> cursor.execute("""SELECT name, phone_number 
>   FROM coworkers 
>   WHERE name=%s 
>   AND clue > %s 
>   LIMIT 5""",
>(name, clue_threshold))
> 
> What does """ means in python?

This is what's known as a "triple quoted string"  It's just like a 
regular string, except that it run across newlines.  Very handy for 
things like embedding SQL code in a Python program!

It works with single quotes too (i.e. '''this is
a very long string
spread out over several lines'''
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sorting dictionary by datetime value

2014-02-08 Thread Tim Chase
On 2014-02-08 19:29, Chris Angelico wrote:
> On Sat, Feb 8, 2014 at 7:25 PM, Igor Korot 
> wrote:
> >> Try this:
> >>
> >> sorted_items = sorted(my_dict.keys(), key=my_dict.get)
> >
> > This code fail.
> 
> Actually, it's a list of keys - notice that I changed
> my_dict.items() into my_dict.keys()?

Unless you need to make a copy (for purposes of altering the original
while iterating), that's the default behavior of iterating over a
dict, so it could be simplified to

  sorted_items = sorted(my_dict, key=my_dict.get)

-tkc


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


.pyc file import errors

2014-02-08 Thread dejmail
hi everyone

I have a Flask app (in virtualenv, installed with --no-site-packages) running 
on Apache 2.4 with mod_wsgi, python2.7.

When I leave it up to Apache to activate the Virtualenv, and I perform the URL 
request, I start getting alot of errors in the logs. It seems to want to create 
.pyc files in the /usr/lib/python2.7 directory, but can't for some reason 
(probably permissions). I don't know why it would want to do this. The program 
still returns "Hello World" as it should though. Peculiarly, this doesn't occur 
on every repeat of the same URL request.

Here are some examples of the output in http://bpaste.net/show/176199/

As the output on bpaste shows, for the packages or files it tries to import 
from the virtualenv directories, I don't see these errors. It only has a 
problem with those it tries to access in the system python directory.

Is this normal ? I would think Apache has activated the virtualenv just fine if 
it is returning the output as it should, but I don't understand the presence of 
all the log errors. Perhaps they are just warnings I can ignore. I have unset 
the default $PYTHON_PATH, and a Django app on the same system does not have 
these problems.

Any ideas, because I'm out of them.

many thanks

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


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread Dave Angel
 cstrutto...@gmail.com Wrote in message:

> 
> I didn't realize I could use formatting with triple quoted strings.  I will 
> look into that.
> 


You probably realize this,  but formatting does not work on
 literals of any kind. It works on str objects,  which can be
 created by any kind of literal, or by concatenation,  or by
 conversion, or by I/O,  or ...

So all 16 (?) variants of string literals may be used.
-- 
DaveA

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


Re: Using virtualenv to bypass sudoer issues

2014-02-08 Thread Jussi Piitulainen
Glenn Hutchings writes:

> On 06/02/14 17:32, Jean-Michel Pichavant wrote:
> 
>  > Assuming I have a debian workstation for which I don't have any
>  > sudo rights, in order to be able to install / remove python
>  > packages, should I be using virtualenv ? Is it a suited solution
>  > ?
> 
> It depends on whether you need to share the installation with anyone
> else.  If not, you could also install packages using:
> 
>  python setup.py install --user
> 
> This will install in your home directory, in the '.local'
> subdirectory. And to run any scripts that get installed, add
> ~/.local/bin to your PATH.

I've used this recently. There's a catch: setup.py requires
setuptools, which also is not in the standard library and cannot be
installed this way unless it has already been installed. There's a
solution: there's a special ez_setup.py for installing setuptools.

With --user, both `python setup.py install' and `python ez_setup.py'
install in site.USER_BASE, which is ~/.local for me; site is in
standard library; the python interpreter determines the version of
python for which the installation is done, so I actually ran these:

$ python3 ez_setup.py --user
$ cd openpyxl-1.8.1
$ python3 setup.py install --user

These installed "eggs" in ~/.local/lib/python3.2/site-packages/, and
in ~/.local/bin a couple of scripts called easy_install, which I
consider poor names to have on my PATH, assuming they are specific to
Python (so I don't ~/.local/bin on my PATH).

Try to import setuptools to see if you have setuptools already. (On
one system, my 2.7 had them, but 3 didn't.)

The nice thing about --user is that the python3 interpreter knows to
add eggs from this location to its sys.path without any further
hassle. There are other options (--home, --prefix) for greater
control.

I chased the links from 
and  to learn all this and find the tools.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the most pythonic way to build up large strings?

2014-02-08 Thread Eric S. Johansson


On 2/8/2014 3:35 AM, Rustom Mody wrote:

On Saturday, February 8, 2014 1:11:53 PM UTC+5:30, cstru...@gmail.com wrote:

I am writing a couple of class methods to build up several lines of html.  Some 
of the lines are conditional and most need variables inserted in them.  
Searching the web has given me a few ideas.  Each has its pro's and cons.

For creating html the method of choice is a template engine -- cheetah, mako
and a dozen others

You can of course roll your own (poor mans version) template engine
For that look up
1. triple quoted strings
2. format operator
as so happens, I'm traveling down a similar path myself. My goal is to 
use it to generate code for disabled, speech recognition using 
programmers (like myself). I believe my best route is using a modified 
version of string.Template but I wonder if my biases keep me from seeing 
a good alternative. I believe the following characteristics are essential:


Easy to speak
output generated with the right indentation
recursive expansion
can list substitution names


Here's an example of use. The current model for visual output is a split 
screen simulated as text between two long lines of 
:


Saying: add method

--- in the GUI/editor 
-

method_name??undefined
-
def method_name():
-

Saying: add three arguments
--- in the GUI/editor 
-

method_name??undefined
argument_1??undefined
argument_2??undefined
argument_3??undefined
-
def method_name(argument_1, argument_2, argument_3):
-

Saying: fix undefined
--- in the GUI/editor 
-

method_name??^
argument_1??undefined
argument_2??undefined
argument_3??undefined
-
def method_name(argument_1, argument_2, argument_3):
-

Saying some method name
--- in the GUI/editor 
-

method_name??some method name
argument_1??undefined
argument_2??undefined
argument_3??undefined
-
def some method name(argument_1, argument_2, argument_3):
-

You repeat the process saying "fix undefined"until all of the 
substitution names were filled in and the expanded template in the lower 
window was correct. The conversion from string names such as "some 
method argument" to codenames (SmMnm) happens in a different part of the 
process.


The problem am working on now is if I change something in the list of 
substitution names (upper window) how does that affect the code 
generated and shown in the lower window? The vast majority of the time, 
deleting a substitution name resets it to undefined. But deleting 
argument is special. At the moment, I'm going with explicit alteration 
of an argument list rather than inferring the argument list from the 
arguments in the substitution name window.


I'm reasonably sure that templates, some sort, are the way to go for 
reducing vocal load. What I'm still wrestling with is the kind of 
overhead I impose on the programmer and that, in turn, defines the kind 
of templates I need for programming by speech.


Solving this problem has made me curse one feature of Python which is 
it's type system. Something, more static, would make things much easier 
because the type information could be used to predict what will be said 
next thereby reducing the number of hacks, such as templates, necessary 
to write code. However, given that one can write Python reasonably well 
using ordinary speech recognition, I can almost live with its type 
system. :-) but, if somebody knows a way to make an empirical 
determination of type, I wouldn't turn away the help

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


urllib2: correct way how to add a header to the *initial* request?

2014-02-08 Thread Matěj Cepl
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hi,

this is probably a dumb question but I just cannot find a way 
how to create AuthHandler which would add Authorization header 
to the FIRST request. The only thing I see in urllib2.py are 
various http_error handler which add Authorization header to the 
ADDITIONAL request which handles the error.

However, when looking at http://www.ietf.org/rfc/rfc2617.txt, 
section 2, I see this:

A client MAY preemptively send the corresponding 
Authorization header with requests for resources in that 
space without receipt of another challenge from the server.

And really many servers (e.g., api.github.com) expect the 
Authorization header to be sent with the initial request, and 
actually they broken 
(http://developer.github.com/v3/auth/#basic-authentication) so 
instead of 401 they send 404 status code.  

Anybody has any idea how to subclass *AuthHandler so that it 
would add the header on the initial request?

Best,

Matěj

-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.22 (GNU/Linux)

iD8DBQFS9kui4J/vJdlkhKwRAqH1AJ9884WT6pjdu/U54khpHUUVWRcFkgCdEQn9
TNtb2lcc4Nrf1zmftn5u8Zg=
=4R/2
-END PGP SIGNATURE-
-- 
https://mail.python.org/mailman/listinfo/python-list


urllib2: correct way how to add a header to the *initial* request?

2014-02-08 Thread Matěj Cepl
Hi,

this is probably a dumb question but I just cannot find a way 
how to create AuthHandler which would add Authorization header 
to the FIRST request. The only thing I see in urllib2.py are 
various http_error handler which add Authorization header to the 
ADDITIONAL request which handles the error.

However, when looking at http://www.ietf.org/rfc/rfc2617.txt, 
section 2, I see this:

   A client MAY preemptively send the corresponding 
   Authorization header with requests for resources in that 
   space without receipt of another challenge from the server.

And really many servers (e.g., api.github.com) expect the 
Authorization header to be sent with the initial request, and 
actually they broken 
(http://developer.github.com/v3/auth/#basic-authentication) so 
instead of 401 they send 404 status code.  

Anybody has any idea how to subclass *AuthHandler so that it 
would add the header on the initial request?

Best,

Matěj


pgphzgMT4PY0G.pgp
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Python3, __slots__ and serialization

2014-02-08 Thread Eric Jacoboni
Hi,

Say i want create a class with a __slots__ tuple in order to prevent
creation of new attributes from outside the class.

Say i want to serialize instances of this class... With pickle, all is
ok : i can dump an object to a file, then reload it.

With PyYAML, i can dump an object to a file, but trying to reload it fails.

If i comment out the __slots__ tuple, all is fine but, then, the class
is "open"... Is there some magic to have both a __slots__ tuple and a
working yaml.load() ?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python3, __slots__ and serialization

2014-02-08 Thread Ned Batchelder

On 2/8/14 1:06 PM, Eric Jacoboni wrote:

Hi,

Say i want create a class with a __slots__ tuple in order to prevent
creation of new attributes from outside the class.

Say i want to serialize instances of this class... With pickle, all is
ok : i can dump an object to a file, then reload it.

With PyYAML, i can dump an object to a file, but trying to reload it fails.

If i comment out the __slots__ tuple, all is fine but, then, the class
is "open"... Is there some magic to have both a __slots__ tuple and a
working yaml.load() ?



Preventing attribute creation runs counter to Python's philosophy, so it 
doesn't surprise me that it causes problems.  There might be a way to 
make it all work together, but I don't know.


__slots__ wasn't meant to be a way to prevent attribute creation, it was 
meant as a way to save space by avoiding the need for an attributes 
dictionary.


Why do you need to prevent attribute creation?  If someone uses your 
class and wants to use it in ways you didn't anticipate, why should you 
try to stop them?


--
Ned Batchelder, http://nedbatchelder.com

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


Re: Python3, __slots__ and serialization

2014-02-08 Thread Ned Batchelder

On 2/8/14 1:29 PM, Ned Batchelder wrote:

On 2/8/14 1:06 PM, Eric Jacoboni wrote:

Hi,

Say i want create a class with a __slots__ tuple in order to prevent
creation of new attributes from outside the class.

Say i want to serialize instances of this class... With pickle, all is
ok : i can dump an object to a file, then reload it.

With PyYAML, i can dump an object to a file, but trying to reload it
fails.

If i comment out the __slots__ tuple, all is fine but, then, the class
is "open"... Is there some magic to have both a __slots__ tuple and a
working yaml.load() ?



Preventing attribute creation runs counter to Python's philosophy, so it
doesn't surprise me that it causes problems.  There might be a way to
make it all work together, but I don't know.

__slots__ wasn't meant to be a way to prevent attribute creation, it was
meant as a way to save space by avoiding the need for an attributes
dictionary.

Why do you need to prevent attribute creation?  If someone uses your
class and wants to use it in ways you didn't anticipate, why should you
try to stop them?



I also looked at why it fails.  I get this error:


  Traceback (most recent call last):
File "", line 1, in 
File 
"/usr/local/virtualenvs/lab/lib/python2.7/site-packages/yaml/__init__.py", 
line 71, in load

  return loader.get_single_data()
File 
"/usr/local/virtualenvs/lab/lib/python2.7/site-packages/yaml/constructor.py", 
line 39, in get_single_data

  return self.construct_document(node)
File 
"/usr/local/virtualenvs/lab/lib/python2.7/site-packages/yaml/constructor.py", 
line 43, in construct_document

  data = self.construct_object(node)
File 
"/usr/local/virtualenvs/lab/lib/python2.7/site-packages/yaml/constructor.py", 
line 90, in construct_object

  data = constructor(self, tag_suffix, node)
File 
"/usr/local/virtualenvs/lab/lib/python2.7/site-packages/yaml/constructor.py", 
line 610, in construct_python_object_new

  return self.construct_python_object_apply(suffix, node, newobj=True)
File 
"/usr/local/virtualenvs/lab/lib/python2.7/site-packages/yaml/constructor.py", 
line 601, in construct_python_object_apply

  self.set_python_instance_state(instance, state)
File 
"/usr/local/virtualenvs/lab/lib/python2.7/site-packages/yaml/constructor.py", 
line 563, in set_python_instance_state

  setattr(object, key, value)
TypeError: can't set attributes of built-in/extension type 'object'

This is set_python_instance_state, the failing code:

def set_python_instance_state(self, instance, state):
if hasattr(instance, '__setstate__'):
instance.__setstate__(state)
else:
slotstate = {}
if isinstance(state, tuple) and len(state) == 2:
state, slotstate = state
if hasattr(instance, '__dict__'):
instance.__dict__.update(state)
elif state:
slotstate.update(state)
for key, value in slotstate.items():
setattr(object, key, value)

Two things seem clear to me:  1) you can define __setstate__ to do what 
you need to do, and 2) this code is simply wrong.  The last line should 
use "instance", not "object", and it might be that the last two lines 
should really be indented withing the elif.


All that said, skip __slots__: it isn't helping you with any actual 
problem, and it's forcing you to jump through hoops, or run code that 
most Python programs don't run.


--
Ned Batchelder, http://nedbatchelder.com
--
https://mail.python.org/mailman/listinfo/python-list


ur'foo' syntax under Python 3

2014-02-08 Thread Lele Gaifax
Hi all,

I'm using Python 3.3, and I was surprised to realize that it does not
support the old Python 2 syntax ur"literal-raw-unicode-strings".

Is there any trick to write such literals in a Python2+3 compatible
source?

Is there a rationale behind the invalid syntax or is it just a glitch?

thanks in advance,
bye, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Re: ur'foo' syntax under Python 3

2014-02-08 Thread Mark Lawrence

On 08/02/2014 19:38, Lele Gaifax wrote:

Hi all,

I'm using Python 3.3, and I was surprised to realize that it does not
support the old Python 2 syntax ur"literal-raw-unicode-strings".

Is there any trick to write such literals in a Python2+3 compatible
source?

Is there a rationale behind the invalid syntax or is it just a glitch?

thanks in advance,
bye, lele.



From 
http://docs.python.org/3.3/reference/lexical_analysis.html#string-and-bytes-literals


"Both string and bytes literals may optionally be prefixed with a letter 
'r' or 'R'; such strings are called raw strings and treat backslashes as 
literal characters. As a result, in string literals, '\U' and '\u' 
escapes in raw strings are not treated specially. Given that Python 
2.x’s raw unicode literals behave differently than Python 3.x’s the 'ur' 
syntax is not supported."


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: ur'foo' syntax under Python 3

2014-02-08 Thread Peter Otten
Lele Gaifax wrote:

> I'm using Python 3.3, and I was surprised to realize that it does not
> support the old Python 2 syntax ur"literal-raw-unicode-strings".
> 
> Is there any trick to write such literals in a Python2+3 compatible
> source?
> 
> Is there a rationale behind the invalid syntax or is it just a glitch?

This is intentional:

http://www.python.org/dev/peps/pep-0414/#exclusion-of-raw-unicode-literals

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


Re: What does """ means in python?

2014-02-08 Thread Walter Hurry
Roy Smith wrote:

> In article <72a7dd52-7619-4520-991e-20db7ce55...@googlegroups.com>,
>  Sam  wrote:
>
>> For string, one uses "" to represent string. Below is a code fragment that 
>> uses """ instead.
>> 
>> cursor.execute("""SELECT name, phone_number 
>>   FROM coworkers 
>>   WHERE name=%s 
>>   AND clue > %s 
>>   LIMIT 5""",
>>(name, clue_threshold))
>> 
>> What does """ means in python?
>
> This is what's known as a "triple quoted string"  It's just like a 
> regular string, except that it run across newlines.  Very handy for 
> things like embedding SQL code in a Python program!
>
> It works with single quotes too (i.e. '''this is
> a very long string
> spread out over several lines'''

PMFJI.

When I asked (here) about this a while ago, some kind soul suggested 
textwrap.dedent.

Any advice as to the pros and cons of the respective approaches (esp. for SQL)?

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


Re: What does """ means in python?

2014-02-08 Thread Chris Angelico
On Sun, Feb 9, 2014 at 6:55 AM, Walter Hurry  wrote:
> When I asked (here) about this a while ago, some kind soul suggested 
> textwrap.dedent.
>
> Any advice as to the pros and cons of the respective approaches (esp. for 
> SQL)?

For SQL? Ignore the extra spaces, it's a free-form language. The only
reason to consider dedent() would be if you're worried about how your
log files will look. The actual execution of SQL won't be bothered by
newlines and spaces/tabs.

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


Re: Finding size of Variable

2014-02-08 Thread David Hutto
On Sat, Feb 8, 2014 at 8:17 AM, Mark Lawrence wrote:

> On 08/02/2014 02:48, Steven D'Aprano wrote:
>
>> On Thu, 06 Feb 2014 05:51:54 -0800, wxjmfauth wrote:
>>
>>  Sorry, I'm only pointing you may lose memory when working with short
>>> strings as it was explained. I really, very really, do not see what is
>>> absurd or obsure in:
>>>
>>>  sys.getsizeof('abc' + 'EURO')
>>
> 46
>>>
 sys.getsizeof(('abc' + 'EURO').encode('utf-32'))
>>
> 37
>>>
>>
>>
>> Why do you care about NINE bytes? The least amount of memory in any PC
>> that I know about is 5 bytes, more than fifty million times more.
>> And you are whinging about wasting nine bytes?
>>
>
One could argue that if you're parsing a particular file, a very large one,
that those 9 bytes can go into the optimization of parsing aforementioned
file. Of, course we have faster processors, so why care?

Because it goes into the optimization of the code one is 'developing' in
python.


>
>> If you care about that lousy nine bytes, Python is not the language for
>> you. Go and program in C, where you can spent ten or twenty times longer
>> programming, but save nine bytes in every string.
>>
>> Nobody cares about your memory "benchmark" except you. Python is not
>> designed to save memory, Python is designed to use as much memory as
>> needed to give the programmer an easier job. In C, I can store a single
>> integer in a single byte. In Python, horror upon horrors, it takes 14
>> bytes!!!
>>
>> py> sys.getsizeof(1)
>> 14
>>
>> We consider it A GOOD THING that Python spends memory for programmer
>> convenience and safety. Python looks for memory optimizations when it can
>> save large amounts of memory, not utterly trivial amounts. So in a Python
>> wide build, a ten-thousand block character string requires a little bit
>> more than 40KB. In Python 3.3, that can be reduced to only 10KB for a
>> purely Latin-1 string, or 20K for a string without any astral characters.
>> That's the sort of memory savings that are worthwhile, reducing memory
>> usage by 75%.
>>
>> Could Python save memory by using UTF-8? Yes. But it would cost
>> complexity and time, strings would be even slower than they are now. That
>> is not a trade-off that the core developers have chosen to make, and I
>> agree with them.
>>
>>
>>
>>
> This is a C +1 to save memory when compared against this Python +1 :)
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask what
> you can do for our language.
>
> Mark Lawrence
>
> ---
> This email is free from viruses and malware because avast! Antivirus
> protection is active.
> http://www.avast.com
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Best Regards,
David Hutto
*CEO:* *http://www.hitwebdevelopment.com *
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ur'foo' syntax under Python 3

2014-02-08 Thread Lele Gaifax
Thank you Peter and Mark for the links.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
l...@metapensiero.it  | -- Fortunato Depero, 1929.

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


Using asyncio for serial port

2014-02-08 Thread james . time4tea

Hiya

I'm looking at using asyncio for creating an socket <-> serial protocol bridge, 
but looking at the current implementation of asyncio it looks to be quite 
socket specific.

I can't see any way to get it to support a simple serial device.

Any advice on where to proceed would be very much appreciated!

Thanks!

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


What are the kinds of software that are not advisable to be developed using Python?

2014-02-08 Thread Sam
I got to know about Python a few months ago and today, I want to develop only 
using Python because of its code readability. This is not a healthy bias. To 
play my own devil's advocate, I have a question. What are the kinds of software 
that are not advisable to be developed using Python?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What are the kinds of software that are not advisable to be developed using Python?

2014-02-08 Thread Denis McMahon
On Sat, 08 Feb 2014 15:54:30 -0800, Sam wrote:

> I got to know about Python a few months ago and today, I want to develop
> only using Python because of its code readability. This is not a healthy
> bias. To play my own devil's advocate, I have a question. What are the
> kinds of software that are not advisable to be developed using Python?

OS Kernels. Hardware drivers.

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What are the kinds of software that are not advisable to be developed using Python?

2014-02-08 Thread Chris Angelico
On Sun, Feb 9, 2014 at 10:54 AM, Sam  wrote:
> I got to know about Python a few months ago and today, I want to develop only 
> using Python because of its code readability. This is not a healthy bias. To 
> play my own devil's advocate, I have a question. What are the kinds of 
> software that are not advisable to be developed using Python?
>

Device drivers and operating systems. Definitely don't try writing
those in pure Python.

Anything that provides a C API is usually easier to program in C, so
you would want to write a Python-callable glue module. Sometimes
you'll find that there's nothing else to do but the module, in which
case you've pretty much written your app in C, but often you can write
a tiny driver script that calls on your functions - which means you
can, later on, write a GUI app that calls on the same functions. But
using ctypes for that is pretty messy and tedious. (Note that you
might be able to write your glue module using Cython. I've never done
this, but that's a sort of half-way mark between Python and C, with
most of the code feeling like Python but the facilities being like C.
Some day, I must try it.)

Projects that are already partly written in some other language, or
which should be written to use libraries in another language, usually
should be written in that language. Modifying your PHPBB forum should
probably be done in PHP, no matter what you think of that language
(and who doesn't).

Web applications that need to run on cheap web hosts usually need to
be written in PHP, too, but Python is available on a good few
"not-so-cheap" hosts, so that might not be a problem.

Heavy computation might be unideal in Python, but if you can grunge it
into NumPy operations, that won't be a problem.

For the rest, Python's probably a fine language. Applications,
servers, pretty much anything will work. Have at it!

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


Re: Python 2.7.6 help with modules

2014-02-08 Thread Chris Angelico
On Sun, Feb 9, 2014 at 11:28 AM, Scott W Dunning  wrote:
> So, this is more like what you’re talking about?
>
 first = number / 10
 second = number % 10
 last = first %10
 rest = second / 10
>
> I feel stupid saying this and it’s probably because of the variables I’m 
> using but I’m not sure what the above is doing.  For some reason it’s 
> confusing me.  I used the number 55664, so the variable (first) is saying 
> take 55664 and / it by 10, then (second) is saying take the original number 
> divide it by 10 and spit out the remainder?  Then from there (last) is saying 
> take the original number / it by 10 then take that number and divide it by 10 
> again and give me the remainder?  Then finally (rest) is saying take the 
> remainder of the original number after dividing by ten and divide that number 
> by 10? I guess I’m confused on the result I’m looking for.
>

Let's try them in the interactive interpreter:

>>> number = 16384
>>> number / 10
1638
>>> number % 10
4

The first division and modulo splits the last digit off the number,
and gives us the rest of the number. So here's what I'd name them:

>>> last_digit = number % 10
>>> all_but_last = number / 10

Now we can get the next digit:

>>> second_last = all_but_last % 10
>>> all_but_last_two = all_but_last / 10

And so on, through five more steps (at the end of which, the "all but"
will have nothing in it).

(Aside: notice that we're not using 'number' any more. So we can
simply put the quotient back into number, and we can make this a loop
that goes until there's nothing left:

>>> while number:
print number % 10
number = number / 10 # or: number /= 10

4
8
3
6
1


Et voila! That's how assembly language programmers display long
numbers. (Converting a single digit into an ASCII character is simply
a matter of adding 48 (hex 30), and then that can be written out.)

End of aside.)

So, going back to the units-of-time problem. We start with a total
number of seconds; note that this is a bit confusing, so let's call
that "total_seconds" and then we can use the name "seconds" to mean
just the seconds component, which will be less than 60. [1] Then we
need to get minutes, hours, and days; whatever remains will be weeks
(so if you want to count the time from the beginning of 1970 to now,
you'll have over two thousand weeks).

total_seconds = 1907043

Okay. How do we get the last digit, pretending that that's in base 60?
Because, if we treat that number as base 60, the last digit is the
number of seconds, and the rest is what we need to split up into
minutes, hours, etc.

seconds = total_seconds % 60
minutes_etcetera = total_seconds / 60

Carry on with that method - work out the number of minutes, and then
the "hours_etc" which has the rest. Then do the same to split off
hours, and then days. See how you go!

ChrisA

[1] For the purposes of this exercise, I'm pretending that this is
Unix time and has no leap seconds. Technically, when you write out
HH:MM:SS, the HH field can go from 00 to 23, the MM field can go from
00 to 59, and the SS field can go from 00 to 61 - yes, it's possible
to have *two* consecutive leap seconds, although this has never yet
happened. But for this, we're working in a system that has seconds
going from 00 to 59.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding size of Variable

2014-02-08 Thread Rustom Mody
On Sunday, February 9, 2014 4:15:50 AM UTC+5:30, David Hutto wrote:
> One could argue that if you're parsing a particular file, a very large one, 
> that those 9 bytes can go into the optimization of parsing aforementioned 
> file. Of, course we have faster processors, so why care? 
> Because it goes into the optimization of the code one is 'developing' in 
> python.

Yes... There are cases when python is an inappropriate language to use...
So???

Its good to get a bit of context here.

loop:
jmf says python is inappropriate.
Someone asks him: Is it? In what case?
jmf: No answer
After a delay of few days jmp to start of loop

[BTW: In my book this classic trolling]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with modules

2014-02-08 Thread Scott W Dunning

On Feb 8, 2014, at 5:56 PM, Chris Angelico  wrote:
> 
> Carry on with that method - work out the number of minutes, and then
> the "hours_etc" which has the rest. Then do the same to split off
> hours, and then days. See how you go!

I did it similar to that but I went backwards.  I started with number of weeks 
and went down to seconds remaining.  Would the result still be the same if the 
order of the code went the other way (i.e.. from minutes to weeks instead of 
the way I did it from weeks to seconds)? 

> [1] For the purposes of this exercise, I'm pretending that this is
> Unix time and has no leap seconds. Technically, when you write out
> HH:MM:SS, the HH field can go from 00 to 23, the MM field can go from
> 00 to 59, and the SS field can go from 00 to 61 - yes, it's possible
> to have *two* consecutive leap seconds, although this has never yet
> happened. But for this, we're working in a system that has seconds
> going from 00 to 59


I honestly do not know what leap seconds are but I’ll take your word for it.  
lol

Thanks again!!!

Scott

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


Re: Python 2.7.6 help with modules

2014-02-08 Thread Chris Angelico
On Sun, Feb 9, 2014 at 12:21 PM, Scott W Dunning  wrote:
> I figured it out!  Thanks Chris!  Taking it one step at a time with the five 
> digit number really helped me to see how to break it all up!  Are you a 
> teacher?  I appreciate the help and the patients!  I like that you don’t just 
> give me the answer that you break it down and help me so that I can figure it 
> out on my own!  Thanks to all of you for all of your help, you guys are 
> helping me a lot and I really appreciate it!
>

Awesome!

No, I'm not a teacher by profession, but I was homeschooled, and since
I'm the second of seven children [1], I got used to teaching things to
my siblings. Also, every week I run a Dungeons and Dragons campaign
online, which requires similar skills. (Teaching maths is way easier
than getting my players to figure out some puzzles. I had no idea
that, in a group of half a dozen nerds, nobody would recognize this
broken text: "In brightest day, in blackest night / No evil ..
sight / Let  worship  mi /  my power  tern" -
I'd have thought the first line alone would be enough for anyone who's
at least mildly nerdy and/or geeky.)

(We start in fifteen minutes. Want to come watch? Just telnet to
minstrelhall.com port 221!)

> Here is how I did it if you’re curious.….
>
> seconds = raw_input("Enter the number of seconds:")
> seconds = int(seconds)
> weeks = seconds/604800
> weeks_rem = seconds % 604800
> days = weeks_rem / 86400
> days_rem = seconds % 86400
> hours = days_rem / 3600
> hours_rem = seconds % 3600
> minutes = hours_rem / 60
> minutes_rem = seconds % 60
> seconds = minutes_rem % 60
>
> print weeks, 'weeks', days, 'days', hours, 'hours', minutes, 'minutes', 
> seconds, 'seconds'
>
> Not sure if that’s the correct way to do it but it works!   If there is any 
> other advice I’ll take it.

That's certainly effective. It's going to give you the right result. I
would be inclined to start from the small end and strip off the
seconds first, then the minutes, etc, because then you're working with
smaller divisors (60, 60, 24, 7 instead of 604800, 86400, 3600, 60);
most people will understand that a week is 7 days, but only people who
work with DNS will know that it's 604800 seconds. But both work.

You'll also note that you're trimming off bits and leaving "residual
seconds". I would put all the "_rem" values back into "seconds", which
would let you use augmented assignment:

weeks = seconds/604800
seconds %= 604800
days = seconds / 86400
seconds %= 86400
hours = seconds / 3600
seconds %= 3600
minutes = seconds / 60
seconds %= 60

This emphasizes that you're stripping components off and keeping the
number of remaining seconds for the next step. But that's pretty
minor. Main thing is, you know what you're doing; you have to not just
get the right answer, but know WHY you get that answer.

ChrisA

[1] Our father's not a captain, but there's still nothing about being
a non-captain with seven children. And that movie is definitely one of
our favorite things.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with modules

2014-02-08 Thread Chris Angelico
On Sun, Feb 9, 2014 at 12:46 PM, Scott W Dunning  wrote:
> On Feb 8, 2014, at 5:56 PM, Chris Angelico  wrote:
>>
>> Carry on with that method - work out the number of minutes, and then
>> the "hours_etc" which has the rest. Then do the same to split off
>> hours, and then days. See how you go!
>
> I did it similar to that but I went backwards.  I started with number of 
> weeks and went down to seconds remaining.  Would the result still be the same 
> if the order of the code went the other way (i.e.. from minutes to weeks 
> instead of the way I did it from weeks to seconds)?
>

The result will be the same. You can work either way. Working from the
smallest up lets you work in different formats - maybe you want to
suppress "0 weeks, 0 days" if it's less than a day, for instance.
Working from the largest down means you can simply write things out as
you get them, and they'll be in the right order for a human to read
(look back up to my aside where I show assembly-language number
display, and you'll see that the digits come out in the wrong order).

>> [1] For the purposes of this exercise, I'm pretending that this is
>> Unix time and has no leap seconds. Technically, when you write out
>> HH:MM:SS, the HH field can go from 00 to 23, the MM field can go from
>> 00 to 59, and the SS field can go from 00 to 61 - yes, it's possible
>> to have *two* consecutive leap seconds, although this has never yet
>> happened. But for this, we're working in a system that has seconds
>> going from 00 to 59
>
>
> I honestly do not know what leap seconds are but I’ll take your word for it.  
> lol

Heh. I mentioned them for completeness only. Look 'em up on Wikipedia
if you like; some years are a smidge longer than 365 or 366 days, by
either one or two seconds. Almost NEVER significant to every-day work,
but you'll see some time formatting libraries that mention them
(mainly because the SS field can go up to 61).

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


Re: Python 2.7.6 help with modules

2014-02-08 Thread Scott W Dunning
I figured it out!  Thanks Chris!  Taking it one step at a time with the five 
digit number really helped me to see how to break it all up!  Are you a 
teacher?  I appreciate the help and the patients!  I like that you don’t just 
give me the answer that you break it down and help me so that I can figure it 
out on my own!  Thanks to all of you for all of your help, you guys are helping 
me a lot and I really appreciate it!

Here is how I did it if you’re curious.….

seconds = raw_input("Enter the number of seconds:")
seconds = int(seconds)
weeks = seconds/604800
weeks_rem = seconds % 604800
days = weeks_rem / 86400
days_rem = seconds % 86400
hours = days_rem / 3600
hours_rem = seconds % 3600
minutes = hours_rem / 60
minutes_rem = seconds % 60
seconds = minutes_rem % 60

print weeks, 'weeks', days, 'days', hours, 'hours', minutes, 'minutes', 
seconds, 'seconds'

Not sure if that’s the correct way to do it but it works!   If there is any 
other advice I’ll take it.  




On Feb 7, 2014, at 11:29 PM, Chris Angelico  wrote:

> On Sat, Feb 8, 2014 at 5:27 PM, Scott W Dunning  wrote:
>> Ok, so it this what you’re talking about?
>> 
>> 
>> number = int(raw_input(“Enter a five digit number:))
>> foo = number % 10
>> bar = number / 10
>> 
>> digit = foo / 10
>> rem = bar % 10
>> 
> 
> Close! But if you print out foo and bar, you'll see that you're naming
> them backwards in the second one. The last digit is the remainder
> (modulo), the rest is the quotient.
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: Python 2.7.6 help with modules

2014-02-08 Thread Scott W Dunning

On Feb 7, 2014, at 11:29 PM, Chris Angelico  wrote
> Close! But if you print out foo and bar, you'll see that you're naming
> them backwards in the second one. The last digit is the remainder
> (modulo), the rest is the quotient.

So, this is more like what you’re talking about?

>>> first = number / 10
>>> second = number % 10
>>> last = first %10
>>> rest = second / 10

I feel stupid saying this and it’s probably because of the variables I’m using 
but I’m not sure what the above is doing.  For some reason it’s confusing me.  
I used the number 55664, so the variable (first) is saying take 55664 and / it 
by 10, then (second) is saying take the original number divide it by 10 and 
spit out the remainder?  Then from there (last) is saying take the original 
number / it by 10 then take that number and divide it by 10 again and give me 
the remainder?  Then finally (rest) is saying take the remainder of the 
original number after dividing by ten and divide that number by 10? I guess I’m 
confused on the result I’m looking for.

I know I’m asking very elementary questions so I really do appreciate not only 
all of your help but, your patients too!!


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


Re: Python 2.7.6 help with modules

2014-02-08 Thread Scott W Dunning

On Feb 8, 2014, at 6:46 PM, Chris Angelico  wrote:
> No, I'm not a teacher by profession, but I was homeschooled, and since
> I'm the second of seven children [1], I got used to teaching things to
> my siblings. Also, every week I run a Dungeons and Dragons campaign
> online, which requires similar skills. (Teaching maths is way easier
> than getting my players to figure out some puzzles.
Well, you’re very good at it!  Like I said I like that you don’t just give me 
the answer you help me figure it out which is what I need.  

> I had no idea
> that, in a group of half a dozen nerds, nobody would recognize this
> broken text: "In brightest day, in blackest night / No evil ..
> sight / Let  worship  mi /  my power  tern" -
> I'd have thought the first line alone would be enough for anyone who's
> at least mildly nerdy and/or geeky.)
Green Lantern oath right?  

> 
> (We start in fifteen minutes. Want to come watch? Just telnet to
> minstrelhall.com port 221!)

Awesome, I may stop by and check it out.  
> 
> That's certainly effective. It's going to give you the right result. I
> would be inclined to start from the small end and strip off the
> seconds first, then the minutes, etc, because then you're working with
> smaller divisors (60, 60, 24, 7 instead of 604800, 86400, 3600, 60);
> most people will understand that a week is 7 days, but only people who
> work with DNS will know that it's 604800 seconds. But both work.

Yeah I had to look up how many seconds were in weeks and days.

> 
> You'll also note that you're trimming off bits and leaving "residual
> seconds". I would put all the "_rem" values back into "seconds", which
> would let you use augmented assignment:

Not sure what augmented assignments are but I’ll look that up.  I’m gonna try 
and figure it out that way also just for the heck of it.  
> 
> weeks = seconds/604800
> seconds %= 604800

Is the ->  seconds %= 604800 
Saying ->  seconds = seconds % 604800?


> days = seconds / 86400
> seconds %= 86400
> hours = seconds / 3600
> seconds %= 3600
> minutes = seconds / 60
> seconds %= 60
> 
> 
> [1] Our father's not a captain, but there's still nothing about being
> a non-captain with seven children. And that movie is definitely one of
> our favorite things.

Seven children is a lot.  It definitely takes a Captain to raise seven 
children.  : )

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


Re: Python 2.7.6 help with modules

2014-02-08 Thread Chris Angelico
On Sun, Feb 9, 2014 at 1:07 PM, Scott W Dunning  wrote:
>
> On Feb 8, 2014, at 6:46 PM, Chris Angelico  wrote:
>
> I had no idea
> that, in a group of half a dozen nerds, nobody would recognize this
> broken text: "In brightest day, in blackest night / No evil ..
> sight / Let  worship  mi /  my power  tern" -
> I'd have thought the first line alone would be enough for anyone who's
> at least mildly nerdy and/or geeky.)
>
> Green Lantern oath right?

Correct! (In this particular case, they were facing a magically-locked
vault; by shining a green light on it - say, from a candle inside a
green bottle - they could see how to open it.)

> You'll also note that you're trimming off bits and leaving "residual
> seconds". I would put all the "_rem" values back into "seconds", which
> would let you use augmented assignment:
>
>
> Not sure what augmented assignments are but I’ll look that up.  I’m gonna
> try and figure it out that way also just for the heck of it.
>
>
> weeks = seconds/604800
> seconds %= 604800
>
>
> Is the ->  seconds %= 604800
> Saying ->  seconds = seconds % 604800?

Pretty much, yeah. If you think about it in English as "Divide seconds
by 604800", most people will think that "seconds" will end up smaller;
so that's represented by augmented assignment. There are also some
optimizations that can be done, and some differences as regards
mutable objects, but with simple arithmetic, it's what you say.

> [1] Our father's not a captain, but there's still nothing about being
> a non-captain with seven children. And that movie is definitely one of
> our favorite things.
>
>
> Seven children is a lot.  It definitely takes a Captain to raise seven
> children.  : )
>

Not sure if you're familiar with the book, musical, or film, "The
Sound of Music", about Captain von Trapp and Maria and their children.
Maria sings (in the film) "A captain with seven children - what's so
fearsome about that?", and somehow my fingers left out the key word
"fearsome" in the above reference, which doesn't make it very clear.
There's nothing *fearsome* about being a non-captain, etc. Also, now
that you know I'm talking about the Sound of Music, you'll know why I
described it as one of our favorite things. :)

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


Re: What are the kinds of software that are not advisable to be developed using Python?

2014-02-08 Thread Roy Smith
In article ,
 Sam  wrote:

> I got to know about Python a few months ago and today, I want to develop only 
> using Python because of its code readability. This is not a healthy bias. To 
> play my own devil's advocate, I have a question. What are the kinds of 
> software that are not advisable to be developed using Python?

If execution speed is the most important thing, Python would be the 
wrong choice.

If you need to get very close to the hardware (such as when writing an 
operating system), Python would be the wrong choice.

If you are writing software for an environment which has a 
single-language ecosystem (i.e. iOS -> Objective C, or Android -> Java), 
Python would be the wrong choice.

If, for security/business reasons, shipping only the executable, without 
the end user having access to your original source, Python would be the 
wrong language.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding size of Variable

2014-02-08 Thread David Hutto
On Sat, Feb 8, 2014 at 8:25 PM, Rustom Mody  wrote:

> On Sunday, February 9, 2014 4:15:50 AM UTC+5:30, David Hutto wrote:
> > One could argue that if you're parsing a particular file, a very large
> one, that those 9 bytes can go into the optimization of parsing
> aforementioned file. Of, course we have faster processors, so why care?
> > Because it goes into the optimization of the code one is 'developing' in
> python.
>
> Yes... There are cases when python is an inappropriate language to use...
> So???
>

I didn't say  she couldn't optimize in another language, and was just
prototyping in Python. I just said she was optimizing her python
code...dufus.



>
> Its good to get a bit of context here.
>
> loop:
> jmf says python is inappropriate.
> Someone asks him: Is it? In what case?
> jmf: No answer
> After a delay of few days jmp to start of loop
>
> loop:
mov head,up_your_ass
push  repeat
pop repeat
jmp loop

[BTW: In my book this classic trolling]
> --
>

And the title of this book would be..."Pieces of Cliche Bullshit Internet
Arguments for Dummies"

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



-- 
Best Regards,
David Hutto
*CEO:* *http://www.hitwebdevelopment.com *
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding size of Variable

2014-02-08 Thread Chris Angelico
On Sun, Feb 9, 2014 at 1:56 PM, David Hutto  wrote:
>>
>> Yes... There are cases when python is an inappropriate language to use...
>> So???
>
>
> I didn't say  she couldn't optimize in another language, and was just
> prototyping in Python. I just said she was optimizing her python
> code...dufus.

And there are a *lot* of cases where that is inappropriate language to
use. Please don't.

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


Re: Finding size of Variable

2014-02-08 Thread David Hutto
On Sat, Feb 8, 2014 at 9:59 PM, Chris Angelico  wrote:

> On Sun, Feb 9, 2014 at 1:56 PM, David Hutto 
> wrote:
> >>
> >> Yes... There are cases when python is an inappropriate language to
> use...
> >> So???
> >
> >
> > I didn't say  she couldn't optimize in another language, and was just
> > prototyping in Python. I just said she was optimizing her python
> > code...dufus.
>
> And there are a *lot* of cases where that is inappropriate language to
> use. Please don't.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>















it's also inappropriate for him to call people trolls, while they're just
commenting on why what she might be using is  a necessity for her
particular case of developing in Python, and not using another language,
yet.


"He started it!" :P

-- 
Best Regards,
David Hutto
*CEO:* *http://www.hitwebdevelopment.com *
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding size of Variable

2014-02-08 Thread Ned Batchelder

On 2/8/14 9:56 PM, David Hutto wrote:




On Sat, Feb 8, 2014 at 8:25 PM, Rustom Mody mailto:rustompm...@gmail.com>> wrote:

On Sunday, February 9, 2014 4:15:50 AM UTC+5:30, David Hutto wrote:
 > One could argue that if you're parsing a particular file, a very
large one, that those 9 bytes can go into the optimization of
parsing aforementioned file. Of, course we have faster processors,
so why care?
 > Because it goes into the optimization of the code one is
'developing' in python.

Yes... There are cases when python is an inappropriate language to
use...
So???


I didn't say  she couldn't optimize in another language, and was just
prototyping in Python. I just said she was optimizing her python
code...dufus.


Please keep the discussion respectful.  Misunderstandings are easy, I 
suspect this is one of them.  There's no reason to start calling people 
names.





Its good to get a bit of context here.

loop:
jmf says python is inappropriate.
Someone asks him: Is it? In what case?
jmf: No answer
After a delay of few days jmp to start of loop

loop:
mov head,up_your_ass
push  repeat
pop repeat
jmp loop


Please keep in mind the Code of Conduct:

http://www.python.org/psf/codeofconduct

Thanks.



[BTW: In my book this classic trolling]
--


And the title of this book would be..."Pieces of Cliche Bullshit
Internet Arguments for Dummies"

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




--
Best Regards,
David Hutto
/*CEO:*/ _http://www.hitwebdevelopment.com_





--
Ned Batchelder, http://nedbatchelder.com

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


Re: Finding size of Variable

2014-02-08 Thread David Hutto
Maybe I'll just roll my fat, bald, troll arse out from under the bridge,
and comment back, off list, next time.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding size of Variable

2014-02-08 Thread Ned Batchelder

On 2/8/14 10:09 PM, David Hutto wrote:

Maybe I'll just roll my fat, bald, troll arse out from under the bridge,
and comment back, off list, next time.




I'm not sure what happened in this thread.  It might be that you think 
Rustom Mody was referring to you when he said, "BTW: In my book this 
classic trolling."  I don't think he was, I think he was referring to JMF.


In any case, perhaps it would be best to just take a break?

--
Ned Batchelder, http://nedbatchelder.com

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


Re: Finding size of Variable

2014-02-08 Thread Rustom Mody
On Sunday, February 9, 2014 8:46:50 AM UTC+5:30, Ned Batchelder wrote:
> On 2/8/14 10:09 PM, David Hutto wrote:
> > Maybe I'll just roll my fat, bald, troll arse out from under the bridge,
> > and comment back, off list, next time.

> I'm not sure what happened in this thread.  It might be that you think 
> Rustom Mody was referring to you when he said, "BTW: In my book this 
> classic trolling."  I don't think he was, I think he was referring to JMF.

Of course!
And given the turn of this thread, we must hand it to jmf for being even better 
at trolling than I thought :-)

See the first para
http://en.wikipedia.org/wiki/Troll_%28Internet%29
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What are the kinds of software that are not advisable to be developed using Python?

2014-02-08 Thread Steven D'Aprano
On Sat, 08 Feb 2014 21:53:00 -0500, Roy Smith wrote:

> In article ,
>  Sam  wrote:
> 
>> I got to know about Python a few months ago and today, I want to
>> develop only using Python because of its code readability. This is not
>> a healthy bias. To play my own devil's advocate, I have a question.
>> What are the kinds of software that are not advisable to be developed
>> using Python?
> 
> If execution speed is the most important thing, Python would be the
> wrong choice.

PyPy can generate code which is comparable to compiled C in speed. 
Perhaps you mean, "if execution speed is the most important thing, using 
a naive Python interpreter may not be fast enough".


> If you need to get very close to the hardware (such as when writing an
> operating system), Python would be the wrong choice.
> 
> If you are writing software for an environment which has a
> single-language ecosystem (i.e. iOS -> Objective C, or Android -> Java),
> Python would be the wrong choice.

https://github.com/kivy/python-for-android
https://ep2013.europython.eu/conference/talks/developing-android-apps-completely-in-python
https://python-for-android.readthedocs.org/en/latest/
http://qpython.com/


> If, for security/business reasons, shipping only the executable, without
> the end user having access to your original source, Python would be the
> wrong language.

Security by obscurity.

Nevertheless, although security by obscurity is ineffective[1], Python 
supports it. You can ship only the .pyc files. For added obscurity, you 
could put the .pyc files in a .zip file and ship that. For even more 
obscurity, you could write a custom importer, and then ship your python 
byte-code hidden in a mp3 or TIFF file.




[1] Shipping only compiled executables hasn't made Windows any more 
secure, prevented viruses and malware from taking advantage of zero-day 
exploits, or prevented software piracy.

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


Re: What are the kinds of software that are not advisable to be developed using Python?

2014-02-08 Thread Michael Torrie
On 02/08/2014 05:11 PM, Chris Angelico wrote:
> On Sun, Feb 9, 2014 at 10:54 AM, Sam  wrote:
>> I got to know about Python a few months ago and today, I want to develop 
>> only using Python because of its code readability. This is not a healthy 
>> bias. To play my own devil's advocate, I have a question. What are the kinds 
>> of software that are not advisable to be developed using Python?
>>
> 
> Device drivers and operating systems. Definitely don't try writing
> those in pure Python.

That all depends. Driving a USB device using libusb and Python might
just be the ticket to get things up and running quickly.  At one time
someone wrote a Linux kernel module that allowed you to use Perl to
implement some kinds of driver things.

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


Re: What are the kinds of software that are not advisable to be developed using Python?

2014-02-08 Thread Asaf Las
On Sunday, February 9, 2014 5:43:47 AM UTC+2, Steven D'Aprano wrote:
> Nevertheless, although security by obscurity is ineffective[1], Python 
> supports it. You can ship only the .pyc files. For added obscurity, you 
> could put the .pyc files in a .zip file and ship that. For even more 
> obscurity, you could write a custom importer, and then ship your python 
> byte-code hidden in a mp3 or TIFF file.

There are some code obfuscator for python but i never used them. 

btw, Python could be language of choice for embedded systems if small footprint 
vm could be developed. had seen similar for java having 10-20 KB byte sized 
interpreter with very limited set of functions.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What are the kinds of software that are not advisable to be developed using Python?

2014-02-08 Thread Chris Angelico
On Sun, Feb 9, 2014 at 3:08 PM, Michael Torrie  wrote:
> On 02/08/2014 05:11 PM, Chris Angelico wrote:
>> On Sun, Feb 9, 2014 at 10:54 AM, Sam  wrote:
>>> I got to know about Python a few months ago and today, I want to develop 
>>> only using Python because of its code readability. This is not a healthy 
>>> bias. To play my own devil's advocate, I have a question. What are the 
>>> kinds of software that are not advisable to be developed using Python?
>>>
>>
>> Device drivers and operating systems. Definitely don't try writing
>> those in pure Python.
>
> That all depends. Driving a USB device using libusb and Python might
> just be the ticket to get things up and running quickly.  At one time
> someone wrote a Linux kernel module that allowed you to use Perl to
> implement some kinds of driver things.

That's not the same; libusb is doing the low-level handling for you.
That's not the sense of "device driver" that gets really close to the
metal. I'm talking about real-time response to signals, I/O port and
interrupt handling, that kind of thing. The device driver will then
expose a higher-level API, maybe as a /dev/something openable, and
Python can control the device using that.

And that's something that Python *is* good at. I wouldn't use Python
to write a device driver for an RTL8169 card, but if I have that card
in my computer, I will totally use Python to create a network
connection and transfer data. I'm just not going to concern myself
with the low-level details when I do. :)

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


Re: What are the kinds of software that are not advisable to be developed using Python?

2014-02-08 Thread Skybuck Flying

"
I got to know about Python a few months ago and today, I want to develop 
only using Python because of its code readability. This is not a healthy 
bias. To play my own devil's advocate, I have a question. What are the kinds 
of software that are not advisable to be developed using Python?

"

Anything that needs to be super reliable.

My experience so far with Python versus Delphi has shown that Python 
requires way more time to debug than Delphi.


The reason for this is the interpreter versus the compiler.

Delphi's compiler will catch many bugs in branches that have not been 
executed or tested.


While Python's interpreter will mostly catch bugs in executed and tested 
branches.


Most of the code that has not been executed yet could contain bugs and even 
syntax/typos/non declared variables and so forth.


Which means that the entire Python program will have to be re-executed from 
the start to ultimately end up in branches that were not executed the last 
time.


This way of debugging will require many many many many many many many many 
runs of the same Python program before it's somewhat debugged.


This is time lost in the debugging phase.

Having said that... time is gained in the programming phase thanks to it's 
shortened syntax.


However there is more... Python may lack some technical language elements 
like, call by reference, and perhaps other low level codes, like 8 bit, 16 
bit, 32 bit integers which play a roll with interfacing with hardware.


Types of software which would not go onto my python list: operating systems, 
drivers, perhaps also virtual machines and even compilers, python slow 
execution speed hampers that as well.


Bye,
 Skybuck. 


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


Re: Sorting dictionary by datetime value

2014-02-08 Thread Larry Hudson

On 02/07/2014 11:06 PM, Igor Korot wrote:

Hi, ALL,
I'm trying to do a very easy task: sort python dictionary by value
where value is a datetime object.

When trying to do that in Python shell everthing works as expected.

C:\Documents and Settings\Igor.FORDANWORK>python
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

a = {}
import datetime
a['1'] = datetime.datetime(2012,12,28,12,15,30,100)
a['2'] = datetime.datetime(2012,12,28,12,17,29,100)
a['3'] = datetime.datetime(2012,12,28,12,16,44,100)
sorted(a.items(), key=a.get)

[('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100)), ('3', datetime.datetim
e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.datetime(2012, 12, 28, 12, 17,
  29, 100))]




However, trying to do the same thing from the script does not sort the
dictionary:

sorted(my_dict.items(), key=my_dict.get, reverse=False)
for key, value in my_dict.items():
  print value, key

the dictionary prints with unsorted items.

I am trying to run it on Windows XP and the data are coming from the DB.

What am I missing?

Thank you.



You are missing the difference between sorted and sort.

The sorted function returns a new list, but leaves the original unchanged.
The sort method changes the list in place, and returns None.

In your first, interactive example, the returned sorted list is automatically displayed -- 
that's why you see the sorted list.  In your script, however, you are sorting the list but not 
saving the result -- it's immediately thrown away.  In effect, your "sorted(...)" line is a 
no-op.  You follow this by displaying the original, unsorted dictionary.


Secondly, your sorting key is wrong.  You are sorting a list not a dictionary.  It is a list of 
(key,value) tuples, which you want to sort on the second element of the tuple.  You can do this 
with a simple lambda expression:  key=lambda t:t[1]


Finally, change your for loop to print the data from this sorted list of tuples, not from the 
dictionary.


 -=- Larry -=-

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


Re: What are the kinds of software that are not advisable to be developed using Python?

2014-02-08 Thread Chris Angelico
On Sun, Feb 9, 2014 at 4:17 PM, Skybuck Flying
 wrote:
> Anything that needs to be super reliable.
>
> My experience so far with Python versus Delphi has shown that Python
> requires way more time to debug than Delphi.
>
> The reason for this is the interpreter versus the compiler.
>
> Delphi's compiler will catch many bugs in branches that have not been
> executed or tested.
>
> While Python's interpreter will mostly catch bugs in executed and tested
> branches.

What this means is that Python forces you to have a good test suite.
Yes, that is a cost; but it's nothing to do with compiled vs
interpreted. These days, language engines aren't strictly one or the
other, they're on a scale. Python will catch syntactic errors at
compile time, but will catch most other forms of error at run time...
except for the ones that, regardless of the language used, simply
result in wrong data. So if you're not testing your code, you can't
rely on it, and that's nothing to do with the language.

A language with typed variable declarations can catch at compile-time
a class of error that Python can't catch until run-time. Yep. But that
doesn't actually answer the question: what should Python not be used
for. If you need to trust your code, you need to test it.

> However there is more... Python may lack some technical language elements
> like, call by reference, and perhaps other low level codes, like 8 bit, 16
> bit, 32 bit integers which play a roll with interfacing with hardware.

I've never yet found a desperate need for call-by-reference. In toy
examples, the equivalent functionality can be achieved by treating a
one-element list as a pointer, and dereferencing it just like you
would in C, with [0] at the end. Never needed it in production code,
though. But again, this doesn't cut out a class of application, just a
style of coding.

As to strict-size integers, Python does have a struct module, and
ctypes, both of which are designed for interfacing with hardware or
low-level APIs. For normal work, you don't need to care about that at
all.

> Types of software which would not go onto my python list: operating systems,
> drivers, perhaps also virtual machines and even compilers, python slow
> execution speed hampers that as well.

A virtual machine where machine code is executed in Python? Well,
depends on how fast you need it to be, but yes, that's quite likely to
demand more oomph than your typical Python interpreter can give you.
Although you could probably play ancient 8088 games, actually
interpreting it all on the fly; on modern hardware, you could probably
get up to the speed of those old CPUs. Of course, if you can get most
of the work done at a lower level, then it's not that kind of VM, and
performance is completely different.

Compilers... yes, on big projects, you'd probably want to write the
compiler in C. I generally say that C is for writing operating systems
and high level languages, and those high level languages are for
writing everything else in. But look at PyPy. It's possible to write a
Python compiler in Python, which can often out-perform CPython. So
it's not so clear-cut :)

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


Vedr: What does """ means in python?

2014-02-08 Thread Gisle Vanem


 
Gisle V.


"Computers are useless. They can only give answers"  --Pablo Picasso
Chris Angelico  wrote:

> For SQL? Ignore the extra spaces, it's a free-form language. The only
> reason to consider dedent() would be if you're worried about how your
> log files will look. The actual execution of SQL won't be bothered by
> newlines and spaces/tabs.

Regrading handy uses of ''', you learned me one trick when using Python 
code in a Windows .bat file:


  rem = '''
  @echo off
  echo This is 
batch
  \python32\python %0
  echo All done
  exit /b
  rem '''
  import 
sys
  print("This is Python")
  for i,p in 
enumerate(sys.path):
     print('sys.path[%2d]: %s' % (i, p))
  print("Python 
done")

You'll have a variable in Python called 'rem' which contains all 
your
batch code :) It exploits the fact that 'rem' makes a 
one-line
comment, but the triple quotes go across multiple lines.

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


Re: Python 2.7.6 help with modules

2014-02-08 Thread Scott W Dunning

On Feb 8, 2014, at 6:46 PM, Chris Angelico  wrote:
> That's certainly effective. It's going to give you the right result. I
> would be inclined to start from the small end and strip off the
> seconds first, then the minutes, etc, because then you're working with
> smaller divisors (60, 60, 24, 7 instead of 604800, 86400, 3600, 60);
> most people will understand that a week is 7 days, but only people who
> work with DNS will know that it's 604800 seconds. But both work.

How would working from the small end (seconds) first and going up to weeks 
allow me to work with the smaller divisors?  Wouldn’t it still have to be in 
seconds?  -- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python 2.7.6 help with modules

2014-02-08 Thread Chris Angelico
On Sun, Feb 9, 2014 at 5:00 PM, Scott W Dunning  wrote:
> On Feb 8, 2014, at 6:46 PM, Chris Angelico  wrote:
>
> That's certainly effective. It's going to give you the right result. I
> would be inclined to start from the small end and strip off the
> seconds first, then the minutes, etc, because then you're working with
> smaller divisors (60, 60, 24, 7 instead of 604800, 86400, 3600, 60);
> most people will understand that a week is 7 days, but only people who
> work with DNS will know that it's 604800 seconds. But both work.
>
>
> How would working from the small end (seconds) first and going up to weeks
> allow me to work with the smaller divisors?  Wouldn’t it still have to be in
> seconds?

Here's how to do it from the small end up:

time = int(raw_input("Enter number of seconds: "))
seconds = time % 60
time /= 60
minutes = time % 60
time /= 60
hours = time % 24
time /= 24
days = time % 7
time /= 7
weeks = time
# Alternative way to format for display:
print("%d weeks, %d days, %02d:%02d:%02d"%(weeks,days,hours,minutes,seconds))

Enter number of seconds: 123456789
204 weeks, 0 days, 21:33:09

Produces the same result, has the same amount of division in it, but
the time keeps shifting down: after the first pair, it's a number of
minutes, after the second pair it's a number of hours, and so on.

By the way, here's the divmod version of the above code:

time = int(raw_input("Enter number of seconds: "))
time, seconds = divmod(time, 60)
time, minutes = divmod(time, 60)
time, hours = divmod(time, 24)
weeks, days = divmod(time, 7)
print("%d weeks, %d days, %02d:%02d:%02d"%(weeks,days,hours,minutes,seconds))

It's now fairly clear what's going on. Each time unit is on the same
line as the number of that time unit that make up the next one.

The same thing, going down, looks like this:

seconds = int(raw_input("Enter number of seconds: "))
weeks, seconds = divmod(seconds, 604800)
days, seconds = divmod(seconds, 86400)
hours, seconds = divmod(seconds, 3600)
minutes, seconds = divmod(seconds, 60)
print("%d weeks, %d days, %02d:%02d:%02d"%(weeks,days,hours,minutes,seconds))

Also fairly clear; each time unit is on the same line as the number of
seconds that make up that unit. Both methods make sense, in their own
way.

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


Re: python and matlab

2014-02-08 Thread pavlovevidence
On Thursday, February 6, 2014 5:30:54 AM UTC-8, Sam Adams wrote:
> is it able to utilize functions written in Python in Matlab?

If it's on Windows, and if it's pure-Python 2.x code, the easiest solution 
would be to use Iron Python or Jython.  Matlab can call Java and .NET code 
natively.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Sorting dictionary by datetime value

2014-02-08 Thread Frank Millman

"Peter Otten" <__pete...@web.de> wrote in message 
news:ld4pon$ni9$1...@ger.gmane.org...
> Frank Millman wrote:
>
> Here you can watch the key calculation at work:
>
 d = {'1': 'abc', '2': 'xyz', '3': 'pqr'}
 def sortkey(value):
> ... key = d.get(value)
> ... print "value:", value, "sort-key:", key
> ... return key
> ...
 sorted(d.items(), key=sortkey)
> value: ('1', 'abc') sort-key: None
> value: ('3', 'pqr') sort-key: None
> value: ('2', 'xyz') sort-key: None
> [('1', 'abc'), ('3', 'pqr'), ('2', 'xyz')]
>

Thanks for that, Peter. That is a clever debugging technique I must use more 
often!

Not only do I now understand it, but I realise that Igor's original example 
worked by sheer coincidence -

$python -V
Python 2.7.3
$python -c "print({'1': None, '2': None, '3': None})"
{'1': None, '3': None, '2': None}
$python -c "print({'1': None, '2': None, '3': None})"
{'1': None, '3': None, '2': None}
$python -c "print({'1': None, '2': None, '3': None})"
{'1': None, '3': None, '2': None}

Keys of '1', '2', '3' are always stored in the sequence '1', 3', '2'.

$python3 -V
Python 3.3.2
$python3 -c "print({'1': None, '2': None, '3': None})"
{'2': None, '3': None, '1': None}
$python3 -c "print({'1': None, '2': None, '3': None})"
{'3': None, '2': None, '1': None}
$python3 -c "print({'1': None, '2': None, '3': None})"
{'1': None, '2': None, '3': None}

Due to the new 'hash randomisation' feature, the order is different every 
time.

In both versions, integer keys of 1, 2, 3 are always stored in the sequence 
1, 2, 3.

Frank



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


Re: Python 2.7.6 help with modules

2014-02-08 Thread Larry Hudson

On 02/08/2014 05:21 PM, Scott W Dunning wrote:

I figured it out!  Thanks Chris!  Taking it one step at a time with the five 
digit number really helped me to see how to break it all up!  Are you a 
teacher?  I appreciate the help and the patients!  I like that you don’t just 
give me the answer that you break it down and help me so that I can figure it 
out on my own!  Thanks to all of you for all of your help, you guys are helping 
me a lot and I really appreciate it!

Here is how I did it if you’re curious.….

seconds = raw_input("Enter the number of seconds:")
seconds = int(seconds)

...


One very trivial nit...  Add one or two spaces at the end of your prompt string.  Simply as a 
matter of aesthetics--it looks nicer if your input doesn't butt up solidly against the prompt.

(Personally, I like two spaces, but YMMV...);-)

Of course, this is just for the appearance, it makes absolutely no difference 
in your results.

 -=- Larry -=-

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


Dictionaries

2014-02-08 Thread worthingtonclinton
why in a for loop can i access values for a dict that i did not address in the 
for loop. 

example:

a = {blah:blah}
b = {blah:blah}

for x in a:

print a[x]

#here's what i don't understand

print b[x]

# it would print the value for dict b even though it wasn't called upon in 
the for loop!


Thanks in advance.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Dictionaries

2014-02-08 Thread Gary Herron

On 02/08/2014 11:07 PM, worthingtonclin...@gmail.com wrote:

why in a for loop can i access values for a dict that i did not address in the 
for loop.

example:

a = {blah:blah}
b = {blah:blah}

for x in a:
 
 print a[x]
 
 #here's what i don't understand
 
 print b[x]
 
 # it would print the value for dict b even though it wasn't called upon in the for loop!



Thanks in advance.


The lookup of a value in dictionary b does not care where the index 
comes from.Quite simply, x has the value of 'blah', and b has 'blah' 
as a key, so b[x] works.


If a and b had different keys, then you would get an error:

>>> a = {'a':1}
>>> b = {'b':2}
>>> for x in a:
...   print x
...   print a[x]
...   print b[x]
...
a
1
Traceback (most recent call last):
  File "", line 4, in 
KeyError: 'a'


Gary Herron

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


Re: Dictionaries

2014-02-08 Thread Frank Millman

 wrote in message 
news:891d3696-4e4e-44cc-a491-6b8fef47f...@googlegroups.com...
> why in a for loop can i access values for a dict that i did not address in 
> the for loop.
>
> example:
>
> a = {blah:blah}
> b = {blah:blah}
>
> for x in a:
>
>print a[x]
>
>#here's what i don't understand
>
>print b[x]
>
># it would print the value for dict b even though it wasn't called upon 
> in the for loop!
>

Iterating over a dictionary returns the keys of the dictionary.

When you say 'for x in a', each iteration sets 'x' to the next key in 
dictionary 'a'.

'x' is a reference to a normal python object. It does not 'know' that it 
came from dictionary 'a', so you can do whatever you like with it. If you 
use it to retrieve a value in dictionary 'b', and the key happens to exist, 
it will return the value. Otherwise it will raise KeyError.

HTH

Frank Millman



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


Re: Dictionaries

2014-02-08 Thread worthingtonclinton
greatly appreciated guys. thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list