Re: optional types

2014-10-29 Thread Nick Cash
 Am I the only one who'd like to see optional types introduced in Python?

Nope! Some dude named Guido would like to see them as well:
https://mail.python.org/pipermail/python-ideas/2014-August/028742.html

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


Re: Template language for random string generation

2014-08-08 Thread Nick Cash

On 08/08/2014 01:45 PM, cwolf.a...@gmail.com wrote:
 On Friday, August 8, 2014 10:35:12 AM UTC-4, Skip Montanaro wrote:
 P.S. Probably a topic for a separate thread, and not actually 
 Python-related, but on a related note, I have never found a free password 
 keeper which works on all my platforms (Mac, Android, Unix). That is one 
 stumbling block (for me) to actually using extremely strong passwords. If 
 you have some thoughts, please contact me off-list.
 Skip - try lastpass.com it's cross platform, include Win, Mac, Linux, 
 Android and iOS.

LastPass is pretty nice (and I use it on Windows, Mac, Linux and Android
myself), but the mobile versions aren't free:
https://lastpass.com/misc_download2.php
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python's re module and genealogy problem

2014-06-11 Thread Nick Cash

On 06/11/2014 10:35 AM, Michael Torrie wrote:
 On 06/11/2014 06:23 AM, BrJohan wrote:
 For some genealogical purposes I consider using Python's re module.

 Rather many names can be spelled in a number of similar ways, and in 
 order to match names even if they are spelled differently, I will build 
 regular expressions, each of which is supposed to match  a number of 
 similar names.
 You might want to search for fuzzy matching algorithms. Years ago, there
 was an algorithm called soundex that would generate fuzzy fingerprints
 for words that would hide differences in spelling, etc.  Unfortunately
 such an algorithm would be language dependent.  The problem you are
 trying to solve is one of those very hard problems in computers and math.


Soundex is actually not horrible, but it is definitely only for English
names. Newer variants of Metaphone
(http://en.wikipedia.org/wiki/Metaphone) are significantly better, and
support quite a few other languages.  Either one would most likely be
better than the regex approach.

Side note: if your data happens to be in MySQL then it has a builtin
sounds_like() function that compares strings using soundex.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: kivy

2014-02-04 Thread Nick Cash
 Is kivy listed in the Python search paths (sys.path)?

yes

To be extra sure, you can start a python interpreter with the commandline 
argument -vv, and when you try to import kivy (or any module) it will show you 
every file/path it checks when trying to find it. This might help you narrow 
down where the problem lies.

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


RE: the Gravity of Python 2

2014-01-09 Thread Nick Cash
 and %s (which is incredibly useful) is not even documented (I suspect it's 
 also not available on all platforms).

The format specifiers available to Python are just whatever is available to the 
underlying c time.h. 
The manpage for strftime indicates that %s isn't part of the C standard, but 
part of Olson's timezone package, which means it's not available on Windows. 
Your suspicion is unfortunately correct.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: cascading python executions only if return code is 0

2013-12-23 Thread Nick Cash
 assert assertions_working()  # refuse to run in -O mode
 
 Can't imagine why that wouldn't work..

Why overthink this? 

assert not sys.flags.optimize

is clearly the one, and only one, obvious way to do it. 

Of course, it works about as well as the rest of these solutions. Which is to 
say, not at all.

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


RE: Does Python optimize low-power functions?

2013-12-06 Thread Nick Cash
My question is, what do Python interpreters do with power operators where the 
power is a small constant, like 2?  Do they know to take the shortcut?

Nope:

Python 3.3.0 (default, Sep 25 2013, 19:28:08) 
[GCC 4.7.2] on linux2
Type help, copyright, credits or license for more information.
 import dis
 dis.dis(lambda x: x*x)
  1   0 LOAD_FAST0 (x) 
  3 LOAD_FAST0 (x) 
  6 BINARY_MULTIPLY  
  7 RETURN_VALUE 
 dis.dis(lambda x: x**2)
  1   0 LOAD_FAST0 (x) 
  3 LOAD_CONST   1 (2) 
  6 BINARY_POWER 
  7 RETURN_VALUE 


The reasons why have already been answered, I just wanted to point out that 
Python makes it extremely easy to check these sorts of things for yourself.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: chunking a long string?

2013-11-08 Thread Nick Cash
 I have a long string (several Mbytes).  I want to iterate over it in 
 manageable chunks

This is a weirdly common question. See 
http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python
 for several solutions.

It's been proposed to be added to itertools before, but rejected: 
https://mail.python.org/pipermail/python-ideas/2012-July/015671.html and 
http://bugs.python.org/issue13095 

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


RE: How to tell an HTTP client to limit parallel connections?

2013-11-08 Thread Nick Cash
What I really need is an HTTP header or meta-tag or something that I can use 
to tell clients to limit themselves to a single connection.

I don't think such a thing exists... but you may be able to solve this 
creatively:

A) Set up a proxy server that multiplexes all of the connections into a single 
one. A reverse proxy could even handle the SSL and alleviate the load on the 
embedded server. Although it sounds like maybe this isn't an option for you?

OR

B) Redesign the page it generates to need fewer requests (ideally, only one): 
inline CSS/JS, data: url images, etc. It's not the prettiest solution, but it 
could work.

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


RE: Code golf challenge: XKCD 936 passwords

2013-10-09 Thread Nick Cash
 # Python-2, sorry
 import os
 print list(set(open('/usr/share/dict/words')))[os.getpid():][:4]

 So that steps by your pid? 

Not really. It seems to rely on list(set(...)) kinda randomizing order... which 
is definitely not safe without hash randomization.  

But this brings up an interesting concept... if we can assume Python 2.7 with 
the -R flag, or Python 3.3+, I think we do get true pseudo-randomization out of 
list(set(...))... which means we can trim some!

print(list(set(open('/usr/share/dict/words')))[:4])

No module imports needed! Although it's not as pretty as the original post, but 
neither was Roy's.

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


RE: Creating a Super Simple WWW Link, Copy, Paste into Spreadsheet Program

2013-06-13 Thread Nick Cash
 there is a python module that reads and writes to excel files.  look 
 for that

More than one, actually, and which to use depends on whether Excel files 
means the .xls or .xlsx 
format.  On Windows, the most flexible solution is going to be to just use COM 
to control the Excel application in reading and writing the files.  Outside 
of Windows, the best bet is usually to work with csv files instead, as Dave 
suggested.

I've had success with the xlrd and xlwt suite of modules 
(http://www.python-excel.org/), using both .xls and .xlsx, on Linux. 

-Nick Cash

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


RE: (any)dbm module lacks a context manager

2013-01-31 Thread Nick Cash
  import dbm
  with dbm.open(mydb, 'c') as d:
 ... d[hello] = world
 ...
 Traceback (most recent call last):
   File stdin, line 1, in module
 AttributeError: '_dbm.dbm' object has no attribute '__exit__'

This error message is somewhat misleading... it actually means you're trying to 
use an object as a context manager, and it doesn't implement the context 
manager protocol (defined __enter__ and __exit__ methods). In this case, 
db.open() returns a dict -like object that is not a context manager. You'd need 
to refactor your code to something like:

import dbm
d = dbm.open(mydb, 'c')
d[hello] = world
d.close() 

You may want to wrap any actions on d with a try-except-finally so you can 
always close the db. If dbm objects were real context managers, they would do 
this for you. 
This does seem like a useful enhancement. It might be slightly involved to do, 
as the dbm module has multiple implementations depending on what libraries are 
available on the OS.

-Nick Cash

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


RE: Split string first data have ,

2013-01-29 Thread Nick Cash
  y = 'abc.p,zip.p,a,b'
  print y
 abc.p,zip.p,a,b
 

 x = what is your question??
 print x

I'm guessing that you want to split on ,, but want the quoted section to be a 
single token? 
Have you looked at the CSV module (http://docs.python.org/3/library/csv.html)?

If my guess is wrong, or you're having difficulties with the csv module, a more 
specific question will help you get the answer you're looking for.

-Nick Cash

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


urllib2 FTP Weirdness

2013-01-23 Thread Nick Cash
Python 2.7.3 on linux

This has me fairly stumped. It looks like
urllib2.urlopen(ftp://some.ftp.site/path;).read()
will either immediately return '' or hang indefinitely. But
response = urllib2.urlopen(ftp://some.ftp.site/path;)
response.read()
works fine and returns what is expected. This is only an issue with urllib2, 
vanilla urllib doesn't do it.

The site I first noticed it on is private, but I can reproduce it with 
ftp://ftp2.census.gov/;.

I've tested the equivalent code on Python 3.2.3 and get the same results, 
except that one time I got a socket error (may have been a spurious network 
blip, though). 


I'm at a loss as to how that could even work differently. My only guess is that 
by not having a reference to the addinfourl response object, something 
important is getting garbage collected or closed... that seems like a stretch, 
though. Is this a urllib2 bug, or am I crazy?

-Nick Cash


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


RE: Vote tallying...

2013-01-18 Thread Nick Cash
 I have a problem which may fit in a mysql database, but which I only
 have python as an alternate tool to solve... so I'd like to hear some
 opinions...

Is there a reason you can't use an RDBMS for this? MySQL would certainly be 
fine, although I always recommend PostgreSQL over it. Based on the amount of 
data you've specified, you really don't want to be rolling your own data 
storage system. If you absolutely cannot install any new software, Sqllite is 
built into Python and would work almost as well.

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


RE: How to get time.strptime()?

2012-12-26 Thread Nick Cash
 Error: AttributeError: 'module' object has no attribute '_strptime'
 
 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'.
 
 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.

I've run into this issue in the past with Python 2.6 (or maybe 2.7) and 
mod_python. My problem was that datetime.strptime imported _strptime at 
runtime, and multiple threads trying to import it at the same time broke an 
internal import lock.

I was able to work around this by simply importing _strptime myself at server 
startup time. This may or may not work for you.

See also: http://code.google.com/p/modwsgi/issues/detail?id=177 and 
http://www.mail-archive.com/python-list@python.org/msg248664.html 


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


RE: urlopen in python3

2012-12-05 Thread Nick Cash
 In python2, this work if something is a regular file on the system as
 well as a remote URL. The 2to3 script convert this to
 urllib.request.urlopen. But it does not work anymore if something
 is just a file name.
 
 My aim is to let the user specify a file on the command line and have
 something that works, whatever the file  actually is: a regular file,
 an http url, etc...

A file path, such as /etc/passwd, isn't properly a URL, so urllib correctly 
refuses to handle it. You can make it a URL by using the file:// protocol, i.e. 
file:///etc/passwd... which appears to work in both python2 and python3.

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


RE: len() on mutables vs. immutables

2012-10-18 Thread Nick Cash
 I'm curious as to the implementation (I'd be happy to dig through the
 source, just don't have the time right now). I've seen various
 implementations across interpreters in the past (some which have been
 rather shocking) and I'd like to get some insight into Python (well,
 CPython at this point anyway).
 
 When len() is called passing an immutable built-in type (such as a
 string), I'd assume that the overhead in doing so is simply a function
 call and there are no on-call calculations done. Is that correct?
 
 I'd also assume that mutable built-in types (such as a bytearray) would
 cache their size internally as a side effect of mutation operations. Is
 that correct? If so, is it safe to assume that at least all built-in
 types observe this behavior, or are there some that incur an O(n) cost
 on every len() call?

It appears that list has len() complexity of O(1)
source: http://wiki.python.org/moin/TimeComplexity
It may be worth mentioning that lists in Python are implemented using arrays 
instead of linked lists.

It's reasonable to assume that other built-in collection types would be 
similar, though I don't see anything explicitly saying so for bytearray.

-Nick Cash

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


RE: get google scholar using python

2012-10-01 Thread Nick Cash
 urllib2.urlopen('http://scholar.google.co.uk/scholar?q=albert
...
 urllib2.HTTPError: HTTP Error 403: Forbidden
 
 
 Will you kindly explain me the way to get rid of this?

Looks like Google blocks non-browser user agents from retrieving this query. 
You *could* work around it by setting the User-Agent header to something fake 
that looks browser-ish, but you're almost certainly breaking Google's TOS if 
you do so.

Should you really really want to, urllib2 makes it easy:
urllib2.urlopen(urllib2.Request(http://scholar.google.co.uk/scholar?q=albert+einstein%2B1905btnG=hl=enas_sdt=0%2C5as_sdtp=;,
 headers={User-Agent:Mozilla/5.0 Cheater/1.0}))

-Nick Cash
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Numpy combine channels

2012-09-10 Thread Nick Cash
 I have an array generated by audiolab of left and right stereo
 channels. It looks like [[1,1],[1,2],[2,3]]. I would like to combine
 the left and right channels to get an array [2,3,5]. Is there a numpy
 command to do that?

You may be over-thinking this, and numpy might not be necessary.

A simple solution would be just a quick list comprehension:

stereo_array = [[1, 1], [1, 2], [2, 3]]
mono_array = [l+r for (l, r) in stereo_array]

Thanks,
Nick Cash


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


RE: vBulletin scraper -- feasible?

2012-06-25 Thread Nick Cash
You may want to look into http://www.crummy.com/software/BeautifulSoup/
It's made for parsing (potentially bad) HTML, and is quite easy to use. I'd say 
it's quite feasible.

Thanks,
Nick Cash
NPC International

-Original Message-
From: python-list-bounces+nick.cash=npcinternational@python.org 
[mailto:python-list-bounces+nick.cash=npcinternational@python.org] On 
Behalf Of Andrew D'Angelo
Sent: Monday, June 25, 2012 14:10
To: python-list@python.org
Subject: vBulletin scraper -- feasible?

Taking a look through vBulletin's HTML, I was wondering whether it would be 
overly difficult to parse it into nice, manipulatible data.
I'd suppose my ultimate goal would be to dynamically parse a vBulletin and feed 
it into a locally hosted NNTP server. 


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


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


RE: Where to set default data - where received, or where used

2012-06-11 Thread Nick Cash
This is really up to your programming style, but I'm of the opinion that 
defining all of the default values in one place keeps maintenance easier.

Of course, if it's done differently elsewhere in your code base, I would aim 
for consistency instead.

Thanks,
Nick Cash

-Original Message-
From: python-list-bounces+nick.cash=npcinternational@python.org 
[mailto:python-list-bounces+nick.cash=npcinternational@python.org] On 
Behalf Of Dennis Carachiola
Sent: Monday, June 11, 2012 13:38
To: python-list@python.org
Subject: Where to set default data - where received, or where used

I'm programming a project which will use a file to save parameters needed by 
the program.  There are already two previous file formats, each of which can 
only be run by the version of the program which created them.  I'm trying to 
avoid that problem in the future.  To do that, I intend to use a dictionary, 
and default data.  I believe that most new versions will add parameters.

Each version of the program will have reasonable default values for each key in 
the dictionary handled by that version.  If an older file is used, the data 
values in that file replace the keys that exist.
The program then can operate on the values supplied by the older data file and 
the default values.  Conversely, if a newer file is used, the data in the file 
replaces the keys in the dictionary.  The program then simply doesn't access 
the newer data values.  I'm hoping that this will make the file backward and 
forward compatible.

Here's my question.  I could do this by creating the dictionary with the 
default values, then read the file into it.  Or I could use a 'get' with 
default values at the location in the program where those values are used.

From what I can see, creating the dictionary with default values puts
everything in one place.  While, supplying the default values at the place 
where they're used places the default values nearest the place where actually 
used.

I can't decide on one way over the other.  Can anyone give me some ideas if one 
is a preferred method, or other criteria I've overlooked?

Thanks,

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


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