Re: Urgent - Would like to see output of each block of python

2016-06-06 Thread Chris Rebert
On Sun, Jun 5, 2016 at 11:57 PM, Archana Sonavane
 wrote:
> Hi Team,
>
> I don't have any idea about python scripts, i have ganglia tool python 
> scripts.
>
> I would like see the output of each code block, could you please guide.
>
> The code as follows:


With regard to your Subject line, please don't attempt to mark posts
as "urgent".
See http://www.catb.org/esr/faqs/smart-questions.html#urgent

Regards,
Chris
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python domain in China. This showed up on Python list

2015-12-01 Thread Chris Rebert
On Tue, Dec 1, 2015 at 2:10 AM, Laura Creighton  wrote:
> I think we have just dodged a bullet, let us now go thank the
> nice people who sent us this and figure out how we should
> secure the domain.
>
> Laura
>
>
> --- Forwarded Message
>
> Return-Path: 
> Date: Tue, 1 Dec 2015 15:12:58 +0800
> From: "Ian Liu" 
> To: 
> Subject: python CN domain and keyword


I hate to break it to you, but this seems to be just another of those
come-ons spammed out by various scummy businesses that trawl WHOIS
databases for people to scam into buying extra/unnecessary domain
names. Google "chinese domain scam" for more info. I've received
similar spams after having registered some .com domains that no
corporation could possibly legitimately want the .cn equivalents of.

Additionally, the two most obvious relevant domains were already
registered by non-PSF parties years ago, and aren't set to expire
imminently. Per https://ewhois.cnnic.cn :

Domain Name: python.cn
Registrant: 北京开心杯科技有限公司
Registrant Contact Email: @happylatte.com
Registration Time: 2003-03-17 12:20:05
Expiration Time: 2017-03-17 12:48:36

Domain Name: python.org.cn
Registrant Contact Email: @lxl.cn
Registration Time: 2007-04-12 14:02:16
Expiration Time: 2016-04-12 14:02:16


Regards,
Chris
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: more itertools

2015-08-31 Thread Chris Rebert
On Mon, Aug 31, 2015 at 6:42 PM, Mark Lawrence  wrote:
> This contained the itertool recipes and was available on pypi but looks like
> it's gone.  Can anybody tell me if it's defunct, superseded or what?

What do you mean? It's still there AFAICT:
https://pypi.python.org/pypi/more-itertools
Still installs fine too:

$ pip install more-itertools
Collecting more-itertools
  Downloading more-itertools-2.2.tar.gz
Installing collected packages: more-itertools
  Running setup.py install for more-itertools
Successfully installed more-itertools-2.2
$ python
Python 2.7.10 (default, Jul 17 2015, 01:43:42)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import more_itertools
>>>

Cheers,
Chris

P.S. It annoys me that the most of "recipes", which IME normally need
no tweaking whatsoever, aren't just part of itertools proper.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python/Github

2015-08-24 Thread Chris Rebert
On Mon, Aug 24, 2015 at 1:14 PM, DBS  wrote:
> Hello,
>
> I'm trying to retrieve the number of commits and changed files on all pull 
> requests submitted to a branch.
>
> The call to get all the pull requests is GET /repos/:owner/:repo/pulls, but 
> it does not return "commits" or "changed_files".
>
> The call that returns number of commits and changed files is GET 
> /repos/:owner/:repo/pulls/:number.
>
> Any guidance on how I can modify the call below to include commits and 
> changed files on all pull requests?
>
> #!/usr/bin/env python
> import json
> import requests


Have you considered using a GitHub API Python library such as
github3.py (https://github.com/sigmavirus24/github3.py ) rather than
reinventing the wheel?

Cheers,
Chris
--
http://chrisrebert.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: looking for standard/builtin dict-like data object

2015-08-10 Thread Chris Rebert
On Mon, Aug 10, 2015 at 8:22 PM, Vladimir Ignatov  wrote:
> Hi,
>
> In my code I often use my own home-brewed object for passing bunch of
> data between functions. Something like:
>
> class Data(object):
> def __init__ (self, **kwargs):
> self.__dict__ = kwargs
>
> 
>
> return Data(attr1=..., attr2=..., attr3=...)
>
> Logically it works like plain dictionary but with nice result.attrX
> syntax on client side (instead of resut['attrX']).   Overall I am
> pretty happy with this approach except that I need to drag Data class
> around my projects and import its module in every code producing data.
>
> I am curious if anybody knows similar "dummy" class located in
> standard libraries? I'd be glad to use it instead.

This is commonly known as a "Bunch" class, after the Python Cookbook
recipe. I don't believe there's any public version of it in the std
lib, but there is a PyPI package for it:
https://pypi.python.org/pypi/bunch

However, if the set of attribute names is fixed and an immutable
datatype is acceptable, you could use the std lib's "namedtuple" type:
https://docs.python.org/3/library/collections.html#collections.namedtuple

Cheers,
Chris
--
http://chrisrebert.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Decimals and other numbers

2015-01-08 Thread Chris Rebert
On Thu, Jan 8, 2015 at 6:33 PM, Devin Jeanpierre  wrote:
> I noticed some very PHP-ish behavior today:
>
 import decimal
 x = 0
 y = float(x)
 z = decimal.Decimal(x)
 x == y == z == x
> True
 x ** x
> 1
 y**y
> 1.0
 z**z
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/lib/python2.7/decimal.py", line 2216, in __pow__
> return context._raise_error(InvalidOperation, '0 ** 0')
>   File "/usr/lib/python2.7/decimal.py", line 3872, in _raise_error
> raise error(explanation)
> decimal.InvalidOperation: 0 ** 0
>
> I'd file a bug report but I'm anticipating some rational (heh)
> explanation. Any ideas?

The `decimal` std lib module implements the General Decimal Arithmetic
Specification (http://speleotrove.com/decimal/decarith.html ).
In the "Exceptional conditions" section
(http://speleotrove.com/decimal/daexcep.html ), it specifies the
following:
Invalid operation
This occurs and signals invalid-operation if:
[...]
* both operands of the power operation are zero

No idea why it chose to differ from IEEE-754.

Cheers,
Chris
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Import Doesn't Import

2014-10-15 Thread Chris Rebert
On Wednesday, October 15, 2014, ryguy7272  wrote:

> So sorry everyone.  I've posted here several times today.  This is VERY
> frustrating.
>
> So, I'm reading this link.
> https://docs.python.org/2/howto/urllib2.html
>

>
Important note!: The "/2/" in the URL means those docs are for Python 2.x
When using Python 3, ensure that the docs you're consulting have a "/3/" in
them instead.


> Fetching URLs
> The simplest way to use urllib2 is as follows:
> import urllib2
> response = urllib2.urlopen('http://python.org/')
> html = response.read()
>
>
> So, I fire up Python, and create a new file and name it and hit F5.
>
> All I have is thins in the file:
> import urllib2
> response = urllib2.urlopen('http://python.org/')
> html = response.read()
>
> It immediately produces this error:
> Traceback (most recent call last):
>   File "C:/Python34/import_web_data.py", line 1, in 
> import urllib2
> ImportError: No module named 'urllib2'
>
>
You're using Python 3, and the urllib2 module no longer exists in Python 3.
The URL/HTTP modules got refactored on Python 3. You want the
`urllib.request` module instead.

Although most folks nowadays use http://docs.python-requests.org/ instead,
though it's third-party and outside the std lib.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Linux distros w/o Python in "base" installation

2014-08-11 Thread Chris Rebert
On Mon, Aug 11, 2014 at 11:53 AM, Grant Edwards  wrote:
> I just installed Arch Linux for the first time, and was surprosed to
> find that Python isn't installed as part of a "base" system.  It's
> also not included in the 'base-devel' package group.  It's trivial to
> install, but I'd still pretty surprised it's not there by default.  I
> guess I've spent too much time with Gentoo, Debian, and RedHat
> derivitives which require Python be installed.
>
> I've probably used at least a dozen Linux distros over the years, and
> this is the first time I've noticed that Python wasn't installed by
> default.
>
> Just for the sake of curiosity, are there any other significant
> desktop/server Linux distros that don't come "out of the box" with
> Python?

It would seem that such distros are opting to not be LSB-compliant?:
http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Languages/LSB-Languages/pylocation.html

Cheers,
Chris
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Distributing python applications as a zip file

2014-07-22 Thread Chris Rebert
On Tue, Jul 22, 2014 at 9:23 PM, Steven D'Aprano  wrote:
> A little known feature of Python: you can wrap your Python application in
> a zip file and distribute it as a single file. The trick to make it
> runnable is to put your main function inside a file called __main__.py
> inside the zip file.

> It's not quite self-contained, as you still need to have Python
> installed, but otherwise it's a good way to distribute a Python
> application as a single file that users can just copy and run.

And if you want something nearly completely self-contained (probably
modulo dynamic linking), it seems that there's PEX
(http://pex.readthedocs.org/en/latest/ ).

Cheers,
Chris
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: UTC "timezone" causing brain explosions

2014-01-29 Thread Chris Rebert
On Wed, Jan 29, 2014 at 1:52 PM, Skip Montanaro  wrote:
> Following up on my earlier note about UTC v. GMT, I am having some
> trouble grokking attempts to convert a datetime into UTC. Consider
> these three values:
>
 import pytz
 UTC = pytz.timezone("UTC")
 LOCAL_TZ = pytz.timezone("America/Chicago")
 LOCAL_TZ
> 
 now = datetime.datetime.now()
 now
> datetime.datetime(2014, 1, 29, 15, 39, 35, 263666)
>
> All well and good, right? The variable "now" is a naive datetime
> object. I happen to be sitting in a chair in the city of Chicago, so
> let's call it what it is, a datetime in the America/Chicago timezone:
>
 s = LOCAL_TZ.localize(now)
 s
> datetime.datetime(2014, 1, 29, 15, 39, 35, 263666, tzinfo= 'America/Chicago' CST-1 day, 18:00:00 STD>)
>
> That looks good to me. Now, let's normalize it to UTC:

I don't think .normalize() doesn't do what you think it does; it's
related to timezones with DST.
I believe you want datetime.astimezone() instead.

 t = UTC.normalize(s)
 t
> datetime.datetime(2014, 1, 29, 15, 39, 35, 263666, tzinfo=)
 t.hour
> 15
>
> WTF? Why isn't the t.hour == 21?

Because you didn't actually perform a proper timezone conversion:
>>> t = s.astimezone(UTC)
>>> t
datetime.datetime(2014, 1, 29, 21, 39, 35, 263666, tzinfo=)
>>> t.hour == 21
True


> That looks correct, but I don't understand why I don't get hour==21
> out of the UTC.normalize call. It's like it's a no-op.

It is indeed a no-op:
"You can take shortcuts when dealing with the UTC side of timezone
conversions. normalize() and localize() are not really necessary when
there are no daylight savings time transitions to deal with."
-- http://pytz.sourceforge.net

Cheers,
Chris
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Postfix conditionals

2014-01-05 Thread Chris Rebert
On Sun, Jan 5, 2014 at 2:12 PM, Göktuğ Kayaalp  wrote:
>
> This was sent to me as a private reply

No, it was sent as a public reply via Reply-All; note that python-list
was CC-ed, which works just fine:
https://mail.python.org/pipermail/python-list/2014-January/663858.html

> to a question that I have posted
> to python-list@python.org, so I am forwarding it to here.

>  Original Message 
> Subject: Re: Postfix conditionals
> Date: Sun, 5 Jan 2014 14:09:14 -0800
> From: Chris Rebert 
> To: Göktuğ Kayaalp 
> CC: Python 

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


Re: Postfix conditionals

2014-01-05 Thread Chris Rebert
On Sun, Jan 5, 2014 at 12:24 PM, Göktuğ Kayaalp  wrote:
> Hi,
>
> AFAIK, we do not have "postfix conditionals" in Python, i.e. a condition
> appended to a
> statement, which determines whether the statement runs or not:
>
>   py> for i in [False]:
>   ... break if not i
>
> The above piece of code is equivalent to this in Python:
>
>   py> for i in [False]:
>   ...if not i
>   ...break
>
> I believe that the first example is superior to the second example when the
> two is compared
> for readability and intuitiveness.

I'm going to have to disagree. I dislike how this obscures the
if-statement, complicates the language grammar, and adds another
unnecessary way to express the same thing (which violates TOOWTDI)
with little countervailing benefit.

> We already have a ternary statement that
> looks similar,
>
>   py> print('hi') if True else None

Actually, to be pedantic, it's a ternary *expression*. Using it purely
for side-effects (i.e. as a statement) is rather unidiomatic, in the
same way that abusing list comprehensions, e.g.:

[print(i) for i in range(42)]

is frowned upon.
Not to mention that the ternary doesn't work for actual statements
(print() is just a function call in Python 3):

>>> (x = 1) if True else (x = 2)
  File "", line 1
(x = 1) if True else (x = 2)
   ^
SyntaxError: invalid syntax

> so I reckon there would be no breakage in old code if this kind of syntax
> was added.  Ruby has
> this, and AFAIK Perl also does.
>
> I lack the knowledge of whether the community has opinions on this kind of
> notation, so I am
> posting this here instead of the ideas list.  What are your thoughts on
> this?

You can already write:

for i in [False]:
if not i: break

if you feel the need for terseness or a one-liner. Perhaps this
satisfies your desire?

Cheers,
Chris
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: a Python Static Analyzer

2013-12-14 Thread Chris Rebert
On Sat, Dec 14, 2013 at 5:31 PM, Dan Stromberg  wrote:
> Where does PySonar2 sit in the spectrum from pylint
> (thorough/pedantic) to pyflakes (relaxed/few-false-positives)?
>
> I use pylint and pyflakes a lot, and I've heard that PyChecker sits in
> between them on this axis.

My impression is that PyChecker has been abandoned.
The last commit in its SourceForge CVS repo is from 2008, and `pip
install PyChecker` fails.

Cheers,
Chris
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to parse JSON passed on the command line?

2013-11-06 Thread Chris Rebert
On Wed, Nov 6, 2013 at 7:53 PM, Anthony Papillion  wrote:
> Hello Everyone,
>
> I'm writing a little helper script in Python that will access a JSON
> formatted argument from the shell when it's called. The parameter will
> look like this:
>
> {"url":"http://www.google.com"}
>
> So, if my program is called "getargfromcli.py" the call will look like this:
>
> getargfromcli.py {"url":"http://www.google.com"}

You probably want
getargfromcli.py '{"url":"http://www.google.com"}'
instead, so that your string of JSON is treated literally by the shell.

> In the case above, I assume my JSON string will be argv[1]. In fact,
> when I do
>
> print sys.argv[1]
>
> It works as expected and prints out the JSON string as expected like
> this: {url:http://www.google.com}

No, that's not JSON anymore! All the required quotation marks have
gone missing. The shell ate them.

Regards,
Chris
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I am never going to complain about Python again

2013-10-09 Thread Chris Rebert
On Wed, Oct 9, 2013 at 9:36 PM, Steven D'Aprano  wrote:
> Just came across this little Javascript gem:
>
> ",,," == Array((null,'cool',false,NaN,4));
>
> => evaluates as true
>
> http://wtfjs.com/2011/02/11/all-your-commas-are-belong-to-Array
>
> I swear, I am never going to complain about Python again.

Oh, indeed, you have no idea.
Since we're talking about JavaScript suckage, I'm obliged to link to
this wonderful video on the subject:
https://www.destroyallsoftware.com/talks/wat

Cheers,
Chris R.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Language design

2013-09-10 Thread Chris Rebert
* No explicit variable declarations (modulo `global`+`nonlocal`) means
that variable name typos can't be reliably detected at compile-time.
* The value of the loop variable at call-time for functions defined
within a loop trips people up.
* No self-balancing tree datatype of any kind is included in the std lib.
* Function scope rather than block scope (e.g. `while` doesn't
introduce a new scope) [Personally, I don't have much of a problem
with this, but some people do.]
* No anonymous block syntax (cf. Ruby or Smalltalk). Makes it
harder/uglier to define/use custom control structures. The `with`
statement would have been unnecessary.

Cheers,
Chris


On Mon, Sep 9, 2013 at 11:09 PM, Steven D'Aprano  wrote:
> Some time ago, Tom Christiansen wrote about the "Seven Deadly Sins of
> Perl":
>
> http://www.perl.com/doc/FMTEYEWTK/versus/perl.html
>
>
> What design mistakes, traps or gotchas do you think Python has? Gotchas
> are not necessarily a bad thing, there may be good reasons for it, but
> they're surprising.
>
> To get started, here are a couple of mine:
>
>
> - Python is so dynamic, that there is hardly anything at all that can be
> optimized at compile time.
>
> - The behaviour of mutable default variables is a gotcha.
>
> - Operators that call dunder methods like __add__ don't use the same
> method resolution rules as regular methods, they bypass the instance and
> go straight to the type, at least for new-style classes.
>
>
>
> --
> Steven
> --
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for a name for a deployment framework...

2013-06-24 Thread Chris Rebert
On Jun 24, 2013 5:36 AM,  wrote:
>
> Hi all,
>
> Any suggestions for a good name, for a framework that does automatic
server deployments?
>
> It's like Fabric, but more powerful.
> It has some similarities with Puppet, Chef and Saltstack, but is written
in Python.

Er, Salt is likewise written in Python.
(Your statement seemed to imply otherwise.)

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: The philosophy behind list indexes

2013-06-14 Thread Chris Rebert
On Jun 14, 2013 10:26 PM,  wrote:
> I bet this is asked quite frequently, however after quite a few hours
searching I haven't found an answer.
>
> What is the thinking behind stopping 'one short' when slicing or
iterating through lists?
>
> By example;
>
> >>> a=[0,1,2,3,4,5,6]
> >>> a
> [0, 1, 2, 3, 4, 5, 6]
> >>> a[2:5]
> [2, 3, 4]
>
> To my mind, it makes more sense to go to 5. I'm sure there's a good
reason, but I'm worried it will result in a lot of 'one-off' errors for me,
so I need to get my head around the philosophy of this behaviour, and where
else it is observed (or not observed.)

I find Dijkstra's explanation rather convincing:
http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Popen and reading stdout in windows

2013-06-11 Thread Chris Rebert
On Jun 11, 2013 12:21 AM, "Pete Forman"  wrote:
>
> "Joseph L. Casale"  writes:
>
> >> You leave out an awful amount of detail. I have no idea what ST is,
> >> so I'll have to guess your real problem.
> >
> > Ugh, sorry guys its been one of those days, the post was rather
> > useless...
> >
> > I am using Popen to run the exe with communicate() and I have sent
> > stdout to PIPE without luck. Just not sure what is the proper way to
> > iterate over the stdout as it eventually makes its way from the
> > buffer.
>
> You could try Sarge which is a wrapper for subprocess providing command
> pipeline functionality.
>
> http://sarge.readthedocs.org/

Or Plumbum: http://plumbum.readthedocs.org

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Split a list into two parts based on a filter?

2013-06-10 Thread Chris Rebert
On Mon, Jun 10, 2013 at 1:34 PM, Roy Smith  wrote:
> I have a list, songs, which I want to divide into two groups.
> Essentially, I want:
>
> new_songs = [s for s in songs if s.is_new()]
> old_songs = [s for s in songs if not s.is_new()]
>
> but I don't want to make two passes over the list.  I could do:
>
> new_songs = []
> old_songs = []
> for s in songs:
> if s.is_new():
> new_songs.append(s)
> else:
> old_songs.append(s)
>
> Which works, but is klunky compared to the two-liner above.  This
> seems like a common enough thing that I was expecting to find
> something in itertools which did this.  I'm thinking something along
> the lines of:
>
> matches, non_matches = isplit(lambda s: s.is_new, songs)
>
> Does such a thing exist?

itertools.groupby() is kinda similar, but unfortunately doesn't fit
the bill due to its sorting requirement.
There is regrettably no itertools.partition(). And given how dead-set
Raymond seems to be against adding things to the itertools module,
there will likely never be.
Maybe more-itertools (https://pypi.python.org/pypi/more-itertools )
would accept a patch?

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding Relative Maxima in Python3

2013-05-31 Thread Chris Rebert
On May 31, 2013 2:46 AM, "Lourens-Jan Ugen" 
wrote:
>
> Hi all,
>
> The last few days I've been working on a script to manipulate some
scientific data. One thing I would like to be able to do is find relative
maxima in a data set.
> I'm using numpy in python3 (which I think I can't do without because of
utf16 encoding of my data source) and a series of np.arrays. When looking
around the web and some forums I came across the scipy function
argrelextrema, which seemed to do just what I wanted. The problem is that I
can't get the function to work, probably because scipy in python3 does not
yet support the argrelextrema function. I can however, not find a reference
to this really being the problem, and was wondering if someone here could
maybe help me out.
> The code I used is shown below. The function to return the maximum values
is called by a different script. Then, when running, it returns an error
like the one below the code.
>
> Script:
> import numpy as np
> import scipy as sp

> Error message:
> Traceback (most recent call last):
>   File "MyScript.py", line 15, in 
> Varrr = FD.max_in_array_range(CalcAndDiffArray, 5 ,MyBound)
>   File "/MyPath/Script.py", line 82, in max_in_array_range
> return sp.argrelmax(TempArray[LowBound:], np.greater)
> AttributeError: 'module' object has no attribute 'argrelmax'

The docs would seem to indicate that that function resides in the "signal"
submodule of scipy:
http://docs.scipy.org/doc/scipy-dev/reference/generated/scipy.signal.argrelmax.html#scipy.signal.argrelmax

Hence, it would be sp.signal.argrelmax() as opposed to just sp.argrelmax()

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Piping processes works with 'shell = True' but not otherwise.

2013-05-29 Thread Chris Rebert
On Sun, May 26, 2013 at 4:58 PM, Luca Cerone  wrote:

> Hi Chris, first of all thanks for the help. Unfortunately I can't provide the 
> actual commands because are tools that are not publicly available.
> I think I get the tokenization right, though.. the problem is not that the 
> programs don't run.. it is just that sometimes I get that error..
>
> Just to be clear I run the process like:
>
> p = subprocess.Popen(['program1','--opt1','val1',...'--optn','valn'], ... the 
> rest)
>
> which I think is the right way to pass arguments (it works fine for other 
> commands)..

>> You may also want to provide /dev/null as p1's stdin, out of an abundance of 
>> caution.
>
> I tried to redirect the output to /dev/null using the Popen argument:
> 'stdin = os.path.devnull' (having imported os of course)..
> But this seemed to cause even more troubles...

That's because stdin/stdout/stderr take file descriptors or file
objects, not path strings.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: detect key conflict in a JSON file

2013-05-29 Thread Chris Rebert
On Wed, May 29, 2013 at 4:16 AM, Jabba Laci  wrote:
> Hi,
>
> How can you detect if a key is duplicated in a JSON file? Example:
>
> {
> "something": [...],
> ...
> "something": [...]
> }
>
> I have a growing JSON file that I edit manually and it might happen
> that I repeat a key. If this happens, I would like to get notified.
> Currently the value of the second key silently overwrites the value of
> the first.

You can pass an appropriate object_pairs_hook function to json.load():
http://docs.python.org/2/library/json.html#repeated-names-within-an-object
http://docs.python.org/2/library/json.html#json.load

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Piping processes works with 'shell = True' but not otherwise.

2013-05-26 Thread Chris Rebert
On May 24, 2013 7:06 AM, "Luca Cerone"  wrote:
>
> Hi everybody,
> I am new to the group (and relatively new to Python)
> so I am sorry if this issues has been discussed (although searching for
topics in the group I couldn't find a solution to my problem).
>
> I am using Python 2.7.3 to analyse the output of two 3rd parties programs
that can be launched in a linux shell as:
>
>  program1 | program2
>
> To do this I have written a function that pipes program1 and program2
(using subprocess.Popen) and the stdout of the subprocess, and a function
that parses the output:
>
> A basic example:
>
> from subprocess import Popen, STDOUT, PIPE
> def run():
>   p1 = Popen(['program1'], stdout = PIPE, stderr = STDOUT)
>   p2 = Popen(['program2'], stdin = p1.stdout, stdout = PIPE, stderr =
STDOUT)

Could you provide the *actual* commands you're using, rather than the
generic "program1" and "program2" placeholders? It's *very* common for
people to get the tokenization of a command line wrong (see the Note box in
http://docs.python.org/2/library/subprocess.html#subprocess.Popen for some
relevant advice).

>   p1.stdout.close()
>   return p2.stdout
>
>
> def parse(out):
>   for row in out:
> print row
> #do something else with each line
>   out.close()
>   return parsed_output
>
>
> # main block here
>
> pout = run()
>
> parsed = parse(pout)
>
> #--- END OF PROGRAM #
>
> I want to parse the output of 'program1 | program2' line by line because
the output is very large.
>
> When running the code above, occasionally some error occurs (IOERROR:
[Errno 0]).

Could you provide the full & complete error message and exception traceback?

> However this error doesn't occur if I code the run() function as:
>
> def run():
>   p = Popen('program1 | program2', shell = True, stderr = STDOUT, stdout
= PIPE)
>   return p.stdout
>
> I really can't understand why the first version causes errors, while the
second one doesn't.
>
> Can you please help me understanding what's the difference between the
two cases?

One obvious difference between the 2 approaches is that the shell doesn't
redirect the stderr streams of the programs, whereas you /are/ redirecting
the stderrs to stdout in the non-shell version of your code. But this is
unlikely to be causing the error you're currently seeing.

You may also want to provide /dev/null as p1's stdin, out of an abundance
of caution.

Lastly, you may want to consider using a wrapper library such as
http://plumbum.readthedocs.org/en/latest/ , which makes it easier to do
pipelining and other such "fancy" things with subprocesses, while still
avoiding the many perils of the shell.

Cheers,
Chris
--
Be patient; it's Memorial Day weekend.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: serialize a class to XML and back

2013-05-26 Thread Chris Rebert
On May 23, 2013 3:42 AM, "Schneider"  wrote:
>
> Hi list,
>
> how can I serialize a python class to XML? Plus a way to get the class
back from the XML?

There's pyxser: http://pythonhosted.org/pyxser/

> My aim is to store instances of this class in a database.

Honestly, I would avoid XML if you can. Consider using JSON (Python
includes the `json` module in the std lib) or pickle instead. Compared to
XML: The former is more standardized (in the context of serializing
objects) and less verbose; the latter is more efficient (if you don't care
about cross-language accessibility); both have more convenient APIs.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Source code as text/plain

2013-05-24 Thread Chris Rebert
On May 24, 2013 9:02 PM, "Carlos Nepomuceno" 
wrote:
>
> I'd like to have the option to download the source code as text/plain
from the docs.python.org pages.
>
> For example: when I'm a docs page, such as:
>
> http://docs.python.org/2/library/string.html
>
> and I click the source code link I'm taken to a Mercurial page:
>
> http://hg.python.org/cpython/file/2.7/Lib/string.py
>
> but over there there's no way to get a clean text/plain version of the
code because the line numbers are included.
>
> A link to the text/plain version on that page would be nice!

It's already there. Click the "raw" link in the sidebar. In this case, at
this moment, it sends you to
http://hg.python.org/cpython/raw-file/f4981d8eb401/Lib/string.py
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: There must be a better way

2013-04-20 Thread Chris Rebert
On Sat, Apr 20, 2013 at 4:46 PM, Colin J. Williams  wrote:
> Below is part of a script which shows the changes made to permit the script
> to run on either Python 2.7 or Python 3.2.
>
> I was surprised to see that the CSV next method is no longer available.
>
> Suggestions welcome.

> if ver == '2':
>headerLine= inData.next()
> else:  # Python version 3.3

> headerLine= inData.__next__()

Use the built-in next() function
(http://docs.python.org/2/library/functions.html#next ) instead:
headerLine = next(iter(inData))

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The type/object distinction and possible synthesis of OOP and imperative programming languages

2013-04-17 Thread Chris Rebert
On Tue, Apr 16, 2013 at 11:40 PM, Steven D'Aprano
 wrote:
> On Tue, 16 Apr 2013 15:38:29 -0700, Mark Janssen wrote:

> >   (Note this contrasts starkly with Java(script), which doesn't seem
> > to be based on anything -- can anyone clarify where Java actually comes
> > from?)
>
> C.

"Influenced by: Ada 83, C++, C#, Eiffel, Generic Java, Mesa, Modula-3,
Oberon, Objective-C, UCSD Pascal, Smalltalk"
"Categories: C programming language family | [...]"
– http://en.wikipedia.org/wiki/Java_(programming_language)

Sincerely,
Chris
--
Read Wikipedia's infoboxes! People work hard on them!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calling python script in dos and passing arguments

2013-04-16 Thread Chris Rebert
On Tue, Apr 16, 2013 at 7:14 AM, PEnergy  wrote:
> Greetings,
>
> I am trying to write a python script that, when called from the DOS prompt, 
> will call another python script and pass it input variables.  My current code 
> will open the other python script but doesn't seem to pass it any values:
>
> import os,sys,subprocess
> subprocess.Popen(['python.exe','C:\NDEX\GRE2\uip\uip_20.py','t3c*'])
>
> Am I missing something or is this type of call not possible through DOS?

1. Backslash is an escape character in Python strings (e.g. "\n" =
newline). You should therefore double-up on your backslashes. (Your
exact string just so happens to work due to a misfeature regarding how
invalid backslash escapes are handled.)
2. Glob/wildcard ("*") expansion is done by the shell, but
subprocess.Popen does not use the shell by default (for good reason!).
Use the `glob` library to do the expansion yourself, in Python:
http://docs.python.org/2/library/glob.html

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threadpool item mailboxes design problem

2013-04-14 Thread Chris Rebert
On Apr 14, 2013 4:27 PM, "Charles Hixson" 
wrote:
>
> What is the best approach to implementing actors that accept and post
messages (and have no other external contacts).

You might look at how some of the existing Python actor libraries are
implemented (perhaps one of these might even save you from reinventing the
wheel):

http://www.pykka.org/en/latest/
http://www.kamaelia.org/Docs/Axon/Axon.html
https://pypi.python.org/pypi/pulsar

Kinda old:
http://candygram.sourceforge.net/contents.html
http://osl.cs.uiuc.edu/parley/

> So far what I've come up with is something like:
> actors = {}
> mailboxs = {}
>
> Stuff actors with actor instances, mailboxes with multiprocessing.queue
instances.   (Actors and mailboxes will have identical keys, which are id#,
but it's got to be a dict rather than a list, because too many are rolled
out to disk.)  And I'm planning of having the actors running simultaneously
and continually in a threadpool that just loops through the actors that are
assigned to each thread of the pool.

> It would, however, be better if the mailbox could be specific to the
threadpool instance, so less space would be wasted.  Or if the queues could
dynamically resize.  Or if there was a threadsafe dict.  Or...  But I don't
know that any of these are feasible.  (I mean, yes, I could write all the
mail to a database, but is that a better answer, or even a good one?)

My recollection is that the built-in collection types are threadsafe at
least to the limited extent that the operations exposed by their APIs (e.g.
dict.setdefault) are atomic.
Perhaps someone will be able to chime in with more details.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.system() with imbeded quotes on centos

2013-04-05 Thread Chris Rebert
On Fri, Apr 5, 2013 at 3:00 PM, Cameron Simpson  wrote:
> On 01Apr2013 20:26, John Gordon  wrote:
> | In <0c9717ca-52dd-49ce-8102-e14328838...@googlegroups.com> cev...@gmail.com 
> writes:
> | > someip = '192.168.01.01'
> | > var1 = 'lynx -dump http://' + someip + 
> '/cgi-bin/.log&.submit=+++Go%21+++  > junk'
> |
> | '&' is a special character in shell commands.  You'll need to quote or
> | escape it.
>
> Or better still, use the subprocess module and avoid going via the
> os.system() altogether:
>
>   http://docs.python.org/2/library/subprocess.html#popen-constructor
>
> If you must go via the os.system(), write yourself a generic function
> to quote a string for the shell, and to quote a bunch of strings
> (essentially " ".join( quoted-individual-strings )). And use it
> rigorously.
>
> Anything else is asking for shell injection attacks/errors, just
> as bad as hand constructing SQL statements.
>
> For example, if I must construct a shell command from arbitrary
> strings (like your URL) I use quote() from this:
>
>   https://bitbucket.org/cameron_simpson/css/src/tip/lib/python/cs/sh.py
>
> That code's nothing special, just what I rolled some years ago for
> exactly this purpose.

No need for third-party code, just use the std lib:
http://docs.python.org/2/library/pipes.html#pipes.quote
http://docs.python.org/3/library/shlex.html#shlex.quote

(But yeah, best of all is to just use `subprocess` with shell=False.)

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Curl and python httplib?

2013-03-28 Thread Chris Rebert
On Wed, Mar 27, 2013 at 7:54 PM, 小学园PHP  wrote:
> Guys,
>
> I take a project that need send request to Hadoop by curl.
> But now, the curl and pycurl can't satisfy my project. So i need use the
> powerful httplib.

I would say that `requests`
(http://docs.python-requests.org/en/latest/ ) is generally preferable
to httplib these days.

> But failed.
>
> my curl request:
> curl -i -X PUT "http://localhost:50070/webhdfs/v1/levi/7?op=CREATE";
>
> my return:
> HTTP/1.1 307 TEMPORARY_REDIRECT
> Content-Type: application/octet-stream
> Location:
> http://58.53.211.47:50075/webhdfs/v1/levi/7?op=CREATE&overwrite=false
> Content-Length: 0
> Server: Jetty(6.1.26)
>
> Now, i change the curl request to httplib:
> import httplib
> import urllib
>
> params=urllib.urlencode({"@op":"CREATE","@user.name":"levi"})
> headers={"Content-type": "application/x-www-form-urlencoded","Accept":
> "text/plain"}
> conn=httplib.HTTPConnection("localhost:50070")
> conn.request("PUT","/webhdfs/v1/levi/7.txt",params,headers)
> response=conn.getresponse()
> print response.status, response.reason
> data=response.read()
> print data
> conn.close()
>
> But it failed:
> #print response.status, response.reason
> 500 Internal Server Error
> #print data
> '{"RemoteException":{"exception":"WebApplicationException","javaClassName":"javax.ws.rs.WebApplicationException","message":null}}'
>
> Who knows why? It's OK when i use curl, so where is the problem in httplib
> method?
> Or some other reasons?

It's rather hard to say when neither the base URL, nor querystring
parameters, nor request body are the same between your curl example
and your httplib example, making them rather incomparable.
Part of the problem may be that you are passing what may have been
intended as querystring parameters (`params`) as the `body` argument
to HTTPConnection.request().
In any case, I would suggest trying to use the `requests` library instead.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Doing both regex match and assignment within a If loop?

2013-03-28 Thread Chris Rebert
On Thu, Mar 28, 2013 at 9:00 PM, Victor Hooi  wrote:
> Hi,
>
> I have logline that I need to test against multiple regexes. E.g.:
>
> import re
>
> expression1 = re.compile(r'')
> expression2 = re.compile(r'')
>
> with open('log.txt') as f:
> for line in f:
> if expression1.match(line):
> # Do something - extract fields from line.
> elif expression2.match(line):
> # Do something else - extract fields from line.
> else:
> # Oh noes! Raise exception.
>
> However, in the "Do something" section - I need access to the match object 
> itself, so that I can strip out certain fields from the line.
>
> Is it possible to somehow test for a match, as well as do assignment of the 
> re match object to a variable?
>
> if expression1.match(line) = results:
> results.groupsdict()...

AFAIK, not without hacks and/or being unidiomatic.

> Obviously the above won't work - however, is there a Pythonic way to tackle 
> this?
>
> What I'm trying to avoid is this:
>
> if expression1.match(line):
> results = expression1.match(line)
>
> which I assume would call the regex match against the line twice - and when 
> I'm dealing with a huge amount of log lines, slow things down.

def process(line):
match = expr1.match(line)
if match:
# ...extract fields…
return something
match = expr2.match(line)
if match:
# ...extract fields…
return something
# etc…
raise SomeError()  # Oh noes!

with open('log.txt') as f:
for line in f:
results = process(line)


If you choose to further move the extractor snippets into their own
functions, then you can do:


# these could be lambdas if they're simple enough
def case1(match):
# ...
def case2(match):
# …
# etc...

REGEX_EXTRACTOR_PAIRS = [
(re.compile(r''), case1),
(re.compile(r''), case2),
# etc...
]

def process(line):
for regex, extractor in REGEX_EXTRACTOR_PAIRS:
match = regex.match(line)
if match:
return extractor(match)
raise SomeError()

Although this second option is likely somewhat less performant, but it
definitely saves on repetition.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OOPv2: [Was: Message passing syntax for objects]

2013-03-19 Thread Chris Rebert
On Tue, Mar 19, 2013 at 9:46 PM, Mark Janssen  wrote:
> Hopefully this won't be considered mail spam, but another quora answer that
> gets to the idea I'm after:  http://qr.ae/TMh7A
>
> Reposted here for those who don't have accounts:
>
> Q. Is it time for us to dump the OOP paradigm? If yes, what can replace it?
>
> When I was using C++ and Java, more of my time was spent fitting the problem
> to languages' OO paradigm than actually solving the problem.
> When I used Python, I found I was focusing on the problem more compared to
> other languages, but still that fitting aspect remained.
>
> I am looking for a language design perspective answer, where we can compare
> logic programming, functional, etc approaches and really see if the OOP
> still fits/necessary and do we need to evolve towards a better paradigm.
>
>
>
> A.  Yes.  OOP was the evolution of applying abstraction to a programming
> language, to the machine, having to remember that we used to deal very
> concretely with switches and bits.  Looking back, it can be seen to having
> gone too far.
>
> We don't need objects.  Programmers don't model physical things within the
> computer, except in the limited domain of (generally game) simulation.   It
> misinformed the paradigm in the wrong direction as many people found when
> programmers started making vast hierarchies of objects to try to categorize
> reality (Suburu inherits from Car inherits from Vehicle inherits from.).
> So we went the wrong direction.
>
> What's to replace it?
>
> There was an interesting discussion on comp.lang.python started in 2004 by
> Mark Hahn about prototypes.  (See: Google Groups and Google Groups).   I
> think somehow it captures the idea pretty well.

(Actual links to the referenced discussion(s) would be appreciated.)

> Prototypes are a refactoring of all the explorations that OOP made, taking
> us back to our roots.  From the high-level abstraction of "objects" in the
> computer, back to the simple, understandable (C) structs and then encoding a
> way to shape or "template" data into a form that can take on abstract
> qualities which can then be used in a very general way.  The key difference?
> You're not way up in abstraction-land attempting to impose your personal
> taxonomy into the machine; instead, you're working from the common-ground of
> the machine and co-ordinating a object/data space into existence where other
> programmers can actually use them.   In other words, you're advancing the
> state-of-the-art of data structuring instead of applying some conceptual,
> hyper-personal abstraction into your code and onto the machine.
>
> The result?  Programmers can start making very simple, loosely-coupled,
> universal types and build upwards with other programmers into more and more
> complex, re-usable "mashups".
>
> This will create modularity, agility, and facilitate the evolution of a
> "universal programming space" as well as create the Open Source Culture that
> the Internet needs to re-start itself.

What a crock. Prototype-based languages are a somewhat interesting
alternative model, but IMO they are nowhere near as "game-changing" as
the post suggests. The elimination of classes simplifies the
MOP/metamodel a bit, but AFAICT doesn't improve much otherwise.
If prototypes were so revolutionary, they would presumably be faring
better in the marketplace of ideas (its promoters have been at it
since about as long as Python's have, apparently with less luck). If
you need evidence, look at JavaScript. It is, for better or worse,
among the most popular languages of our time and it is
prototype-based, but virtually no one chooses to make use of its
prototypical nature; almost all projects seem to end up consciously
re-implementing an approximation of traditional classes on top of the
prototype system (using one of several techniques, there seeming to be
no widespread agreement about which one is superior). Even prior to
JavaScript, in orthodox prototype-based languages, there is/was even a
concept of "traits" objects, which basically play much the same role
as classes in traditional OOP.

Also, I cannot for the life of me figure out how you made the abrupt
leap from message-passing to prototype-based OOP.
I echo Mark Lawrence's comment from your previous thread. +1 best
trolling so far this millennium. Have fun with your architecture
astronautics.

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError: 'float' object is not iterable

2013-03-14 Thread Chris Rebert
On Thu, Mar 14, 2013 at 3:12 AM, Ana Dionísio  wrote:
> Hi!!
>
> I keep having this error and I don't know why: TypeError: 'float' object is 
> not iterable.

In general, in the future, always include the full exception
Traceback, not just the final error message. The extra details this
provides can greatly aid debugging.

> I have this piece of code, that imports to python some data from Excel and 
> saves it in a list:
>
> "
> t_amb = []
>
> for i in range(sh2.nrows):
> t_amb.append(sh2.cell(i,2).value)

`t_amb` is a list, and you are apparently putting floats (i.e. real
numbers) into it. `t_amb` is a list of floats.
Therefore every item of `t_amb` (i.e. `t_amb[x]`, for any `x` that's
within the bounds of the list's indices) will be a float.
(Also, you may want to rewrite this as a list comprehension;
http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions
)

> print t_amb
>
> "
> Here is everything ok.
>
> But then, I need to pass the data again to exel, so I wrote this:
>
> "
> a=8
This duplicate assignment is pointless.

> for b in range (len(t_amb)):
> a=8
> for d in t_amb[b]:

Given our earlier conclusion, we likewise know that `t_amb[b]` will be
a float (we merely replaced the arbitrary `x` with `b`). A single
float is a scalar, not a collection, so it's nonsensical to try and
iterate over it like you are in "for d in t_amb[b]:"; a number is not
a list. `t_amb[b]` is a lone number, and numbers contain no
items/elements over which to iterate. Perhaps you want just "d =
t_amb[b]" ? Remember that in a `for` loop, the expression after the
`in` (i.e. `t_amb[b]`) is evaluated only once, at the beginning of the
loop, and not repeatedly. In contrast, assuming this were a valid
`for` loop, `d` would take on different values at each iteration of
the loop.

In any case, it's rarely necessary nowadays to manually iterate over
the range of the length of a list; use `enumerate()` instead;
http://docs.python.org/2/library/functions.html#enumerate

> a=a+1
> sheet.write(a,b+1,d)
> "
>
> The error appear in "for d in t_amb[b]:" and I don't understand why. Can you 
> help me?

I hope this explanation has been sufficiently clear. If you haven't
already, you may wish to review the official Python tutorial at
http://docs.python.org/2/tutorial/index.html . You may also find it
helpful to run your program step-by-step in the interactive
interpreter/shell, printing out the values of your variables along the
way so as to understand what your program is doing.

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: iterating over a list as if it were a circular list

2013-03-07 Thread Chris Rebert
On Mar 7, 2013 1:29 AM, "Sven"  wrote:
>
> Stupid keyboard shortcuts, sent it too early. Apologies
>
>
> I was wondering what the best approach for the following might be.
>
> Say you have a list P of points and another list N of other items. You
can always assume that
>
> len(N) <= len(P)
>
> Now I would like to iterate over P and place one N at each point. However
if you run out of N I'd like to restart from N[0] and carry on until all
the points have been populated.

> Additionally, what if I wanted to pull a random element from N, but I
want to ensure all elements from N have been used before starting to pick
already chosen random elements again.
> So far I thought of duplicating the list and removing the randomly chosen
elements from the list, and when it's empty, re-copying it. But that seems
a little "wrong" if you know what I mean.

Just iterate over the list in order, and random.shuffle() the list each
time you reach the end of it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: iterating over a list as if it were a circular list

2013-03-07 Thread Chris Rebert
On Mar 7, 2013 1:24 AM, "Sven"  wrote:
>
> I was wondering what the best approach for the following might be.
>
> Say you have a list P of points and another list N of other items. You
can always assume that
>
> len(N) <= len(P)
>
> Now I would like to iterate over P and place one N at each point. However
if you run out of N I'd like to restart from N[0] and carry on until all
the points have been populated.

Untested due to the late hour:

import itertools

for p, n in itertools.izip(P, itertools.cycle(N)):
# do whatever
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unhelpful traceback

2013-03-06 Thread Chris Rebert
On Wed, Mar 6, 2013 at 10:33 PM, John Nagle  wrote:
> Here's a traceback that's not helping:

> UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in
> position 14: ordinal not in range(128)

> The program is converting some .CSV files that come packaged in .ZIP
> files.  The files are big, so rather than expanding them, they're
> read directly from the ZIP files and processed through the ZIP
> and CSV modules.

> This works for data records that are pure ASCII, but as soon as some
> non-ASCII character comes through, it fails.

I'd recommend using the `unicodecsv` package, which, unlike the std
lib `csv` module, is properly Unicode-compatible:
https://pypi.python.org/pypi/unicodecsv

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Insert comma in number?

2013-03-06 Thread Chris Rebert
On Wed, Mar 6, 2013 at 3:39 PM, eli m  wrote:
> I have a python program that accepts input and calculates the factorial of 
> that number, and i want to know if i can make it so commas get inserted in 
> the number.
> For example: instead of 1000 it would say 1,000

Use the "," (i.e. comma) format() specifier directive. See
http://docs.python.org/2/library/string.html#format-specification-mini-language
See also: http://www.python.org/dev/peps/pep-0378/

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: In win32 and linux platform, os modules has diffreent output order, is it a bug?

2013-03-01 Thread Chris Rebert
On Fri, Mar 1, 2013 at 12:43 AM, Honghe Wu  wrote:
> env: python 2.7.3
>
> 6 test files' name in a directory as below:
> 12ab  Abc  Eab  a1bc  acd  bc
>
> the following is test code:
> for root, dirs, files in os.walk(os.getcwd()):
> print files
>
> the output in win32 platform is:
> ['12ab', 'a1bc', 'Abc', 'acd', 'bc', 'Eab']
>
> but in linux is:
> ['Eab', 'acd', 'a1bc', '12ab', 'bc', 'Abc' ]
>
> they are so different. a bug?

Nope. When os.walk() fetches a listing of the contents of a directory,
it internally uses os.listdir() (or a moral equivalent thereof). The
docs for os.listdir() state that "The [returned] list is in arbitrary
order.". The order is dependent on the OS and filesystem, and likely
also more obscure factors (e.g. the order in which the files were
created). The lack of any required ordering allows for improved I/O
performance in many/most cases.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Searching for a replacement for PIL

2013-02-26 Thread Chris Rebert
On Tue, Feb 26, 2013 at 10:17 PM, Thorsten Kiefer  wrote:
> Hi,
> my actual program imports ImageTk, to generate TK compatible images.
> But it seems like PIL is no longer supported.

Have you investigated the Pillow fork?
https://pypi.python.org/pypi/Pillow/

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using urllib on a more complex site

2013-02-24 Thread Chris Rebert
On Sunday, February 24, 2013, Adam W. wrote:

> I'm trying to write a simple script to scrape
> http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day
>
> in order to send myself an email every day of the 99c movie of the day.
>
> However, using a simple command like (in Python 3.0):
> urllib.request.urlopen('
> http://www.vudu.com/movies/#tag/99centOfTheDay/99c%20Rental%20of%20the%20day').read(
> )
>
> I don't get the all the source I need, its just the navigation buttons.
>  Now I assume they are using some CSS/javascript witchcraft to load all the
> useful data later, so my question is how do I make urllib "wait" and grab
> that data as well?
>

urllib isn't a web browser. It just requests the single (in this case,
HTML) file from the given URL. It does not parse the HTML (indeed, it
doesn't care what kind of file you're dealing with); therefore, it
obviously does not retrieve the other resources linked within the document
(CSS, JS, images, etc.) nor does it run any JavaScript. So, there's nothing
to "wait" for; urllib is already doing everything it was designed to do.

Your best bet is to open the page in a web browser yourself and use the
developer tools/inspectors to watch what XHR requests the page's scripts
are making, find the one(s) that have the data you care about, and then
make those requests instead via urllib (or the `requests` 3rd-party lib, or
whatever). If the URL(s) vary, reverse-engineering the scheme used to
generate them will also be required.

Alternatively, you could use something like Selenium, which let's you drive
an actual full web browser (e.g. Firefox) from Python.

Cheers,
Chris


-- 
Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: webbrowser.open("./documentation/help.html")-- No Go in Windows

2013-02-24 Thread Chris Rebert
On Sun, Feb 24, 2013 at 12:28 PM, llanitedave  wrote:
> On Sunday, February 24, 2013 1:35:31 AM UTC-8, Chris Rebert wrote:
>> On Feb 24, 2013 1:21 AM, "llanitedave"  wrote:
>> > I created an html help page for my Python 2.7.3 application and put it in 
>> > a documentation folder.  I used webbrowser.open() to fetch the page.
>> > On linux -- KDE specifically, the command opens the local file on my 
>> > default browser with no issues.  However, on Windows 7, it opens Internet 
>> > Explorer, which doesn't even search the local folder, but goes straight to 
>> > the web and does a Google search, returning nothing but useless noise.
>> > My default browser on Windows is Chrome, so my intention is getting 
>> > undermined right from the start.
>> > How do I get a local html file to open properly from Python in Windows?
>>
>> Sounds like this might be your problem:
>> http://bugs.python.org/issue8936
>>
>> The fix would seem to be ensuring that the URL you pass includes the scheme 
>> (in your case, "file:").
>
> Holy Toledo!  That's a two-year-old bug spanning two versions of the language!
>
> BTW, Chris, the snippet I showed in the title essentially WAS the exact code.

Sorry, my bad. This is why I dislike messages that put critical info
*only* in the subject line; I tend not to reread the subject line once
I've opened the message.

>  It's a method with that single line called from a wxPython Help menu.  I 
> can't really put an absolute pathname into the argument, because the 
> application is going to be distributed to a variety of computers at my 
> workplace, and there's no assurance that it will go into (or remain in)a 
> particular folder.

As Demian demonstrated, you can simply compute the absolute path from
the relative path at runtime; although I would probably toss an
abspath() call in for good measure
(http://docs.python.org/2/library/os.path.html#os.path.abspath ).

> This to me illustrates the downside of the Python philosophy of "There should 
> be only one obvious way to do things".  If that one obvious way has a fatal 
> bug, you're pretty much SOL.

On the other hand, you don't have to investigate which of N APIs is
the "fixed"/"correct" one (Which PHP MySQL function is safe from SQL
injection again?), and you only have wait for 1 fix instead of N. But
yes, some of Python's included batteries are due for some recharging.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: webbrowser.open("./documentation/help.html")-- No Go in Windows

2013-02-24 Thread Chris Rebert
On Feb 24, 2013 1:21 AM, "llanitedave"  wrote:
>
> I created an html help page for my Python 2.7.3 application and put it in
a documentation folder.  I used webbrowser.open() to fetch the page.
>
> On linux -- KDE specifically, the command opens the local file on my
default browser with no issues.  However, on Windows 7, it opens Internet
Explorer, which doesn't even search the local folder, but goes straight to
the web and does a Google search, returning nothing but useless noise.
>
> My default browser on Windows is Chrome, so my intention is getting
undermined right from the start.
>
> How do I get a local html file to open properly from Python in Windows?

Sounds like this might be your problem:
http://bugs.python.org/issue8936

The fix would seem to be ensuring that the URL you pass includes the scheme
(in your case, "file:").

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: webbrowser.open("./documentation/help.html")-- No Go in Windows

2013-02-24 Thread Chris Rebert
On Feb 24, 2013 1:21 AM, "llanitedave"  wrote:
>
> I created an html help page for my Python 2.7.3 application and put it in
a documentation folder.  I used webbrowser.open() to fetch the page.
>
> On linux -- KDE specifically, the command opens the local file on my
default browser with no issues.  However, on Windows 7, it opens Internet
Explorer, which doesn't even search the local folder, but goes straight to
the web and does a Google search, returning nothing but useless noise.
>
> My default browser on Windows is Chrome, so my intention is getting
undermined right from the start.
>
> How do I get a local html file to open properly from Python in Windows?

Please provide the exact code snippet that you're using.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python scheduler

2013-02-20 Thread Chris Rebert
On Wed, Feb 20, 2013 at 8:04 PM, Rita  wrote:
> Hello,
>
> Here is what I am trying to do. (Currently, I am doing this in cron but i
> need much more granularity). I am trying to run program every 20 secs and
> loop forever. I have several of these types of processes, some should run
> every 5 mins, 10 secs, 20 secs, 1 min and so forth. I was wondering what is
> the best way to do this?

I've had a good experience with APScheduler
(http://pythonhosted.org/APScheduler/ ), although my particular
use-case was trivial, but it totally supports periodic scheduling.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calendar module question

2013-02-17 Thread Chris Rebert
On Saturday, February 16, 2013, Phil wrote:

> Thank you for reading this.
>
> My adventures with Python have just begun and during the few weeks I have
> tried many IDEs. The following piece of code fails under all IDEs, and the
> interpreter, except under the Wing IDE.
>
> Why would this code work under the Wing IDE and nowhere else? Could there
> be a different calendar module included with Wing?
>
> import calendar
>
> cal = calendar.prcal(2013)
> print cal
>
> Traceback (most recent call last):
>   File "calendar.py", line 1, in 
> import calendar
>   File "/home/phil/calendar.py", line 3, in 
> cal = calendar.prcal(2013)
> AttributeError: 'module' object has no attribute 'prcal'
>

You named your own script file "calendar.py". As a result, when you
did `import calendar`, due to the way Python 2.x searches for modules, it
imports your file instead of the `calendar` module in the standard library,
thus leading to the above exception. Because of this sort of problem, it
is/was considered bad practice to give a module/package the same name as
any std lib module.

However, if you are running a recent-ish version of Python, adding `from
__future__ import absolute_import` may resolve the problem. See PEP 328 for
details. Absolute imports were thankfully made the default in Python 3.


-- 
Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python trademark under attack -- the PSF needs your help

2013-02-15 Thread Chris Rebert
On Feb 15, 2013 8:13 AM, "Jason Swails"  wrote:

> I'm not offering much help here, more like wondering aloud.  Doesn't
Google (not to mention other software companies) have an interest staked in
binding the Python name with the Python language?  I can't imagine
python.co.uk staging a successful campaign against one of the best-known
companies in computers (that employs Python's creator, no less).

That very last part is actually no longer the case:
https://tech.dropbox.com/2012/12/welcome-guido/
Regardless, your general point still stands.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: call shell from python

2013-02-11 Thread Chris Rebert
On Mon, Feb 11, 2013 at 9:13 PM, contro opinion  wrote:
 import os
 os.system("i=3")
> 0
 os.system("echo $i")
>
> 0
> how can i get the value of  i?

Your example is too heavily contrived for me to give a much more
specific/useful answer than "use the `subprocess` module":
http://docs.python.org/2/library/subprocess.html#using-the-subprocess-module

Of course, if all you want to do is manipulate environment variables,
then there's `os.environ`:
http://docs.python.org/2/library/os.html#os.environ

Good luck, my pseudonymous contrarian compadre.
– Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Alternative to Apache+mod_fcgid+Flup?

2013-02-11 Thread Chris Rebert
On Feb 11, 2013 6:16 AM, "Gilles"  wrote:
>
> Hello
>
> I read this article...
>
> "Why is WSGI deployment under FASTCGI so painful?"
>
http://blog.dscpl.com.au/2011/09/why-is-wsgi-deployment-under-fastcgi-so.html
>
> ... and was wondering what better alternative is available to run
> Python web scripts?

If you want to stick with Apache, mod_wsgi is the obvious alternative.

> Someone recommends nginx + uWSGI.What do you think?

We use that combination at my workplace, and it works well.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cafebabe python macosx easter egg?

2013-02-10 Thread Chris Rebert
On Sun, Feb 10, 2013 at 10:10 PM, Rodrick Brown  wrote:
> Subject: cafebabe python macosx easter egg?
>
> $ hexdump -n4 -C $(which python) | awk '{print $2 $3 $4 $5 }'
cafebabe

~ $ # Huh. Let's google...
http://en.wikipedia.org/wiki/Hexspeak :
"0xCAFEBABE ("cafe babe") is used by Mach-O to identify Universal
object files, and by the Java programming language to identify Java
bytecode class files."


Some background:

http://en.wikipedia.org/wiki/Mach-O : "Mach-O […] is a file format for
executables […] used by most systems based on the Mach kernel" (OS X's
kernel is based on the Mach kernel.)

http://en.wikipedia.org/wiki/Universal_binary :
"A universal binary is, in Apple parlance, an executable file or
application bundle that runs natively on either PowerPC or […] Intel
64-based Macintosh computers; it is an implementation of the concept
more generally known as a fat binary."


Confirmation:

"OS X ABI Mach-O File Format Reference":
https://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/MachORuntime/Reference/reference.html
:
struct fat_header
[...]
Fields:
magic
An integer containing the value 0xCAFEBABE in big-endian byte order format.


So, there you have it. Mach-O and Java bytecode just happen to use the
same magic number. Coincidence.

Cheers,
Chris R.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Floating point calculation problem

2013-02-02 Thread Chris Rebert
On Sat, Feb 2, 2013 at 2:27 AM, Schizoid Man  wrote:
> I have a program that performs some calculations that runs perfectly on
> Python 2.7.3. However, when I try to execute it on Python 3.3.0 I get the
> following error:
>numer = math.log(s)
> TypeError: a float is required
>
> The quantity s is input with the following line: s = input("Enter s:   ")
>
> To get rid of the compile error, I can cast this as a float: s =
> float(input("Enter s:   "))

> How is
> Python dynamically typed if I need to cast (in version 3.3.0 at least) to
> get rid of the compile error?

It's *not* a compile error; it's a *runtime* error raised inside
math.log() when that function is called (with an invalid argument).
IIRC, the only compile-time error in Python is SyntaxError (and its
subclass, IndentationError).
Python is also strongly-typed, which is why, at runtime, an exception
is thrown instead of some implicit type coercion being attempted; such
coercion tends to hide genuine bugs, hence why Python avoids it.

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to use subprocess to execute an exe with args and an output

2013-01-30 Thread Chris Rebert
On Wed, Jan 30, 2013 at 9:15 AM, noydb  wrote:
> I am looking for some guidance on using subprocess to execute an EXE with 
> arguments and an output.  The below code works in that it returns a 0 exit 
> code, but no output file is created.  I have tried a few different versions 
> of this code (used Popen instead, some stderr/stdout), but no luck.  Can 
> anyone offer an explanation or suggestion?  (GPSBabel is freeware)
> Python 2.7 on windows7 64-bit
>
> import subprocess
> subprocess.call([r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
> "-i", "gdb", "-f", r"C:\Temp\GDBdata\testgps28.gdb",
> "-o", "gpx", r"C:\Temp\gpx\test28output.gpx"])

If my cursory reading of GPSBabel's documentation is right, you're
missing a "-F" before the output filepath. Try:

subprocess.call([
r"C:\Program Files (x86)\GPSBabel\gpsbabel.exe",
"-i", "gdb",
"-f", r"C:\Temp\GDBdata\testgps28.gdb",
"-o", "gpx",
"-F", r"C:\Temp\gpx\test28output.gpx",
])

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: security quirk

2013-01-29 Thread Chris Rebert
On Tue, Jan 29, 2013 at 8:55 PM, RichD  wrote:
> I read Wall Street Journal, and occasionally check
> articles on their Web site.  It's mostly free, with some items
> available to subscribers only.  It seems random, which ones
> they block, about 20%.
>
> Anywho, sometimes I use their search utility, the usual author
> or title search, and it blocks, then I look it up on Google, and
> link from there, and it loads!  ok, Web gurus, what's going on?

http://www.google.com/search?btnG=1&pws=0&q=first+click+free

BTW, this has absolutely jack squat to do with Python. Please direct
similar future inquiries to a more relevant forum.

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Split string data have ","

2013-01-29 Thread Chris Rebert
On Jan 29, 2013 9:05 AM, "moonhkt"  wrote:
>
> Hi All
>
> Python 2.6.2 on AIX 5.3
> How to using split o
>
> >>> y = '"abc.p,zip.p",a,b'
> >>> print y
> "abc.p,zip.p",a,b
> >>>
>
> >>> k= y.split(",")
> >>> print k[0]
> "abc.p
> >>>
>
> Need Result, First element is
> abc.p,zip.p

Try the csv module or the shlex module.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Increase value in hash table

2013-01-22 Thread Chris Rebert
On Jan 22, 2013 11:31 PM, "moonhkt"  wrote:
>
> Hi Al
>
> I have Data file have below
>
> Data file
> V1
> V2
> V3
> V4
> V4
> V3
>
> How to using count number of data ?
>
> Output
> V1 = 1
> V2 = 1
> V3 =2
> V4 = 2

Construct a frequency table using collections.Counter:

http://docs.python.org/2.7/library/collections.html#collections.Counter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Safely add a key to a dict only if it does not already exist?

2013-01-18 Thread Chris Rebert
On Friday, January 18, 2013, Steven D'Aprano wrote:

> I wish to add a key to a dict only if it doesn't already exist, but do it
> in a thread-safe manner.
>
> The naive code is:
>
> if key not in dict:
> dict[key] = value
>
>
> but of course there is a race condition there: it is possible that

another thread may have added the same key between the check and the
> store.
>
> How can I add a key in a thread-safe manner?
>

I'm not entirely sure, but have you investigated dict.setdefault() ?


-- 
Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: License status of pycollada?

2013-01-07 Thread Chris Rebert
On Sunday, January 6, 2013, Gene Heskett wrote:

> Greetings all;
>
> Trying to collect all the dependencies of FreeCad-0.13, but it appears that
> pycollada is behind some sort of a login/paywall on github.  Is anyone here
> familiar with how that works?
>

Er, what? The repo seems freely browseable.
Looks like it's under a standard 3-clause BSD-style license:
https://github.com/pycollada/pycollada/blob/master/COPYING


-- 
Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.path.realpath(path) bug on win7 ?

2013-01-05 Thread Chris Rebert
On Sat, Jan 5, 2013 at 10:55 PM, iMath <2281570...@qq.com> wrote:
>
> os.path.realpath(path)  bug on win7 ?
>
> Temp.link is a Symbolic link
> Its target location is C:\test\test1
> But
> >>> os.path.realpath(r'C:\Users\SAMSUNG\Temp.link\test2')
> 'C:\\Users\\SAMSUNG\\Temp.link\\test2'
>
> I thought the return value should be ' C:\\test\\test1\\test2'
>
> Is it a bug ? anyone can clear it to me ?

What does os.path.islink('C:/Users/SAMSUNG/Temp.link') report?

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Yet another attempt at a safe eval() call

2013-01-03 Thread Chris Rebert
On Thu, Jan 3, 2013 at 3:25 PM, Grant Edwards  wrote:
>
> I've written a small assembler in Python 2.[67], and it needs to
> evaluate integer-valued arithmetic expressions in the context of a
> symbol table that defines integer values for a set of names.  The
> "right" thing is probably an expression parser/evaluator using ast,
> but it looked like that would take more code that the rest of the
> assembler combined, and I've got other higher-priority tasks to get
> back to.
>
> How badly am I deluding myself with the code below?

Given http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
and similar, I suspect the answer is "a fair bit".

> def lessDangerousEval(expr):
> global symbolTable
> if 'import' in expr:
> raise ParseError("operand expressions are not allowed to contain the 
> string 'import'")
> globals = {'__builtins__': None}
> locals  = symbolTable
> return eval(expr, globals, locals)
>
> I can guarantee that symbolTable is a dict that maps a set of string
> symbol names to integer values.

Using the aformentioned article as a basis, I was able to get this
doozy working, albeit under Python 3:

$ python3
Python 3.3.0 (default, Nov  4 2012, 17:47:16)
[GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.57))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> expr = "[klass for klass in ().__class__.__bases__[0].__subclasses__() if 
>>> klass.__name__ == 
>>> 'Codec'][0].encode.__globals__['__builtins__']['__im'+'port__']('os').remove"
>>> eval(expr, {'__builtins__': None}, {})

>>>

Since the original attack was itself devised against Python 2.x, it's
highly likely that similar convoluted attacks against 2.x remain
possible, unless perhaps you were use a modified interpreter.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pylint, was Re: pygame - importing GL - very bad...

2013-01-02 Thread Chris Rebert
On Wed, Jan 2, 2013 at 10:01 PM, Ben Finney  wrote:
> Ian Kelly  writes:
>
>> On Wed, Jan 2, 2013 at 7:24 PM, someone  wrote:
>> > 1) class somethingWork: Invalid name "somethingWork" (should match
>> > [A-Z_][a-zA-Z0-9]+$), I'm not that good at regular exps, but I
>> > suppose it wants my class name to start with a capital letter ?
>>
>> Yes, PEP-8 recommends CamelCase for class names.
>
> PEP 8 discourages camelCase for names except for compatibility purposes,
> and recommends TitleCase for class names.

If we must quibble over meta-nomenclature...
http://www.python.org/dev/peps/pep-0008/ :
"""
The following naming styles are commonly distinguished:
[...]
* CapitalizedWords (or CapWords, or CamelCase -- so named because of
the bumpy look of its letters [3]). […]
* mixedCase (differs from CapitalizedWords by initial lowercase character!)
"""

The term "TitleCase" does not make an appearance in the document.

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Handling Special characters in python

2013-01-02 Thread Chris Rebert
On Wed, Jan 2, 2013 at 5:39 AM,   wrote:
> On Wednesday, January 2, 2013 12:02:34 PM UTC+5:30, Chris Rebert wrote:
>> On Jan 1, 2013 8:48 PM,  wrote:
>> > On Wednesday, January 2, 2013 12:00:06 AM UTC+5:30, Chris Rebert wrote:
>> > > On Jan 1, 2013 3:41 AM,  wrote:
>> > > > I am facing one issue in my module. I am gathering data from sql 
>> > > > server database. In the data that I got from db contains special 
>> > > > characters like "endash". Python was taking it as "\x96". I require 
>> > > > the same character(endash). How can I perform that. Can you please 
>> > > > help me in resolving this issue.
>>
>> > > 1. What library are you using to access the database?
>> > > 2. To confirm, it's a Microsoft SQL Server database?
>> > > 3. What OS are you on?
>>
>> > 1. I am using "pymssql" module to access the database.
>> > 2. Yes, It is a SQL server database.
>> > 3. I am on Ubuntu 11.10
>>
>> Did you set "client charset" (to "UTF-8", unless you have good reason to 
>> choose otherwise) in freetds.conf? That should at least ensure that the 
>> driver itself is exchanging bytestrings via a well-defined encoding.
>> If you want to work in Unicode natively (Recommended), you'll probably need 
>> to ensure that the columns are of type NVARCHAR as opposed to VARCHAR. 
>> Unless you're using SQLAlchemy or similar (which I personally would 
>> recommend using), you may need to do the .encode() and .decode()-ing 
>> manually, using the charset you specified in freetds.conf.
>>
>> Sorry my advice is a tad general. I went the alternative route of SQLAlchemy 
>> + PyODBC + Microsoft's SQL Server ODBC driver for Linux 
>> (http://www.microsoft.com/en-us/download/details.aspx?id=28160 ) for my 
>> current project, which likewise needs to fetch data from MS SQL to an Ubuntu 
>> box. The driver is intended for Red Hat and isn't packaged nicely (it 
>> installs via a shell script), but after that was dealt with, things have 
>> gone smoothly. Unicode, in particular, seems to work properly.
>
> Thanks Chris Rebert for your suggestion, I tried with PyODBC module, But at 
> the place of "en dash(-)", I am getting '?' symbol. How can I overcome this.

I would recommend first trying the advice in the initial part of my
response rather than the latter part. The latter part was more for
completeness and for the sake of the archives, although I can give
more details on its approach if you insist.

Additionally, giving more information as to what exactly you tried
would be helpful. What config / connection settings did you use? Of
what datatype is the relevant  column of the table? What's your code
snippet look like? Etc..

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Handling Special characters in python

2013-01-01 Thread Chris Rebert
On Jan 1, 2013 8:48 PM,  wrote:
> On Wednesday, January 2, 2013 12:00:06 AM UTC+5:30, Chris Rebert wrote:
> > On Jan 1, 2013 3:41 AM,  wrote:
> >
> > > I am facing one issue in my module. I am gathering data from sql
server database. In the data that I got from db contains special characters
like "endash". Python was taking it as "\x96". I require the same
character(endash). How can I perform that. Can you please help me in
resolving this issue.
> >
> > 1. What library are you using to access the database?
> > 2. To confirm, it's a Microsoft SQL Server database?
> > 3. What OS are you on?
>
> 1. I am using "pymssql" module to access the database.
> 2. Yes, It is a SQL server database.
> 3. I am on Ubuntu 11.10

Did you set "client charset" (to "UTF-8", unless you have good reason to
choose otherwise) in freetds.conf? That should at least ensure that the
driver itself is exchanging bytestrings via a well-defined encoding.
If you want to work in Unicode natively (Recommended), you'll probably need
to ensure that the columns are of type NVARCHAR as opposed to VARCHAR.
Unless you're using SQLAlchemy or similar (which I personally would
recommend using), you may need to do the .encode() and .decode()-ing
manually, using the charset you specified in freetds.conf.

Sorry my advice is a tad general. I went the alternative route of
SQLAlchemy + PyODBC + Microsoft's SQL Server ODBC driver for Linux (
http://www.microsoft.com/en-us/download/details.aspx?id=28160 ) for my
current project, which likewise needs to fetch data from MS SQL to an
Ubuntu box. The driver is intended for Red Hat and isn't packaged nicely
(it installs via a shell script), but after that was dealt with, things
have gone smoothly. Unicode, in particular, seems to work properly.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Handling Special characters in python

2013-01-01 Thread Chris Rebert
On Jan 1, 2013 3:41 AM,  wrote:
>
> I am facing one issue in my module. I am gathering data from sql server
database. In the data that I got from db contains special characters like
"endash". Python was taking it as "\x96". I require the same
character(endash). How can I perform that. Can you please help me in
resolving this issue.

1. What library are you using to access the database?
2. To confirm, it's a Microsoft SQL Server database?
3. What OS are you on?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: father class name

2012-12-31 Thread Chris Rebert
On Mon, Dec 31, 2012 at 1:23 AM, Ben Finney  wrote:
> Chris Rebert  writes:
>
>> By contrast, in the first part of the *expression*
>> `haha(object).theprint()`, you passed an argument (namely, `object`).
>> Since __init__() wasn't expecting any arguments whatsoever, you
>> therefore got an error.
>
> Why is everyone talking about the initialiser, ‘__init__’?
>
> When:
>
>> >>>> haha(object).theprint()
>> > Traceback (most recent call last):
>> >   File "", line 1, in 
>> > TypeError: object.__new__() takes no parameters
>
> The error is talking about the constructor, ‘__new__’.

Because the difference between the two (and indeed, the very purpose
of the latter) is a topic of intermediate/advanced difficulty, and the
OP appears to be a newbie.
As I stated, but your quotation omitted:
>> Note: I'm oversimplifying things a bit for the sake of understandability.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: father class name

2012-12-30 Thread Chris Rebert
On Sun, Dec 30, 2012 at 8:18 PM, contro opinion  wrote:
> here is my haha  class
> class  haha(object):
>   def  theprint(self):
> print "i am here"
>
 haha().theprint()
> i am here
 haha(object).theprint()
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: object.__new__() takes no parameters
>
> why   haha(object).theprint()  get wrong output?

The fact that `haha(object)` is textually part of the *declaration*
`class haha(object):` has no bearing on how one instantiates an
instance of the class `haha`.
In the `class` statement, `haha` is being declared to be a subclass of
class `object` (that's what it means for `object` to be in the
parentheses after the class name in a `class` statement; the syntax is
"class ():").

In the first part of the *expression* `haha().theprint()`, you are
using the function-call operator on the `haha` class itself, which has
the effect of instantiating it; since you gave no arguments in the
function call, haha's initializer (i.e. its __init__() method) was
given no arguments. Since you didn't define an __init__() method for
haha, haha inherited the default __init__() method from class
`object`, which takes no arguments, so your call was fine and worked
as expected.

By contrast, in the first part of the *expression*
`haha(object).theprint()`, you passed an argument (namely, `object`).
Since __init__() wasn't expecting any arguments whatsoever, you
therefore got an error.

The parentheses in a `class` statement do NOT signify a function call;
they are part of the syntax of the `class` statement itself.

Cheers,
Chris
--
Note: I'm oversimplifying things a bit for the sake of understandability.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: class problem

2012-12-30 Thread Chris Rebert
On Sun, Dec 30, 2012 at 10:36 PM, contro opinion  wrote:
> here is my haha  class
> class  haha(object):
>   def  theprint(self):
> print "i am here"
>
 haha().theprint()
> i am here
 haha(object).theprint()
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: object.__new__() takes no parameters
>
> why   haha(object).theprint()  get wrong output?

This marks the third time today that you've posted *this exact same
question* to python-list AKA comp.lang.python.
You received multiple answers the first time you posted it. Please
desist from posting it any further, lest ye get plonk-ed.
If you did not understand the responses you obtained, please reply in
the original thread and ask for clarification, rather than re-posting.

– Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get the source of html in lxml?

2012-12-30 Thread Chris Rebert
On Sun, Dec 30, 2012 at 10:32 PM, contro opinion  wrote:
> import urllib
> import lxml.html
> down='http://blog.sina.com.cn/s/blog_71f3890901017hof.html'
> file=urllib.urlopen(down).read()
> root=lxml.html.document_fromstring(file)
> body=root.xpath('//div[@class="articalContent  "]')[0]
> print body.text_content()
>
> When i run the code, what i get is the text content ,how can i get the html
> source code of it?

print lxml.html.tostring(body)

Did you read through the lxml.html documentation?
http://lxml.de/lxmlhtml.html
It includes several examples that make use of lxml.html.tostring().

RTFineM-ly Yours,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: email.message.Message - as_string fails

2012-12-28 Thread Chris Rebert
On Dec 28, 2012 4:26 AM, "Helmut Jarausch" 
wrote:
>
> Hi,
>
> I'm trying to filter an mbox file by removing some messages.
> For that I use
> Parser= FeedParser(policy=policy.SMTP)
> and 'feed' any lines to it.
> If the mbox file contains a white line followed by '^From ',
> I do
>
> Msg= Parser.close()
>
> (lateron I delete the Parser and create a new one by
> Parser= FeedParser(policy=policy.SMTP)
> )
>
> I can access parts of the message by  Msg['Message-ID'], e.g.
> but even for the very first message, trying to print it or convert it to
a string
> by  MsgStr=Msg.as_string(unixfrom=True)
>
> lets Python (3.3.1_pre20121209) die with
>
> Traceback (most recent call last):
>   File "Email_Parse.py", line 35, in 
> MsgStr=Msg.as_string(unixfrom=True)
>   File "/usr/lib64/python3.3/email/message.py", line 151, in as_string
> g.flatten(self, unixfrom=unixfrom)
>   File "/usr/lib64/python3.3/email/generator.py", line 112, in flatten
> self._write(msg)
>   File "/usr/lib64/python3.3/email/generator.py", line 171, in _write
> self._write_headers(msg)
>   File "/usr/lib64/python3.3/email/generator.py", line 198, in
_write_headers
> self.write(self.policy.fold(h, v))
>   File "/usr/lib64/python3.3/email/policy.py", line 153, in fold
> return self._fold(name, value, refold_binary=True)
>   File "/usr/lib64/python3.3/email/policy.py", line 176, in _fold
> (len(lines[0])+len(name)+2 > maxlen or
> IndexError: list index out of range
>
>
> What am I missing?

Perhaps the message is malformed. What does Msg.defects give you?

Could you post the line strings you fed to the parser that together
constitute the first message (redacted if necessary)?

P.S. Your naming conventions (with respect to capitalization) disagree with
those of Python.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a function while defining it

2012-12-27 Thread Chris Rebert
On Dec 26, 2012 11:55 PM, "Abhas Bhattacharya" 
wrote:
>
> On Thursday, 27 December 2012 10:22:15 UTC+5:30, Tim Roberts  wrote:
> > Abhas Bhattacharya  wrote:
> >
[Oh god please stop/avoid using Google Groups with its godawful
reply-quoting style that adds excessive blank lines]
> > >While I am defining a function, how can I access the name (separately
as
> > >string as well as object) of the function without explicitly naming
> > >it(hard-coding the name)?
> > >For eg. I am writing like:
> >
> > >def abc():
> > >#how do i access the function abc here without hard-coding the
name?
> >
> > Why?  Of what value would that be?

> Because I have this situation:
> I have used a dictionary with "function_name":value pair in the top of
the code. Now when some function is called, I need to print the value
assigned to its name in the dictionary (the functions are defined after the
dictionary). Now there is only one bad way-around for me: I need to
hard-code the name in the function like this:
> def function_name():
> print(dict_name.get("function_name"))
> but ofcourse it is a bad thing to do because I have a lot of this type of
 functions. It would be better if I can can use the same code for all of
them, because they are all essentially doing the same thing.

I agree with the general outline of Mitya's suggestion, i.e. refactor the
"print the associated value" step into a separate function, thus obviating
the self-reference issue; it'd be bad to repeat that code in each function
anyway.

Anyhow, here's a simple variation that exploits decorators (because they're
generally awesome & one of my favorite features):

def printing_name_beforehand(func):
def wrapper(*args, **kwargs):
print(the_dict.get(func.__name__))
return func(*args, **kwargs)
return wrapper

Usage:

@printing_name_beforehand
def some_func(...):
# whatever

(Forgive me if there are typos; composing this reply on a tablet is
cumbersome.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a function while defining it

2012-12-27 Thread Chris Rebert
On Dec 25, 2012 6:06 PM, "Abhas Bhattacharya" 
wrote:
>
> While I am defining a function, how can I access the name (separately as
string as well as object) of the function without explicitly naming
it(hard-coding the name)?
> For eg. I am writing like:
> def abc():
> #how do i access the function abc here without hard-coding the name?

Not possible per se without resorting to sys._getframe() or similar hackery.
A simple+elegant way to do this would require PEP 3130 (
http://www.python.org/dev/peps/pep-3130/ ) or similar, but that particular
proposal got rejected.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get time.strptime()?

2012-12-26 Thread Chris Rebert
On Wednesday, December 26, 2012, Gnarlodious wrote:

> Error: AttributeError: 'module' object has no attribute '_strptime'
>

 Please include the full Traceback, not just the final error message.

This problem is driving me crazy. It only happens in Python 3.3.0, while on
> my server running 3.1.3 it behaves as expected. When I try to access
> time.strptime() it errors with
>
> AttributeError: 'module' object has no attribute '_strptime'.


Might be the wrong `time` module. What's `print time.__file__` output?
Did you perchance create a time.py file in your project?

This error only occurs under mod_wsgi, when running as a one-shot webapp it
> behaves normally. All other functionalities of the time module are normal.
>
> If anyone could explain why it thinks I want an underscored name maybe it
> would help.
>
> Thanks.
>
> -- Gnarlie
> --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pygnomevfs get_local_path_from_uri replacement

2012-12-22 Thread Chris Rebert
On Sat, Dec 22, 2012 at 12:57 AM, Daniel Fetchinson
 wrote:
> Hi folks, I realize this is slightly off topic and maybe belongs to a
> gnome email list but it's nevertheless python:
>
> I use an old python program that was written for gnome 2 and gtk 2 and
> uses the function get_local_path_from_uri. More specifically it uses
> gnomevfs.get_local_path_from_uri.
>
> Now with gnome 3 the module pygnomevfs does not exist anymore and
> after checking the source for pygnomevfs it turns out it's written in
> C using all the header files and stuff from gnome 2. So I can't just
> lift it from the source. I was hoping it's pure python in which case I
> could have simply lifted it.
>
> Does anyone know what a good replacement for get_local_path_from_uri
> is? Is there a gtk/gnome/etc related python package that contains it
> which would work with gnome 3? Or a totally gnome-independent python
> implementation?

The commit https://mail.gnome.org/archives/commits-list/2009-May/msg05733.html
suggests that get_local_path_from_uri() might have been defined as
(taking slight liberties):
gnome_vfs_unescape_string(remove_host_from_uri(uri))
Assuming these functions do the "obvious" things implied by their
names (you can probably chase down the Gnome VFS source or docs to
check; I don't care enough to bother), given a general URI
"protocol://host/path", it presumably returns either
"protocol:///path" (`protocol:` likely being "file:" in this case) or
"/path", in either case with `path` having been un-percent-escaped.
The latter transform can be done using
http://docs.python.org/2/library/urllib.html#urllib.unquote

Alternately, you might call the Gnome VFS C API directly via
http://docs.python.org/2/library/ctypes.html

Merry Solstice,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pexpect and buffering

2012-12-15 Thread Chris Rebert
On Dec 15, 2012 4:51 AM,  wrote:
>
> Hello,
>
> I'm trying to use pexpect to grab interactions with Python's REPL.  I am
having trouble with tracebacks.  Possibly it is related to buffering (hence
the subject line) but I admit that's a guess.

Why are you doing this in the first place? Why invoke an external Python
shell when you're in a Python program to begin with? Seems terribly
unnecessarily roundabout.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: JSON logging ?

2012-12-12 Thread Chris Rebert
On Dec 11, 2012 7:33 AM, "Bart Thate"  wrote:

> pickle uses eval still ? or is is considered safe now ? i was told not to
use eval() stuff on data.

I don't believe pickle uses eval() per se, but per the red warning box in
its docs, it's still not safe when given untrusted input. IIRC, among other
things, in order to unpickle non-built-in classes, it is capable of
performing imports; this feature is rife for abuse by an adversary.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is __ne__ method autogenerated?

2012-12-06 Thread Chris Rebert
On Thursday, December 6, 2012, INADA Naoki wrote:

> The reference says:
>
>   The truth of x==y does not imply that x!=y is false.
>   Accordingly, when defining __eq__(), one should also
>   define __ne__() so that the operators will behave as expected.
>
> (http://docs.python.org/3/reference/datamodel.html#object.__eq__)
>
> But I saw different behavior on 3.3:
> https://gist.github.com/4231096
>
> Could anyone teach me what happen about my code?
>

The reference is not completely accurate in this case. See
 http://bugs.python.org/issue4395
"Document auto __ne__ generation; [...]"


-- 
Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to pass "echo t | " input to subprocess.check_output() method

2012-11-26 Thread Chris Rebert
On Nov 26, 2012 3:03 AM, "Kushal Kumaran" 
wrote:
> dacha...@gmail.com writes:
> > I want to list the repositories in svn using python. For this i have
used below command,
> > " res = subprocess.check_output(["svn.exe", "list", "
Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) "
> >
> > but it throws an exception, since it requires an user input to validate
certificate,
> > " (R)eject, accept (t)emporarily or accept (p)ermanently? "
> >
> > from Command prompt im able to pass the input while calling the
process, and im able to get the output
> >
> > "echo t | svn list Https://127.0.0.1:443/svn/Repos"
> >
> > But i dont know how to pass the "echo t | " in subprocess.check_output
while calling a process.
>
> You could pass in a stdin argument to subprocess.check_output with a
> value of 't\n'.

Strings aren't acceptable stdin values, so that wouldn't work.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to pass "echo t | " input to subprocess.check_output() method

2012-11-26 Thread Chris Rebert
On Nov 26, 2012 2:41 AM,  wrote:
>
> Hi all,
>
> I want to list the repositories in svn using python. For this i have used
below command,
> " res = subprocess.check_output(["svn.exe", "list", "
Https://127.0.0.1:443/svn/Repos"], stderr=subprocess.STDOUT) "
>
> but it throws an exception, since it requires an user input to validate
certificate,
> " (R)eject, accept (t)emporarily or accept (p)ermanently? "
>
> from Command prompt im able to pass the input while calling the process,
and im able to get the output
>
> "echo t | svn list Https://127.0.0.1:443/svn/Repos"
>
> But i dont know how to pass the "echo t | " in subprocess.check_output
while calling a process.
> Is there a way to do this?

Use subprocess.Popen.communicate() instead, passing "t\n" as the input.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mysql insert with tuple

2012-11-21 Thread Chris Rebert
On Wed, Nov 21, 2012 at 9:19 AM, Christian  wrote:
> Hi ,
>
> my purpose is a generic insert via  tuple , because the number of fields and 
> can differ. But  I'm stucking .
>
> ilist=['hello',None,7,None,None]
>
> #This version works, but all varchar fields are in extra '' enclosed.
> con.execute(""" INSERT INTO {} VALUES %r; """.format(table) , (tuple(ilist),))

A. "%r" is not a valid SQL/MySQLdb parameter specification (nor part
of Python's format() mini-language).
B. You don't need to enclose `ilist` in a singleton tuple. (Or convert
it to a tuple, for that matter.)

This should work:
# assuming `table` isn't obtained from untrusted input...
paramspec = ",".join(["%s"] * len(ilist))
con.execute("""INSERT INTO {} VALUES {};""".format(table, paramspec) , ilist)

But really, I would recommend instead using a more abstract database
library (e.g. SQLAlchemy, SQLObject, etc.), which safely and
conveniently constructs the SQL strings for you.

> #This produce (1054, "Unknown column 'None' in 'field list'"),
> #but without None values it works.
> con.execute(""" INSERT INTO {} VALUES %r; """.format(table) % (tuple(ilist),))

This is an SQL injection (http://en.wikipedia.org/wiki/SQL_injection )
waiting to happen!

Regards,
Chris

P.S. Where's my mining fact? ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Web Frameworks Excessive Complexity

2012-11-21 Thread Chris Rebert
On Wed, Nov 21, 2012 at 10:57 AM, rh  wrote:
> On Wed, 21 Nov 2012 10:12:26 -0800
> Chris Rebert  wrote:
>> On Wed, Nov 21, 2012 at 9:49 AM, rh 
>> wrote:
>> > On Tue, 20 Nov 2012 20:41:42 +0300
>> > Andriy Kornatskyy  wrote:

>> > I'm looking at different technology right now on which to base a
>> > site. I tried pyramid and after install it consumed 92MB of disk.
>> > It seemed large and it turns out that it installed its own version
>> > of python. Seems more complex to me, yet another python on disk.
>>
>> That's how virtualenvs (http://www.virtualenv.org/ ) normally work.
>> Not really Pyramid's fault, it's more a deficiency of the current
>> Python package management tools.
>
> There's still 92MB under pyramid, I just installed a new virtualenv and
> installed wheezy.web, grand total 3.3MB.
>
> What deficiency?

"install[ing] its own version of python"

-- Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Constructing JSON data structures from non-string key python dictionaries

2012-11-21 Thread Chris Rebert
On Wed, Nov 21, 2012 at 7:48 AM, MRAB  wrote:
> On 2012-11-21 14:59, saikari78 wrote:
>>
>> Hi,
>>
>> I'm using the json module to  create a JSON string, then inserting that
>> string into a html template containing a javascript function (from the
>> highcharts library: http://www.highcharts.com/)

Nontrivial templating of JavaScript is generally a bad/inelegant
approach. I would instead suggest generating the JSON separately and
loading it from JavaScript via $.getJSON or similar. Or sticking the
JSON into a hidden part of the webpage and then using JSON.parse().

>> The json string I'm trying to create is to initialize a data variable in
>> the javascript function, that has the following example format.
>>
>>  data = [{
>>  y: 55.11,
>>  color: colors[0],
>>  drilldown: {
>>  name: 'MSIE versions',
>>  categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0',
>> 'MSIE 9.0'],
>>  data: [10.85, 7.35, 33.06, 2.81],
>>  color: colors[0]
>>  }
>>  }]
>>
>> However, I don't know how to do that because dictionary keys in python
>> need to be strings. If I try to do the following, Python,of course,
>> complains that y,color,drilldown, etc are not defined.
>>
>>
>> import json
>>
>> data = [ { y:55.11, color:colors[0], drilldown:{name: 'MSIE
>> versions',categories: ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],data:
>> [10.85, 7.35, 33.06, 2.81],color: colors[0] }} ]
>>
>> data_string = json.dumps(data)
>>
>>
>> Many thanks for any suggestions on how to do this.
>>
> Just quote them:
>
>
> data = [ { 'y':55.11, 'color':colors[0], 'drilldown':{'name': 'MSIE
> versions','categories': ['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE
> 9.0'],'data': [10.85, 7.35, 33.06, 2.81],'color': colors[0] }} ]
>
> Incidentally, dictionary keys in Python don't have to be strings, but
> merely 'hashable', which includes integers, floats and tuples amongst
> others.

On Wed, Nov 21, 2012 at 8:04 AM,   wrote:
> Thanks for your reply, but the javascript function expects option names to be 
> unquoted, otherwise it won't work.

As a user of HighCharts (and thus, unfortunately, JavaScript), I can
tell you that that's absolutely incorrect.
In JavaScript, {x : y}, {"x" : y}, and {'x' : y} are all equivalent
(at least when x is a valid JavaScript identifier; consult some
non-w3schools JavaScript docs).
Plus, you say you're using JSON; JSON *explicitly mandates that the
keys be quoted* (see RFC 4627).

You are experiencing Python NameErrors because {"x" : y} and {x : y}
aren't equivalent in Python. Python doesn't limit dicts keys to
strings, so `x` is a variable in the latter snippet; x's value is used
as the key.
You cannot expect to take arbitrary, unmodified JavaScript
code/literals, copy-paste them into Python, and expect them to work.


TL;DR:
# foo.py
from json import dumps

colors = SOME_LIST

data = [dict( # use dict() to avoid tedious quoting
y=55.11,
color=colors[0],
drilldown=dict(
name='MSIE versions',
categories=['MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0', 'MSIE 9.0'],
data=[10.85, 7.35, 33.06, 2.81],
color=colors[0],
)
)]

your_json = dumps(data)
# ...serve the JSON somehow...

// bar.js
// Not industrial-strength. Assumes the use of jQuery.
// ...
$.getJSON(SOME_URL, function (data) {
// use 'data', which will be a JavaScript object by this point
});
// ...


Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Web Frameworks Excessive Complexity

2012-11-21 Thread Chris Rebert
On Wed, Nov 21, 2012 at 9:49 AM, rh  wrote:
> On Tue, 20 Nov 2012 20:41:42 +0300
> Andriy Kornatskyy  wrote:
>> Cyclomatic (or conditional) complexity is a metric used to indicate
>> the complexity of a source code. Excessive complexity is something
>> that is beyond recommended level of 10 (threshold that points to the
>> fact the source code is too complex and refactoring is suggested).
>> Here is a list of web frameworks examined: bottle, cherrypy,
>> circuits, django, flask, pyramid, pysi, tornado, turbogears, web.py,
>> web2py and wheezy.web.
>>
>> You can read more here:
>>
>> http://mindref.blogspot.com/2012/11/python-web-excessive-complexity.html
>
> You are the author of wheezy.web right? Can't blame you for trying to
> market your product. The conclusions, or lack of, are meaningless to me.
> I have to get in and drive the car before I go all in and buy it.
>
> I'm looking at different technology right now on which to base a site.
> I tried pyramid and after install it consumed 92MB of disk. It seemed
> large and it turns out that it installed its own version of python.
> Seems more complex to me, yet another python on disk.

That's how virtualenvs (http://www.virtualenv.org/ ) normally work.
Not really Pyramid's fault, it's more a deficiency of the current
Python package management tools.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Splitting a line while keeping quoted items together

2012-11-19 Thread Chris Rebert
On Monday, November 19, 2012, wrote:

> I am working on a cmd.Cmd-based program, and normally could just split the
> string and get the right parts.
>
> Now I have a case where I could have two or three words in the string that
> need to be grouped into the same thing.
>
> Then I realized that I'm not the only person who has had to deal with
> this, and I'm wondering if my solution is the best one out there or if this
> is as ugly at it feels?
>
> Code below
> ...
>
> #x('Seattle 456') -> ('Seattle', '456')
> #x('"Portland Alpha" 123') -> ('Portland Alpha', '123')
> #x("'Portland Beta' 789') -> ('Portland Beta', '789')
>
>  

This seem really ugly. Is there a cleaner way to do this? Is there a
> keyword I could search by to find something nicer?
>

Use the "shlex" module in the std lib?

Cheers,
Chris


-- 
Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem with subprocess.call and windows schtasks

2012-11-18 Thread Chris Rebert
On Sun, Nov 18, 2012 at 5:48 AM, Tom Borkin  wrote:
> Hi,
> I have this code:
>
> #!\Python27\python
>
> import subprocess
> #subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program
> Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True)
> subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
> /ST 07:50'], shell=True)
> With either call, I get this error:
> C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
> The system cannot find the path specified.
>
> If I remove the ", shell=True" I get this:
> C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
>  C:\Program Files\Apache Group\Apache2\htdocs\ccc\cron_alert_activity.py,
> line 4, in 
>   subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
> /ST 07:50'])
>  File "C:\Python27\lib\subprocess.py", line 493, in call
>   return Popen(*popenargs, **kwargs).wait()
>  File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread,
> errwrite)
>  File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
> startupinfo)
> WindowsError: [Error 2] The system cannot find the file specified
> The file exists in said directory. I can execute it from the cmd prompt.

Per the docs 
(http://docs.python.org/2/library/subprocess.html#frequently-used-arguments
):
"If passing a single string [as the `args` argument], either `shell`
must be True (see below) or else the string must simply name the
program to be executed **without specifying any arguments.**"
(emphasis mine)

> So I tried this:
> pgm = "SchTasks"
> args = ['/Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50']
> #args = ['/Create', '/SC ONCE', '/TN "test"', '/TR "run_alert.py"', '/ST
> 07:50']
> cmd = [pgm]
> cmd.extend(args)
> subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
> but got this error:
> ERROR: Invalid argument/option - <>
>
> If I use the other args list I get this error:
> ERROR: Invalid argument/option - '/SC ONCE'
> so apparently it liked the first argument.
>
> Please advise.

Your tokenization of your command is incorrect. Consult the Note box
in the docs regarding `args` tokenization, and apply it to your
command:
http://docs.python.org/2/library/subprocess.html#subprocess.Popen

The-docs-are-your-friend-ly Yours,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Executing .exe on a remote Windows machine

2012-11-08 Thread Chris Rebert
On Thursday, November 8, 2012, Kevin Holleran wrote:

> On Thu, Nov 8, 2012 at 9:43 AM, Kevin Holleran 
> 
> > wrote:
>
>> My goodness psexec.
>>
>> thanks can't believe that didn't come to me...
>>
>>
>>
>>
>> On Thu, Nov 8, 2012 at 9:31 AM, Tim Golden 
>> 
>> > wrote:
>>
>>> On 08/11/2012 14:25, Kevin Holleran wrote:
>>> > Good morning,
>>> >
>>> > I wrote a python script to connect out to a bunch of my remote machines
>>> > that are running some software.  It modifies a bunch of the config
>>> files
>>> > for me.  After making the changes, I need to restart the software.  The
>>> > way to do this is to call an .exe passing in a argument 'restart'
>>> >  Simply restarting services is NOT acceptable & rebooting the machine
>>> > isn't either.
>>> >
>>> > I was trying to find a way to simply call the .exe on the remote
>>> machine
>>> > with subprocess but how can I get it to execute on the remote machine?
>>> >  These machines do not have SSH.
>>>
>>> WMI can usually help with this (although there are limitations on what
>>> you can execute via WMI). Also people recommend sysinternals' psexec.
>>> (I've never tried it myself).
>>>
>>> TJG
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>>
>>
>>
>
> OK, not quite resolved yet
>
> My code
>
> [code]
> try:
> print("Attempting to restart Splunk...")
> subprocess.call(["psexec", "" + host, "'c:\\Program
> Files\\Splunk\\bin\\splunk.exe'", "restart"])
> [/code]
>
> & am getting:
>
> [output]
> Attempting to restart Splunk...
>
> PsExec v1.98 - Execute processes remotely
> Copyright (C) 2001-2010 Mark Russinovich
> Sysinternals - www.sysinternals.com
>
>
> PsExec could not start 'c:\Program Files\Splunk\bin\splunk.exe' restart on
> [IP_ADDRESS]:
> The filename, directory name, or volume label syntax is incorrect.
> [/output]
>
> I am simply trying to restart the splunk forwarder instance
>
> Any thoughts??
>

Remove the apostrophes surrounding the path to Splunk's executable. The
subprocess module already takes care of the quoting for you, so the
apostrophes are unnecessary and are being interpreted literally.


-- 
Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multi-dimensional list initialization

2012-11-04 Thread Chris Rebert
On Sun, Nov 4, 2012 at 10:27 PM, Demian Brecht  wrote:
> So, here I was thinking "oh, this is a nice, easy way to initialize a 4D 
> matrix" (running 2.7.3, non-core libs not allowed):
>
> m = [[None] * 4] * 4
>
> The way to get what I was after was:
>
> m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]]
>
> (Obviously, I could have just hardcoded the initialization, but I'm too lazy 
> to type all that out ;))
>
> The behaviour I encountered seems a little contradictory to me.
> [None] * 4 creates four distinct elements in a single array
> while [[None] * 4] * 4 creates one distinct array of four distinct elements, 
> with three references to it:

Incorrect. In /both/ cases, the result is a list of length 4, whose
elements are 4 (references to) the exact same object as the original
list's element.
Put simply, the list multiplication operator never copies objects; it
just makes additional references to them.

However, unlike a list object (as in your latter example), the object
`None` is completely immutable (and what's more, a singleton value),
so you just-so-happen *not to be able to* run into the same problem of
mutating an object (assignment to an index of a list constitutes
mutation of that list) that is referenced in multiple places, for you
cannot mutate None in the first place!:
>>> x = None
>>> x.a = 42
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'NoneType' object has no attribute 'a'
>>> # it doesn't overload any mutating operators:
>>> type(None).__dict__.keys()
['__hash__', '__repr__', '__doc__']
>>> # and it obviously has no instance variables,
>>> # so, we can't modify it in any way whatsoever!
(Lists, on the other hand, define item assignment, .pop(), .remove(),
and a few other mutator methods.)

 a = [None] * 4
 a[0] = 'a'
 a
> ['a', None, None, None]
>
 m = [[None] * 4] * 4
 m[0][0] = 'm'
 m
> [['m', None, None, None], ['m', None, None, None], ['m', None, None, None], 
> ['m', None, None, None]]
>
> Is this expected behavior

Yes. It's also a FAQ:
http://docs.python.org/2/faq/programming.html#how-do-i-create-a-multidimensional-list

> and if so, why?

It's a general (albeit AFAIK unstated) principle that Python never
copies objects unless you explicitly ask it to. You have encountered
one example of this rule in action.

> In my mind either result makes sense, but the inconsistency is what throws me 
> off.

It is perfectly consistent, once you understand what list
multiplication actually does.

Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python-forum

2012-11-02 Thread Chris Rebert
On Fri, Nov 2, 2012 at 1:19 AM, Sacha Rook  wrote:
> Hi does anyone know where the python-form.org site has gone?

Some googling suggests that it's under new management:
http://mcompute.co.uk/showthread.php?tid=2161

But comp.lang.python/python-list is better anyway [ ;-) ], and you're
already here, so why not stay a while?

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Negative array indicies and slice()

2012-10-29 Thread Chris Rebert
On Mon, Oct 29, 2012 at 1:24 AM,   wrote:
> On Sunday, October 28, 2012 9:44:56 PM UTC-7, alex23 wrote:
>> On Oct 29, 2:09 pm, Andrew  wrote:

>> class RangedSlicer(list):

>> Then wrap your lists with your RangedSlicer class as needed.
>
> Hmmm...
>
> I began a test in an interactive shell:
 class RangedSlicer(list):
> ... def __getitem__(self,item):
> ... print item
> …

This just defines a class; it doesn't modify in-place the normal
behavior of plain lists. You have to actually *use* the class.

 a=[1,2,3,4,5]

You never wrapped `a` in a RangedSlicer or otherwise made use of RangedSlicer!
You wanted:
a = RangedSlicer([1,2,3,4,5])

 a.__getitem__( slice(1,5) )
> [2, 3, 4, 5]
>
> Very odd...  I would have expected [1,2,3,4]

"[2, 3, 4, 5]" is the return value from `a.__getitem__( slice(1,5) )`
(or, equivalently, from `[1,2,3,4,5][1:5]`). It is not the result of
"print item"; that line of code is never executed since you never used
the RangedSlicer class at all.

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Negative array indicies and slice()

2012-10-29 Thread Chris Rebert
On Mon, Oct 29, 2012 at 1:08 AM,   wrote:
> On Sunday, October 28, 2012 10:14:03 PM UTC-7, Paul Rubin wrote:
>> Andrew writes:

> I'm getting very frustrated with the editor provided for this group... It 
> keeps posting prematurely, and putting my email in even when I tell it not to 
> each time; and there is no way to edit a post... but deleting is ok...

This is a Usenet newsgroup[1], not a web forum. There are noteworthy
differences between the two.
FWICT, you happen to be accessing us via Google Groups, which is
widely acknowledged to suck. We are not hosted *by* Google Groups;
they just happen to carry our posts.
Personally, I'd suggest using our mailing list mirror instead:
http://mail.python.org/mailman/listinfo/python-list
Or use some other, better newsgroup provider that also carries us.

[1]: http://en.wikipedia.org/wiki/Usenet

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Negative array indicies and slice()

2012-10-29 Thread Chris Rebert
On Mon, Oct 29, 2012 at 12:54 AM, Andrew  wrote:
> On Sunday, October 28, 2012 9:26:01 PM UTC-7, Ian wrote:
>> On Sun, Oct 28, 2012 at 10:00 PM,  Andrew wrote:

> The slice class when passed to __getitem__()  was created to merely pass two 
> numbers and a stride to __getitem__;  As far as I know slice() itself does 
> *nothing* in the actual processing of the elements.  So, it's *redundant* 
> functionality, and far worse, it's restrictive.
>
> The philosophy of Python is to have exactly one way to do something when 
> possible; so, why create a stand alone class that does nothing an existing 
> class could already do, and do it better ?
>
> A simple list of three values would be just as efficient as slice()!
> xrange is more flexible, and can be just as efficient.
>
> So, Have I misunderstood the operation of slice()?  I think I might have... 
> but I don't know.

`slice` is intentionally lenient about the types of the start, stop, and step:
>>> class Foo:
... def __getitem__(self, slice_):
... print(slice_)
... return 42
...
>>> Foo()["a":"b":"c"]
slice('a', 'b', 'c')
42
>>>
Thus, the thing being sliced is free to interpret the parts of the
slice however it wishes; hence, slice() is unable to contain the
"processing" you speak of.
By contrast, xrange() limits itself to integers.
To support the more general case, the slice syntax thus produces a
`slice` rather than an `xrange`.
Doubtlessly, there are also historical issues involved. As implied by
the ugliness of its name, `xrange` was added to the language
relatively later.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Numpy module

2012-10-28 Thread Chris Rebert
On Sun, Oct 28, 2012 at 10:40 PM,   wrote:
> I've learned a lot about Ubuntu just trying to install numpy for Python 
> 3.2.3. I've finally managed to put it in the Python3.2 directory but when I 
> try to import it, I still get there's "no module named numpy." There are 
> other modules in the same directory, like 'email' and it imports fine.

A. It properly belongs under "site-packages"
B. You ought to just install it using pip
(http://www.pip-installer.org ) or apt-get, rather than manually.

> Does Numpy 1.6.2 not run with Python 3.2.3?

They are compatible.
http://scipy.github.com/faq.html#do-numpy-and-scipy-support-python-3-x :
"The first release of NumPy to support Python 3 was NumPy 1.5.0."

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python question for some

2012-10-28 Thread Chris Rebert
On Sun, Oct 28, 2012 at 4:51 PM, Mark L. Hotz  wrote:
> I have what I think should be a relatively simple question for someone who
> is knowledgeable about Python.
>
> At the IDLE prompt, when I enter “b” > 99, it responds True. In fact, it
> doesn’t matter which number is entered here, “b” is always greater (e.g. “b”
>> 1 == True; “b” > 10 == True, or “b” < 99 = False).
>
> Why is this true?

Per http://docs.python.org/2/library/stdtypes.html#comparisons :
"Objects of different types, except different numeric types and
different string types, […] are ordered consistently but arbitrarily
(so that sorting a heterogeneous array yields a consistent result)."
Note that the "except" part just means that, e.g. floats and ints can
be compared with each other, and Unicode and byte strings can be
compared with each other. It does NOT mean that numbers and strings
can be meaningfully compared with each other.

This is fixed in Python 3, where such nonsensical comparisons will
instead raise TypeError.

>  If I use ord(“b”) it returns 98, so Python cannot be
> using the ASCII or Unicode value when interpreting “b” > 99.

It has nothing to do with implicit casting between strings and numbers
(which, as a general rule, Python does not do).

From the same linked section as before:
"CPython implementation detail: Objects of [incompatible types] are
ordered by their type names"

So ints come before strs because "int" comes before "str" lexicographically.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help understanding an Object Oriented Program example

2012-10-28 Thread Chris Rebert
On Sun, Oct 28, 2012 at 4:30 PM, goldtech  wrote:
> Hi,
>
> Trying to learn Python OOP. An example from a book, may not be
> formated after sending post but:
>
> class Contact:
> all_contacts = []
> def __init__(self, name, email):
> self.name = name
> self.email = email
> Contact.all_contacts.append(self)
>
> OK, no I do this:
>
 c = Contact('aaa','bbb')
 c = Contact('ccc','ddd')
 c = Contact('eee','fff')
 for i in Contact.all_contacts:
> print i.name + '  ' + i.email
>
>
> aaa  bbb
> ccc  ddd
> eee  fff

 c.name
> 'eee'
>
> So wouldn't be good to add a check that the var (in this case c) does
> not point to any object before creating an object to keep the list
> correct?

I'm unclear on how the list would become "incorrect" or exactly what
sort of check you're thinking of. Please explain what you mean in
greater detail.
Keep in mind that checking for the "definedness" of variables in
Python is generally considered bad and is often infeasible.

> Also all_contacts is a class variable. I think the author is hinting
> that this would be a good idea for a contact list, But I don't fully
> see the usage of it.

I would think he just wants to demonstrate the use of class variables
as opposed to instance variables. It's probably not a good idea for a
serious contact list implementation. But the general technique to
allow a class to keep track of all its instances can sometimes be
useful (e.g. for caching).

> How would each object use a class variable like
> this? What would be the dot notation?

All of the following would work:
Contact.all_contacts  # as in the example
self.__class__.all_contacts
self.all_contacts  # probably not advisable

Which one you ought to use becomes complicated when you consider the
general case where there may be sub/superclasses, where you may want
to rebind the variable, and where there may be an instance variable of
the same name.
Class variables are generally used quite infrequently compared to
regular instance variables.

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fastest data / database format for reading large files

2012-10-28 Thread Chris Rebert
On Tue, Oct 16, 2012 at 11:35 AM, Pradipto Banerjee
 wrote:
> I am working with a series of large files with sizes 4 to 10GB and may need 
> to read these files repeated. What data format (i.e. pickle, json, csv, etc.) 
> is considered the fastest for reading via python?

Pickle /ought/ to be fastest, since it's binary (unless you use the
oldest protocol version) and native to Python. Be sure to specify
HIGHEST_PROTOCOL and use cPickle.
http://docs.python.org/2/library/pickle.html#module-cPickle
http://docs.python.org/2/library/pickle.html#pickle.HIGHEST_PROTOCOL

You might consider using SQLite (or some other database) if you will
be doing queries over the data that would be amenable to SQL or
similar.
http://docs.python.org/2/library/sqlite3.html

Cheers,
Chris

P.S. The verbose disclaimer at the end of your emails is kinda annoying...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: turn list of letters into an array of integers

2012-10-24 Thread Chris Rebert
On Wed, Oct 24, 2012 at 9:27 PM, seektime  wrote:
> On Tuesday, October 23, 2012 11:07:29 PM UTC-7, Chris Rebert wrote:

>> P.S.: I'm guessing you obtained `L` from file.readlines() or similar;
>> it is worth noting for future reference that the readlines() method is
>> considered somewhat deprecated.
>
> Thanks to everyone lots of great comments are actionable suggestions.
>
> My intension is to used the numpy/scipy packages to solve the task at hand. I 
> agree that there's no point in loading a file into a format which only needs 
> to be converted right after loading. But I'm new to Python the f.readline(s) 
> command, according to the 2.7.3 tutorial and manual, is pretty much all there 
> is for file i/o. If, as you indicated, f.readlines() is deprecated then what 
> should I use instead? I'm using ver. 2.6 on Linux (it's a bit dated, I know).

Just iterate over the file directly using a for-loop (e.g. `for line
in some_file:`). Each iteration yields one line of the file. I used a
very minor variation of this approach in my code (a list comprehension
is just syntax sugar for a for-loop).

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: turn list of letters into an array of integers

2012-10-23 Thread Chris Rebert
On Tue, Oct 23, 2012 at 10:23 PM, seektime  wrote:
> Here's some example code. The input is a list which is a "matrix" of letters:
>a  b  a
>b  b  a
>
> and I'd like to turn this into a Python array:

You mean a Python list. The datatype Python calls an `array` is very
different and relatively uncommonly used.
Although, confusingly, Python's lists are implemented using C arrays
rather than linked lists.

>   1 2 1
>   2 2 1
>
> so 1 replaces a, and 2 replaces b. Here's the code I have so far:
>
 L=['a b a\n','b b a\n']

 seq
> '1 2 1\n 2 2 1\n'
>
> My question is how can I turn "seq" into a python array?

I'd say you're asking the wrong question. The better question is "Why
wasn't the result a list in the first place?". Many transformations
are cumbersome to express over just strings, which is why the first
job of most programs is to parse their input into a more convenient
structure that is suited to their main task(s).

This (along with some other improvements) leads to a better, somewhat
different program/algorithm:

letter2number = {'a': 1, 'b': 2}
with open("path/to/file.txt", "r") as f:
result = [[letter2number[letter] for letter in
line.strip().split()] for line in f]

If it's safe to assume that the correspondence between the letters and
numbers isn't completely arbitrary, some further improvements are also
possible.

Some relevant docs:
http://docs.python.org/library/stdtypes.html#string-methods
http://docs.python.org/tutorial/datastructures.html#list-comprehensions

Cheers,
Chris

P.S.: I'm guessing you obtained `L` from file.readlines() or similar;
it is worth noting for future reference that the readlines() method is
considered somewhat deprecated.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Remove uncide notation

2012-10-18 Thread Chris Rebert
On Thu, Oct 18, 2012 at 2:27 AM, Ashish Jain  wrote:
> Hi,
>
> I have a html string in an object, when I do repr() of that object, I get 
> value as:
>
> {'Id' : 1, 'Body': u' Hello '}
>
> I don't wish to have 'u' as the character in my string representation. As 
> this is not a valid json notation now.

If you want JSON, then *use the freakin' `json` std lib module*!
http://docs.python.org/library/json.html

repr(...) != JSON
[It's similar only coincidentally, and only to a degree.]

Regards,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: simple string format question

2012-10-15 Thread Chris Rebert
On Mon, Oct 15, 2012 at 5:12 AM, Neal Becker  wrote:
> Is there a way to specify to format I want a floating point written with no 
> more
> than e.g., 2 digits after the decimal?  I tried {:.2f}, but then I get all
> floats written with 2 digits, even if they are 0:
>
> 2.35 << yes, that's what I want
> 2.00 << no, I want just 2 or 2.

Not that I can find. Seems you'll have to implement it yourself.


In the event that your project uses Django, there happens to be a
template tag for this (pass it -2 in your case):
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#floatformat

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's the tidy/elegant way to protect this against null/empty parameters?

2012-10-15 Thread Chris Rebert
On Mon, Oct 15, 2012 at 4:23 AM,   wrote:
> I want to fix an error in some code I have installed, however I don't
> really want to just bodge it.

"bodge". Well, I learned a new word this morning!

> The function producing the error is:-
>
> def get_text(self, idx):   # override !
> node = self.items[idx]
>
> a= [
> ", ".join(node.tags),
> node.comment,
> node.folderName,
> cd2rd(node.date),
> node.name,
> '[' + self.rating_stars[node.rating] + ']'
> ] [self.select]
>
> return a
>
>
> The error occurs when node[] (or at least its members) turn out to be
> empty,

To be precise: when node.tags contains one or more `None`s (Python's
equivalent of what other languages call "null" or "nil").
That's what the traceback is saying.

> you get a Traceback that ends with:-
>
>   File "/usr/lib/jbrout/jbrout/listview.py", line 608, in draw_cell 
> layout.set_text(self.get_text(thumbnail_num))

Ah, so this is apparently regarding https://code.google.com/p/jbrout/
. Would have been nice not to have had to search and then only locate
it indirectly. Something to consider next time you write in...
Make sure you report your bug upstream!

>   File "/usr/lib/jbrout/jbrout.py", line 325, in get_text ", 
> ".join(node.tags),
>   TypeError: sequence item 0: expected string, NoneType found
>
> Now its *probably* something higher up the tree causing the problem
> (it's only one particular image in 20 thousand or so that breaks
> things) but I really want to just get things working.  So, what's the
> neatest way to protect the get_text() method from empty data?

Filter out the `None`s with a generator expression:
", ".join(tag for tag in node.tags if tag is not None),

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: trouble with nested closures: one of my variables is missing...

2012-10-13 Thread Chris Rebert
On Saturday, October 13, 2012, Cameron Simpson wrote:

> I'm having some trouble with closures when defining a decorator.



> However, I can't make my make_file_property function work. I've stripped
> the code down and it does this:



>   Traceback (most recent call last):
> File "foo.py", line 21, in 
>   def f(self, foo=1):
> File "foo.py", line 4, in file_property
>   return make_file_property()(func)
> File "foo.py", line 10, in made_file_property
>   if attr_name is None:
>   UnboundLocalError: local variable 'attr_name' referenced before
> assignment
>
> Observe above that 'unset_object' is in locals(), but not 'attr_name'.
> This surprises me.
>
> The stripped back code (missing the internals of the file property
> watcher) looks like this:
>
>   import sys
>
>   def file_property(func):
> return make_file_property()(func)
>
>   def make_file_property(attr_name=None, unset_object=None, poll_rate=1):
> print >>sys.stderr, "make_file_property(attr_name=%r, unset_object=%r,
> poll_rate=%r): locals()=%r" % (attr_name, unset_object, poll_rate,locals())
> def made_file_property(func):


You're missing a "nonlocal" declaration here.

  print >>sys.stderr, "made_file_property(func=%r): locals()=%r" %
> (func, locals())
>   if attr_name is None:
> attr_name = '_' + func.__name__


 You assign to it, but there's no nonlocal declaration, so Python thinks
it's a local var, hence your error.

Pardon my brevity and some lack of trimming; I'm on a smartphone and in a
rush.

- Chris

  lock_name = attr_name + '_lock'
>   def getprop(self):
> with getattr(self, lock_name):
>   # innards removed here
>   pass
> return getattr(self, attr_name, unset_object)
>   return property(getprop)
> return made_file_property
>
>   @file_property
>   def f(self, foo=1):
> print "foo=%r" % (foo,)
>
>   @make_file_property(attr_name="_blah")
>   def f2(self, foo=2):
> print "foo=%r" % (foo,)
>
> Can someone explain what I'm doing wrong, or tell me this is a python
> bug?
> --
> Cameron Simpson >
>
> Bolts get me through times of no courage better than courage gets me
> through times of no bolts!
> - Eric Hirst >
> --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
Cheers,
Chris
--
http://rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   8   9   10   >