Re: [Tutor] Alternatives to append() for "growing" a list

2013-11-30 Thread Steven D'Aprano
On Sun, Dec 01, 2013 at 02:32:38PM +1000, Amit Saha wrote:
> Hello,
> 
> I was told by someone (as a comment) that a code snippet such as this
> "would make Pythonistas talk my ear off about how evil the append()"
> function is:

There is absolutely nothing wrong with append. Either you have 
misunderstood, or the person who told you this was smoking crack.

> >>> mylist = []
> >>> mylist.append(1)
> # a number of times over

However, growing a list one item at a time using append like this is too 
much hard work. Some better solutions:

# You have an existing list, and want to append a bunch of things to it
mylist.extend([x, y+1, z*2])

# You want a list consisting of the same objects repeated many times
mylist = [0, 1, 2]*100  # like [0, 1, 2, 0, 1, 2, 0, 1, 2, ...]

# You want to create a list containing known objects
mylist = [1, 2, 4, 8, 16, 32, 64]

# Like above, but using a list comprehension
mylist = [2**n for n in range(7)]

# You don't know how many things you want to add.
mylist = []
x = 1
while x < 100:
mylist.append(x)
x = 3*x-1

and so on. As you can see, append has its job to do.


> I have some ideas that on an append() the list's internal size
> increases (doubled?) since CPython over-allocates memory, and such.

Not just on append. Any operation that adds or deletes items from a list 
might trigger a re-size. If the list needs to increase, it will double 
in size up to some maximum, then it will grow by a smaller amount. The 
purpose of this is that *on average* appending to the list will take a 
fixed amount of time.


> So, the question is: what is the alternative to growing a list when I
> have no idea what items or how many may be there?

Don't worry about growing the list. That's all handled for you as part 
of the list interface. The whole point of using Python is to not have to 
worry about internal details like that.



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Alternatives to append() for "growing" a list

2013-11-30 Thread Dave Angel
On Sun, 1 Dec 2013 14:32:38 +1000, Amit Saha  
wrote:
I was told by someone (as a comment) that a code snippet such as 

this

"would make Pythonistas talk my ear off about how evil the append()"
function is:



>>> mylist = []
>>> mylist.append(1)


Nothing evil about append.  Many times the list can be built more 
prettily with a list comprehension,  but if you're building it in a 
for loop, append does the job.


--
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Alternatives to append() for "growing" a list

2013-11-30 Thread Amit Saha
Hello,

I was told by someone (as a comment) that a code snippet such as this
"would make Pythonistas talk my ear off about how evil the append()"
function is:

>>> mylist = []
>>> mylist.append(1)
# a number of times over

I have some ideas that on an append() the list's internal size
increases (doubled?) since CPython over-allocates memory, and such.

So, the question is: what is the alternative to growing a list when I
have no idea what items or how many may be there?


Thanks,
Amit.

-- 
http://echorand.me
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Output not legible when debugging with ipdb

2013-11-30 Thread Peter Zorn
PyReadline is installed with the Anaconda distribution, and Powershell 
displays colored IPython output, but not when I use ipdb for whatever 
reason.


I figured out that

from IPython.core.debugger import Tracer; debug_here = Tracer()
debug_here()

seems to yield the desired result: a debugger with colored code, tab 
completion, etc


(see 
http://ipython.org/ipython-doc/stable/api/generated/IPython.core.debugger.html?highlight=debugger#IPython.core.debugger)


Thanks,
Peter

On 27.11.2013 00:24, eryksun wrote:

On Tue, Nov 26, 2013 at 4:28 PM, Walter Prins  wrote:

honest.  Regarding Powershell (vs for example cmd.exe): The (slightly)
perplexing/irritating/annoying thing is that the older cmd.exe shell, which
uses a standard old-school NT console window, does support ANSI escape
sequences (but is otherwise pretty braindead IMHO, for example does not
support arbitrary resizing, copy and paste is clunky and so on), while the
text mode/console window hosting Powershell behaves somewhat differently,
and is (IIRC) at least resizable, but apparently doesn't support escape
sequences, so there appears to be some differences -- though I realize these
must be due to features in the shells themselves since they share some
common console window functionality.

The cmd shell's built-in "type" and "echo" commands don't parse escape
sequences. I think "color" is the only command that sets character
attributes, and only for the entire screen buffer (e.g. "color 0D"
sets light purple text on a black background).

Adding escape-sequence support to existing programs such as cmd.exe
has to be done in the console itself or by hacking the console API.
ConEmu manages a hidden console window and does all of its own
display. There's also ANSICON, which injects a DLL (ansi32.dll or
ansi64.dll) into the process. My guess is that the DLL hooks kernel32
WriteConsole to look for escape sequences and then scripts the console
API. However it works, it's very simple to use:

ANSICON
https://github.com/adoxa/ansicon

Console API
http://msdn.microsoft.com/en-us/library/ms682073

I agree the Windows console is braindead. It was fine for its day in
NT 3 and 4 back in the 90s, but it hasn't improved much in 20 years,
and I doubt it ever will. Maybe with PowerShell you're thinking of the
Integrated Scripting Environment:

http://technet.microsoft.com/en-us/library/dd819514

For IPython, try the Qt console:

http://ipython.org/ipython-doc/stable/interactive/qtconsole.html


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] empty delimiters, and None

2013-11-30 Thread ugajin
Thanks for the helpful comments Eryksun/Steve

Mis-configured environment (see below)?
I rather felt that setting the 2nd parameter to None, was side stepping an 
underlying issue.
However I experimented further and find; locale.setlocale(locale.LC_ALL, 
'en_GB') also works OK. Perhaps this is a better fix than 
locale.setlocale(locale.LC_ALL, None) but again it may be side stepping the 
underlying issue.

Here is link: https://answers.launchpad.net/inkscape/+question/239599. I think 
you need to register to access the both the question and Bug #803791 report. 
You may not want to do this.

I believe the system locale is set correctly:

Apples-iMac-4:~ apple$ locale
LANG="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_CTYPE="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_ALL=

The underlying problem appears to be with the application launcher script (see 
below).

To explain 'booted from Unix file' is difficult for me, especially if you are 
unfamiliar with OSX, It is a way of running the application together with a 
Terminal window.

Inkscape is an Open Source SVG editor with some 'proprietary' features that are 
not part of the SVG standard.
The Measure Path script, can calculate either the length of, or the area 
enclosed by a path. 
The value is displayed as text, which is added and anchored to the path that is 
measured. 
Variables include precision (no. of significant places) and unit type (px, mm & 
etc.), as well as offset & font size (for text output).

I did try contacting one of its authors, and I have raised the matter as a 
question, (see link above). The initial response was; "A workaround is to edit 
the launcher script (to force a valid UTF-8 language settings)."  The fix 
provided (shown below) caused Inkscape to crash immediately after the launch 
routine completed:

Quit all running instances of Inkscape. Then
1) Select Inkscape (the 
application) in the Finder
2) Choose 'Show Package Contents' from the context 
menu
3) Browse to 'Contents > Resources > bin'
4) Open the file 'inkscape' in a 
plain-text editor (e.g. TextWrangler)
5) Scroll down to line 127
6) Insert a 
new line before line 127 with this content:
export LANG="en_US.UTF-8"
7) Save & close file (make sure that your text editor does _not_ add a
hidden 
suffix to the file, else the application won't launch anymore)
8) Relaunch 
Inkscape
If you prefer a different UI language, adjust the string "en_US.UTF-8"
as 
needed.

Lines 123 to 127 of the launcher script read:

# NOTE: Have to add ".UTF-8" to the LANG since omitting causes Inkscape
#   to crash on startup in locale_from_utf8().
export LANG="`grep \"\`echo $LANGSTR\`_\" /usr/share/locale/locale.alias | \
tail -n1 | sed 's/\./ /' | awk '{print $2}'`.UTF-8"
echo "Setting Language: $LANG" 1>&2

I am told this 'fix' works for OSX v10.5.8 (Leopard) and OSX 10.7.5 (Lion).  
However, I run OSX v10.6.8 (Snow Leopard).

I am also interested in identifying the underlying issue, although I am far out 
of my comfort zone, and which seems to be with the launcher script. From the 
various postings and comments read, this seems to be something of an old 
chestnut. Maybe you can't suggest an alternative fix to the above, but maybe 
you can comment to help explain lines 123 - 127.

Many many thanks, once more.

-A

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] empty delimiters, and None

2013-11-30 Thread spir

On 11/29/2013 02:19 PM, uga...@talktalk.net wrote:

I have also looked at locale.py Line 494 of which is the last line of a def 
(def function?) I include this below, hopefully this may save you searching for 
locale.py (Pyhon 2.6) should you need it and wish to answer the above 
questions, it may help.

def setlocale(category, locale=None):

 """ Set the locale for the given category.  The locale can be
 a string, a locale tuple (language code, encoding), or None.

 Locale tuples are converted to strings the locale aliasing
 engine.  Locale strings are passed directly to the C lib.

 category may be given as one of the LC_* values.

 """
 if locale and type(locale) is not type(""):
 # convert to string
 locale = normalize(_build_localename(locale))
 return _setlocale(category, locale)


As a side-note, in addition to what other have said, the headline of the 
function def


def setlocale(category, locale=None)

says that None is the default (standard) value for the parameter 'locale'. This 
means that, if ever you provide no value for it when calling setlocale, then the 
value None is used in standard. So, you could as well call it like:


locale.setlocale(locale.LC_ALL) # no value at all for param 'locale'

instead of you correction

locale.setlocale(locale.LC_ALL, None)

for the initial version

locale.setlocale(locale.LC_ALL, '')

This is actually good coding practice (in all language which have default 
values).

Denis
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] empty delimiters, and None

2013-11-30 Thread eryksun
On Sat, Nov 30, 2013 at 7:04 AM,   wrote:
> believe the system locale is set correctly:
>
> Apples-iMac-4:~ apple$ locale
> LANG="en_GB.UTF-8"

Does `locale -a` report that en_US is available?

> 127
6) Insert a new line before line 127 with this content:
> export LANG="en_US.UTF-8"

Did you try en_GB.UTF-8 here?

> Lines 123 to 127 of the launcher script read:
>
> # NOTE: Have to add ".UTF-8" to the LANG since omitting causes Inkscape
> #   to crash on startup in locale_from_utf8().
> export LANG="`grep \"\`echo $LANGSTR\`_\" /usr/share/locale/locale.alias | \
> tail -n1 | sed 's/\./ /' | awk '{print $2}'`.UTF-8"
> echo "Setting Language: $LANG" 1>&2

The current version uses the value of AppleCollationOrder or
AppleLocale (e.g. defaults read .GlobalPreferences AppleLocale) to
find the locale in the locale.alias file, and defaults to
"en_US.UTF-8". I don't know if "en_US" is always available in OS X,
but surely "C.UTF-8" would be. I don't know why it can't modify the
existing LANG to use the UTF-8 codeset. Finally, I can't speak for OS
X, but the glibc locale.alias on Linux is obsolete and doesn't have an
alias for English.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] strip and split?

2013-11-30 Thread eryksun
On Sat, Nov 30, 2013 at 1:40 PM, richard kappler  wrote:
>
>
> disk usage(total=302264549376, used=73844322304, free=213066088448,
> percent=24.4)
>
> So if I want only the number following percent, I get that I need to convert
> this to a built in type and do some split and strip, but that's where I'm
> floundering. Might I get a little guidance on this please?

disk_usage returns a subclass of tuple, created by
collections.namedtuple. The subclass adds a property for each tuple
item. This helps code to be more self-documenting without sacrificing
the efficiency of tuples.

usage = collections.namedtuple(
'usage', 'total used free percent')

>>> usage.__base__


>>> type(usage.percent)


The property fget for each tuple item is an operator.itemgetter instance:

>>> type(usage.percent.fget)


For example:

u = usage(302264549376, 73844322304, 213066088448, 24.4)
fget = operator.itemgetter(3)

>>> u.percent
24.4
>>> fget(u)
24.4
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] strip and split?

2013-11-30 Thread Don Jennings

On Nov 30, 2013, at 4:32 PM, Wolfgang Maier wrote:

> richard kappler  gmail.com> writes:
> 
>> 
>> I'm using psutil to generate some somatic data with the following script:
>> 
>> import psutil as ps
>> 
>> cpu = ps.cpu_percent()
>> mem = ps.virtual_memory()
>> disk = ps.disk_usage('/')
>> 
>> All works well, but except for cpu I am struggling to learn how to strip
> out what I don't need.
>> 
>> For example, once I do the above, if I then enter "disk" I get:
>> 
>> disk usage(total=302264549376, used=73844322304, free=213066088448,
> percent=24.4)
>> 
>> So if I want only the number following percent, I get that I need to
> convert this to a built in type and do some split and strip, but that's
> where I'm floundering. Might I get a little guidance on this please?
>> 
> 
> I don't think you have to do any such thing. AFAICT, your code sets disk to
> a usage object that has all the information stored in its attributes. Try, 
> e.g.,
> 
> disk.percent

Yes! I like that much better than my suggestion.

Take care,
Don

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] strip and split?

2013-11-30 Thread Don Jennings

On Nov 30, 2013, at 1:40 PM, richard kappler wrote:

> I'm using psutil to generate some somatic data with the following script:
> 
> import psutil as ps
> 
> cpu = ps.cpu_percent()
> mem = ps.virtual_memory()
> disk = ps.disk_usage('/')
> 
> All works well, but except for cpu I am struggling to learn how to strip out 
> what I don't need.
> 
> For example, once I do the above, if I then enter "disk" I get:
> 
> disk usage(total=302264549376, used=73844322304, free=213066088448, 
> percent=24.4)
> 
> So if I want only the number following percent, I get that I need to convert 
> this to a built in type and do some split and strip

You could take that approach, but you might find that this one is easier:

>>> psutil.disk_usage('/')[-1:][0]
91.2

Take care,
Don

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] strip and split?

2013-11-30 Thread Wolfgang Maier
richard kappler  gmail.com> writes:

> 
> I'm using psutil to generate some somatic data with the following script:
> 
> import psutil as ps
> 
> cpu = ps.cpu_percent()
> mem = ps.virtual_memory()
> disk = ps.disk_usage('/')
> 
> All works well, but except for cpu I am struggling to learn how to strip
out what I don't need.
> 
> For example, once I do the above, if I then enter "disk" I get:
> 
> disk usage(total=302264549376, used=73844322304, free=213066088448,
percent=24.4)
> 
> So if I want only the number following percent, I get that I need to
convert this to a built in type and do some split and strip, but that's
where I'm floundering. Might I get a little guidance on this please?
>

I don't think you have to do any such thing. AFAICT, your code sets disk to
a usage object that has all the information stored in its attributes. Try, e.g.,

disk.percent

and it should give you what you want.

Best,
Wolfgang



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] strip and split?

2013-11-30 Thread richard kappler
I'm using psutil to generate some somatic data with the following script:

import psutil as ps

cpu = ps.cpu_percent()
mem = ps.virtual_memory()
disk = ps.disk_usage('/')

All works well, but except for cpu I am struggling to learn how to strip
out what I don't need.

For example, once I do the above, if I then enter "disk" I get:

disk usage(total=302264549376, used=73844322304, free=213066088448,
percent=24.4)

So if I want only the number following percent, I get that I need to
convert this to a built in type and do some split and strip, but that's
where I'm floundering. Might I get a little guidance on this please?

regards, Richard

-- 

*Mater tua criceta fuit, et pater tuo redoluit bacarum sambucus*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pretty printing XML using LXML on Python3

2013-11-30 Thread Stefan Behnel
SM, 29.11.2013 22:21:
> On Thu, Nov 28, 2013 at 2:45 PM, eryksun wrote:
>> On Thu, Nov 28, 2013 at 2:12 PM, SM wrote:
>>> Run with Python3:
>>>
>>> $ python3 testx.py
>>> b'\n  \n  some text\n\n'
>>
>> print() first gets the object as a string. tostring() returns bytes,
>> and bytes.__str__ returns the same as bytes.__repr__.

Meaning, it's a pure matter of visual representation on the screen, not a
difference in the data.


>> You can decode
>> the bytes before printing, or instead use tounicode():
>>
>> >>> s = etree.tounicode(root, pretty_print=True)
>> >>> print(s)
>> 
>>   
>>   some text
>> 
> 
> Thank you, eryksun. using tounicode seems to work on this small piece of
> code. It still has issues with my code which is generating a big XML code.

Well, I'm sure you are not generating a large chunk of XML just to print it
on the screen, so using tostring(), as you did before, is certainly better.

However, if it's really that much output, you should serialise into a file
instead of serialising it into memory first and then writing that into a
file. So, use ElementTree.write() to write the output into a file directly.

Stefan


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor