Re: Source code as text/plain
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: serialize a class to XML and back
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: Piping processes works with 'shell = True' but not otherwise.
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: detect key conflict in a JSON file
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.
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: Finding Relative Maxima in Python3
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: Split a list into two parts based on a filter?
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: Popen and reading stdout in windows
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: Newbie: The philosophy behind list indexes
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: Looking for a name for a deployment framework...
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: How to print a number as if in the python interpreter?
On Fri, Jul 6, 2012 at 3:38 PM, Peng Yu wrote:
> Hi,
>
> In [2]: sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
> Out[2]: 0.
>
> In ipython, I got the above output. But I got a different output from
> "print". Is there a way to print exact what I saw in ipython?
>
> ~/linux/test/python/man/library/math/fsum$ cat main.py
> #!/usr/bin/env python
> print sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
> ~/linux/test/python/man/library/math/fsum$ ./main.py
> 1.0
chris@mbp ~ $ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = sum(0.1 for i in range(10))
>>> x # the interpreter implicitly repr()s the result of an expression
0.
>>> print x # whereas `print` str()s its operands
1.0
>>> (str(x), repr(x)) # as proof and for clarity
('1.0', '0.')
Beware the subtleties of floating-point arithmetic!
http://docs.python.org/tutorial/floatingpoint.html
Cheers,
Chris
--
http://mail.python.org/mailman/listinfo/python-list
Re: something go wrongly
On Sat, Jul 7, 2012 at 10:23 AM, levi nie wrote: > my code: > > aList=[1,2,3,4,5,6,7,8,9,10] > xList=[1,2,3] > print "now aList is",aList.extend(xList) > > output: > now aList is None > > what i want is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3] See http://stackoverflow.com/a/1682601 list.extend(), list.append(), etc. operate in-place, mutating the existing list object. Such methods return None to emphasize that they do not create new lists. Regards, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: help needed with subprocess, pipes and parameters
On Friday, July 13, 2012, nuffi wrote:
>
> If I copy and paste the following command into a command window, it does
> what I need.
>
> c:\Programs\bob\bob.exe -x -y "C:\text\path\to some\file.txt" |
> c:\Programs\kate\kate.exe -A 2 --dc "Print Media Is Dead" --da "Author"
> --dt "Title" --hf "Times" --bb "14" --aa "" --font "Ariel" -
> "C:\rtf\path\to some\file.rtf"
>
> My mission is to recreate this command within a python script, so that I
> can pass a bunch of different parameters into it, and use it as a batch
> over a bunch of different papers.
>
>
> The code I came up with looks like this:
>
> import os, glob, subprocess
>
> sourceDir = "c:\\text\\"
> destDir = "c:\\rtf\\"
> bobPath = "C:\\Programs\\bob\\bob.exe"
> katePath = "C:\\Programs\\kate\\kate.exe"
>
> def get_new_path(doc):
> blah = doc.replace(sourceDir, destDir)
> if not os.path.isdir(blah):
> os.makedirs(blah)
> rtf = blah.replace('.txt', '.rtf')
> pathString = '- "' + (os.path.join(rtf)) + '"'
> return(pathString)
>
>
> def convert_doc(doc):
> dc = '--dc "Print Media Is Dead"'
> da = '--da "Author"'
> dt = '--dt "Title"'
> hf = '--hf "Times"'
> fn = '--font "Ariel"'
> bb = '--bb "14"'
> docpath = '"' + (os.path.join(doc)) + '"'
> path = get_new_path(doc)
> A = '-A 2'
> bob = subprocess.Popen([bobPath, '-x', '-y', docpath], stdout =
> subprocess.PIPE,)
> kate = subprocess.Popen([katePath, A, dc, da, dt, hf, fn, bb, path],
> stdin = bob.stdout, stdout = subprocess.PIPE,)
Your tokenization of the command is wrong. Read the Note box in the
subprocess docs for guidance on how to get it right:
http://docs.python.org/library/subprocess.html#popen-constructor
Cheers,
Chris via iPhone
--
Cheers,
Chris
--
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list
Re: Diagramming code
On Sun, Jul 15, 2012 at 6:26 PM, hamilton wrote: > Subject: Diagramming code > > Is there any software to help understand python code ? What sort of diagrams? Control flow diagrams? Class diagrams? Sequence diagrams? Module dependency diagrams? There are many different types you could be referring to. Here's a relatively comprehensive list: http://en.wikipedia.org/wiki/Unified_Modeling_Language#Diagrams_overview Regards, Chris -- UML: Kill it with fire! -- http://mail.python.org/mailman/listinfo/python-list
Re: Diagramming code
On Sun, Jul 15, 2012 at 6:57 PM, hamilton wrote: > On 7/15/2012 7:38 PM, Chris Rebert wrote: >> >> On Sun, Jul 15, 2012 at 6:26 PM, hamilton wrote: >>> >>> Subject: Diagramming code >>> >>> Is there any software to help understand python code ? >> >> What sort of diagrams? Control flow diagrams? Class diagrams? Sequence >> diagrams? Module dependency diagrams? There are many different types >> you could be referring to. Here's a relatively comprehensive list: >> http://en.wikipedia.org/wiki/Unified_Modeling_Language#Diagrams_overview >> >> Regards, >> Chris >> -- >> UML: Kill it with fire! > > OK then, let me ask, how do you guys learn/understand large projects ? In case you're responding to my trailing semi-satirical comment, let me clarify: I was remarking on UML specifically, not software-related diagrams in general. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: ssl: add msg_callback
On Wed, Jul 25, 2012 at 8:47 PM, Thiébaud Weksteen wrote: > > Hi python-list, > > I wrote a patch for Python 3.2.3 to expose the function > SSL_CTX_set_msg_callback in the module _ssl. > > I was actually surprise this function was not already in the > standard library as it is really handy: Well, the underscore in the module name indicates that such modules are private implementation details, hence why _ssl is undocumented. > "SSL_CTX_set_msg_callback() or SSL_set_msg_callback() can be used > to define a message callback function cb for observing all SSL/TLS > protocol messages (such as handshake messages) that are received or sent." > > Here is the patch: > > https://github.com/tweksteen/abrupt-usecase/blob/master/python-ssl-3.2.3.patch > > Let me know your opinion on that. Does it worth being included? python-dev would probably be the best place to ask, since they have the ultimate say on whether to accept your patch: http://mail.python.org/mailman/listinfo/python-dev/ and/or you can file a bug in the tracker with your patch attached: http://bugs.python.org/ On the face of it, the feature sounds fairly useful for debugging. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: keyerror '__repr__'
On Sat, Aug 4, 2012 at 7:48 AM, vijay shanker wrote:
> hi
> i have this class book
>
> class book:
> def __init__(self,name,price):
> self.name = name
> self.price = price
>
> def __getattr__(self,attr):
> if attr == '__str__':
> print 'intercepting in-built method call '
> return '%s:%s' %
> (object.__getattribute__(self,'name'),object.__getattribute___(self,'price'))
> else:
> return self.__dict__[attr]
>
b = book('the tempest',234)
b
> Traceback (most recent call last):
> File "", line 1, in
> File "", line 11, in __getattr__
> KeyError: '__repr__'
>
> i am missing on a concept here. please enlighten me.
A. You ought to be subclassing the `object` class so that your class
is new-style (see
http://docs.python.org/reference/datamodel.html#new-style-and-classic-classes
); "classic" classes are deprecated. Incidentally, you can't intercept
special method lookups on new-style classes like you do in your code
snippet (see
http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes
). You'll need to define actual __repr__() and/or __str__() methods.
B. The interactive interpreter uses repr(), rather than str(), to
stringify results.
$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
>>> class Foo(object):
... def __str__(self): return "bar"
... def __repr__(self): return "qux"
...
>>> Foo()
qux
>>>
See http://docs.python.org/reference/datamodel.html#object.__repr__
Cheers,
Chris
--
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list
Re: Arithmetic with Boolean values
On Sat, Aug 11, 2012 at 3:30 PM, John Ladasky
wrote:
> for x in range(1 + not(len(L) % 2)):
> # Do stuff
>
> This provoked a SyntaxError. I investigated this further with my interpreter
> (ipython).
> In [5]: not(1)
> Out[5]: False
>
> In [6]: not(len(L) % 2)
> Out[6]: False
>
> In [7]: 1 + not(len(L) % 2)
>
>File "", line 1
> 1 + not(len(L) % 2)
>^
> SyntaxError: invalid syntax
> Why is using a logical "not" function, as shown in [7], returning a different
> result than the test for equivalency as shown in [10]?
Note that, in Python, `not` is an unary operator (it's a language
keyword in fact), as opposed to a function:
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
>>> len("abc")
3
>>> # functions are first-class entities in Python, so we can reference them
>>> len
>>> not(1)
False
>>> # but `not` isn't a function
>>> not
File "", line 1
not
^
SyntaxError: invalid syntax
>>> # hence why we don't need to call it
>>> not 1
False
The parentheses in `not(foo)` are just grouping the `foo`
subexpression; they're not indicating a function call. Therefore, in
idiomatic Python, such parentheses are omitted whenever possible
(which is the majority of the time), and when they are absolutely
necessary, a space is included before the opening paren, because we're
not making a function call.
Thus, you're simply running into an operator precedence issue, which,
as Chris A. explained, can be remedied by adding grouping parens
around the entire `not` expression; e.g. `(not foo)`
Cheers,
Chris R.
--
http://mail.python.org/mailman/listinfo/python-list
Re: it's really strange.how does it work?
On Tue, Aug 14, 2012 at 10:07 PM, levi nie wrote: > ok,what does "start, stop = 0, start" in the code mean? > it's really strange.how does it work? It's just parallel assignment (http://en.wikipedia.org/wiki/Assignment_%28computer_science%29#Parallel_assignment ). As to exactly how it works: http://docs.python.org/reference/simple_stmts.html#assignment-statements : "If the target [of the assignment] is a comma-separated list: The [value being stored] must be an iterable with the same number of items as there are targets in the target list, and the items are assigned, from left to right, to the corresponding targets." [not a completely direct quote] Tuples are iterable (e.g. we can write `for item in some_tuple:`; in laymen's terms, it's similar to being a sequence). Recall that commas, and not parentheses, are what create tuples according to Python's syntax: $ python Python 2.7.2 (default, Jun 20 2012, 16:23:33) Type "help", "copyright", "credits" or "license" for more information. >>> x = 1,2 >>> x (1, 2) >>> type(x) >>> So, your code snippet creates an anonymous temporary tuple of length 2 [i.e. (0, start) ], and the assignment then unpacks that tuple into the 2 respective variables. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie ``print`` question
On Sun, Sep 2, 2012 at 10:23 AM, gwhite wrote: > I can't figure out how to stop the "add a space at the beginning" > behavior of the print function. > print 1,;print 2, > 1 2 > > See the space in between the 1 and the 2 at the output print to the > command console? > > The help for print is: > > "A space is written before each object is (converted and) written, > unless the output system believes it is positioned at the beginning of > a line." > > So it is apparently doing what it is supposed to do. > > Is there a way to stop this? If you were to use Python 3.x, yes. Otherwise, no. > Or is there a different function that > will only print what you have in the formatted string? Use the .write() method of the sys.stdout file object. http://docs.python.org/library/sys.html#sys.stdout Alternatively, you can stick with `print` and rework your code so that it outputs an entire line at a time (thus, there'd only be 1 argument passed to `print`, so its "spaces between arguments" feature wouldn't come into play). Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie ``print`` question
On Sun, Sep 2, 2012 at 1:24 PM, Mark Lawrence wrote: > On 02/09/2012 20:58, me wrote: >> >> Well you can convert the ints to str then concatenate them. >> >> print "1" + "2" >> > > Please post other parts of the thread so people can get the context or don't > bother posting at all, thanks. Please also adjust your mail client so that your "From" is more informative and less confusing than simply "me". Regards, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: get the matched regular expression position in string.
On Mon, Sep 3, 2012 at 1:18 AM, contro opinion wrote:
> Subject: get the matched regular expression position in string.
As is often the case in Python, string methods suffice. Particularly
for something so simple, regexes aren't necessary.
> Here is a string :
>
> str1="ha,hihi,a,ok"
>
> I want to get the position of "," in the str1,Which can count 3,8,14.
Note that Python's indices start at 0. Your example output uses 1-based indices.
> how can I get it in python ?
def find_all(string, substr):
start = 0
while True:
try:
pos = string.index(substr, start)
except ValueError:
break
else:
yield pos
start = pos + 1
str1 = "ha,hihi,a,ok"
print list(find_all(str1, ",")) #===> [2, 7, 13]
In the future, I recommend reading the documentation for Python's string type:
http://docs.python.org/library/stdtypes.html#string-methods
What opinion are you contrary ("contro") to anyway?
Regards,
Chris
--
http://mail.python.org/mailman/listinfo/python-list
Re: subprocess call is not waiting.
On Thu, Sep 13, 2012 at 11:36 AM, wrote:
> Thanks, guys.
> MRAB-RedHat 6 64-bit, Python 2.6.5
In your Unix shell, what does the command:
type htar
output?
> JM-Here's the relevant stuff from my last try.
If you could give a complete, self-contained example, it would assist
us in troubleshooting your problem.
> I've also tried with subprocess.call. Just now I tried shell=True, but it
> made no difference.
It's possible that htar uses some trickery to determine whether it's
being invoked from a terminal or by another program, and changes its
behavior accordingly, although I could not find any evidence of that
based on scanning its manpage.
> sticking a print(out) in there just prints a blank line in between each
> iteration. It's not until the 5 trials are finished that I am told: download
> failed, etc.
>
> from os.path import exists
> from subprocess import call
> from subprocess import Popen
> from shlex import split
> from time import sleep
>
> while (exists(file)==0) and (nTries < 5):
`file` is the name of a built-in type in Python; it should therefore
not be used as a variable name.
Also, one would normally write that as:
while not exists(file) and nTries < 5:
>a = Popen(split('htar -xvf ' + htarArgs), stdout=PIPE, stderr=PIPE)
What's the value of `htarArgs`? (with any sensitive parts anonymized)
Also, you really shouldn't use shlex.split() at run-time like that.
Unless `htarArgs` is already quoted/escaped, you'll get bad results
for many inputs. Use shlex.split() once at the interactive interpreter
to figure out the general form of the tokenization, then use the
static result in your program as a template.
>(out,err) = a.communicate()
>if exists(file)==0:
> nTries += 1
> sleep(0.5)
>
> if exists(file)==0: # now that the file should be moved
>print('download failed: ' + file)
>return 1
>
> I've also tried using shell=True with popopen.
I presume you meant Popen.
Cheers,
Chris
--
http://mail.python.org/mailman/listinfo/python-list
Re: subprocess call is not waiting.
On Thu, Sep 13, 2012 at 8:17 AM, wrote: > I have a subprocess.call > But it doesn't work as intended. > Should I just go back to os.system? Did the os.system() version work? As of recent Python versions, os.system() is itself implemented using the `subprocess` module, so if it does work, then it assuredly can be made to work using the `subprocess` module instead. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: subprocess call is not waiting.
On Fri, Sep 14, 2012 at 5:22 AM, wrote:
> os.system worked fine, and I found something in another section of code that
> was causing the "Too many open errors." (I was fooled, because output from
> subprocess call didn't seem to be coming out until the open files error.
>
> I'll go back and play with subprocess.call more, since os.system works.
> That's interesting about using shlex at run time. Is that just for the sake
> of computational cost?
No, like I said, you'll also get incorrect results. shlex isn't magic.
If the exact command line it's given wouldn't work in the shell, then
it won't magically fix things. Many (most?) dynamic invocations of
shlex.split() are naive and flawed:
>>> import shlex
>>> filename = "my summer vacation.txt"
>>> # the following error is less obvious when the command is more complex
>>> # (and when the filename isn't hardcoded)
>>> cmd = "cat " + filename
>>> shlex.split(cmd)
['cat', 'my', 'summer', 'vacation.txt']
>>> # that's wrong; the entire filename should be a single list element
Equivalent bash error:
chris@mbp ~ $ cat my summer vacation.txt
cat: my: No such file or directory
cat: summer: No such file or directory
cat: vacation.txt: No such file or directory
The right way, in bash:
chris@mbp ~ $ cat my\ summer\ vacation.txt
Last summer, I interned at a tech company and...
chris@mbp ~ $ cat 'my summer vacation.txt'
Last summer, I interned at a tech company and…
And indeed, shlex will get that right too:
>>> shlex.split("cat my\ summer\ vacation.txt")
['cat', 'my summer vacation.txt']
>>> shlex.split("cat 'my summer vacation.txt'")
['cat', 'my summer vacation.txt']
BUT that presumes that your filenames are already pre-quoted or have
had backslashes added, which very seldom is the case in reality. So,
you can either find an escaping function and hope you never forget to
invoke it (cf. SQL injection), or you can figure out the general
tokenization and let `subprocess` handle the rest (cf. prepared
statements):
>>> split('cat examplesimplefilename')
['cat', 'examplesimplefilename']
>>> # Therefore…
>>> def do_cat(filename):
... cmd = ['cat', filename] # less trivial cases would be more interesting
... call(cmd)
...
>>> filename = "my summer vacation.txt"
>>> # remember that those quotes are Python literal syntax and aren't in the
>>> string itself
>>> print filename
my summer vacation.txt
>>> do_cat(filename)
Last summer, I interned at a tech company and…
>>>
Generally, use (a) deliberately simple test filename(s) with shlex,
then take the resulting list and replace the filename(s) with (a)
variable(s).
Or, just figure out the tokenization without recourse to shlex; it's
not difficult in most cases!
The Note in the Popen docs covers some common tokenization mistakes people make:
http://docs.python.org/library/subprocess.html#subprocess.Popen
Cheers,
Chris
--
http://mail.python.org/mailman/listinfo/python-list
Re: datetime issue
On Sat, Sep 15, 2012 at 10:33 AM, Νικόλαος Κούρας wrote: > Hello again, > > one small matter too. > > # get some enviromental values > locale.setlocale(locale.LC_ALL, 'el_GR') > date = datetime.datetime.now().strftime( '%y-%m-%d %H:%M:%S' ) > > although iam setting greek as locale Locales don't affect timezones. Otherwise, how would expatriate communities, or countries wide enough to span several timezones, properly configure their software? > the time is 8 hours before, like in texas, us Which is where HostGator operates out of. > How can i change this to save the correct Greek time in variable $date ? Use the `pytz` package that Jason pointed out. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: 'indent'ing Python in windows bat
On Mon, Sep 17, 2012 at 8:28 PM, Dennis Lee Bieber wrote: > On Mon, 17 Sep 2012 21:08:32 -0400, David Smith > declaimed the following in gmane.comp.python.general: > >> >> How do I "indent" if I have something like: >> if (sR=='Cope'): sys.exit(1) elif (sR=='Perform') sys.exit(2) else >> sys.exit(3) >> > If the sole purpose of the BAT file is to set return codes, I > wouldn't bother using Python... Python is /not/ a shell language > substitute FWIW, it kinda was, for the Amoeba OS, originally: http://docs.python.org/faq/general#why-was-python-created-in-the-first-place Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Passing arguments to & executing, a python script on a remote machine from a python script on local machine (using ssh ?)
On Wed, Sep 19, 2012 at 12:50 PM, ashish wrote:
> 2. I have a python script, local.py, running on 'local' which needs to pass
> arguments ( 3/4 string arguments, containing whitespaces like spaces, etc )
> to a python script, remote.py running on 'remote' (the remote machine).
> 3. Has anybody been able to do this using os.system ?
>
> I tried this
import os
os.system ("ssh remoteuser@remote python remote.py arg1 arg2 arg3")
>
> This worked, but if the arguments i tried to pass, had spaces, i was not able
> to 'escape' the spaces.
Use the `subprocess` module instead (with shell=False). You then won't
need to worry about escaping.
http://docs.python.org/library/subprocess.html
Cheers,
Chris
--
http://mail.python.org/mailman/listinfo/python-list
Re: python idioms : some are confusing
On Thu, Sep 20, 2012 at 10:34 PM, Vineet wrote: > Amongst the python idioms, how the below-mentioned make sense? These aren't idioms (that term has a specific technical meaning in programming); they're *way* too abstract to be idioms. "Design principles" or "design guidelines" would be a better description. > ## There should be one-- and preferably only one --obvious way to do it. > Although that way may not be obvious at first unless you're Dutch. > > --- In programming, there can be a number of ways, equally efficient, to do > certain thing. Yes, but that brings with it the cost of having to understand/learn them all, because you'll encounter them when reading/maintaining/modifying others' code. And you'll have to evaluate them all to choose which one you should use (which might even vary from moment to moment depending on the circumstances). And you'll have to watch out for subtle variants that actually do something significantly different. Better to keep things simple in the X% of cases where the differences don't matter enough, and save those brain cycles for other, more important things. See also: the so-called "paradox of choice". Further reading: the criticisms on http://c2.com/cgi/wiki?ThereIsMoreThanOneWayToDoIt > ## Although never is often better than *right* now. > > --- How come "never" is better that "right now" ? Because "right now" is so quick that it was likely hastily hacked together and thus of poor (or at least lesser) quality. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Functional way to compare things inside a list
On Fri, Sep 21, 2012 at 1:23 AM, Chris Angelico wrote: > On Fri, Sep 21, 2012 at 8:58 AM, wrote: > That gets the result, but probably not in the cleanest way. I'm not > sure off-hand if Python has a convenient way to curry a function, http://docs.python.org/library/functools.html#functools.partial Cheers, Chris of the Northern Hemisphere -- http://mail.python.org/mailman/listinfo/python-list
Re: Functional way to compare things inside a list
On Thu, Sep 20, 2012 at 3:58 PM, wrote:
> Hi,
>
> list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
Are the dictionaries each guaranteed to only contain a single
key-value pair? (Or is your example just simplistic?)
> I want to check for a value (e.g. '4'), and get the key of the dictionary
> that contains that value.
And what if there is no such dictionary? Or what if there are multiple
such dictionaries?
> (Yep, this is bizarre.)
>
> some_magic(list, '4')
> => '3'
>
> What's the functional way to do it?
Why do you care about the paradigm used?
> Is it possible to do it with a one-liner?
Who cares? It's possible to implement more complicated things in one
line of APL, but most people probably wouldn't recommend it.
Regards,
Chris R.
--
http://mail.python.org/mailman/listinfo/python-list
Re: For Counter Variable
On Tue, Sep 25, 2012 at 2:46 AM, Mark Lawrence wrote: > On 25/09/2012 10:32, [email protected] wrote: >> >> I wrote my first program on a PDP-8. I discovered Python >> at release 1.5.? >> >> Now years later... I find Python more and more unusable. >> I'm toying more and more with the go language. I really >> appreciate and rediscover the strictness I learned with >> Pascal. > > So go and use go as nobody here is stopping you. Well, the PSU might, except they emphatically do not exist... Cheers, Chris -- PEP-401 compliant -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pass FILE *
On Fri, Sep 28, 2012 at 2:55 PM, xDog Walker wrote: > > The function I am trying to call wants a FILE *: > > dlg_progressbox(const char *title, > const char *cprompt, > int height, > int width, > int pauseopt, > FILE *fp) > > I can open the file to be referenced: > > fp = os.fdopen(self.pipefds[0], 'r') > > Every thing I've tried ends with ctypes raising a TypeError. What specifically did you try? A tiny bit of googling suggests the following approach: http://stackoverflow.com/questions/3794309/python-ctypes-python-file-object-c-file/3794401#3794401 Related POSIX docs: http://pubs.opengroup.org/onlinepubs/009695399/functions/fdopen.html Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Why is pylaucher in Python 3.3 being installed in Windows folder?
On Wed, Oct 3, 2012 at 8:21 PM, Ian Kelly wrote: > On Wed, Oct 3, 2012 at 8:04 PM, Steven D'Aprano > wrote: >> On Wed, 03 Oct 2012 14:13:10 -0700, Piotr Dobrogost wrote: >> >>> Why is pylauncher in Python 3.3 being installed in Windows folder and >>> not in Program Files folder? Installing into Windows folder was maybe >>> acceptable 10 years ago but not now... >> >> Read the PEP: >> >> http://www.python.org/dev/peps/pep-0397/ > > The PEP explains why it's in the Windows folder as opposed to the > System32 folder, but not why either of those locations should be > preferable to Program Files. Presumably because Program Files isn't part of the $PATH. http://superuser.com/questions/124239/what-is-the-default-path-environment-variable-setting-on-fresh-install-of-window Contrast (from the PEP): "However, the Windows directory is always on the path." Now, as for why the launcher must be on the $PATH…*shrugs* Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Can somebody give me an advice about what to learn?
On Wed, Oct 3, 2012 at 11:31 AM, Wolfgang Keller wrote: >> I'm really new to Usenet/Newsgroups, but... I'd like to learn some >> new programming language, because I learnt a bit of Perl though its >> OOP is ugly. So, after searching a bit, I found Python and Ruby, and >> both of they are cute. So, assuming you'll say me "learn python", why >> should I learn it over Ruby? > > The point why Ruby was started (perceived deficit of object-orientation) > has been remedied since Python 2.2. Not completely. At the least, there's arguably still the issue of len() and friends (vs. `.length` etc.), and also of `self` being explicit. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Unpaking Tuple
On Sat, Oct 6, 2012 at 3:09 AM, sajuptpm wrote: > Hi, > > I am using python 2.6. > > I need a way to make following code working without any ValueError . a, b, c, d = (1,2,3,4) a, b, c, d = (1,2,3). > > Note: Number of values in the tuple will change dynamically. Then you arguably want a list, not a tuple. But at any rate: shortfall = 4 - len(your_tuple) your_tuple += (None,) * shortfall # assuming None is a suitable default a, b, c, d = your_tuple If you also need to handle the "too many items" case, use slicing: a, b, c, d = your_tuple[:4] Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: question on log as an instance method
On Sun, Oct 7, 2012 at 1:33 AM, Franck Ditter wrote: > Hi ! Here is Python 3.2.3, MacOSX-Lion > > Question 0 : I may consider + as an hidden instance method , as > 1+2 is equivalent to (1).__add__(2) ? No, it's not nearly that simple. It's technically equivalent to operator.add(1, 2) [ http://docs.python.org/library/operator.html#operator.add ], which hints that there's additional logic involved. Some examples of the complexities (in the general case): * special methods are looked up on the objects' classes, ignoring per-instance attributes; see http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes * trying to add objects of incompatible types raises TypeError rather than AttributeError (which one might otherwise expect when a class doesn't define __add__() [or similar]) * such TypeErrors are often raised directly by the interpreter itself, rather than from within the body of some specific implementation of an operator special method * falling back to reflected methods (in the case of +, __radd__() [ http://docs.python.org/reference/datamodel.html#object.__radd__ ]) when the normal method fails or is not defined * returning NotImplemented triggers fallback (or if already attempting fallback, may cause the operation to fail) > Question 2 : After importing math, Why would that be relevant? Python is not generally the sort of language that would have the mere importation of a std lib module significantly affect language semantics. > why can't I consider log as > an instance method, after all ? (4).__log__() > AttributeError: 'float' object has no attribute '__log__' Because Python just simply did not choose to make "take the (natural) logarithm of" a built-in, overloadable operation (hence, in part, why you had to `import math` to even be able to access that calculation). And it further didn't happen to define math.log() in terms of a .log() or .__log__() instance method. Basically, it's somewhat arbitrary and some historical reasons are involved. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: getting the state of an object
On Sun, Oct 7, 2012 at 1:50 AM, Franck Ditter wrote:
> Hi !
>
> Another question. When writing a class, I have often to
> destructure the state of an object as in :
>
> def foo(self) :
> (a,b,c,d) = (self.a,self.b,self.c,self.d)
> ... big code with a,b,c,d ...
I would generally strongly frown on doing that (at least if the
destructuring is actually that trivial). The "self."s are only 5
characters; that's hardly burdensome or verbose. Just write it out
each time, i.e.:
def foo(self):
… code with self.a, self.b, self.c, self.d ...
Or in the extreme case, use "s" instead of "self" (using the name
"self" is just a convention; it's not a language keyword or anything).
> So I use the following method :
>
> def state(self) :
> return (self.a,self.b,self.c,self.d)
>
> so as to write :
>
> def foo(self) :
> (a,b,c,d) = self.state()
> ... big code with a,b,c,d ...
>
> This is probably not the best Python way to code, is it ?
Indeed it isn't. Don't bother with the pointless destructuring.
And/or refactor your method so it's less big.
> Is there a simple way to get the *ordered* list of instance
> variables as given in the parameter list of __init__ ?
There is no guaranteed correspondence whatsoever between __init__()'s
parameters and the resulting object's instance variables. At a
minimum, such a hack would fail to account for instance variables that
are merely derived from the parameters (e.g. self.length =
len(some_list_param) ) or are initialized to constant values (e.g.
self.cache = {} ). And then there's private instance variables (e.g.
self._foo = foo_param ); if the parameter is named "_foo", you're
doing it wrong.
That said, you can obtain the names of the parameters using the
`inspect` module in the std lib.
> __dict__ gives it but not in order…
You can actually "fix" that, not that I at all recommend doing so:
http://docs.python.org/dev/reference/datamodel.html#preparing-the-class-namespace
Cheers,
Chris
--
http://mail.python.org/mailman/listinfo/python-list
Re: question on log as an instance method
On Sun, Oct 7, 2012 at 1:33 AM, Franck Ditter wrote: > As a matter of netiquette, please don't post from a plausible-but-invalid email address, especially at a domain that doesn't seem to belong to you. (I got a mailer-daemon bounce when replying to your posts.) If you must use an invalid address, then please make use of the ".invalid" TLD (that's what it's for!). Regards, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: trouble with nested closures: one of my variables is missing...
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
Re: What's the tidy/elegant way to protect this against null/empty parameters?
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: simple string format question
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: Remove uncide notation
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: turn list of letters into an array of integers
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: turn list of letters into an array of integers
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: fastest data / database format for reading large files
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: Help understanding an Object Oriented Program example
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: Simple Python question for some
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: Numpy module
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: Negative array indicies and slice()
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: Negative array indicies and slice()
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()
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: python-forum
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: Multi-dimensional list initialization
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: Executing .exe on a remote Windows machine
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: Problem with subprocess.call and windows schtasks
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: Splitting a line while keeping quoted items together
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: Web Frameworks Excessive Complexity
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: Constructing JSON data structures from non-string key python dictionaries
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
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: mysql insert with tuple
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: how to pass "echo t | " input to subprocess.check_output() method
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: how to pass "echo t | " input to subprocess.check_output() method
On Nov 26, 2012 3:03 AM, "Kushal Kumaran" wrote: > [email protected] 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: Is __ne__ method autogenerated?
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: JSON logging ?
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: Pexpect and buffering
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: pygnomevfs get_local_path_from_uri replacement
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: How to get time.strptime()?
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: Finding the name of a function while defining it
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: Finding the name of a function while defining it
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: email.message.Message - as_string fails
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: how to get the source of html in lxml?
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: class problem
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: father class name
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: father class name
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: Handling Special characters in python
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: Handling Special characters in python
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
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: pylint, was Re: pygame - importing GL - very bad...
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: Yet another attempt at a safe eval() call
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: os.path.realpath(path) bug on win7 ?
On Sat, Jan 5, 2013 at 10:55 PM, iMath <[email protected]> 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: License status of pycollada?
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: Safely add a key to a dict only if it does not already exist?
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: Increase value in hash table
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: Split string data have ","
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: security quirk
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: how to use subprocess to execute an exe with args and an output
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: Floating point calculation problem
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
python.org mailman web interface problem
Accessing http://mail.python.org/mailman/listinfo/ currently gives: """ Bug in Mailman version 2.1.12 We're sorry, we hit a bug! Please inform the webmaster for this site of this problem. Printing of traceback and other system information has been explicitly inhibited, but the webmaster can find this information in the Mailman error logs. """ Posting in the hopes one of the list admins notices this so that it can be fixed. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: String interning in Python 3 - missing or moved?
On Mon, Jan 23, 2012 at 4:38 PM, Chris Angelico wrote: > Python 2 can intern 'str' (bytes) strings (with the eponymous builtin, > and with C API functions), though not unicode. Python 3 does not have > that builtin, nor the C API; I can't find any support for either str > or bytes. > > Has it been moved, or is interning as a concept deprecated? The former, into `sys`: http://docs.python.org/dev/library/sys.html#sys.intern Search the "What's New"s in the future. http://docs.python.org/release/3.1.3/whatsnew/3.0.html#builtins Cheers, ChrisR -- http://mail.python.org/mailman/listinfo/python-list
Re: String interning in Python 3 - missing or moved?
On Tue, Jan 24, 2012 at 12:17 AM, Stefan Behnel wrote: > Chris Angelico, 24.01.2012 05:47: >> Lua and Pike both quite happily solved hash collision attacks in their >> interning of strings by randomizing the hash used, because there's no >> way to rely on it. Presumably (based on the intern() docs) Python can >> do the same, if you explicitly intern your strings first. Is it worth >> recommending that people do this with anything that is >> client-provided, and then simply randomize the intern() hash? > > If you want to encourage them to fill up their memory with user provided > data in a non-erasable way, Actually, quoth intern()'s docs: "Interned strings are not immortal; you must keep a reference to the return value of intern() around to benefit from it." Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: How to work around a unicode problem?
On Tue, Jan 24, 2012 at 3:57 AM, wrote: > I have a small python program that uses the pyexiv2 package to view > exif data in image files. > > I've hit a problem because I have a filename with accented characters > in its path and the pyexiv2 code traps as follows:- > > Traceback (most recent call last): > File "/home/chris/bin/eview.py", line 87, in > image = pyexiv2.ImageMetadata(filepath) > File "/usr/lib/python2.7/dist-packages/pyexiv2/metadata.py", line 65, in > __init__ > self.filename = filename.encode(sys.getfilesystemencoding()) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 38: > ordinal not in range(128) > > Without digging deep into pyexiv2 is there any way I can work around > this error? The accented characters aren't in the filename itself, > they're in the directory path. After glancing at the docs, (untested): with open(filepath) as f: image = pyexiv2.ImageMetadata.from_buffer(f.read()) Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: search google with python
On Wed, Jan 25, 2012 at 1:55 AM, Tracubik wrote: > Hi all, > i'ld like to make a simple program for searching images from python. > All it have to do is make a search in google images and return the link > of the images (20 images is enough i think) > > Is there any API or modules i can use? https://developers.google.com/image-search/v1/jsondevguide http://docs.python.org/library/json.html http://docs.python.org/library/urllib2.html Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Find the mime type of a file.
On Wed, Jan 25, 2012 at 9:04 AM, Olive wrote: > I want to have a list of all the images in a directory. To do so I want > to have a function that find the mime type of a file. I have found > mimetypes.guess_type but it only works by examining the extension. In > GNU/Linux the "file" utility do much better by actually looking at the > file. Is there an equivalent function in python (as a last resort I can > always use the external file utility). There's 3rd-party Python bindings for the library that underlies the `file` command: https://github.com/ahupp/python-magic And there's an unrelated pure(?) Python standalone module from A-A-P: http://www.a-a-p.org/exec/ref-filetype.html Tip: google "file type detection python" Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Current Web URL
On Wed, Jan 25, 2012 at 11:38 AM, William Abdo wrote:
> Hi All,
>
> I have been breaking my brains to find a solution to determining what the
> current URL is in a web browser navigation bar.
>
> It cannot be a prefixed values since I will not know what platform it is
> running from at the time it is opened by the users.
>
> Can this URL be extracted from the navigation bar so it can be parsed?
>
> I tried this “response = urllib2.urlopen('http://www.google.com').geturl()”
> however it seems to always to give me the google url and not the one in the
> navigation bar.
Could you clarify where the Web browser is running and where the
Python script is running in your situation?
Is the Web browser only notional (i.e. you just want to get the final
URL you would reach after requesting some given URL as if your script
were a browser)?
If no, what if there are multiple browsers or browser windows open?
Which URL bar would you want the URL from?
Cheers,
Chris
--
http://mail.python.org/mailman/listinfo/python-list
Re: PyPI - how do you pronounce it?
On Fri, Jan 27, 2012 at 11:48 PM, Chris Angelico wrote: > Hopefully this will be a step up from Rick's threads in usefulness, > but I'm aware it's not of particularly great value! > > How do you pronounce PyPI? Is it: > * Pie-Pie? Personally, yes. Reflecting upon it, I now recognize this is ambiguous with PyPy, but context should make it clear verbally. Cheers, ChrisR -- http://mail.python.org/mailman/listinfo/python-list
Re: Questions regarding the daemon module.
On Sat, Jan 28, 2012 at 5:54 AM, David Lambert wrote: > I was looking for a simple way to daemonize a Python process, and found: > > http://www.python.org/dev/peps/pep-3143/ > > I used easy_install to add this package (I thought), but when I attempted to > use the example in the above link, I got the error: > > > AttributeError: 'module' object has no attribute 'DaemonContext' > > To my surprise when looking at the module that was installed, I found > something completely different to what was in the documentation: > dir(daemon) > ['__builtins__', '__doc__', '__file__', '__name__', '__package__', > 'basic_daemonize', 'checkPID', 'daemonize', 'errno', 'os', 'sys', > 'writePID'] print daemon.__doc__ > None print daemon.daemonize.__doc__ > None > > > Further experimentation with this module yielded a working daemon, but I am > concerned regarding its parentage and lack of documentation. Could someone > explain these discrepancies? You seem to have installed the "daemon" package: http://pypi.python.org/pypi/daemon/ If you read the PEP more closely, you'll see that the PEP 3143 reference implementation is instead the "python-daemon" package: http://pypi.python.org/pypi/python-daemon/ Both packages provide modules named "daemon", partially hence your confusion. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading Adobe PDF File
On Sat, Jan 28, 2012 at 9:52 PM, Shrewd Investor wrote: > Hi, > > I have a very large Adobe PDF file. I was hoping to use a script to > extract the information for it. Is there a way to loop through a PDF > file using Python? Haven't used it myself, but: http://www.unixuser.org/~euske/python/pdfminer/ > Or do I need to find a way to convert a PDF file into a text file? If > so how? The pdf2txt.py script from the same package happens to do exactly this. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Does python(django) have an official database driver to access SQLFire
On Sat, Jan 28, 2012 at 11:12 PM, Niu.Jack wrote: > I have a question on Python. Does python(django) have an official database > driver to access SQLFire? Or is there any roadmap to deliver an official > database driver? Sounds like no; SQLFire's FAQ (http://communities.vmware.com/docs/DOC-16640 ) and docs mention only JDBC & ADO.NET support. A convoluted solution involving Jython or IronPython might be possible however. Cheers, Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: autoconf error on Windows
On Sun, Jan 29, 2012 at 12:52 AM, Alec Taylor wrote:
> PyCrypto's install is giving an autoconf error on Windows, whether I
> install from the git repo or normally.
>
> Traceback (most recent call last):
> File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
> cmd_obj.run()
>
> File "C:\Projects\satchmo_test\satchmo_test\src\pycrypto\setup.py",
> line 274, in run
> raise RuntimeError("autoconf error")
>
> RuntimeError: autoconf error
>
>
> Command C:\Python27\python.exe -c "import setuptools;
> __file__='C:\\Projects\\satchmo_test\\satchmo_test\\src\\pycrypto\\setup.py';
> exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__,
> 'exec'))" develop --no-deps failed with error code 1
> Full output: http://pastebin.com/Dp3aw077
Judging by the earlier "'sh' is not recognized as an internal or
external command, operable program or batch file." error message and
after scanning thru the setup.py, sounds like you need to have MinGW
(http://www.mingw.org ) installed. FWICT, there don't seem to be any
current Windows binaries for PyCrypto.
Cheers,
Chris
--
http://mail.python.org/mailman/listinfo/python-list
