Re: Change a file type in Python?

2013-12-01 Thread Mark Lawrence

On 01/12/2013 00:04, Eamonn Rea wrote:

Thanks for the help!

Ok, I'll look into the mailing list.



It's very useful, you can even see things in context, which is 
conspicious by its absence above :)


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: Change a file type in Python?

2013-12-01 Thread Steven D'Aprano
On Sun, 01 Dec 2013 16:03:17 +1100, Chris Angelico wrote:

> Most of it is getting annoyed at the results of 3, and then attacking 3.

I know the feeling. I've never trusted 3, I've always felt that it's 
plotting something. And it looks like half an 8, but it's not. What's 
with that?


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


Re: Python Unicode handling wins again -- mostly

2013-12-01 Thread wxjmfauth
Le dimanche 1 décembre 2013 00:07:36 UTC+1, Ned Batchelder a écrit :
> On 11/30/13 5:37 PM, Gregory Ewing wrote:
> 
> > [email protected] wrote:
> 
> >> And do you know the origin of this typographical feature?
> 
> >> Because, mechanically, the dot of the "i" broke too often.
> 
> >>
> 
> >> In my opinion, a very plausible explanation.
> 
> >
> 
> > It doesn't sound very plausible to me, because there
> 
> > are a lot more stand-alone 'i's in English text than
> 
> > there are ones following an f. What is there to stop
> 
> > them from breaking?
> 
> >
> 
> > It's more likely to be simply a kerning issue. You
> 
> > want to get the stems of the f and the i close together,
> 
> > and the only practical way to do that with mechanical
> 
> > type is to merge them into one piece of metal.
> 
> >
> 
> > Which makes it even sillier to have an 'ffi' character
> 
> > in this day and age, when you can simply space the
> 
> > characters so that they overlap.
> 
> >
> 
> 
> 
> The fi ligature was created because visually, an f and i wouldn't work 
> 
> well together: the crossbar of the f was near, but not connected to the 
> 
> serif of the i, and the terminal bulb of the f was close to, but not 
> 
> coincident, with the dot of the i.
> 
> 
> 
> This article goes into great detail, and has a good illustration of how 
> 
> an f and i can clash, and how an fi ligature can fix the problem: 
> 
> http://opentype.info/blog/2012/11/20/whats-a-ligature/ . Note the second 
> 
> fi illustration, which demonstrates using a ligature to make the letters 
> 
> appear *less* connected than they would individually!
> 
> 
> 
> This is also why "simply spacing the characters" isn't a solution: a 
> 
> specially designed ligature looks better than a separate f and i, no 
> 
> matter how minutely kerned.
> 
> 
> 
> It's unfortunate that Unicode includes presentation alternatives like 
> 
> the fi (and ff, fl, ffi, and fl) ligatures.  It was done to be a 
> 
> superset of existing encodings.
> 
> 
> 
> Many typefaces have other non-encoded ligatures as well, especially 
> 
> display faces, which also have alternate glyphs.  Unicode is a funny mix 
> 
> in that it includes some forms of alternates, but can't include all of 
> 
> them, so we have to put up with both an ad-hoc Unicode that includes 
> 
> presentational variants, and also some other way to specify variants 
> 
> because Unicode can't include all of them.
> 


I'm speaking about those times where the "characters" (some) were
not even built with metal, but with wood (see Garamond, Bodoni).

-

Unicode is "only" collecting "characters" in the sense "abstract
entities". What is supposed to be a "character" is one problem.
How a tool is supposed to handle these "characters" is a problem
too, but a different one.

"Unicode" is not a coding scheme, it is a "repertoire".

Illustrative examples instead of explanations.

The ffl ligature is a "character" because it has always
existed.

The & and œ are considered today as unique "characters".
They were historically "ligaturated forms".

The Fahrenheit, Kelvin and Celsius are considered as
"characters", despite Fahrenheit, Kelvin are "letters".

Text justification. Calculating the space between "words"
in "rendering units" makes sense. Using a specific "character"
like a thin space to force a predefined space makes sense too.

The miscellaneous zeroes one may see, like uppercase O, O with
a dot in the center or a striked O are all the same zero, but
with stylistic variants, => a single "character" in the unicode
table.

... but this medieval "character" existing in two forms (I do not
remember which one) was finally registrated as two "characters",
and not as a stylistic variant of a single "character".

There are no "characters" for the symbols of the chemical elements,
a latin script is good enough.

The QPlainTextEdit widget from Qt does not know '\n'. It uses
only the paragraph separator and the line separator. To render
a paragraph separator, it uses one another "character", the
pilcrow.

The µ "character" in the iso-8859-1 coding scheme is a greek
letter, it must be used or percieved as a SI unit prefix.
Unicode category: Ll, unicode name: micro sign.

How to place an arrow (vector) on top of an ê, if one cann't
decompose it?

Related, there are dotless variants of i and j.

STIX fonts with the huge number of math symbols, not
yet in the unicode repertoire but present in the PUA.

etc.

Unicode is quite open. It's a good idea to keep that
openess to the developer. Shortly, if a coder decomposes
a "character" like "â" in a "a" plus a "^", it's up to
the developer to know what to do when reversing such a
string and to count this sequence as two real "characters".

jmf



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


Re: Python Unicode handling wins again -- mostly

2013-12-01 Thread Serhiy Storchaka

30.11.13 02:44, Steven D'Aprano написав(ла):

(2) If you reverse that string, does it give "lëon"? The implication of
this question is that strings should operate on grapheme clusters rather
than code points. Python fails this test:

py> print("noe\u0308l"[::-1])
leon


>>> print(unicodedata.normalize('NFC', "noe\u0308l")[::-1])
lëon


(3) What are the first three characters? The author suggests that the
answer should be "noë", in which case Python fails again:

py> print("noe\u0308l"[:3])
noe


>>> print(unicodedata.normalize('NFC', "noe\u0308l")[:3])
noë


(4) Likewise, what is the length of the decomposed string? The author
expects 4, but Python gives 5:

py> len("noe\u0308l")
5


>>> print(len(unicodedata.normalize('NFC', "noe\u0308l")))
4


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


Extending the 'function' built-in class

2013-12-01 Thread G.
Hi, I can't figure out how I can extend the 'function' built-in class. I tried:
  class test(function):
def test(self):
  print("test")
but I get an error. Is it possible ?

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


Re: Extending the 'function' built-in class

2013-12-01 Thread Roy Smith
In article <[email protected]>,
 "G."  wrote:

> Hi, I can't figure out how I can extend the 'function' built-in class. I 
> tried:
>   class test(function):
> def test(self):
>   print("test")
> but I get an error. Is it possible ?
> 
> Regards, G.

It really helps to give us some basic information when asking questions.  
To start, what version of Python are you using, and what error message 
do you get?

At least in Python 2 (but, I'm guessing, maybe, you're using Python 3, 
since you put parens in your print() statement?), there is no built-in 
class called "function".  There are built-in functions, and they are of 
type builtin_function_or_method.  When I try to subclass that by doing:

class foo(type(open)):
pass

I get:

Traceback (most recent call last):
  File "", line 1, in 
TypeError: Error when calling the metaclass bases
type 'builtin_function_or_method' is not an acceptable base type

So, we're back to asking what version you're using and what error 
message you got.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extending the 'function' built-in class

2013-12-01 Thread G.
Le 01-12-2013, Roy Smith  a écrit :
>
> class foo(type(open)):
> pass
>
> I get:
>
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: Error when calling the metaclass bases
> type 'builtin_function_or_method' is not an acceptable base type
>
> So, we're back to asking what version you're using and what error 
> message you got.

Hi, I don't care that much for the version, since I wanted rather to perform
some tests. I tried your code with various versions and got the same message
than yours. Thus I guess the type can't be extended. Regards, G.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extending the 'function' built-in class

2013-12-01 Thread Tim Chase
On 2013-12-01 19:18, G. wrote:
> Hi, I can't figure out how I can extend the 'function' built-in
> class. I tried: class test(function):
> def test(self):
>   print("test")
> but I get an error. Is it possible ?

While I don't have an answer, I did find this interesting.  First,
"function" doesn't seem to be in the default __buitin__ namespace:

  >>> function
  Traceback (most recent call last):
File "", line 1, in 
  NameError: name 'function' is not defined

I presume you're doing it with the following:

  >>> from types import FunctionType
  >>> class MyFunc(FunctionType):
  ... pass
  ... 
  Traceback (most recent call last):
File "", line 1, in 
  TypeError: Error when calling the metaclass bases
  type 'function' is not an acceptable base type

but, as you mention, the inability to subclass it is somewhat
peculiar.  It appears to be metaclass-related.

I'm not quite sure *why* one might want to subclass FunctionType, but
I'm also not sure why you should be *prevented* from subclassing it.

-tkc


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


Re: Extending the 'function' built-in class

2013-12-01 Thread Gary Herron

On 12/01/2013 11:18 AM, G. wrote:

Hi, I can't figure out how I can extend the 'function' built-in class. I tried:
   class test(function):
 def test(self):
   print("test")
but I get an error. Is it possible ?

Regards, G.


What error do you get?
What version of Python?
What OS?

And in particular: What 'function' built-in class?  I know of no such 
thing, and the error message I get with your code says exactly that:

  NameError: name 'function' is not defined
Did you not get that same error?

All of which begs the questions: What do you think the function class 
is, and why are you trying to extend it?


Gary Herron

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


Re: Extending the 'function' built-in class

2013-12-01 Thread G.
Le 01-12-2013, Gary Herron  a écrit :
> And in particular: What 'function' built-in class?  I know of no such 
> thing, and the error message I get with your code says exactly that:
>NameError: name 'function' is not defined
> Did you not get that same error?

Yes, indeed. The 'function' built-in class was the following one:
>>> type(lambda x: 2*x)


but I am interested by answers concerning other similar types also.

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


Re: Extending the 'function' built-in class

2013-12-01 Thread Robert Kern

On 2013-12-01 19:43, Tim Chase wrote:


I'm not quite sure *why* one might want to subclass FunctionType, but
I'm also not sure why you should be *prevented* from subclassing it.


Previously:

http://grokbase.com/t/python/python-list/033r5nks47/type-function-does-not-subtype#20030324rcnwbkfedhzbaf3vmiuer3z4xq

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Python Unicode handling wins again -- mostly

2013-12-01 Thread wxjmfauth
0.11.13 02:44, Steven D'Aprano написав(ла):
> (2) If you reverse that string, does it give "lëon"? The implication of
> this question is that strings should operate on grapheme clusters rather
> than code points. ...
> 

BTW, a grapheme cluster *is* a code points cluster.

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


Re: Python Unicode handling wins again -- mostly

2013-12-01 Thread Tim Delaney
On 2 December 2013 07:15,  wrote:

> 0.11.13 02:44, Steven D'Aprano написав(ла):
> > (2) If you reverse that string, does it give "lëon"? The implication of
> > this question is that strings should operate on grapheme clusters rather
> > than code points. ...
> >
>
> BTW, a grapheme cluster *is* a code points cluster.
>

Anyone with a decent level of reading comprehension would have understood
that Steven knows that. The implied word is "individual" i.e. "... rather
than [individual] code points".

Why am I responding to a troll? Probably because out of all his baseless
complaints about the FSR, he *did* have one valid point about performance
that has now been fixed.

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


Checking Common File Types

2013-12-01 Thread jade
Hello, 
I'm trying to create a script that checks all the files in my 'downloaded' 
directory against common file types and then tells me how many of the files in 
that directory aren't either a GIF or a JPG file. I'm familiar with basic 
Python but this is the first time I've attempted anything like this and I'm 
looking for a little help or a point in the right direction? 

file_sigs = {'\xFF\xD8\xFF':('JPEG','jpg'),  '\x47\x49\x46':('GIF','gif')}
def readFile():filename = r'c:/temp/downloads'  fh = open(filename, 
'r') file_sig = fh.read(4) print '[*] check_sig() File:',filename #, 'Hash 
Sig:', binascii.hexlify(file_sig) 
RegardsJade



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


wiimote IR tracking in python

2013-12-01 Thread martin koh
Hi all..may i know how to do the IR tracking by using wiimote? i did tried the 
source code from 
(http://ph-elec.com/archives/simple-python-wii-mote-test/) using command prompt 
but i can't compile successfully, i think is because i don't have any idea how 
to import the cwiid module. Can anyone please provide me some informations 
about this? Thanks so much.
  -- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python Unicode handling wins again -- mostly

2013-12-01 Thread Mark Lawrence

On 01/12/2013 20:54, Tim Delaney wrote:

On 2 December 2013 07:15, mailto:[email protected]>> wrote:

0.11.13 02:44, Steven D'Aprano написав(ла):
 > (2) If you reverse that string, does it give "lëon"? The
implication of
 > this question is that strings should operate on grapheme clusters
rather
 > than code points. ...
 >

BTW, a grapheme cluster *is* a code points cluster.


Anyone with a decent level of reading comprehension would have
understood that Steven knows that. The implied word is "individual" i.e.
"... rather than [individual] code points".

Why am I responding to a troll? Probably because out of all his baseless
complaints about the FSR, he *did* have one valid point about
performance that has now been fixed.

Tim Delaney




I don't remember him ever having a valid point, so FTR can we have a 
reference please.  I do remember Steven D'Aprano showing that there was 
a regression which I flagged up here http://bugs.python.org/issue16061. 
 It was fixed by Serhiy Storchaka, who appears to have forgotten more 
about Python than I'll ever know, grrr!!! :)


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: wiimote IR tracking in python

2013-12-01 Thread Johannes Findeisen
On Mon, 2 Dec 2013 05:59:07 +0800
martin koh wrote:

> Hi all..may i know how to do the IR tracking by using wiimote? i did tried 
> the source code from 
> (http://ph-elec.com/archives/simple-python-wii-mote-test/) using command 
> prompt but i can't compile successfully, i think is because i don't have any 
> idea how to import the cwiid module. Can anyone please provide me some 
> informations about this? Thanks so much.

Did you took a look here: http://abstrakraft.org/cwiid/ ?

There you find the sources and installation instructions in a README
file.

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


Re: Python Unicode handling wins again -- mostly

2013-12-01 Thread Tim Delaney
On 2 December 2013 09:06, Mark Lawrence  wrote:

> I don't remember him ever having a valid point, so FTR can we have a
> reference please.  I do remember Steven D'Aprano showing that there was a
> regression which I flagged up here http://bugs.python.org/issue16061.  It
> was fixed by Serhiy Storchaka, who appears to have forgotten more about
> Python than I'll ever know, grrr!!! :)
>

>From your own bug report (quoting Steven): "Nevertheless, I think there is
something here. The consequences are nowhere near as dramatic as jmf claims
..."

His initial postings did lead to a regression being found.

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


Re: Checking Common File Types

2013-12-01 Thread Chris Angelico
On Mon, Dec 2, 2013 at 5:27 AM, jade  wrote:
> file_sigs = {'\xFF\xD8\xFF':('JPEG','jpg'),
>  '\x47\x49\x46':('GIF','gif')}
>
> file_sig = fh.read(4)

You're reading in four bytes, but your signatures are three bytes long. :)

After that, all you need to do is look up file_sig in the file_sigs
dictionary, and see if anything's there. Look at the Python docs for
the dict type; there's an easy way to do this.

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


Re: Python Unicode handling wins again -- mostly

2013-12-01 Thread Mark Lawrence

On 01/12/2013 22:29, Tim Delaney wrote:

On 2 December 2013 09:06, Mark Lawrence mailto:[email protected]>> wrote:

I don't remember him ever having a valid point, so FTR can we have a
reference please.  I do remember Steven D'Aprano showing that there
was a regression which I flagged up here
http://bugs.python.org/__issue16061
.  It was fixed by Serhiy
Storchaka, who appears to have forgotten more about Python than I'll
ever know, grrr!!! :)


 From your own bug report (quoting Steven): "Nevertheless, I think there
is something here. The consequences are nowhere near as dramatic as jmf
claims ..."

His initial postings did lead to a regression being found.

Tim Delaney




I'll begrudgungly concede that point, but must state that it was was an 
edge case that is unlikely to have too much impact in the real world. 
Unfortunately he's still making his ridiculous claims about the FSR, 
hence my nickname of "Joseph McCarthy".  I'll admit to liking that, it 
just feels right to me, YMMV.


What also really riles me is that he uses double spaced google crap, 
despite repeated requests from various people here for others to fix how 
they use it, or get a decent email client.


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: Python Unicode handling wins again -- mostly

2013-12-01 Thread Ethan Furman

On 12/01/2013 02:06 PM, Mark Lawrence wrote:


I don't remember him [jmf] ever having a valid point, so FTR can we have a 
reference please.  I do remember Steven D'Aprano
showing that there was a regression which I flagged up here 
http://bugs.python.org/issue16061.  It was fixed by Serhiy
Storchaka, who appears to have forgotten more about Python than I'll ever know, 
grrr!!! :)


The initial complaint came, unsurprisingly, from jmf.  But don't worry much, even a stopped clock has a better track 
record... it's at least right twice a day.  ;)


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


Re: Python Unicode handling wins again -- mostly

2013-12-01 Thread Mark Lawrence

On 01/12/2013 22:50, Ethan Furman wrote:

On 12/01/2013 02:06 PM, Mark Lawrence wrote:


I don't remember him [jmf] ever having a valid point, so FTR can we
have a reference please.  I do remember Steven D'Aprano
showing that there was a regression which I flagged up here
http://bugs.python.org/issue16061.  It was fixed by Serhiy
Storchaka, who appears to have forgotten more about Python than I'll
ever know, grrr!!! :)


The initial complaint came, unsurprisingly, from jmf.  But don't worry
much, even a stopped clock has a better track record... it's at least
right twice a day.  ;)

--
~Ethan~


I had to chuckle, "initial complaint" indeed!!!  He first started 
complaining in August 2012 in this thread 
https://mail.python.org/pipermail/python-list/2012-August/628650.html. 
Then he continued in September 2012 in this thread 
https://mail.python.org/pipermail/python-list/2012-September/631613.html, which 
lead to issue 16061.  He's been continuing to moan on and off ever 
since, but funnily enough has *NEVER* produced a single shred of 
evidence to back his claims.  We'll have to wait until the cows come 
home before he does.


Contrast that to the Victor Stinner statement here 
http://bugs.python.org/issue16061#msg171413 "Python 3.3 is 2x faster 
than Python 3.2 to replace a character with another if the string only 
contains the character 3 times. This is not acceptable, Python 3.3 must 
be as slow as Python 3.2!"  Thinking about that I really do want the 
Python 2 code back.  Apart from the PEP 393 implementation being faster, 
using less memory and being correct, it has nothing to offer.  Now what 
Python sketch does that remind me of? :)


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: Need help with programming in python for class (beginner level)

2013-12-01 Thread Julio Schwarzbeck

On 11/29/2013 04:31 PM, [email protected] wrote:

It's for a school assignment. Basically, I need to roll 5 dies with 6 sides each. So basically, 6 random 
numbers. That part is easy. Then I need to add it up. Ok, done that. However, I also need to say something 
along the lines of "your total number was X". That's what I'm having trouble with. I added the dice 
rolls together and put them into a variable I called "number" but it seems to glitch out that 
variable is in any command other than "print number". Like, if I try to write:

print "your total number was:" number ""

It just doesn't work.

Here is my code so far:


import cgi

form = cgi.FieldStorage()
name = form.getvalue("name")
value = form.getvalue("value")


print """Content-type: text/html
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>

A CGI Script

"""
import random

die1 = random.randint(1,6)
die2 = random.randint(1,6)
die3 = random.randint(1,6)
die4 = random.randint(1,6)
die5 = random.randint(1,6)
print die1, die2, die3, die4, die5
number = die1 + die2 + die3 + die4 + die5
print "The total rolled was: "number" "

print "Thanks for playing, "  + name +  "."
print "You bet the total would be at least " + value + "."
print ""



My two "favorites":

print 'The total rolled was: %s ' % number
[py 2.7+] print('The total rolled was: {} '.format(number))

Cheers,

-- Speedbird

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


Re: Extending the 'function' built-in class

2013-12-01 Thread Mark Janssen
> Hi, I can't figure out how I can extend the 'function' built-in class. I 
> tried:
>   class test(function):
> def test(self):
>   print("test")
> but I get an error. Is it possible ?

It has to do with differing models of computation, and python isn't
designed for this.  Perhaps you're searching for the ultimate lambda?.
-- 
MarkJ
Tacoma, Washington
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extending the 'function' built-in class

2013-12-01 Thread alex23

On 2/12/2013 5:18 AM, G. wrote:

Hi, I can't figure out how I can extend the 'function' built-in class. I tried:
   class test(function):
 def test(self):
   print("test")
but I get an error. Is it possible ?


Others have pointed out that you cannot subclass the function type. 
Could you explain what you're trying to achieve? It's possible you could 
use a decorator instead:


def test(fn):
def _test():
print('test')
fn.test = _test
return fn

@test
def foo():
pass

>>> foo.test()
test

(Note that I've only included _test inside the decorator to show that 
you can create a closure to include the wrapped function, as a way of 
replicating 'self' in your class definition.)

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


RE: Checking Common File Types

2013-12-01 Thread jade


> To: [email protected]
> From: [email protected]
> Subject: Re: Checking Common File Types
> Date: Sun, 1 Dec 2013 18:23:22 -0500
> 
> On Sun, 1 Dec 2013 18:27:16 +, jade  declaimed the
> following:
> 
> >Hello, 
> >I'm trying to create a script that checks all the files in my 'downloaded' 
> >directory against common file types and then tells me how many of the files 
> >in that directory aren't either a GIF or a JPG file. I'm familiar with basic 
> >Python but this is the first time I've attempted anything like this and I'm 
> >looking for a little help or a point in the right direction? 
> >
> >file_sigs = {'\xFF\xD8\xFF':('JPEG','jpg'),  '\x47\x49\x46':('GIF','gif')}
> 
>   Apparently you presume the file extensions are inaccurate, as you are
> digging into the files for signatures.
> 
> >def readFile():filename = r'c:/temp/downloads'  fh = open(filename, 
> >'r') file_sig = fh.read(4) print '[*] check_sig() File:',filename #, 
> >'Hash Sig:', binascii.hexlify(file_sig) 
> 
>   Note: if you are hardcoding forward slashes, you don't need the raw
> indicator...
> 
>   That said, what is "c:/temp/downloads"? You apparently are opening IT
> as the file to be examined. Is it supposed to be a directory containing
> many files, a file containing a list of files, ???
> 
>   What is "check_sig" -- it looks like a function you haven't defined --
> but it's inside the quotes making a string literal that will never be
> called anyway.
> 
>   If you are just concerned with one directory of files, you might want
> to read the help file on the glob module, along with os.path
> (join/splitext/etc). Or just string methods...
> 
> >>> import glob
> >>> import os.path
> >>> TARGET = os.path.join(os.environ["USERPROFILE"],
> ...   "documents/BW-conversion/*")
> >>> TARGET = os.path.join(os.environ["USERPROFILE"],
> ...   "documents/BW-conversion/*")
> >>> files = glob.glob(TARGET)
> >>> for fn in files:
> ...   fp, fx = os.path.splitext(fn)
> ...   print "File %s purports to be of type %s" % (fn, fx.upper())
> ... 
> File C:\Users\Wulfraed\documents/BW-conversion\BW-1.jpg purports to be of
> type .JPG
> File C:\Users\Wulfraed\documents/BW-conversion\BW-2.jpg purports to be of
> type .JPG
> File C:\Users\Wulfraed\documents/BW-conversion\BW-3.jpg purports to be of
> type .JPG
> File C:\Users\Wulfraed\documents/BW-conversion\BW-4.jpg purports to be of
> type .JPG
> File C:\Users\Wulfraed\documents/BW-conversion\BWConv.html purports to be
> of type .HTML
> File C:\Users\Wulfraed\documents/BW-conversion\roo_b1.jpg purports to be of
> type .JPG
> File C:\Users\Wulfraed\documents/BW-conversion\roo_b2.jpg purports to be of
> type .JPG
> File C:\Users\Wulfraed\documents/BW-conversion\roo_b3.jpg purports to be of
> type .JPG
> File C:\Users\Wulfraed\documents/BW-conversion\roo_b4.jpg purports to be of
> type .JPG
> File C:\Users\Wulfraed\documents/BW-conversion\roo_b5.jpg purports to be of
> type .JPG
> File C:\Users\Wulfraed\documents/BW-conversion\roo_b6.jpg purports to be of
> type .JPG
> File C:\Users\Wulfraed\documents/BW-conversion\roo_col.jpg purports to be
> of type .JPG
> >>> 
> -- 
>   Wulfraed Dennis Lee Bieber AF6VN
> [email protected]://wlfraed.home.netcom.com/
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list



Hi, thanks for all your replies. I realised pretty soon after I asked for help 
that I was trying to read the wrong amount of bytes and set about completely 
rewriting my code (after a coffee break)
import sys, os, binascii
def readfile():

dictionary = {'474946':('GIF', 'gif'), 'ffd8ff':('JPEG', 'jpeg')}try:   
 files = os.listdir('C:\\Temp\\downloads')for item in 
files:f = open('C:\\Temp\\downloads\\'+ item, 'r')
file_sig = f.read(3)file_sig_hex = binascii.hexlify(file_sig)   
 if file_sig_hex in dictionary:
print item + ' is a image file, it is a ' + file_sig
else:print item + ' is not an image file, it is' 
+file_sig
print file_sig_hex

except:print 'Error. Try again'
finally:if 'f' in locals():f.close()
def main(): readfile()
if __name__ == '__main__':main()
As of right now my script prints out 'Error Try again' but when i comment out 
this part of the code;
  if file_sig_hex in dictionary:print item + ' is a 
image file' + dictionary 
else:print item + ' is not an image file, is it' 
+dictionary 

it prints the file signatures to the screen, however what I'm trying to do with 
the if statement is tell me if the file is an image and give me is signature 
and if it is not, I want it to tell me and still give me it's signature and 
tell me what type of file it is. Can anyone point out an obvious error? 
RegardsJade 

Re: Checking Common File Types

2013-12-01 Thread rusi
On Monday, December 2, 2013 5:11:15 AM UTC+5:30, jade wrote:
> > To: [email protected]
> > From: [email protected]
> > Subject: Re: Checking Common File Types
> > Date: Sun, 1 Dec 2013 18:23:22 -0500
> > 
> > On Sun, 1 Dec 2013 18:27:16 +, jade  declaimed the
> > following:
> > 
> > >Hello, 
> > >I'm trying to create a script that checks all the files in my 'downloaded' 
> > >directory against common file types and then tells me how many of the 
> > >files in that directory aren't either a GIF or a JPG file. I'm familiar 
> > >with basic Python but this is the first time I've attempted anything like 
> > >this and I'm looking for a little help or a point in the right direction? 
> > >
> > >file_sigs = {'\xFF\xD8\xFF':('JPEG','jpg'),  '\x47\x49\x46':('GIF','gif')}
> > 
> > Apparently you presume the file extensions are inaccurate, as you are
> > digging into the files for signatures.
> > 
> > >def readFile():filename = r'c:/temp/downloads'  fh = 
> > >open(filename, 'r') file_sig = fh.read(4) print '[*] check_sig() 
> > >File:',filename #, 'Hash Sig:', binascii.hexlify(file_sig) 
> > 
> > Note: if you are hardcoding forward slashes, you don't need the raw
> > indicator...
> > 
> > That said, what is "c:/temp/downloads"? You apparently are opening IT
> > as the file to be examined. Is it supposed to be a directory containing
> > many files, a file containing a list of files, ???
> > 
> > What is "check_sig" -- it looks like a function you haven't defined --
> > but it's inside the quotes making a string literal that will never be
> > called anyway.
> > 
> > If you are just concerned with one directory of files, you might want
> > to read the help file on the glob module, along with os.path
> > (join/splitext/etc). Or just string methods...
> > 
> > >>> import glob
> > >>> import os.path
> > >>> TARGET = os.path.join(os.environ["USERPROFILE"],
> > ... "documents/BW-conversion/*")
> > >>> TARGET = os.path.join(os.environ["USERPROFILE"],
> > ... "documents/BW-conversion/*")
> > >>> files = glob.glob(TARGET)
> > >>> for fn in files:
> > ... fp, fx = os.path.splitext(fn)
> > ... print "File %s purports to be of type %s" % (fn, fx.upper())
> > ... 
> > File C:\Users\Wulfraed\documents/BW-conversion\BW-1.jpg purports to be of
> > type .JPG
> > File C:\Users\Wulfraed\documents/BW-conversion\BW-2.jpg purports to be of
> > type .JPG
> > File C:\Users\Wulfraed\documents/BW-conversion\BW-3.jpg purports to be of
> > type .JPG
> > File C:\Users\Wulfraed\documents/BW-conversion\BW-4.jpg purports to be of
> > type .JPG
> > File C:\Users\Wulfraed\documents/BW-conversion\BWConv.html purports to be
> > of type .HTML
> > File C:\Users\Wulfraed\documents/BW-conversion\roo_b1.jpg purports to be of
> > type .JPG
> > File C:\Users\Wulfraed\documents/BW-conversion\roo_b2.jpg purports to be of
> > type .JPG
> > File C:\Users\Wulfraed\documents/BW-conversion\roo_b3.jpg purports to be of
> > type .JPG
> > File C:\Users\Wulfraed\documents/BW-conversion\roo_b4.jpg purports to be of
> > type .JPG
> > File C:\Users\Wulfraed\documents/BW-conversion\roo_b5.jpg purports to be of
> > type .JPG
> > File C:\Users\Wulfraed\documents/BW-conversion\roo_b6.jpg purports to be of
> > type .JPG
> > File C:\Users\Wulfraed\documents/BW-conversion\roo_col.jpg purports to be
> > of type .JPG
> > >>> 
> > -- 
> > Wulfraed Dennis Lee Bieber AF6VN
> > [email protected]://wlfraed.home.netcom.com/
> > 
> > -- 
> > https://mail.python.org/mailman/listinfo/python-list
>
>
>
> Hi, thanks for all your replies. I realised pretty soon after I asked for 
> help that I was trying to read the wrong amount of bytes and set about 
> completely rewriting my code (after a coffee break)
>
> import sys, os, binascii
>
> def readfile():
>
>
>     dictionary = {'474946':('GIF', 'gif'), 'ffd8ff':('JPEG', 'jpeg')}
>     try:
>         files = os.listdir('C:\\Temp\\downloads')        
>         for item in files:
>             f = open('C:\\Temp\\downloads\\'+ item, 'r')
>             file_sig = f.read(3)
>             file_sig_hex = binascii.hexlify(file_sig)
>                         
>             if file_sig_hex in dictionary:
>                 print item + ' is a image file, it is a ' + file_sig
>
>             else:
>                 print item + ' is not an image file, it is' +file_sig
>
>             print file_sig_hex
>
>     
>
>     except:
>         print 'Error. Try again'
>
>     finally:
>         if 'f' in locals():
>             f.close()
>
> def main():
>  
>     readfile()
>
> if __name__ == '__main__':
>     main()
>
> As of right now my script prints out 'Error Try again' but when i comment out 
> this part of the code;
>
>           if file_sig_hex in dictionary:
>                 print item + ' is a image file' + dictionary 
>
>             else:
>                 print item + ' is not an image file, is it' +dictionary 
>
>             
>
> it 

Re: Extending the 'function' built-in class

2013-12-01 Thread Steven D'Aprano
On Sun, 01 Dec 2013 19:18:58 +, G. wrote:

> Hi, I can't figure out how I can extend the 'function' built-in class. I
> tried:
>   class test(function):
> def test(self):
>   print("test")
> but I get an error. Is it possible ?


You cannot subclass the function type directly, but you can extend 
functions.

Firstly, rather than subclassing, you can use delegation and composition. 
Google for more info on delegation and composition as an alternative to 
subclassing:

https://duckduckgo.com/html/?q=delegation%20as%20alternative%20to%
20subclassing


You can also add attributes to functions:

def spam():
pass

spam.eggs = 23


Want to add a method to a function? You can do that too:

from types import MethodType
spam.method = MethodType(
lambda self, n: "%s got %d as arg" % (self.__name__, n),
spam)


It's even simpler if it doesn't need to be a method:

spam.function = lambda n: "spam got %d as arg" % n)

Want more complex behaviour? Write a callable class:

class MyCallable(object):
def __call__(self, arg):
pass

func = MyCallable()



There are plenty of ways to extend functions. Subclassing isn't one of 
them.



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