Re: CLP stats: last 500 posts

2016-12-10 Thread Steve D'Aprano
On Sat, 10 Dec 2016 03:15 pm, DFS wrote:

> On 12/09/2016 08:39 PM, Steve D'Aprano wrote:
>> On Sat, 10 Dec 2016 08:07 am, DFS wrote:
>>
>>>
>>> As of 04:04PM December 09, 2016
>>>
>>> Posts  85 Posters
>> [...]
>>
>>
>> Interesting stats, but couldn't you have post-processed the results
>> to avoid including the defamatory spam posts?
> 
> 
> Normally I don't censor, at all.  But the spams are apparently way
> off-topic, so I'll filter out Subjects containing certain keywords.

Its not just the Subject, but also the fake Sender. There are at least five
distinct senders which are (apparently) defamatory messages in Italian.
They're all pretty obvious spam, in all caps, with various email addresses.

"... MEGLIO ..." 



> The spammer will still be counted, but the stats won't show all those
> stupid Subjects.

I don't mind if the spammer is counted. They probably should be collated
together, and count as a single sender using multiple addresses. But the
false name should be expunged or elided.


>> Your post is likely to be removed from the official web archive as
>> it contains defamatory material.
> 
> Google seems to archive most Usenet posts, but there is no 'official web
> archive'.  Nor will those posts be auto-removed from GoogleGroups for
> their content.

comp.lang.python is a mirror of the python-list at python dot org mailing
list, which has an official web archive:

https://mail.python.org/pipermail/python-list/

There are many unofficial ones as well.

There are a few other people who are banned from the mailing list but still
post to the newsgroup.


> Why does this wackjob post all that Italian-language spam to clp anyway?

Why do wackjobs do anything? He has a bee in his bonnet about some other
fellow, I don't even know if its a politician or just some guy he knows,
and (apparently) spams dozens of newsgroups with defamatory posts accusing
him of being a paedophile, a criminal, and more.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: % string formatting - what special method is used for %d?

2016-12-10 Thread Steve D'Aprano
On Sat, 10 Dec 2016 06:06 pm, Veek M wrote:

> When we do:
> 
> print '%s %d' % ('hello', 10)
> 
> what special method is being invoked internally within the string-
> format-specifier?

%d requires the argument to be an int, or able to be converted to int using
the __int__ special method.


py> class X(object):
... def __int__(self):
... return 42
...
py> "%d" % X()
'42'



> format() invokes format__
> print invokes __str__

print actually invokes __str__ or __repr__, whichever is available.



> I'm basically trying to make sense of:
> 
> raise TypeError('urkle urkle %s' % list(dictionary))
> <=> raise TypeError('urkle urkle %s' % [ key1, val1, key2, val2 ]


The raise TypeError part of the code is irrelevant to your question. You
should always simplify your code to only the part that is relevant. 

raise TypeError(some_string)

behaves the same regardless of how some_string is made.


> So, the % operator reads the format specifier and notices %s and
> therefore calls __str__ in the list class to figure out how to represent
> [ key1, val1, key2, val2 ].
> 
> However what if I use %d? How do the other format specs work?


The format specifiers are similar to these:

%s => str(obj), which ends up calling __str__ or __repr__

%r => repr(obj), which ends up calling __repr__ or __str__

%c => chr(obj), or obj must be a string of length 1

%d %i %u => int(obj), which ends up calling __int__

%x %X => int(obj), then convert to hexadecimal

%o => int(obj), then convert to octal

%e %E %f %g %G => float(obj), which ends up calling __float__

%% => a literal % sign





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: CLP stats: last 500 posts

2016-12-10 Thread Terry Reedy

On 12/10/2016 3:13 AM, Steve D'Aprano wrote:

On Sat, 10 Dec 2016 03:15 pm, DFS wrote:



Normally I don't censor, at all.  But the spams are apparently way
off-topic, so I'll filter out Subjects containing certain keywords.


python-list is a spam-moderated list. 95+% of spam is filtered out.


Its not just the Subject, but also the fake Sender. There are at least five
distinct senders which are (apparently) defamatory messages in Italian.


This person actively evades our filters.


They're all pretty obvious spam, in all caps, with various email addresses.

"... MEGLIO ..." 




The spammer will still be counted,


Why reward someone who actively evades defenses?  If you want to count 
spam, it is mostly missing, at least as far as python-list is concerned.



comp.lang.python is a mirror of the python-list at python dot org mailing
list, which has an official web archive:

https://mail.python.org/pipermail/python-list/


These slanderous posts, in particular, are hand-removed from the archive 
when they get past the automatic filters.  They are no more part of 
python-list than other spam.


--
Terry Jan Reedy


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


Painting Contractors in Vasant nagar | Painting Contractors in Rajaji nagar

2016-12-10 Thread busybizz bizz
Painting Contractors in Bangalore - List of top painting services, works, 
companies in Bangalore and get painting price, companies contact addresses, 
phone numbers, ratings and evaluations right away on busybizz.com
http://busybizz.com/Painting-Contractors-Bangalore.php
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: CLP stats: last 500 posts

2016-12-10 Thread Steve D'Aprano
On Sat, 10 Dec 2016 09:28 pm, Terry Reedy wrote:

>>> The spammer will still be counted,
> 
> Why reward someone who actively evades defenses?  If you want to count
> spam, it is mostly missing, at least as far as python-list is concerned.

Its not a reward. Spammers are not like trolls, they don't hang around to
see the result of their posts. There no evidence at all that this Italian
spammer is looking for replies or responses to his(?) posts. He apparently
just fires them out.

I think that it is relevant that comp.lang.python receives X spam messages
from a certain person. It gives a picture of the health of the newsgroup:
how much of it is spam? Hopefully only a small amount.


> These slanderous posts, in particular, are hand-removed from the archive
> when they get past the automatic filters.  They are no more part of
> python-list than other spam.

Indeed. But although c.l.p is a mirror of the mailing list, it is not a
*perfect* mirror. The two do diverge: some things go to the mailing list
but apparently never make it to the newsgroup, and some things get to the
newsgroup but don't make it to the mailing list.

The stats generated by DFS are relevant to that.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: CLP stats: last 500 posts

2016-12-10 Thread Skip Montanaro
On Sat, Dec 10, 2016 at 4:28 AM, Terry Reedy  wrote:
>> comp.lang.python is a mirror of the python-list at python dot org mailing
>> list, which has an official web archive:
>>
>> https://mail.python.org/pipermail/python-list/
>
>
> These slanderous posts, in particular, are hand-removed from the archive
> when they get past the automatic filters.  They are no more part of
> python-list than other spam.

Are they still getting past SpamBayes? A couple months ago, I trained
the instance on m.p.o on a few of those spams. I haven't seen any
others since then, and no messages on the postmaster list discussing
them. I just skimmed the archives for November and December but saw no
examples. Generally, when Ralf or Mark delete spams, then manually
rewrite the Subject: and From: headers and zero out the message body.
I saw nothing like those placeholders either.

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


Re: CLP stats: last 500 posts

2016-12-10 Thread Wildman via Python-list
On Fri, 09 Dec 2016 16:07:16 -0500, DFS wrote:

> code (py2.7)
> --
> import sys as y,nntplib as t,datetime as d
> s=''
> g=y.argv[1]
> n=t.NNTP(s,119,'','')
> r,a,b,e,gn=n.group(g)
> def printStat(st,hd,rg):
>   r,d=n.xhdr(st,'%s-%s'%rg)
>   p=[]
>   for i in range(len(d)):
>   v=d[i][1]
>   if st=='Subject':v=v[4:] if v[:3]=='Re:' else v
>   p.append(v)
>   x=[(i,p.count(i)) for i in set(p)]
>   x.sort(key=lambda s:(-s[1],s[0].lower()))
>   print('Posts  %s %s'%(len(set(p)),hd))
>   for v in x: print(' %s %s'%(v[1],v[0]))
>   print
> print 'As of '+d.datetime.now().strftime("%I:%M%p %B %d, %Y") + '\n'
> m=(int(e)-int(y.argv[3])+1,int(e))
> printStat("From","Posters",m)
> printStat("Subject","Subjects",m)
> printStat("User-Agent","User-Agents",m)
> n.quit()
> --
> 
> usage on Windows:
> $ python stats.py group last N
> $ python stats.py comp.lang.python last 500

Do you happen to have a translation of the code that will
run on Linux?

$ ./nntp.py comp.lang.python last 500
Traceback (most recent call last):
  File "./nntp.py", line 7, in 
n=t.NNTP(s,119,'','')
  File "/usr/lib/python2.7/nntplib.py", line 119, in __init__
self.sock = socket.create_connection((host, port))
  File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

-- 
 GNU/Linux user #557453
The cow died so I don't need your bull!
-- 
https://mail.python.org/mailman/listinfo/python-list


Pexpect

2016-12-10 Thread Iranna Mathapati
Hi Team,



The fallowing script(function) is working if its in PDB mode(trace line by
line) but same script is not working if its remove/comment PDB and run one
shot.



log.info("Time out expection")

uut_con1 = pexpect.spawn('telnet %s'%power_ip)

uut_con1.logfile = sys.stdout

uut_con1.delaybeforesend = None

time.sleep(30)

uut_con1.send("%s\r" % username)

uut_con1.expect([r"Password:"],timeout=30)

uut_con1.send("%s\r" % pass_word)

time.sleep(30)

uut_con1.send("\r")

uut_con1.expect([r'#?'],timeout=30)

uut_con1.send("power outlets %d off\r"%outlet)

uut_con1.expect([r'wish to turn outlet'],timeout=30)

uut_con1.send("y\r")

uut_con1.send("\r")

uut_con1.expect([r'#?'],timeout=30)

uut_con1.send("power outlets %d on\r"%outlet)

uut_con1.expect([r'wish to turn outlet'],timeout=30)

uut_con1.send("y\r")

uut_con1.send("\r")

uut_con1.expect([r'#?'],timeout=30)

uut_con1.close()

uut_con.close()

time.sleep(300)


check_prompt(self,username,pass_word,dev_ip,dev_port,power_ip,outlet)



*I got fallowing error:*



2016-12-10T02:24:42: %aetest-INFO: Time out expection

Trying 172.31.206.143...

Connected to 172.31.206.143.

Escape character is '^]'.

Login for PX2 CLI

Username: admin

power outlets 21 off



Welcome to PX2 CLI!

Last login: 2001-06-30 11:53:59 EDT [CLI (Telnet) from 172.31.144.6]



#

# power outlets 21 off



yower outlets 21 on outlet 21 off? [y/n] y



power outlets 21 on

# 2016-12-10T02:26:12: %aetest-ERROR: Caught exception during execution:

2016-12-10T02:26:13: %aetest-ERROR: Traceback (most recent call last):

2016-12-10T02:26:13: %aetest-ERROR:   File
"/auto/n3k-qa/CODC/rajtamil/pyATS2.7/hlite/eor/systest/scripts/NAT/ATS_CLEAN_N9K.py",
line 354, in connect_devices

2016-12-10T02:26:13: %aetest-ERROR:
check_prompt(self,username,pass_word,dev_ip,dev_port,power_ip,outlet)

2016-12-10T02:26:13: %aetest-ERROR:   File
"/auto/n3k-qa/CODC/rajtamil/pyATS2.7/hlite/eor/systest/scripts/NAT/ATS_CLEAN_N9K.py",
line 282, in check_prompt

2016-12-10T02:26:13: %aetest-ERROR: uut_con1.expect([r'wish to turn
outlet'],timeout=30)

2016-12-10T02:26:13: %aetest-ERROR:   File
"/auto/n3k-qa/CODC/svanalin/pyats2/lib/python2.7/site-packages/pexpect/spawnbase.py",
line 321, in expect

2016-12-10T02:26:13: %aetest-ERROR: timeout, searchwindowsize, async)

2016-12-10T02:26:13: %aetest-ERROR:   File
"/auto/n3k-qa/CODC/svanalin/pyats2/lib/python2.7/site-packages/pexpect/spawnbase.py",
line 345, in expect_list

2016-12-10T02:26:13: %aetest-ERROR: return exp.expect_loop(timeout)

2016-12-10T02:26:13: %aetest-ERROR:   File
"/auto/n3k-qa/CODC/svanalin/pyats2/lib/python2.7/site-packages/pexpect/expect.py",
line 107, in expect_loop

2016-12-10T02:26:13: %aetest-ERROR: return self.timeout(e)

2016-12-10T02:26:13: %aetest-ERROR:   File
"/auto/n3k-qa/CODC/svanalin/pyats2/lib/python2.7/site-packages/pexpect/expect.py",
line 70, in timeout

2016-12-10T02:26:13: %aetest-ERROR: raise TIMEOUT(msg)

2016-12-10T02:26:13: %aetest-ERROR: TIMEOUT: Timeout exceeded.

2016-12-10T02:26:13: %aetest-ERROR: 

2016-12-10T02:26:13: %aetest-ERROR: command: /usr/bin/telnet

2016-12-10T02:26:13: %aetest-ERROR: args: ['/usr/bin/telnet',
'172.31.206.143']

2016-12-10T02:26:13: %aetest-ERROR: buffer (last 100 chars): ' 21 off?
[y/n] y\r\n\r\npower outlets 21 on\r\n# '

2016-12-10T02:26:13: %aetest-ERROR: before (last 100 chars): ' 21 off?
[y/n] y\r\n\r\npower outlets 21 on\r\n# '

2016-12-10T02:26:13: %aetest-ERROR: after: 

2016-12-10T02:26:13: %aetest-ERROR: match: None

2016-12-10T02:26:13: %aetest-ERROR: match_index: None

2016-12-10T02:26:13: %aetest-ERROR: exitstatus: None

2016-12-10T02:26:13: %aetest-ERROR: flag_eof: False

2016-12-10T02:26:13: %aetest-ERROR: pid: 9163

2016-12-10T02:26:13: %aetest-ERROR: child_fd: 18

2016-12-10T02:26:13: %aetest-ERROR: closed: False

2016-12-10T02:26:13: %aetest-ERROR: timeout: 30

2016-12-10T02:26:13: %aetest-ERROR: delimiter: 

2016-12-10T02:26:13: %aetest-ERROR: logfile: ', mode
'w' at 0xf7712078>

2016-12-10T02:26:13: %aetest-ERROR: logfile_read: None

2016-12-10T02:26:13: %aetest-ERROR: logfile_send: None

2016-12-10T02:26:13: %aetest-ERROR: maxread: 2000

2016-12-10T02:26:13: %aetest-ERROR: ignorecase: False

2016-12-10T02:26:13: %aetest-ERROR: searchwindowsize: None

2016-12-10T02:26:13: %aetest-ERROR: delaybeforesend: None

2016-12-10T02:26:13: %aetest-ERROR: delayafterclose: 0.1

2016-12-10T02:26:13: %aetest-ERROR: delayafterterminate: 0.1

2016-12-10T02:26:13: %aetest-ERROR: searcher: searcher_re:

2016-12-10T02:26:13: %aetest-ERROR: 0: re.compile("wish to turn outlet")

2016-12-10T02:26:13: %aetest-INFO: The result of subsection connect_devices
is => ERRORED





Regards,
Iranna M
-- 
htt

Re: CLP stats: last 500 posts

2016-12-10 Thread Wildman via Python-list
On Sat, 10 Dec 2016 12:31:33 -0500, DFS wrote:

> On 12/10/2016 12:06 PM, Wildman wrote:
>> On Fri, 09 Dec 2016 16:07:16 -0500, DFS wrote:
>>
>>> code (py2.7)
>>> --
>>> import sys as y,nntplib as t,datetime as d
>>> s=''
>>> g=y.argv[1]
>>> n=t.NNTP(s,119,'','')
>>> r,a,b,e,gn=n.group(g)
>>> def printStat(st,hd,rg):
>>> r,d=n.xhdr(st,'%s-%s'%rg)
>>> p=[]
>>> for i in range(len(d)):
>>> v=d[i][1]
>>> if st=='Subject':v=v[4:] if v[:3]=='Re:' else v
>>> p.append(v)
>>> x=[(i,p.count(i)) for i in set(p)]
>>> x.sort(key=lambda s:(-s[1],s[0].lower()))
>>> print('Posts  %s %s'%(len(set(p)),hd))
>>> for v in x: print(' %s %s'%(v[1],v[0]))
>>> print
>>> print 'As of '+d.datetime.now().strftime("%I:%M%p %B %d, %Y") + '\n'
>>> m=(int(e)-int(y.argv[3])+1,int(e))
>>> printStat("From","Posters",m)
>>> printStat("Subject","Subjects",m)
>>> printStat("User-Agent","User-Agents",m)
>>> n.quit()
>>> --
>>>
>>> usage on Windows:
>>> $ python stats.py group last N
>>> $ python stats.py comp.lang.python last 500
>>
>> Do you happen to have a translation of the code that will
>> run on Linux?
>>
>> $ ./nntp.py comp.lang.python last 500
>> Traceback (most recent call last):
>>   File "./nntp.py", line 7, in 
>> n=t.NNTP(s,119,'','')
>>   File "/usr/lib/python2.7/nntplib.py", line 119, in __init__
>> self.sock = socket.create_connection((host, port))
>>   File "/usr/lib/python2.7/socket.py", line 553, in create_connection
>> for res in getaddrinfo(host, port, 0, SOCK_STREAM):
>> socket.gaierror: [Errno -2] Name or service not known
> 
> 
> That code runs unchanged on py2.7 on Linux (I just now tested it).
> 
> You just need to put in your own credentials for the newsserver, user 
> and password (lines 2 and 4).

OK, thanks.  That didn't occur to me although it should have.
 
-- 
 GNU/Linux user #557453
The cow died so I don't need your bull!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: CLP stats: last 500 posts

2016-12-10 Thread Peter Otten
Wildman via Python-list wrote:

> On Fri, 09 Dec 2016 16:07:16 -0500, DFS wrote:
> 
>> code (py2.7)
>> --
>> import sys as y,nntplib as t,datetime as d
>> s=''
>> g=y.argv[1]
>> n=t.NNTP(s,119,'','')
>> r,a,b,e,gn=n.group(g)
>> def printStat(st,hd,rg):
>> r,d=n.xhdr(st,'%s-%s'%rg)
>> p=[]
>> for i in range(len(d)):
>> v=d[i][1]
>> if st=='Subject':v=v[4:] if v[:3]=='Re:' else v
>> p.append(v)
>> x=[(i,p.count(i)) for i in set(p)]
>> x.sort(key=lambda s:(-s[1],s[0].lower()))
>> print('Posts  %s %s'%(len(set(p)),hd))
>> for v in x: print(' %s %s'%(v[1],v[0]))
>> print
>> print 'As of '+d.datetime.now().strftime("%I:%M%p %B %d, %Y") + '\n'
>> m=(int(e)-int(y.argv[3])+1,int(e))
>> printStat("From","Posters",m)
>> printStat("Subject","Subjects",m)
>> printStat("User-Agent","User-Agents",m)
>> n.quit()
>> --
>> 
>> usage on Windows:
>> $ python stats.py group last N
>> $ python stats.py comp.lang.python last 500
> 
> Do you happen to have a translation of the code that will
> run on Linux?
> 
> $ ./nntp.py comp.lang.python last 500
> Traceback (most recent call last):
>   File "./nntp.py", line 7, in 
> n=t.NNTP(s,119,'','')
>   File "/usr/lib/python2.7/nntplib.py", line 119, in __init__
> self.sock = socket.create_connection((host, port))
>   File "/usr/lib/python2.7/socket.py", line 553, in create_connection
> for res in getaddrinfo(host, port, 0, SOCK_STREAM):
> socket.gaierror: [Errno -2] Name or service not known
> 

That's not a Linux problem. For the code to run in 

>> s=''

>> n=t.NNTP(s,119,'','')

you need to replace the '<...>' strings with a real news server, user, and 
password. If you use Gmane no password is required:

n = t.NNTP("news.gmane.org")

However, they use a different name for the "comp.lang.python" group, so you 
have to modify the command line accordingly:

$ python stats.py gmane.comp.python.general last 500


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


Re: CLP stats: last 500 posts

2016-12-10 Thread Terry Reedy

On 12/10/2016 9:43 AM, Steve D'Aprano wrote:

On Sat, 10 Dec 2016 09:28 pm, Terry Reedy wrote:


The spammer will still be counted,


Why reward someone who actively evades defenses?  If you want to count
spam, it is mostly missing, at least as far as python-list is concerned.


Its not a reward. Spammers are not like trolls, they don't hang around to
see the result of their posts.


To me, the relevant difference is between posts related to python and 
those not.  It is usually clear which is which.


> There no evidence at all that this Italian

spammer is looking for replies or responses to his(?) posts. He apparently
just fires them out.

I think that it is relevant that comp.lang.python receives X spam messages
from a certain person. It gives a picture of the health of the newsgroup:
how much of it is spam? Hopefully only a small amount.


Python-list gets unrelated-to-python spam from lots of people.  They are 
not outliers (unlike jmf's now blocked trolls), but contaminents from a 
different universe.


I agree that the fraction of messages that are clearly spam has some 
interest in itself, and definitely should be as small as possible. But I 
contend that they should be excluded from a study of the universe of 
python-related messages.


My other point is that this small sliver that used to get passed through 
is extremely biased and statistically worthless as a study of 
python-list spamming.  If one wanted to study the rate and nature of 
contamination, or the effectiveness of filtering, one would need access 
to the raw stream of submissions.


--
Terry Jan Reedy

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


Can't find setuptools

2016-12-10 Thread Alonso López
Hi, I'm trying to install the package py-webrtcvad 
(github.com/wiseman/py-webrtcvad) and running into problems. Here's a summary 
of what I've tried so far:


·Installed Python 3.5 and set up the Windows path environment so that 
it works from any directory.

·Installed pip for Python.

·Tried to install the package with python -m pip install webrtcvad, but 
it failed, returning the error "Unable to find vcvarsall.bat".

·I found a blog that seems to deal with the problem vcvarsall.bat 
problem: 
blogs.msdn.microsoft.com/pythonengineering/2016/04/11/unable-to-find-vcvarsall-bat/.
 Following the directions of that blog:

o   I installed Visual C++ Build Tools 2015. I tried running the program again 
straight away (without updating setuptools) and I received a lot of error 
messages which I didn't write down.

o   Following the directions in https://pypi.python.org/pypi/setuptools I 
removed the version of setuptools that came with my Python installation (v20), 
and installed the latest version (v30). This time the error message was 
"ImportError: No module named 'pip.utils.setuptools_build'".

o   Asked for assistance in the Python official chat. They made three 
suggestions:

§  Updating pip with python -m pip install --upgrade pip. Didn't work. Again, 
the error "ImportError: No module named 'pip.utils.setuptools_build'".

§  Reinstalling Visual C++ Build Tools 2015. No difference, same error again.

§  Reinstalling Python itself. The Python installer offers three choices: 
Repair, modify and uninstall.

·Repair: Didn't work. Same error.

·Modify: Doesn't look like it offers useful modifications for this.

·Uninstall: Uninstalled and reinstalled. Still the same error.
I'm out of ideas. Can you help me?

Thank you,

Alonso López

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


Re: % string formatting - what special method is used for %d?

2016-12-10 Thread Veek M
Steve D'Aprano wrote:

> On Sat, 10 Dec 2016 06:06 pm, Veek M wrote:
> 
>> When we do:
>> 
>> print '%s %d' % ('hello', 10)
>> 
>> what special method is being invoked internally within the string-
>> format-specifier?
> 
> %d requires the argument to be an int, or able to be converted to int
> using the __int__ special method.
> 
> 
> py> class X(object):
> ... def __int__(self):
> ... return 42
> ...
> py> "%d" % X()
> '42'
> 
> 
> 
>> format() invokes format__
>> print invokes __str__
> 
> print actually invokes __str__ or __repr__, whichever is available.
> 
> 
> 
>> I'm basically trying to make sense of:
>> 
>> raise TypeError('urkle urkle %s' % list(dictionary))
>> <=> raise TypeError('urkle urkle %s' % [ key1, val1, key2, val2 ]
> 
> 
> The raise TypeError part of the code is irrelevant to your question.
> You should always simplify your code to only the part that is
> relevant.
> 
> raise TypeError(some_string)
> 
> behaves the same regardless of how some_string is made.
> 
> 
>> So, the % operator reads the format specifier and notices %s and
>> therefore calls __str__ in the list class to figure out how to
>> represent
>> [ key1, val1, key2, val2 ].
>> 
>> However what if I use %d? How do the other format specs work?
> 
> 
> The format specifiers are similar to these:
> 
> %s => str(obj), which ends up calling __str__ or __repr__
> 
> %r => repr(obj), which ends up calling __repr__ or __str__
> 
> %c => chr(obj), or obj must be a string of length 1
> 
> %d %i %u => int(obj), which ends up calling __int__
> 
> %x %X => int(obj), then convert to hexadecimal
> 
> %o => int(obj), then convert to octal
> 
> %e %E %f %g %G => float(obj), which ends up calling __float__
> 
> %% => a literal % sign
> 
> 
> 
> 
> 

Well take a look at this:
###
#!/usr/bin/python

class Foo(int):
def __init__(self, value):
self.value = value

def __str__(self):
print '__str__'
return str(self.value)

def __int__(self):
print '__int__'
return self.value + 1


#'%s' % Foo(10) # %s is mapped to __str__
'%d' % Foo(20)
###

here, '__str__' prints because when you do:
'%s' % x
the __str__ method is invoked. So internally %s invokes __str__ 
independent of print.

However the next line doesn't trigger any similar invocation with 
__int__ or__str__? (but int(Foo(10)) would invoked __int__)

Is there a way to trigger special methods using %d etc OR is this 
restricted to %s and why?








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


Help in creating a dynamic/loop based on variables and CSV files

2016-12-10 Thread Umar Yusuf
Hi all,
I need your help with any of these questions?

1- 
http://stackoverflow.com/questions/41083699/python-create-dynamic-loop-based-on-variables-and-csv

2- 
http://stackoverflow.com/questions/41081800/python-pandas-how-to-use-dataframe-cell-to-search-another-dataframe-column-and

Thanks in advance for your time.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: % string formatting - what special method is used for %d?

2016-12-10 Thread Ian Kelly
On Sat, Dec 10, 2016 at 11:40 PM, Veek M  wrote:
> Well take a look at this:
> ###
> #!/usr/bin/python
>
> class Foo(int):
> def __init__(self, value):
> self.value = value
>
> def __str__(self):
> print '__str__'
> return str(self.value)
>
> def __int__(self):
> print '__int__'
> return self.value + 1
>
>
> #'%s' % Foo(10) # %s is mapped to __str__
> '%d' % Foo(20)
> ###
>
> here, '__str__' prints because when you do:
> '%s' % x
> the __str__ method is invoked. So internally %s invokes __str__
> independent of print.
>
> However the next line doesn't trigger any similar invocation with
> __int__ or__str__? (but int(Foo(10)) would invoked __int__)

This is probably because Foo inherits from int. Foo(20) is already an
int so there is no conversion to be done; Python simply uses the int
value and ignores the __int__ method in this case.

> Is there a way to trigger special methods using %d etc OR is this
> restricted to %s and why?

For an object that is already an int, probably not.

However you may want to revisit your decision to make Foo inherit from
int and question whether that is really sensible if you're also
wanting to override the __int__ method. What does that mean if
something is an int but also provides a method to convert to int? It's
a contradiction.
-- 
https://mail.python.org/mailman/listinfo/python-list