Re: Lie Hetland book: Beginning Python..
Gerhard Häring ha scritto: > The reason why pysqlite 0.x/1.x used paramstyle "pyformat", based on > Python string substitution for SQL parameters is that at the time > pysqlite was started, SQLite 2.x did not have any support for parameter > binding. So we had to "fake" it in Python, just like the MySQL interface > does (for the same reasons). Thanks Gerhard for your valuable help. -- http://mail.python.org/mailman/listinfo/python-list
string find/replace
Hello, I want the Python equivalent of the Perl expression: s/([a-z])([A-Z])/\1 \2/g In plain language: place a space between a lowercase and uppercase letter. I get lost in the RE module. Can someone help me? Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: string find/replace
On 2010-12-01, Peter Otten <__pete...@web.de> wrote: import re re.compile("([a-z])([A-Z])").sub(r"\1 \2", "camelCase") > 'camel Case' Very simple if you know it. Thank you! -- http://mail.python.org/mailman/listinfo/python-list
UTF-8 question from Dive into Python 3
Hi, recently I had to study *seriously* Unicode and encodings for one project in Python but I left with a couple of doubts arised after reading the unicode chapter of Dive into Python 3 book by Mark Pilgrim. 1- Mark says: "Also (and you’ll have to trust me on this, because I’m not going to show you the math), due to the exact nature of the bit twiddling, there are no byte-ordering issues. A document encoded in UTF-8 uses the exact same stream of bytes on any computer." Is it true UTF-8 does not have any "big-endian/little-endian" issue because of its encoding method? And if it is true, why Mark (and everyone does) writes about UTF-8 with and without BOM some chapters later? What would be the BOM purpose then? 2- If that were true, can you point me to some documentation about the math that, as Mark says, demonstrates this? thank you Carlo -- http://mail.python.org/mailman/listinfo/python-list
Re: UTF-8 question from Dive into Python 3
On 17 Gen, 23:34, Antoine Pitrou wrote: > On Mon, 17 Jan 2011 14:19:13 -0800 (PST) > > carlo wrote: > > Is it true UTF-8 does not have any "big-endian/little-endian" issue > > because of its encoding method? > > Yes. > > > And if it is true, why Mark (and > > everyone does) writes about UTF-8 with and without BOM some chapters > > later? What would be the BOM purpose then? > > "BOM" in this case is a misnomer. For UTF-8, it is only used as a > marker (a magic number, if you like) to signal than a given text file > is UTF-8. The UTF-8 "BOM" does not say anything about byte order; and, > actually, it does not change with endianness. > > (note that it is not required to put an UTF-8 "BOM" at the beginning of > text files; it is just a hint that some tools use when > generating/reading UTF-8) > > > 2- If that were true, can you point me to some documentation about the > > math that, as Mark says, demonstrates this? > > Math? UTF-8 is simply a byte-oriented (rather than word-oriented) > encoding. There is no math involved, it just works by construction. > > Regards > > Antoine. thank you all, eventually found http://www.unicode.org/versions/Unicode5.2.0/ch03.pdf#G7404 which clears up. No math in fact, as Tim and Antoine pointed out. -- http://mail.python.org/mailman/listinfo/python-list
Simple Python struct issue
I saw an article on O'Reilly about using NumPy and Dislin to analyze and visualize WAV files. It's a really fantastic article but was a little out of date. I updated the script to work with the newer modules &etc but am still having trouble getting it working. The line temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - 128.0 always returns the same error: "Traceback (most recent call last): File "pysono.py", line 31, in temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - 128.0 struct.error: unpack requires a string argument of length 256" when I do python pysono.py test.wav 256 I'm sure it's probably something simple but I just can't see what it is! Here's the original code: http://onlamp.com/python/2001/01/31/graphics/pysono.py Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple Python struct issue
On Oct 2, 12:15 pm, Simon Forman wrote: > On Fri, Oct 2, 2009 at 12:07 PM, Carlo DiCelico > > > > > > wrote: > > I saw an article on O'Reilly about using NumPy and Dislin to analyze > > and visualize WAV files. It's a really fantastic article but was a > > little out of date. I updated the script to work with the newer > > modules &etc but am still having trouble getting it working. > > > The line > > > temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - > > 128.0 > > > always returns the same error: "Traceback (most recent call last): > > File "pysono.py", line 31, in > > temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - > > 128.0 > > struct.error: unpack requires a string argument of length 256" when I > > do python pysono.py test.wav 256 > > > I'm sure it's probably something simple but I just can't see what it > > is! > > > Here's the original > > code:http://onlamp.com/python/2001/01/31/graphics/pysono.py > > > Thanks! > > In: > > struct.unpack("%dB" % (fft_length), tempb) > > tempb is not length 256. > > Also, note that (foo) is the same as just foo. To create a tuple of > length 1 you must say (foo,) > > HTH, > ~Simon I'm sorry, I'm not sure what you're referring to when you say "Also, note that (foo) is the same as just foo. To create a tuple of length 1 you must say (foo,)". The 256 is passed into the script as an argument and then assigned to the variable fft_length, which you can see in that line. So, whatever value I pass in comes out in the error. What you're saying is that the error happens because tempb isn't the length of any of those values (i.e., 128, 256, 512, etc)? (1) How could I determine the length of tempb? and (2) It's doing this -> tempb = fp.readframes(fft_length); right before doing the struct.unpack (); could the splitting of the frames into frames of length fft_length be causing this error? Thanks for the help! Carlo -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple Python struct issue
On Oct 2, 3:17 pm, Simon Forman wrote: > On Fri, Oct 2, 2009 at 12:35 PM, Carlo DiCelico > > > > > > wrote: > > On Oct 2, 12:15 pm, Simon Forman wrote: > >> On Fri, Oct 2, 2009 at 12:07 PM, Carlo DiCelico > >> wrote: > >> > I saw an article on O'Reilly about using NumPy and Dislin to analyze > >> > and visualize WAV files. It's a really fantastic article but was a > >> > little out of date. I updated the script to work with the newer > >> > modules &etc but am still having trouble getting it working. > > >> > The line > > >> > temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - > >> > 128.0 > > >> > always returns the same error: "Traceback (most recent call last): > >> > File "pysono.py", line 31, in > >> > temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - > >> > 128.0 > >> > struct.error: unpack requires a string argument of length 256" when I > >> > do python pysono.py test.wav 256 > > >> > I'm sure it's probably something simple but I just can't see what it > >> > is! > > >> > Here's the original > >> > code:http://onlamp.com/python/2001/01/31/graphics/pysono.py > > >> > Thanks! > > >> In: > > >> struct.unpack("%dB" % (fft_length), tempb) > > >> tempb is not length 256. > > >> Also, note that (foo) is the same as just foo. To create a tuple of > >> length 1 you must say (foo,) > > >> HTH, > >> ~Simon > > > I'm sorry, I'm not sure what you're referring to when you say "Also, > > note that (foo) is the same as just foo. To create a tuple of length > > 1 you must say (foo,)". > > Sorry about that. All I mean is that in python putting parentheses > around an expression "(some_object)" does not create a tuple unless > you also use a comma "," > > I hope the following examples show what I mean: > > In [1]: (2) > Out[1]: 2 > > In [2]: (2,) > Out[2]: (2,) > > In [3]: 2, > Out[3]: (2,) > > In [4]: n = 2, > > In [5]: "%s" % (n,) > Out[5]: '(2,)' > > In [6]: "%s" % (n) > Out[6]: '2' > > In [7]: "%s" % n > Out[7]: '2' > > Your code: > > "%dB" % (fft_length) > > is the same as > > "%dB" % fft_length > > But what you probably meant was > > "%dB" % (fft_length,) > > Note the comma. People do this to prevent string formatting errors if > fft_length should ever happen to be a tuple rather than an int (or > whatever.) > > > The 256 is passed into the script as an > > argument and then assigned to the variable fft_length, which you can > > see in that line. So, whatever value I pass in comes out in the error. > > What you're saying is that the error happens because tempb isn't the > > length of any of those values (i.e., 128, 256, 512, etc)? > > Yes, whatever tempb is, it isn't "a string argument of length 256" as > the traceback says. > > > (1) How > > could I determine the length of tempb? > > If it's a string, len(tempb) will return it's length. > > > and (2) It's doing this -> > > tempb = fp.readframes(fft_length); right before doing the struct.unpack > > (); could the splitting of the frames into frames of length fft_length > > be causing this error? > > See MRAB's reply. :] > > > Thanks for the help! > > You're very welcome. > > One other thing, in the code you posted it has "Float", but in the > traceback it has "float". Generally speaking when you post code to a > newsgroup for help you should paste in the exact code, rather than > retyping it manually. > > Warm regards, and happy hacking! Ah, yes, makes so much more sense now. Thanks very much for the help! The discrepancy between Float and float has to do with the fact that in the code I linked to, he's using an old version of numpy. In my code, I'm using the new version of numpy, which uses float instead of Float. Sorry about that, thanks again. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple Python struct issue
On Oct 2, 12:49 pm, MRAB wrote: > Carlo DiCelico wrote: > > I saw an article on O'Reilly about using NumPy and Dislin to analyze > > and visualize WAV files. It's a really fantastic article but was a > > little out of date. I updated the script to work with the newer > > modules &etc but am still having trouble getting it working. > > > The line > > > temp[i,:] = array(struct.unpack("%dB"%(fft_length), tempb),Float) - > > 128.0 > > > always returns the same error: "Traceback (most recent call last): > > File "pysono.py", line 31, in > > temp[i,:] = array(struct.unpack("%dB"%(fft_length),tempb),float) - > > 128.0 > > struct.error: unpack requires a string argument of length 256" when I > > do python pysono.py test.wav 256 > > > I'm sure it's probably something simple but I just can't see what it > > is! > > > Here's the original > > code:http://onlamp.com/python/2001/01/31/graphics/pysono.py > > .readframes(n) returns (at most) n _frames_ as a bytestring (str), not n > bytes. I tried reading 256 frames from a .wav file containing stereo at > 16 bits per channel and got 1024 bytes (4 bytes per frame, not > surprisingly!). Yes! Thank you very much for pointing this out, don't know how I missed it! -- http://mail.python.org/mailman/listinfo/python-list
Image to SVG conversion with Python
I need to convert JPEG and PNG files to SVG. I'm currently using PIL to generate the JPEG/PNG files to begin with. However, I need to be able to scale the generated images up in size without a loss of image quality. Using SVG seems to be the best way to do this, other than generating the images in the maximum size/resolution in the first place, which would be too resource intensive. I've looked around online and have found some tools for creating SVGs but none for converting an image into SVG. Does anyone have any experience doing this? What would be the best way to go about this? Thanks, Carlo -- http://mail.python.org/mailman/listinfo/python-list
Re: Image to SVG conversion with Python
On Nov 16, 11:48 am, Dave Angel wrote: > Carlo DiCelico wrote: > > I need to convert JPEG and PNG files to SVG. I'm currently using PIL > > to generate the JPEG/PNG files to begin with. However, I need to be > > able to scale the generated images up in size without a loss of image > > quality. Using SVG seems to be the best way to do this, other than > > generating the images in the maximum size/resolution in the first > > place, which would be too resource intensive. > > > I've looked around online and have found some tools for creating SVGs > > but none for converting an image into SVG. > > > Does anyone have any experience doing this? What would be the best way > > to go about this? > > > Thanks, > > Carlo > > I have no direct experience with SVG, but have used and analyzed other > formats. > > I expect it's unreasonable to convert jpg or png files to SVG. The > latter is a vector format, and can't efficiently represent pixels, which > is all you have in the jpg files. And even if you did it brute force, > it still wouldn't scale any better than the original jpg. If the jpg > file was generated from lines, and wasn't too crowded, it *might* be > possible to analyze it to reconstruct the vectors, but it would be both > very slow, and inaccurate. > > In Photoshop PSD files, you can have vector layers and RGB layers (plus > other kinds). You convert a vector layer (such as text) to bits by > rasterizing (or flattening). And once you do, it no longer scales > cleanly. For instance, when I'm sending composite images to a printer, > I get much better quality sending the raster portion separate from the > text, either as layers in a PSD file, or in a PDF file, or even as two > separate files that they will merge later. (In that case, I usually > send a low-res flattened file, so they can see how it's supposed to look) > > I'd say you should change your python code to generate the svg files > first (perhaps usinghttp://code.activestate.com/recipes/325823/) > > Then you might want to use (http://www.imagemagick.org/script/index.php > ) to convert it to jpg or other format. > > DaveA Thanks, this makes perfect sense. -- http://mail.python.org/mailman/listinfo/python-list
Re: ctypes list library
Diez B. Roggisch wrote: > luca72 schrieb: >> There is a command for ctypes that help me to know the entry points >> inside a library. > > dir() on a loaded library? > > But it won't do you any good, without having the header-file you can't > possibly know what the functions take for parameters. I was trying this right now, but I can't even get the exported function names: >>> from ctypes import * >>> l = cdll.msvcrt >>> type(l) >>> dir(l) ['_FuncPtr', '__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_func_flags_', '_func_restype_', '_handle', '_name'] shouldn't the C functions names be there? -- http://mail.python.org/mailman/listinfo/python-list
Re: str.format fails with JSON?
Em terça-feira, 21 de fevereiro de 2017 11:39:13 UTC-3, Chris Angelico escreveu: > On Wed, Feb 22, 2017 at 1:23 AM, Carlo Pires wrote: > > Hi, > > > > When I run this piece of code: > > > > 'From {"value": 1}, value={value}'.format(value=1) > > Firstly, this code is in error; I suspect you wanted a couple of > literal braces, so what you want is: What I wanted was an output without errors, like: From {"value": 1}, value=1 I think Python should ignore value not matching integers or identifiers. This will make str.format more robust and usable. It cannot be used if the text has JSON as string, for instance. -- https://mail.python.org/mailman/listinfo/python-list