Help with changes in traceback stack from Python 2.7 to Python 3.x

2014-04-25 Thread Andrew Konstantaras

I wrote the following code that works in Python 2.7 that takes the variables 
passed to the function into a dictionary.  The following call:

strA = 'a'
intA = 1
dctA = makeDict(strA, intA)

produces the following dictionary:

 {'strA':'a', 'intA':1}

To access the names passed into the function, I had to find the information by 
parsing through the stack.  The code that used to work is:

from traceback import extract_stack

def makeDict(*args):
   strAllStack = str(extract_stack())
   intNumLevels = len(extract_stack())
   intLevel = 0
   blnFinished = False
   while not blnFinished:
   strStack = str(extract_stack()[intLevel])
   if strStack.find("makeDict(")>0:
   blnFinished = True
   intLevel += 1
   if intLevel >= intNumLevels:
   blnFinished = True
   strStartText = "= makeDict("
   intLen = len(strStartText)
   intOpenParenLoc = strStack.find(strStartText)
   intCloseParenLoc = strStack.find(")", intOpenParenLoc)
   strArgs = strStack[ intOpenParenLoc+intLen : intCloseParenLoc ].strip()
   lstVarNames = strArgs.split(",")
   lstVarNames = [ s.strip() for s in lstVarNames ]  
   if len(lstVarNames) == len(args):

   tplArgs = map(None, lstVarNames, args)
   newDict = dict(tplArgs)
   return newDict
   else:
   return "Error, argument name-value mismatch in function 'makeDict'. lstVarNames: " 
+ str(lstVarNames) + "\n args: " + str(args), strAllStack

The same code does not work in Python 3.3.4.  I have tried parsing through the 
stack information and frames and I can't find any reference to the names of the 
arguments passed to the function.  I have tried inspecting the function and 
other functions in the standard modules, but I can't seem to find anything that 
will provide this information.

Can anyone point me in the direction to find this information?  Any help is 
appreciated.

---Andrew

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


Re: Play back and record sound *at the same time* for n seconds

2014-04-25 Thread Ben Finney
tfischer.n...@gmail.com writes:

> to program an art project that involves sound feedback I need a way to
> get a python script to to play back and to record sound *at the same
> time* for n seconds.

This is largely unrelated to whether you use Python. Rather, you need to
know what operating system (OS) services are available on your chosen
OS, that will allow the capability you're asking for.

Then, once you know what OS service to use for simultaneous sound
channels, you can look for a way to use Python to interface with that.

What OS will you be using? I know that the JACK library
http://jackaudio.org/> has the capabilities you're asking about,
which would make GNU+Linux a good choice.

-- 
 \   “As the most participatory form of mass speech yet developed, |
  `\the Internet deserves the highest protection from governmental |
_o__)   intrusion.” —U.S. District Court Judge Dalzell |
Ben Finney

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


Play back and record sound *at the same time* for n seconds

2014-04-25 Thread tfischer . ncku
Hello,

to program an art project that involves sound
feedback I need a way to get a python script to
to play back and to record sound *at the same time*
for n seconds.

Which strategy/sound module would you recommend,
and could you post dummy code that shows how to
get it to play and record simultaneously?

Thanks a lot!
Tom
-- 
https://mail.python.org/mailman/listinfo/python-list


Using Python to get push notifications from an RSS feed?

2014-04-25 Thread John Salerno
As a little project for myself (and to help get immediate player notes for my 
fantasy baseball team), I'm wondering what modules I might need to do this. 
Basically, I'd like to get immediate notification when a new player note has 
been added to an RSS feed.

Since it will only be for specified players, I suppose I would use something 
like ElementTree to read the data and figure out if the update is about a 
relevant player, but how do I check the RSS feed (constantly) in the first 
place? I was reading a little about Universal Feed Parser, but I'm still not 
sure about how to do the "push" notification aspect.

I'd like immediate notifications if possible, or will I have to settle for 
periodically checking the feed? If the latter, how do I ensure I only check the 
newest updates since the last time, and not everything?

Thanks!
John
-- 
https://mail.python.org/mailman/listinfo/python-list


feedparser error

2014-04-25 Thread tad na
python 2.7.2

The following code has an error and I can not figure out why:

import feedparser
d = feedparser.parse('http://bl.ocks.org/mbostock.rss')
numb = len(d['entries'])
for post in d.entries:
print post.pubDate+"\n"

---
the error is :

print post.pubDate+"\n"
  File "build\bdist.win32\egg\feedparser.py", line 416, in __getattr__
raise AttributeError, "object has no attribute '%s'" % key
AttributeError: object has no attribute 'pubDate'

---

The only thing I can think of is feedparser does not like 
uppercase(pubDate)?
I can not change someone else's rss.   What can I do here?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: retrieve source code from code object as returned by compile()

2014-04-25 Thread Gregory Ewing

Amirouche Boubekki wrote:
in python3, I do inspect.getsource(object) [doc 
], I 
don't know the limitations.


The limitation relevant here is that it requires the
original source file to be present. :-)

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


Re: Installing PyGame?

2014-04-25 Thread Gregory Ewing

Ryan Hiebert wrote:

> I've chosen to use
MacPorts because it keeps things separate, because when things get hosed 
using the system libraries, I don't have to erase my whole system to get 
back to a "vanilla" OS X install.


I don't know what you're doing to hose your system that badly.
I've never had a problem that couldn't be fixed by deleting
whatever the last thing was I added that caused it.

Also the problems I had with one of the third-party package
managers was because it *didn't* keep its own stuff properly
separated. It installed libraries on my regular library path
so that they got picked up by things that they weren't
appropriate for.

I'm not saying that MacPorts is a bad thing. If it's the
*only* thing you use, it's probably fine. But I use a wide
variety of libraries, not all of them available that way,
and many of them installed from source, and I find it's
less hassle overall to do everything the native MacOSX way
wherever possible.

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


Re: Installing PyGame?

2014-04-25 Thread Gregory Ewing

Ned Deily wrote:
I disagree that 
installing a bunch of disparate software from various sources via binary 
installers and/or source is to be preferred to a modern third-party 
package manager on OS X like MacPorts or Homebrew.  That's just setting 
yourself up for a long-term maintenance headache.  What could be easier 
than:


sudo port install py27-game


That's fine if it works, but the OP said he'd already tried
various things like that and they *didn't* work for him. And
I've had trouble in the past with MacPorts and/or Fink (can't
remember exactly which one it was) installing libraries that
were incompatible with other things I use and messing them
up, so I've learned to be wary of them.

Those problems were probably due to some unusual features of
my setup, and wouldn't occur for most other people. But
because I don't use those tools, I can't give any
recommendations about how to troubleshoot them. All I can
do is explain what works for me.

--
Greg

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


Re: Proper deletion of selected items during map iteration in for loop

2014-04-25 Thread Tim Chase
On 2014-04-25 14:50, Terry Reedy wrote:
> If you expect to delete more than half the keys *and* if there are
> no other references to the dict, such that you need the particular
> object mutated, this might be better.

If that's your precondition, then it might be better to do something
like

  keep = {}
  for (k, v) in m.items():
if f(k):
  #  do some processing of v and save result elsewhere
else:
  keep[k] = v
  m.clear()
  m.update(keep)
  del keep  # optionally free this back up when done

This would allow things that refer to "m" to retain the same
reference, but should have the same result as deleting them without
having to duplicate what the OP describes as a large volume of data.

Either way, the options are mostly

1) clone the entire .items() into a list, and iterate over that for
deleting from your underlying view (what Chris & Matthew suggest)

2) iterate over the items, storing up the ones to delete and delete
them all at the end (what the OP mentions having tried before)

3) iterate over the items, storing up the ones to keep, delete the
rest, and then put the kept ones back (what I code above)

The choice between #2 and #3 hinge on whether you expect to delete
more than half the items.  If you plan to delete more than half, store
the ones to keep (#3); if you plan to delete less than half, store
the ones to delete (#2).

-tkc





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


Re: MacOS 10.9.2: threading error using python.org 2.7.6 distribution

2014-04-25 Thread Ned Deily
In article 
,
 Chris Angelico  wrote:

> On Fri, Apr 25, 2014 at 11:43 PM, Matthew Pounsett
>  wrote:
> > If I insert that object into the test code and run it instead of 
> > MyThread(), I get the error.  I can't see anything in there that should 
> > cause problems for the threading module though... especially since this 
> > runs fine on another system with the same version of python.
> >
> > Any thoughts on what's going on here?
> 
> First culprit I'd look at is the mixing of subprocess and threading.
> It's entirely possible that something goes messy when you fork from a
> thread.

FWIW, the Python 2 version of subprocess is known to be thread-unsafe.  
There is a Py2 backport available on PyPI of the improved Python 3 
subprocess module:

http://bugs.python.org/issue20318
https://pypi.python.org/pypi/subprocess32/

-- 
 Ned Deily,
 n...@acm.org

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


xlutils 1.7.0 released!

2014-04-25 Thread Chris Withers

Hi All,

I'm pleased to announce the release of xlutils 1.7.1:

http://pypi.python.org/pypi/xlutils/1.7.1

This release has a couple of small changes:

- Add support for time cells in when using View classes.

- Add support for ``.xlsx`` files when using View classes, at the
  expense of formatting information being available.

Full docs here:

http://pythonhosted.org/xlutils/

Details of all things Python and Excel related can be found here:

http://www.python-excel.org/

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
https://mail.python.org/mailman/listinfo/python-list


Re: Proper deletion of selected items during map iteration in for loop

2014-04-25 Thread Terry Reedy

On 4/25/2014 2:04 PM, Matthew Barnett wrote:

On 2014-04-25 18:53, Charles Hixson wrote:

What is the proper way to delete selected items during iteration of a
map?  What I want to do is:

for (k, v) in m.items():
 if f(k):
#  do some processing of v and save result elsewhere
del m[k]

But this gives (as should be expected):
  RuntimeError: dictionary changed size during iteration
In the past I've accumulated the keys to be deleted in a separate list,
but this time there are likely to be a large number of them, so is there
some better way?


The other way is to build a new dictionary.


If you expect to delete more than half the keys *and* if there are no 
other references to the dict, such that you need the particular object 
mutated, this might be better.



Actually, there's a third way. Iterate over a snapshot:

for (k, v) in list(m.items()):
 if f(k):
 #  do some processing of v and save result elsewhere
 del m[k]


Since a pre-snapshot of items or even just keys will be longer than a 
list of keys to be deleted, I would stick with the latter.


--
Terry Jan Reedy

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


Re: xlutils 1.7.1 released!

2014-04-25 Thread Chris Withers

*sigh* subject line fail...

On 25/04/2014 19:48, Chris Withers wrote:

Hi All,

I'm pleased to announce the release of xlutils 1.7.1:

http://pypi.python.org/pypi/xlutils/1.7.1

This release has a couple of small changes:

- Add support for time cells in when using View classes.

- Add support for ``.xlsx`` files when using View classes, at the
   expense of formatting information being available.

Full docs here:

http://pythonhosted.org/xlutils/

Details of all things Python and Excel related can be found here:

http://www.python-excel.org/

cheers,

Chris



--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
https://mail.python.org/mailman/listinfo/python-list


Re: possible bug in re expression?

2014-04-25 Thread Terry Reedy

On 4/25/2014 12:30 PM, Robin Becker wrote:

Whilst translating some javascript code I find that this

A=re.compile('.{1,+3}').findall(p)

doesn't give any error, but doesn't manage to find the strings in p that
I want len(A)==>0, the correct translation should have been

A=re.compile('.{1,3}').findall(p)

which works fine.

should

re.compile('.{1,+3}')

raise an error? It doesn't on python 2.7 or 3.3.


And it should not because it is not an error. '+' means 'match 1 or more 
occurrences of the preceding re' and the preceding re is ','.


>>> re.match('a{1,+3}', 'a{1,,,3}').group()
'a{1,,,3}'

I suppose that one could argue that '{' alone should be treated as 
special immediately, and not just when a matching '}' is found, and 
should disable other special meanings. I wonder what JS does if there is 
no matching '}'?


--
Terry Jan Reedy

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


Re: Proper deletion of selected items during map iteration in for loop

2014-04-25 Thread Matthew Barnett

On 2014-04-25 18:53, Charles Hixson wrote:

What is the proper way to delete selected items during iteration of a
map?  What I want to do is:

for (k, v) in m.items():
 if f(k):
#  do some processing of v and save result elsewhere
del m[k]

But this gives (as should be expected):
  RuntimeError: dictionary changed size during iteration
In the past I've accumulated the keys to be deleted in a separate list,
but this time there are likely to be a large number of them, so is there
some better way?


The other way is to build a new dictionary.

Actually, there's a third way. Iterate over a snapshot:

for (k, v) in list(m.items()):
if f(k):
#  do some processing of v and save result elsewhere
del m[k]

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


Re: Proper deletion of selected items during map iteration in for loop

2014-04-25 Thread Chris Angelico
On Sat, Apr 26, 2014 at 3:53 AM, Charles Hixson
 wrote:
> What is the proper way to delete selected items during iteration of a map?
> What I want to do is:
>
> for (k, v) in m.items():
>if f(k):
>   #  do some processing of v and save result elsewhere
>   del m[k]
>
> But this gives (as should be expected):
> RuntimeError: dictionary changed size during iteration
> In the past I've accumulated the keys to be deleted in a separate list, but
> this time there are likely to be a large number of them, so is there some
> better way?

One easy way is to explicitly coalesce the items() view into a list:

for k, v in list(m.items()):

That would work, but you effectively duplicate your entire dictionary
into a list. More likely, you should simply snapshot the keys:

for k in list(m):
# Your example code isn't using v, but I
# assume the real code does
v = m[k]
if f(k):
# as above
del m[k]

This and your previous technique of accumulating a delete-me list
would be the two standard ways to filter a dictionary with a loop. (I
say "with a loop" because there's a third way to filter a dictionary,
and that's with a comprehension. But I don't know of a really clean
way to save v elsewhere for the cases where the dict isn't keeping
that key.)

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


Re: possible bug in re expression?

2014-04-25 Thread MRAB

On 2014-04-25 17:55, Chris Angelico wrote:

On Sat, Apr 26, 2014 at 2:30 AM, Robin Becker  wrote:

Whilst translating some javascript code I find that this

A=re.compile('.{1,+3}').findall(p)

doesn't give any error, but doesn't manage to find the strings in p that I
want len(A)==>0, the correct translation should have been

A=re.compile('.{1,3}').findall(p)

which works fine.

should

re.compile('.{1,+3}')

raise an error? It doesn't on python 2.7 or 3.3.


I would say the surprising part is that your js code doesn't mind an
extraneous character in the regex. In a brace like that, negative
numbers have no meaning, so I would expect the definition of the regex
to look for digits, not "anything that can be parsed as a number". So
you've uncovered a bug in your code that just happened to work in js.

Should it raise an error? Good question. Quite possibly it should,
unless that has some other meaning that I'm not familiar with. Do you
know how it's being interpreted? I'm not entirely sure what you mean
by "len(A)==>0", as ==> isn't an operator in Python or JS. Best way to
continue, I think, would be to use regular expression matching (rather
than findall'ing) and something other than dot, and tabulate input
strings, expected result (match or no match), what JS does, and what
Python does. For instance:

Regex: "^a{1,3}$"

"": Not expected, not Python
"a": Expected, Python
"aa": Expected, Python
"aaa": Expected, Python
"": Not expected, not Python

Just what we'd expect. Now try the same thing with the plus in there.
I'm finding that none of the above strings yields a match. Maybe
there's something else being matched?


The DEBUG flag helps to show what's happening:

>>> r = re.compile('.{1,+3}', flags=re.DEBUG)
any None
literal 123
literal 49
max_repeat 1 4294967295
  literal 44
literal 51
literal 125

When it's parsing the pattern it's doing this:

.OK, match any character
{Looks like the start of a quantifier
1OK, the minimum count
,OK, the maximum count probably follows
+Error; it looks like the '{' was a literal

Trying again from the brace:

{Literal
1Literal
,Literal
+Repeat the previous item one or more times
3Literal
}Literal

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


Proper deletion of selected items during map iteration in for loop

2014-04-25 Thread Charles Hixson
What is the proper way to delete selected items during iteration of a 
map?  What I want to do is:


for (k, v) in m.items():
   if f(k):
  #  do some processing of v and save result elsewhere
  del m[k]

But this gives (as should be expected):
RuntimeError: dictionary changed size during iteration
In the past I've accumulated the keys to be deleted in a separate list, 
but this time there are likely to be a large number of them, so is there 
some better way?


--
Charles Hixson

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


Re: how to split this kind of text into sections

2014-04-25 Thread Terry Reedy

On 4/25/2014 9:07 AM, oyster wrote:

I have a long text, which should be splitted into some sections, where
all sections have a pattern like following with different KEY.


Computers are worse at reading your mind than humans. If you can write 
rules that another person could follow, THEN we could help you translate 
the rules to Python.


If you have 1 moderate length file or a few short files, I would edit 
them by hand to remove ignore lines and put a blank line between 
sections. A program to do the rest would then be easy.



the above text should be splitted as a LIST with 3 items, and I also
need to know the KEY for LIST is ['I am section', 'let's continue', 'I
am using']:


This suggests that the rule for keys is 'first 3 words of a line, with 
contractions counted as 2 words'. Correct?


Another possible rule is 'a member of the following list: ...', as you 
gave above but presumably expanded.


--
Terry Jan Reedy

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


Re: retrieve source code from code object as returned by compile()

2014-04-25 Thread Amirouche Boubekki
in python3, I do inspect.getsource(object)
[doc],
I don't know the limitations.

On Python 2, there is meta .

My interest is different, I use to retrieve the definition of function to
submit it to a database, instead of stored procedures, but I have the
source of the code. It can also be used to retrieve the ast.


2014-04-25 4:50 GMT+02:00 Justin Ezequiel :

> On Thursday, April 24, 2014 1:53:38 PM UTC+8, Gregory Ewing wrote:
> > Alternatively you could create a .pyc file out of the code
> > object and then use Easy Python Decompiler on that. The
> > following snippet of code should do that:
> >
> > (Taken from:
> >
> > http://stackoverflow.com/questions/8627835/generate-pyc-from-python-ast)
>
> Woohoo! Success! Thank you Greg!
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Moving to an OOP model from an classically imperitive one

2014-04-25 Thread Amirouche Boubekki
Héllo,

I have no definitive answer regarding the OOP/functional mismatch.

2014-04-24 18:53 GMT+02:00 :

> > A reasonable compromise might be to keep the *data* assocated
> >
> > with a SubuserProgram in a class, maybe together with a few
> >
> > methods that are tightly coupled to it, but have the major
> >
> > pieces of functionality such as install() implemented by
> >
> > separate functions that operate *on* the class, rather than
> >
> > being inside it.
> >
>
> I think this is sound advice.  I'm still not sure what I'll come up with.
>
> One of the other reasons why an OOP model might be right for me is that of
> caching.  I currently load a lot of attributes regarding programs from
> disk, and I do so multiple times, I could either pass those attributes
> around, OR, using a class, I could store those attributes in the object
> after loading them just once.


You could also use a generator with a somekind of memoization/cache.


> I have no experience with OOP except in the domain of GUIs (where it seems
> inescapable, all major toolkits use OOP)


I have some experience in GUIs with Python in a proprietary framework.

I have a bias for OOP and Python in particular. I started exploring Scheme
through Guile and other languages more or less. I am asking myself the
question "what is the interest of functional programming or so called".
Scheme doesn't enforce functional code or immutability. It's actually "more
open" somewhat because of the macro system among other things. Outside any
performance considerations or implementations details like GIL. Scheme and
(all?) *LISP don't use the infix notation*. probably because you can work
around it but IMO not fully. The infix notations allows to write things
like: people.move_to(paris). It's easy to read, as «Subject Verb
Complement»... Class based OOP is prefered because of that, in a lot of
situations. IMO, if OOP won, it's because of readability, and in particular
Class based OOP. JavaScript OOP is useless in asynchronous context, see
http://repl.it/Rrf/2. Maybe you can work around this feature with
JavaScript descriptors easly, I did not try that.

so I'm not yet sure how this will turn out.
>

I skimmed through the code, but it's very superficial reading, like I
installed pycharm to ask for help...:

- CamelCase is usually for class names. That said, sometime variable
holding classes are called my_object_class or my_object_factory
- I think permissions.py would be easier to read with a class... or a
function that initialize the dictionary as intented and just use dict
methods onward.
- To I read code, I have a systematic method I call
gunshot/destructive/production rules/function inlining/develop (as the dual
of factorize).

I started with subuser.py, I got: http://dpaste.com/1797075/

def getSubuserCommands():
  """ Returns a list of commands that may be called by the user. """

  def isPathToSubuserCommand(path):
  directory, executableName = os.path.split(path)
  return executableName.startswith("subuser-")

  externalCommandPaths = queryPATH(isPathToSubuserCommand)

  externalCommands = []
  subuserPrefixLength=len("subuser-")

  for externalCommandPath in externalCommandPaths:
commandDir, executableName = os.path.split(externalCommandPath)
commandName = executableName[subuserPrefixLength:]
externalCommands.append(commandName)

  external_subuser_commands = list(set(externalCommands))

  return list(set(
os.listdir(paths.getSubuserCommandsDir())).difference(nonCommands)) +
external_subuser_commands


It's kind of easier to read.

Gremlin is a DSL that allows to navigate a graph. That's exactly what
happens here (and all the
time long
now
).wrapped_ 
in(tesseract).
I'd like to write the above as:

>>> subuser.commands = (subuser.path.directories +
subuser.user.path.directories).filter(is_command).distinct().memoize()

I think it's possible in Ruby.

I'm quite interested by the subject. Elm language (see
reactconf)
and your graphical
IDEis very
interesting, do you know about Domain
Specific Modeling Forum ? I started to put some
thinking grouped together. Also I started proto-py to gather code ideas,
but it's still offline. My plan is to mimick subuser to see, what happens.

Can be of interest Scala @ Systems @
Twitter

Nice project by the way, with a better "see also" section that I could do :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: possible bug in re expression?

2014-04-25 Thread Chris Angelico
On Sat, Apr 26, 2014 at 2:30 AM, Robin Becker  wrote:
> Whilst translating some javascript code I find that this
>
> A=re.compile('.{1,+3}').findall(p)
>
> doesn't give any error, but doesn't manage to find the strings in p that I
> want len(A)==>0, the correct translation should have been
>
> A=re.compile('.{1,3}').findall(p)
>
> which works fine.
>
> should
>
> re.compile('.{1,+3}')
>
> raise an error? It doesn't on python 2.7 or 3.3.

I would say the surprising part is that your js code doesn't mind an
extraneous character in the regex. In a brace like that, negative
numbers have no meaning, so I would expect the definition of the regex
to look for digits, not "anything that can be parsed as a number". So
you've uncovered a bug in your code that just happened to work in js.

Should it raise an error? Good question. Quite possibly it should,
unless that has some other meaning that I'm not familiar with. Do you
know how it's being interpreted? I'm not entirely sure what you mean
by "len(A)==>0", as ==> isn't an operator in Python or JS. Best way to
continue, I think, would be to use regular expression matching (rather
than findall'ing) and something other than dot, and tabulate input
strings, expected result (match or no match), what JS does, and what
Python does. For instance:

Regex: "^a{1,3}$"

"": Not expected, not Python
"a": Expected, Python
"aa": Expected, Python
"aaa": Expected, Python
"": Not expected, not Python

Just what we'd expect. Now try the same thing with the plus in there.
I'm finding that none of the above strings yields a match. Maybe
there's something else being matched?

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


possible bug in re expression?

2014-04-25 Thread Robin Becker

Whilst translating some javascript code I find that this

A=re.compile('.{1,+3}').findall(p)

doesn't give any error, but doesn't manage to find the strings in p that I want 
len(A)==>0, the correct translation should have been


A=re.compile('.{1,3}').findall(p)

which works fine.

should

re.compile('.{1,+3}')

raise an error? It doesn't on python 2.7 or 3.3.
--
Robin Becker

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


Re: len() of unsized object - ks test

2014-04-25 Thread Jamie Mitchell
On Friday, April 25, 2014 3:07:54 PM UTC+1, Jamie Mitchell wrote:
> Hello all,
> 
> 
> 
> I am trying to perform a Kolmogorov-Smirnov test in Python but I'm having a 
> few difficulties.
> 
> 
> 
> # My files are netCDF so I import them as follows:
> 
> 
> 
> control=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_concatannavg_1D.nc','r')
> 
> 
> 
> # The string is simply a 1D array
> 
> 
> 
> # Then performing the ks test:
> 
> 
> 
> kstest(control,'norm')
> 
> 
> 
> # I then get the following error:
> 
> 
> 
> File "", line 1, in 
> 
>   File "/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py", 
> line 3413, in kstest
> 
> N = len(vals)
> 
> TypeError: len() of unsized object
> 
> 
> 
> Any ideas on why this isn't working would be great.
> 
> 
> 
> Thanks,
> 
> 
> 
> Jamie

Thanks for your help.

Steven your right I wasn't reading in the file on netCDF4.Dataset, I was just 
opening it. I have rectified it now - a silly mistake!

Thanks again,

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


Re: how to split this kind of text into sections

2014-04-25 Thread Steven D'Aprano
On Fri, 25 Apr 2014 09:18:22 -0400, Roy Smith wrote:

> In article ,
>  oyster  wrote:

>> [demo text starts]
>> a line we do not need
>> I am section axax
>> I am section bbb, we can find that the first 2 lines of this section
>> all startswith 'I am section'
>> .(and here goes many other text)... let's continue to
>>  let's continue, yeah
>>  .(and here goes many other text)...
>> I am using python
>> I am using perl
>>  .(and here goes many other text)...
>> [demo text ends]
> 
> This kind of looks like a standard INI file.

I don't think so. INI files are a collection of KEY=VALUE or KEY:VALUE 
pairs, and the example above shows nothing like that. The only thing 
which is even vaguely ini-like is the header [demo text starts], and my 
reading of that is that it is *not* part of the file, but just an 
indication that the OP is giving a demo.




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: len() of unsized object - ks test

2014-04-25 Thread Steven D'Aprano
On Fri, 25 Apr 2014 07:07:54 -0700, Jamie Mitchell wrote:

> Hello all,
> 
> I am trying to perform a Kolmogorov-Smirnov test in Python but I'm
> having a few difficulties.
> 
> # My files are netCDF so I import them as follows:
> 
> control=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/
south_west/swhcontrol_swest_concatannavg_1D.nc','r')
> 
> # The string is simply a 1D array
> 
> # Then performing the ks test:
> 
> kstest(control,'norm')
> 
> # I then get the following error:
> 
> File "", line 1, in 
>   File
>   "/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py",
>   line 3413, in kstest
> N = len(vals)
> TypeError: len() of unsized object
> 
> Any ideas on why this isn't working would be great.

My guess is that you are calling len() on something that doesn't have a 
length :-)

But seriously, first of, what does "vals" contain? Look at the 
documentation for the kstest function. Does it have a parameter called 
"vals"? If so, what argument are you providing for that? Make sure that 
it is what you expect it is.

What does the documentation for netCDF4.Dataset say? Perhaps you have to 
*read* from the file, not just open it?

control = netCDF4.Dataset(filename,'r')
data = control.read()  # Perhaps this?
kstest(data,'norm')

But I'm just guessing. You'll need to read the documentation to see what 
arguments are expected, then you'll need to check what arguments you're 
actually providing. Doing:

print(repr(control))

may help.


-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to split this kind of text into sections

2014-04-25 Thread Steven D'Aprano
On Fri, 25 Apr 2014 21:07:53 +0800, oyster wrote:

> I have a long text, which should be splitted into some sections, where
> all sections have a pattern like following with different KEY. And the
> /n/r can not be used to split
> 
> I don't know whether this can be done easily, for example by using RE
> module

[... snip example ...]

> I hope I have state myself clear.

Clear as mud.

I'm afraid I have no idea what you mean. Can you explain the decision 
that you make to decide whether a line is included, or excluded, or part 
of a section?



> [demo text starts]
> a line we do not need

How do we decide whether the line is ignored? Is it the literal text "a 
line we do not need"?

for line in lines:
if line == "a line we do not need\n":
# ignore this line
continue


> I am section axax
> I am section bbb, we can find that the first 2 lines of this section all
> startswith 'I am section'


Again, is this the *literal* text that you expect?

> .(and here goes many other text)... let's continue to
>  let's continue, yeah
>  .(and here goes many other text)...
> I am using python
> I am using perl
>  .(and here goes many other text)...
> [demo text ends]
> 
> the above text should be splitted as a LIST with 3 items, and I also
> need to know the KEY for LIST is ['I am section', 'let's continue', 'I
> am using']:

How do you decide that they are the keys?


> lst=[
>  '''I am section axax
> I am section bbb, we can find that the first 2 lines of this section all
> startswith 'I am section'
> .(and here goes many other text)...''',
> 
> '''let's continue to
>  let's continue, yeah
>  .(and here goes many other text)...''',
> 
> 
> '''I am using python
> I am using perl
>  .(and here goes many other text)...'''
> ]

Perhaps it would be better if you show a more realistic example.



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to split this kind of text into sections

2014-04-25 Thread Jussi Piitulainen
oyster writes:

> I have a long text, which should be splitted into some sections, where
> all sections have a pattern like following with different KEY.

itertools.groupby, if you know how to extract a key from a given line.

> And the /n/r can not be used to split

Yet you seem to want to have each line as a unit? You could group
lines straight from some file object using itertools.groupby and then
''.join each group.

(It's \n and \r, and \r\n when they are both there, but you can just
let Python read the lines.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: len() of unsized object - ks test

2014-04-25 Thread MRAB

On 2014-04-25 15:07, Jamie Mitchell wrote:

Hello all,

I am trying to perform a Kolmogorov-Smirnov test in Python but I'm having a few 
difficulties.

# My files are netCDF so I import them as follows:

control=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_concatannavg_1D.nc','r')


A brief look at the documentation online says that 'control' is an
instance that _describes_ the dataset:

"""A netCDF Dataset is a collection of dimensions, groups, variables and 
attributes. Together they describe the meaning of data and relations 
among data fields stored in a netCDF file."""


So I don't think that it's the data itself.


# The string is simply a 1D array

# Then performing the ks test:

kstest(control,'norm')

# I then get the following error:

File "", line 1, in 
   File "/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py", line 
3413, in kstest
 N = len(vals)
TypeError: len() of unsized object

Any ideas on why this isn't working would be great.



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


Re: how to split this kind of text into sections

2014-04-25 Thread Tim Chase
On 2014-04-25 23:31, Chris Angelico wrote:
> On Fri, Apr 25, 2014 at 11:07 PM, oyster 
> wrote:
> > the above text should be splitted as a LIST with 3 items, and I
> > also need to know the KEY for LIST is ['I am section', 'let's
> > continue', 'I am using']:
> 
> It's not perfectly clear, but I think I have some idea of what
> you're trying to do. Let me restate what I think you want, and you
> can tell be if it's correct.
> 
> You have a file which consists of a number of lines. Some of those
> lines begin with the string "I am section", others begin "let's
> continue", and others begin "I am using". You want to collect those
> three sets of lines; inside each collection, every line will have
> that same prefix.
> 
> Is that correct? If so, we can certainly help you with that. If not,
> please clarify. :)

My reading of it (and it took me several tries) was that two
subsequent lines would begin with the same N words.  Something like
the following regexp:

  ^(\w.{8,}).*\n\1.*

as the delimiter (choosing "6" arbitrarily as an indication of a
minimum match length to).

A naive (and untested) bit of code might look something like

  MIN_LEN = 6
  def overlap(s1, s2):
chars = []
for c1, c2 in zip(s1,s2):
  if c1 != c2: break
  chars.append(c1)
return ''.join(chars)
  prevline = ""
  output_number = 1
  output = defaultdict(list)
  key = None
  with open("input.txt") as f:
for line in f:
  if len(line) >= MIN_LEN and prevline[:MIN_LEN] == line[:MIN_LEN]: 
key = overlap(prevline, line)
  output[key].append(line)
  prevline = line

There are some edge-cases such as when multiple sections are
delimited by the same overlap, but this should build up a defaultdict
keyed by the delimiters with the corresponding lines as the values.

-tkc



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


len() of unsized object - ks test

2014-04-25 Thread Jamie Mitchell
Hello all,

I am trying to perform a Kolmogorov-Smirnov test in Python but I'm having a few 
difficulties.

# My files are netCDF so I import them as follows:

control=netCDF4.Dataset('/data/cr1/jmitchel/Q0/swh/controlperiod/south_west/swhcontrol_swest_concatannavg_1D.nc','r')

# The string is simply a 1D array

# Then performing the ks test:

kstest(control,'norm')

# I then get the following error:

File "", line 1, in 
  File "/usr/local/sci/lib/python2.7/site-packages/scipy/stats/stats.py", line 
3413, in kstest
N = len(vals)
TypeError: len() of unsized object

Any ideas on why this isn't working would be great.

Thanks,

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


Re: MacOS 10.9.2: threading error using python.org 2.7.6 distribution

2014-04-25 Thread Chris Angelico
On Fri, Apr 25, 2014 at 11:43 PM, Matthew Pounsett
 wrote:
> If I insert that object into the test code and run it instead of MyThread(), 
> I get the error.  I can't see anything in there that should cause problems 
> for the threading module though... especially since this runs fine on another 
> system with the same version of python.
>
> Any thoughts on what's going on here?

First culprit I'd look at is the mixing of subprocess and threading.
It's entirely possible that something goes messy when you fork from a
thread.

Separately: You're attempting a very messy charset decode there. You
attempt to decode as UTF-8, errors ignored, and if that fails, you log
an error... and continue on with the original bytes. You're risking
shooting yourself in the foot there; I would recommend you have an
explicit fall-back (maybe re-decode as Latin-1??), so the next code is
guaranteed to be working with Unicode. Currently, it might get a
unicode or a str.

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


Re: soap library for python-3.x

2014-04-25 Thread Ben Finney
"Deepika Nagpal"  writes:

> We have to start a project of developing a soap server in python-3.x
> for which I am looking for a soap server side library. Please let me
> know what are the options available and which of these libraries are
> actively maintained. Thanks.

You can start with https://wiki.python.org/moin/WebServices>. Good
hunting!

> 
> ::Disclaimer::
> 
> The contents of this email and any attachment(s) are confidential and
> intended for the named recipient(s) only. It shall not […]

This is nonsense (you deliberately sent this message to a public forum),
and obnoxious in its demands. Please ensure your future messages are
sent without this boilerplate.

-- 
 \ “I used to think that the brain was the most wonderful organ in |
  `\   my body. Then I realized who was telling me this.” —Emo Philips |
_o__)  |
Ben Finney

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


MacOS 10.9.2: threading error using python.org 2.7.6 distribution

2014-04-25 Thread Matthew Pounsett

I've run into a threading error in some code when I run it on MacOS that works 
flawlessly on a *BSD system running the same version of python.  I'm running 
the python 2.7.6 for MacOS distribution from python.org's downloads page.

I have tried to reproduce the error with a simple example, but so far haven't 
been able to find the element or my code that triggers the error.  I'm hoping 
someone can suggest some things to try and/or look at.  Googling for "pyton" 
and the error returns exactly two pages, neither of which are any help.

When I run it through the debugger, I'm getting the following from inside 
threading.start().  python fails to provide a stack trace when I step into 
_start_new_thread(), which is a pointer to thread.start_new_thread().  It looks 
like threading.__bootstrap_inner() may be throwing an exception which 
thread.start_new_thread() is unable to handle, and for some reason the stack is 
missing so I get no stack trace explaining the error.

It looks like thread.start_new_thread() is in the binary object, so I can't 
actually step into it and find where the error is occurring.

> /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py(745)start()
-> _start_new_thread(self.__bootstrap, ())
(Pdb) s
> /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py(750)start()
-> self.__started.wait()
(Pdb) Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from
Warning: No stack to get attribute from

My test code (which works) follows the exact same structure as the failing 
code, making the same calls to the threading module's objects' methods:


import threading

class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
print "MyThread runs and exits."

def main():
try:
t = MyThread()
t.start()
except Exception as e:
print "Failed with {!r}".format(e)

if __name__ == '__main__':
main()


The actual thread object that's failing looks like this:

class RTF2TXT(threading.Thread):
"""
Takes a directory path and a Queue as arguments.  The directory should be
a collection of RTF files, which will be read one-by-one, converted to
text, and each output line will be appended in order to the Queue.
"""
def __init__(self, path, queue):
threading.Thread.__init__(self)
self.path = path
self.queue = queue

def run(self):
logger = logging.getLogger('RTF2TXT')
if not os.path.isdir(self.path):
raise TypeError, "supplied path must be a directory"
for f in sorted(os.listdir(self.path)):
ff = os.path.join(self.path, f)
args = [ UNRTF_BIN, '-P', '.', '-t', 'unrtf.text',  ff ]
logger.debug("Processing file {} with args {!r}".format(f, args))
p1 = subprocess.Popen( args, stdout=subprocess.PIPE,
universal_newlines=True)
output = p1.communicate()[0]
try:
output = output.decode('utf-8', 'ignore')
except Exception as err:
logger.error("Failed to decode output: {}".format(err))
logger.error("Output was: {!r}".format(output))

for line in output.split("\n"):
line = line.strip()
self.queue.put(line)
self.queue.put("")

Note: I only run one instance of this thread.  The Queue object is used to pass 
work off to another thread for later processing.

If I insert that object into the test code and run it instead of MyThread(), I 
get the error.  I can't see anything in there that should cause problems for 
the threading module though... especially since this runs fine on another 
system with the same version of python.

Any thoughts on what's going on here?



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


Re: how to split this kind of text into sections

2014-04-25 Thread Chris Angelico
On Fri, Apr 25, 2014 at 11:07 PM, oyster  wrote:
> the above text should be splitted as a LIST with 3 items, and I also need to
> know the KEY for LIST is ['I am section', 'let's continue', 'I am using']:

It's not perfectly clear, but I think I have some idea of what you're
trying to do. Let me restate what I think you want, and you can tell
be if it's correct.

You have a file which consists of a number of lines. Some of those
lines begin with the string "I am section", others begin "let's
continue", and others begin "I am using". You want to collect those
three sets of lines; inside each collection, every line will have that
same prefix.

Is that correct? If so, we can certainly help you with that. If not,
please clarify. :)

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


Re: how to split this kind of text into sections

2014-04-25 Thread Roy Smith
In article ,
 oyster  wrote:

> I have a long text, which should be splitted into some sections, where
> all sections have a pattern like following with different KEY. And the /n/r
> can not be used to split
> 
> I don't know whether this can be done easily, for example by using RE module
> 
> [demo text starts]
> a line we do not need
> I am section axax
> I am section bbb, we can find that the first 2 lines of this section all
> startswith 'I am section'
> .(and here goes many other text)...
> let's continue to
>  let's continue, yeah
>  .(and here goes many other text)...
> I am using python
> I am using perl
>  .(and here goes many other text)...
> [demo text ends]

This kind of looks like a standard INI file.  Check out 
https://docs.python.org/2/library/configparser.html, it may do what you 
need.
-- 
https://mail.python.org/mailman/listinfo/python-list


how to split this kind of text into sections

2014-04-25 Thread oyster
I have a long text, which should be splitted into some sections, where
all sections have a pattern like following with different KEY. And the /n/r
can not be used to split

I don't know whether this can be done easily, for example by using RE module

[demo text starts]
a line we do not need
I am section axax
I am section bbb, we can find that the first 2 lines of this section all
startswith 'I am section'
.(and here goes many other text)...
let's continue to
 let's continue, yeah
 .(and here goes many other text)...
I am using python
I am using perl
 .(and here goes many other text)...
[demo text ends]

the above text should be splitted as a LIST with 3 items, and I also need
to know the KEY for LIST is ['I am section', 'let's continue', 'I am
using']:
lst=[
 '''I am section axax
I am section bbb, we can find that the first 2 lines of this section all
startswith 'I am section'
.(and here goes many other text)...''',

'''let's continue to
 let's continue, yeah
 .(and here goes many other text)...''',


'''I am using python
I am using perl
 .(and here goes many other text)...'''
]

I hope I have state myself clear.

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


RE: Executing pl/sql script File in python without sqlclient

2014-04-25 Thread Joseph L. Casale
> Is there a way to execute pl/sql Script files through Python without 
> sqlclient.

https://code.google.com/p/pypyodbc/ might work for you...

> i have checked cx_oracle and i guess it requires oracle client, so is there a 
> way to execute without oracle client.

Right, as the name implies it uses the oci interface and hence needs that 
library available.

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


soap library for python-3.x

2014-04-25 Thread Deepika Nagpal

Hi,

We have to start a project of developing a soap server in python-3.x for which 
I am
looking for a soap server side library. Please let me know what are the options
available and which of these libraries are actively maintained. Thanks.


Regards,
Deepika Nagpal


::Disclaimer:: 

 
The contents of this email and any attachment(s) are confidential and intended
for the named recipient(s) only. It shall not attach any liability on C-DOT. 
Any views or opinions presented in this email are solely those of the author 
and  may  not  necessarily  reflect  the  opinions  of  C-DOT.  Any  form of 
reproduction, dissemination, copying, disclosure, modification, distribution 
and / or publication of this message without the prior written consent of the 
author of this e-mail is strictly prohibited. If you have received this email 
in error please delete it and notify the sender immediately.



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