Where to put data

2012-01-25 Thread bvdp
I'm having a disagreement with a buddy on the packaging of a program we're doing in Python. It's got a number of modules and large number of library files. The library stuff is data, not code. I'd like to put the modules in /usr/lib/pythonX.Y/mymodules or wherever setup.py decides. And the dat

Re: Where to put data

2012-01-25 Thread bvdp
Right now my program does a search for modules in "all the normal places", which seems to work for windows, mac and linux. Once the modules are found I just insert that location into sys.path[0]. Which permits the modules to reside anywhere on the HDD. However, I have feeling that this isn't qu

Re: Where to put data

2012-01-25 Thread bvdp
> I would not put anything in the toplevel Python folder. You need to > place everything under site-packages --> "Python27\Lib\site-packages > \PackageName\blah". Of course client created files should be saved to > a more accessible place. Oh. Just looking at my setup (Ubunutu 11.10) and I see th

Re: Where to put data

2012-01-26 Thread bvdp
On Wednesday, January 25, 2012 8:30:54 PM UTC-7, Michael Torrie wrote: > Unless you are writing a python library that will be used by others, I > don't think that where you put your files has anything to do with being > "pythonic" or not. Just do what works for your OS. Yes. I agree and it's nic

Re: Where to put data

2012-01-27 Thread bvdp
On Thursday, January 26, 2012 8:20:24 PM UTC-7, Michael Torrie wrote: > > > I'm getting mangled by the debian maintainers and friends who seem to > > believe that python modules need to go into /usr/lib/python... > > I guess the maintainers aren't distinguishing between python apps and > their sub

Re: Where to put data

2012-01-27 Thread bvdp
On Friday, January 27, 2012 3:15:44 PM UTC-7, John Nagle wrote: > On 1/25/2012 9:26 AM, bvdp wrote: > > I'm having a disagreement with a buddy on the packaging of a program > > we're doing in Python. It's got a number of modules and large number > > of library f

Raise X or Raise X()?

2012-03-11 Thread bvdp
Which is preferred in a raise: X or X()? I've seen both. In my specific case I'm dumping out of a deep loop: try: for ... for ... for ... if match: raise StopInteration() else ... except StopInteration: print "found it" -- http://mail.python.org/mailma

Re: Raise X or Raise X()?

2012-03-11 Thread bvdp
Thanks all for the comments. > Personally, I used "raise X" to mean "this doesn't need arguments and > should never have any" and "raise X()" to mean "this needs arguments but > I'm too lazy to provide them right now". Think of it as a FIXME. Yes, that makes as much sense as anything else :)

TK program problem

2011-05-20 Thread bvdp
I've just done an update to my system here to Ubuntu 11.04. Mostly no problems ... but I have an important (to me) python/TK program that's stopped working. Well, it works ... mostly. The python version is 2.7.1+ (no idea what the + means!). I _think_ I have traced the problem to certain menus wh

Re: TK program problem

2011-05-20 Thread bvdp
> I'm not a tk user, but it sounds like it has regressed from accepting > arbitrary callables as callbacks to accepting functions specifically. > > What happens if you replace: > > ("Favorites", selectFav), > > with: > > ("Favorites", lambda: selectFav()), Okay, this works. Great and thanks! Seem

Re: TK program problem

2011-05-20 Thread bvdp
Probably the fix is to use a function :) > The docs [1] say that a callback is a function, so I guess that if it > worked before it was just luck.  You should bring it up on the tkinter > list and see what they have to say about it, though. > > I'm a bit confused about why you would want to use a

Re: TK program problem

2011-05-20 Thread bvdp
On May 20, 4:29 pm, Ian Kelly wrote: > On Fri, May 20, 2011 at 5:07 PM, bvdp wrote: > > You mention the tkinter group. Ummm, what group is that??? > > http://tkinter.unpythonic.net/wiki/TkinterDiscuss Thanks. New one for me. I'll subscribe and see if they know about t

Re: TK program problem

2011-05-21 Thread bvdp
Thanks, Peter, for the detailed explanation. I was going to write a bit of sample/minimal code to demo this, but you nicely beat me to it! > Here's a minimal script to reproduces the problem: > > $ cat tkcallclass.py > import Tkinter as tk > > root = tk.Tk() > root.withdraw() > > class Classic: >

Re: TK program problem

2011-05-21 Thread bvdp
On May 20, 4:37 pm, rantingrick wrote: > Thats sounds to me a lot like hammering square pegs though round > holes... Perhaps you should explain first in "plain english" what Ahh, but what fun would the Internet, Usenet and programming be without round holes and square pegs. I thought my English

Re: TK program problem

2011-05-21 Thread bvdp
> One of the purposes and advantages of Python 3 is having only one class > system. Best to always use new-style classes in Python 2.2+ unless you > understand and need old-style classes (and need should be never for most > people). > Thanks for this. I'll keep it in mind! One thing I really don

from xx import yy

2017-11-12 Thread bvdp
I'm having a conceptual mind-fart today. I just modified a bunch of code to use "from xx import variable" when variable is a global in xx.py. But, when I change/read 'variable' it doesn't appear to change. I've written a bit of code to show the problem: mod1.py myvar = 99 def setvar(x): glo

Re: from xx import yy

2017-11-13 Thread bvdp
On Sunday, November 12, 2017 at 7:18:04 PM UTC-7, bvdp wrote: > I'm having a conceptual mind-fart today. I just modified a bunch of code to > use "from xx import variable" when variable is a global in xx.py. But, when I > change/read 'variable' it doesn't

Re: from xx import yy

2017-11-16 Thread bvdp
On Tuesday, November 14, 2017 at 2:53:22 PM UTC-7, Cameron Simpson wrote: > On 13Nov2017 08:58, bvdp wrote: > >On Sunday, November 12, 2017 at 7:18:04 PM UTC-7, bvdp wrote: > >> I'm having a conceptual mind-fart today. I just modified a bunch of code > >> to us

"normalizing" a value

2015-07-01 Thread bvdp
Not sure what this is called (and I'm sure it's not normalize). Perhaps "scaling"? Anyway, I need to convert various values ranging from around -50 to 50 to an 0 to 12 range (this is part of a MIDI music program). I have a number of places where I do: while x < 0: x += 12 while x >= 12:

Re: "normalizing" a value

2015-07-01 Thread bvdp
On Wednesday, July 1, 2015 at 6:27:57 PM UTC-7, rand...@fastmail.us wrote: > On Wed, Jul 1, 2015, at 20:12, bvdp wrote: > > Not sure what this is called (and I'm sure it's not normalize). Perhaps > > "scaling"? > > > > Anyway, I need to convert

Re: "normalizing" a value

2015-07-01 Thread bvdp
On Wednesday, July 1, 2015 at 7:15:28 PM UTC-7, Steven D'Aprano wrote: > On Thu, 2 Jul 2015 10:12 am, bvdp wrote: > > > Not sure what this is called (and I'm sure it's not normalize). Perhaps > > "scaling"? > > > Could be normalising, could b

Re: "normalizing" a value

2015-07-01 Thread bvdp
On Wednesday, July 1, 2015 at 7:23:19 PM UTC-7, rand...@fastmail.us wrote: > On Wed, Jul 1, 2015, at 21:49, bvdp wrote: > > Interesting that negative values translate properly. That's an > > non-intuitive result to me. Guess I should have studied that math stuff >

Re: "normalizing" a value

2015-07-02 Thread bvdp
On Wednesday, July 1, 2015 at 8:37:18 PM UTC-7, Dennis Lee Bieber wrote: > On Wed, 1 Jul 2015 18:49:34 -0700 (PDT), bvdp declaimed > the following: > > > > >Thanks guys. Yes, that is exactly what I want. I have a number of places > >where a MIDI note value is being

Dealing with exceptions

2013-03-02 Thread bvdp
Every time I write a program with exception handling (and I suppose that includes just about every program I write!) I need to scratch my brain when I create try blocks. For example, I'm writing a little program do copy specific files to a USB stick. To do the actual copy I'm using: try:

Re: Dealing with exceptions

2013-03-02 Thread bvdp
> > IOError and OSError should cover all copy problems, I think. How do you know that? I can figure it out as well by running the program, but I'd like to make the determination of what to catch when I'm writing the code. -- http://mail.python.org/mailman/listinfo/python-list

Re: Dealing with exceptions

2013-03-02 Thread bvdp
> > Here's a bit of a left-field thought: Maybe none of them. > Not far left at all :) > > What are you actually doing when you get an exception? Can you > > plausibly recover? If not - that is, if you're going to abort the > > whole operation anyway - then save yourself the trouble of writi

Circular import problem

2007-07-12 Thread bvdp
I'm going quite nutty here with an import problem. I've got a fairly complicated program (about 12,000 lines in 34 modules). I just made some "improvements" and get the following error: bob$ mma Traceback (most recent call last): File "/usr/local/bin/mma", line 55, in import MMA.main Fil

Re: Circular import problem

2007-07-13 Thread bvdp
> Seehttp://effbot.org/zone/import-confusion.htm > Try to move the circular references later in the code (maybe inside a > function, when it is required), or much better, refactor it so there is no > circularity. > > -- > Gabriel Genellina Yes, thanks. I'd read that page before posting. Helpful.

Re: Circular import problem

2007-07-13 Thread bvdp
Just as a bit of a followup, I have fixed the problem in my code. I changed the order of some of the imports in some other modules. What I was doing was more guesswork and good luck ... but it works. I really wonder if there is a better way to figure these problems out. Reading a few of the other

Re: Circular import problem

2007-07-14 Thread bvdp
> > But, I still don't understand how python can access a function in a > > file I have NOT included. In this case, to get things to work, I DO > > NOT "import MMA.grooves" but later in the module I access a function > > with "xx=MMA.grooves.somefunc()" and it finds the function, and works > > jus

Re: Building musical chords starting from (a lot of) rules

2008-11-14 Thread bvdp
Mr.SpOOn wrote: Hi, I'm writing a method to create musical chords. This method must follow a specific set of syntax rules. At least, this is my idea, but maybe there's a better way. Anyway, in the code I have class Chord which is a set. The costrunction of a chord is based on a root note and a

Re: Building musical chords starting from (a lot of) rules

2008-11-16 Thread bvdp
Mr.SpOOn wrote: So for example in jazz music it is more common the minor seventh than the major, so writing just G7 you mean the dominant seventh chord (with minor seventh) and you have to write just the major one with maj7. A minor 7th has a flatted 3rd (ie. C, Eb, G, Bb). Don't confuse your

unescape escapes in strings

2009-02-23 Thread bvdp
When reading lines of data from a file in the from (no quotes!) foo\x20bar and I assign to a variable in a line line like: f = file('infile', 'r') for a in f: print a the string is read in as string with the literal characters 'f', 'o' ... 'x' , '2' ... as compared to an assi

Re: unescape escapes in strings

2009-02-23 Thread bvdp
MRAB wrote: bvdp wrote: When reading lines of data from a file in the from (no quotes!) foo\x20bar and I assign to a variable in a line line like: f = file('infile', 'r') for a in f: print a the string is read in as string with the literal characters &#

Re: unescape escapes in strings

2009-02-23 Thread bvdp
Perfect ... thanks. >>> a = a.decode("string-escape") Using "string-escape" does the trick! Wonderful, this python. And the quick answers on this group. -- http://mail.python.org/mailman/listinfo/python-list

more on unescaping escapes

2009-02-23 Thread bvdp
So, we think something is working and send of a bug fix to our client :) I'm not sure I understand this at all and wonder if there is bug? >>> a="c:\\Program\x20Files\\test" >>> a 'c:\\Program Files\\test' so far, so good. >>> a.decode("string-escape") 'c:\\Program Files\test' Umm, not so go

Re: more on unescaping escapes

2009-02-23 Thread bvdp
I'm getting hopelessly lost in a series of \\\ s :) Let's see if this makes sense: >>> a='c:\\Program Files\\test' >>> a.decode('string-escape') 'c:\\Program Files\test' In this case there are still 2 '\'s before the P; but only 1 before the 't'. Now, when it comes time to open the file w

Re: more on unescaping escapes

2009-02-23 Thread bvdp
andrew cooke wrote: do you know that a string with the letter "r" in front doesn't escape slashes? it's intended for regular expressions, but would simplify things for you here too. just do a=r'c:\\Program Files\test' Yes, I knew that. Unfortunately in my program loop I really don't have

Re: more on unescaping escapes

2009-02-23 Thread bvdp
Tim Wintle wrote: On Mon, 2009-02-23 at 17:00 -0700, bvdp wrote: Let's see if this makes sense: >>> a='c:\\Program Files\\test' >>> a.decode('string-escape') 'c:\\Program Files\test' Hint: try running print a and see wha

Re: more on unescaping escapes

2009-02-23 Thread bvdp
Chris Rebert wrote: On Mon, Feb 23, 2009 at 4:26 PM, bvdp wrote: [problem with Python and Windows paths using backslashes] Is there any particular reason you can't just internally use regular forward-slashes for the paths? They work in Windows from Python in nearly all cases and you can e

Re: more on unescaping escapes

2009-02-23 Thread bvdp
Bear in mind that it's the string as it really is that is being operated on, not the representation of it that you displayed Yes, that is the confusion ... what is displayed and what's actually in the string. I think I understand it all now :) Thanks. -- http://mail.python.org/mailman/lis

Re: more on unescaping escapes

2009-02-23 Thread bvdp
Gabriel Genellina wrote: En Mon, 23 Feb 2009 22:46:34 -0200, bvdp escribió: Chris Rebert wrote: On Mon, Feb 23, 2009 at 4:26 PM, bvdp wrote: [problem with Python and Windows paths using backslashes] Is there any particular reason you can't just internally use regular forward-slashe

Re: more on unescaping escapes

2009-02-23 Thread bvdp
Gabriel Genellina wrote: En Mon, 23 Feb 2009 23:31:20 -0200, bvdp escribió: Gabriel Genellina wrote: En Mon, 23 Feb 2009 22:46:34 -0200, bvdp escribió: Chris Rebert wrote: On Mon, Feb 23, 2009 at 4:26 PM, bvdp wrote: [problem with Python and Windows paths using backslashes] Is there

Re: more on unescaping escapes

2009-02-24 Thread bvdp
Adam Olsen wrote: On Feb 23, 7:18 pm, bvdp wrote: Gabriel Genellina wrote: En Mon, 23 Feb 2009 23:31:20 -0200, bvdp escribió: Gabriel Genellina wrote: En Mon, 23 Feb 2009 22:46:34 -0200, bvdp escribió: Chris Rebert wrote: On Mon, Feb 23, 2009 at 4:26 PM, bvdp wrote: [problem with

Re: Newb question: underscore

2008-06-05 Thread bvdp
My guess would be someone has used the common convention of naming the "get the corresponding localised version of this string from the application's gettext database" function as '_' for convenience. Funny that this comes up. I just noticed this in some code I was looking at the other day.

Simple and safe evaluator

2008-06-11 Thread bvdp
Is there a simple/safe expression evaluator I can use in a python program. I just want to pass along a string in the form "1 + 44 / 3" or perhaps "1 + (-4.3*5)" and get a numeric result. I can do this with eval() but I really don't want to subject my users to the problems with that method.

Re: Simple and safe evaluator

2008-06-11 Thread bvdp
Matimus wrote: On Jun 11, 1:25 pm, bvdp <[EMAIL PROTECTED]> wrote: Is there a simple/safe expression evaluator I can use in a python program. I just want to pass along a string in the form "1 + 44 / 3" or perhaps "1 + (-4.3*5)" and get a numeric result. I can do thi

Re: Simple and safe evaluator

2008-06-11 Thread bvdp
Simon Forman wrote: On Jun 11, 1:25 pm, bvdp <[EMAIL PROTECTED]> wrote: Is there a simple/safe expression evaluator I can use in a python program. I just want to pass along a string in the form "1 + 44 / 3" or perhaps "1 + (-4.3*5)" and get a numeric result. I can

Re: Simple and safe evaluator

2008-06-11 Thread bvdp
I'm finding my quest for a safe eval() quite frustrating :) Any comments on this: Just forget about getting python to do this and, instead, grab my set of values (from a user supplied text file) and call an external program like 'bc' to do the dirty work. I think that this would avoid someone

Re: Simple and safe evaluator

2008-06-11 Thread bvdp
Matimus wrote: The solution I posted should work and is safe. It may not seem very readable, but it is using Pythons internal parser to parse the passed in string into an abstract symbol tree (rather than code). Normally Python would just use the ast internally to create code. Instead I've writ

Re: Simple and safe evaluator

2008-06-12 Thread bvdp
Matimus wrote: On Jun 11, 9:16 pm, George Sakkis <[EMAIL PROTECTED]> wrote: On Jun 11, 8:15 pm, bvdp <[EMAIL PROTECTED]> wrote: Matimus wrote: The solution I posted should work and is safe. It may not seem very readable, but it is using Pythons internal parser to parse the pass

Re: howto split string with both comma and semicolon delimiters

2008-06-12 Thread bvdp
dmitrey wrote: hi all, howto split string with both comma and semicolon delimiters? i.e. (for example) get ['a','b','c'] from string "a,b;c" I have tried s.split(',;') but it don't work Thx, D. Howabout: s = s.replace(";", ",") s = s.split(",") -- http://mail.python.org/mail

Re: Simple and safe evaluator

2008-06-12 Thread bvdp
George Sakkis wrote: You probably missed the point in the posted examples. A malicious user doesn't need to modify your program code to have access to far more than you would hope, just devise an appropriate string s and pass it to your "safe" eval. Oppps, I did miss the point. I was assuming

Re: Simple and safe evaluator

2008-06-16 Thread bvdp
x try: ast = compile(text, "", 'eval', _ast.PyCF_ONLY_AST) except: error("Expression error in '%s'" % text) etx = text # for error reporting, bvdp return _traverse(ast.body) try: import _ast num_eval = safe_eval except:

Re: Simple and safe evaluator

2008-06-16 Thread bvdp
George Sakkis wrote: On Jun 16, 4:47 pm, bvdp <[EMAIL PROTECTED]> wrote: 2. I thought I'd be happy with * / + -, etc. Of course now I want to add a few more funcs like int() and sin(). How would I do that? For the builtin eval, just populate the globals dict with the names you w

Re: Simple and safe evaluator

2008-06-16 Thread bvdp
[EMAIL PROTECTED] wrote: On Jun 17, 8:02 am, bvdp <[EMAIL PROTECTED]> wrote: Thanks. That was easy :) The change to the _ast version is left as an exercise to the reader ;) And I have absolutely no idea on how to do this. I can't even find the _ast import file on my system. I

Re: Simple and safe evaluator

2008-06-20 Thread bvdp
Aahz wrote: In article <[EMAIL PROTECTED]>, Simon Forman <[EMAIL PROTECTED]> wrote: FWIW, I got around to implementing a function that checks if a string is safe to evaluate (that it consists only of numbers, operators, and "(" and ")"). Here it is. :) What's safe about "1000 ** 1000

Function to import module to namespace

2008-06-29 Thread bvdp
Is it possible to do this from a function: import a module and append the defs in that module to an existing module/namesapce. So, in my code I have something like: # main code import mods def loadmore(n): import_module(n, mods) # end of main this will permit the addition of the th

Re: Function to import module to namespace

2008-06-29 Thread bvdp
Terry Reedy wrote: bvdp wrote: Is it possible to do this from a function: import a module and append the defs in that module to an existing module/namesapce. So, in my code I have something like: # main code import mods def loadmore(n): import_module(n, mods) # end of main

Re: Function to import module to namespace

2008-06-29 Thread bvdp
Terry Reedy wrote: > > > Do you mean something like this? > >>> math.__dict__.update(string.__dict__) > >>> dir(math) > ['Formatter', 'Template', '_TemplateMetaclass', '__builtins__', I think this is working First off, 2 module files: funcs.py def func1(): print "I'm func1

Re: Function to import module to namespace

2008-06-29 Thread bvdp
John Machin wrote: Good questions. Short answer ... probably 'cause I've not thought the problem though completely :) > You are updating with *everything* in the 'more' module, not just the > functions. This includes such things as __name__, __doc__, __file__. > Could have interesting side-e

Re: Function to import module to namespace

2008-06-30 Thread bvdp
John Machin wrote: On Jun 30, 11:45 am, bvdp <[EMAIL PROTECTED]> wrote: John Machin wrote: Good questions. Short answer ... probably 'cause I've not thought the problem though completely :) > You are updating with *everything* in the 'more' module, not just th

Pop return from stack?

2010-08-14 Thread bvdp
Assuming I have a module 'foo.py' with something like this: def error(s): print "Error", s sys.exit(1) def func(s): ... do some processing ... call error() if bad .. go to system exit. ... more processing and then I write a new program, test.py, which: import foo def myerr

Re: Pop return from stack?

2010-08-14 Thread bvdp
> An exception will walk up the stack, calling any cleaning-up code that needs > to be done (removing object references, executing finally: blocks, exiting > context managers properly. It won't break anything. Don't be afraid of > Python's high-level features! Okay, I believe you (and the rest of

Re: Pop return from stack?

2010-08-14 Thread bvdp
On Aug 14, 5:23 pm, Steven D'Aprano wrote: > This general technique is called "monkey patching". > New term for me :) > > Now, if an error is encountered myerror() is called. Fine. But execution > > resumes in func(). Not exactly what I wanted. > > Of course it does. Your new error handler fail

Re: Pop return from stack?

2010-08-15 Thread bvdp
On Aug 15, 12:52 pm, John Nagle wrote: > On 8/14/2010 4:05 PM, bvdp wrote: > > > Assuming I have a module 'foo.py' with something like this: > > > def error(s): > >      print "Error", s > >      sys.exit(1) > > > def func(s): > >

Re: Pop return from stack?

2010-08-21 Thread bvdp
On Aug 20, 6:41 pm, Gregory Ewing wrote: > bvdp wrote: > > The whole problem I was having is that I was trying to tie a small > > application (an helper to the main application) to use a bit of the > > existing code as a pseudo-library. > > This is precisely the rea

Creating slice notation from string

2009-09-02 Thread bvdp
I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Is it possible to take an arbitrary string in the form "1:2", "1", ":-1", etc. and feed it to slice() and then apply the result to an existing list? For example, I have a normal python lis