Re: installing libraries like numpy scipy matplotlib

2015-07-05 Thread Jens Thoms Toerring
memilanuk memila...@gmail.com wrote:
 On 07/04/2015 07:58 AM, Jens Thoms Toerring wrote:
  PIYUSH KUMAR okkpiy...@gmail.com wrote:
  I have never used linux in my life.. only windows based computing.. So I
  have problems in installing third party libraries in python.
 
  It depends. One question is if there's already a ready-for-use
  package for the third party library you want to install. If that
  is the case then the next question is which distro you're using

 I could be wrong, but I think the point was that he's not using Linux, 
 while the majority of instructions for getting and using various 
 scientific libraries for Python assume that the user *is* using Linux.

Reading it the first time I understood it too mean that the OP
is completely new to Linux (otherwise why mention Linux at all
and not ask directly How do I install something under Windows?).
But on second reading I see that it could also have beenmeant the
other way round and it's not about Linux at all;-)

Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: installing libraries like numpy scipy matplotlib

2015-07-04 Thread Jens Thoms Toerring
PIYUSH KUMAR okkpiy...@gmail.com wrote:
 I have never used linux in my life.. only windows based computing.. So I
 have problems in installing third party libraries in python.

It depends. One question is if there's already a ready-for-use
package for the third party library you want to install. If that
is the case then the next question is which distro you're using
- there are different package-management systems.

Since you mentioned Pyke: if you got Ubuntu or Debian there's
a package for it you can simply install it using the command

  sudo apt-get install python-pyke

(The 'sudo' bit is for temporarily assuming the permissions
to install the package, you're going to be asked for your
password. 'apt-get' is the program for installing and de-
installing packages. And 'python-pyke' is the name of the
package._

If you also want the documentation installed add 'python-pyke-doc'.
If you have some other distribution there might be a different
program for installing new packages. And there's basically always
also some program with a graphical user interface, wrapped around
that and which shows you which packages exist (thousands).

If there's no package for what you want you need to download
the sources and install them yourself. For the details there
usually is a file named README.txt and/or INSTALL.txt (or
similar). Often all you need to use is the three commands

  ./configure
  make
  sudo make install

If what you want to install depends on further software you
also need to install that before creating and installing of
what you need will succeed. So, again check if a ready-made
package for that dependencies available or download, compile
and install that. This can, in some cases, become a bit of
work if A need B, B in turn need C and D, D needs E etc.;-)

 So can somebody just explain me how many softwares or other python packages
 I have to install before installing any of these.

Sorry, but I don't understand this sentence. But in case for what
you want to install there's a package for your distro you don't
need to worry at all - all dependencies will get installed
automatically (so if you want to install package A and that
needs package B, B will installed automatically before A is
installed).

 I also had problems installing PYKE last month.

What were these problems?
   Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help needed to create a Python extension library for an existing shared memory hash table library

2014-03-23 Thread Jens Thoms Toerring
Simon Hardy-Francis simo...@gmail.com wrote:
 Hi Python fans, I just released my first open source project ever called
 SharedHashFile [1]. It's a shared memory hash table written in C. Some guy
 on Quora asked [2] whether there's an extension library for Python coming
 out. I would like to do one but I know little about Python. I was wondering
 if anybody in this group has experience writing extension libraries for
 Python?

There are, as far as I know, a number of tools that can help you
to create extension modules from C/C++ libraries (i.e. Python bin-
dings for the library). One that I have used successfully with a
largish C++ library of mine is shortly described here

http://www.riverbankcomputing.com/software/sip/intro

and the documentation and download are at

http://pyqt.sourceforge.net/Docs/sip4/
http://www.riverbankcomputing.com/software/sip/download

Since I haven't used any of the alternatives I can't comment on
how good they are. A list of them can be found here

https://wiki.python.org/moin/IntegratingPythonWithOtherLanguages

   Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Updating a filename's counter value failed each time

2013-06-17 Thread Jens Thoms Toerring
In article kpne3k$1066$1...@news.ntua.gr you wrote:
 After a user selects a file from the form, that sleection of his can be 
 found form reading the variable 'filename'

 If the filename already exists in to the database i want to update its 
 counter and that is what i'm trying to accomplish by:

 ---
 if form.getvalue('filename'):
 cur.execute('''UPDATE files SET hits = hits + 1, host = %s, lastvisit 
 = 
 %s WHERE url = %s''', (host, lastvisit, filename) )

There are (single) quotes missing around (at least) the file
name (the 'url' column) which I'm rather sure is a string -
you need them around all strings you use in SQL statements.

I don't know which database and interface you're using but I would
guess that many have the ability to inserting quotes where neces-
sary etc. E.g. with sqlite3 you would use

   cur.execute('UPDATE files SET hits = hits + 1, host = ?, lastvisit = ? '
   'WHERE url = ?', (host, lastvisit, filename) )

and the quotes required around (at least) the 'filename' string
will be inserted automatically.

Also take care to check the filename you insert - a malicous
user might cobble together a file name that is actually a SQL
statement and then do nasty things to your database. I.e. never
insert values you received from a user without checking them.

 For some reason this never return any data, because for troubleshooting 
 i have tried:

 data = cur.fetchone()

There's nothing that your SQL statement (if correct) would return,
so what do you expect to have returned by the fetchone() method?

Perhaps there's something like the 'rowcount' property in sqlite3
which returns the number of rows modified by an INSERT or UPDATE.

 Since for sure the filename the user selected is represented by a record 
 inside 'files' table why its corresponding counter never seems to get 
 updated?

I would guess because you forgot the uotes around string
values in your SQL statement which thus wasn't executed.

  Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Updating a filename's counter value failed each time

2013-06-17 Thread Jens Thoms Toerring
Νίκος supp...@superhost.gr wrote:
 On 17/6/2013 8:54 μμ, Jens Thoms Toerring wrote:
  Also take care to check the filename you insert - a malicous
  user might cobble together a file name that is actually a SQL
  statement and then do nasty things to your database. I.e. never
  insert values you received from a user without checking them.

 Yes in generally user iput validation is needed always, but here here 
 the filename being selected is from an html table list of filenames.

 But i take it you eman that someone might tried it to pass a bogus 
 filename value from the url like:

 http://superhost.gr/cgi-bin/files.py?filename=Select.;

 Si that what you mean?

Well, you neer wrote where this filename is coming from.
so all I could assume was that the user can enter a more
or less random file name. If he only can select one from
a list you put together there's probably less of a problem.

 But the comma inside the execute statement doesn't protect me from such 
 actions opposed when i was using a substitute operator?

  I would guess because you forgot the uotes around string
  values in your SQL statement which thus wasn't executed.

 i tried you suggestions:

 cur.execute('''UPDATE files SET hits = hits + 1, host = %s, lastvisit = 
 %s WHERE url = %s''', (host, lastvisit, filename) )

 seems the same as:

 cur.execute('''UPDATE files SET hits = hits + 1, host = %s, lastvisit = 
 %s WHERE url = %s''', (host, lastvisit, filename) )

 since everything is tripled quoted already what would the difference be 
 in %s opposed to plain %s ?

As I wrote you need *single* quotes around strings in
SQL statements. Double quotes won't do - this is SQL
and not Python so you're dealing with a different lan-
guage and thus different rules apply. The triple single
quotes are seen by Python, but SQL needs its own.

Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Updating a filename's counter value failed each time

2013-06-17 Thread Jens Thoms Toerring
MRAB pyt...@mrabarnett.plus.com wrote:
 On 17/06/2013 19:32, Jens Thoms Toerring wrote:
  As I wrote you need *single* quotes around strings in
  SQL statements. Double quotes won't do - this is SQL
  and not Python so you're dealing with a different lan-
  guage and thus different rules apply. The triple single
  quotes are seen by Python, but SQL needs its own.
 
 The query looks safe to me as he _is_ using a parametrised query.

Perhaps - the OP never told which API (or database) he
is using. What about some API that simply connects the
first argument of exxecute() with the second with just
a simple '%' to construct the string for the SQL state-
ment? In that case there would be no single quotes a-
round strings, or would there?

   Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to create new python file with increament number, if doesn't exist?

2013-05-27 Thread Jens Thoms Toerring
Avnesh Shakya avnesh.n...@gmail.com wrote:
I want to create a new python file like 'data0.0.5', but if it is already
 exist then it should create 'data0.0.6', if it's also exist then next like
 'data0.0.7'. I have done, but with range, please give me suggestion so that
 I can do it with specifying range.
 I was trying this way and it's working also..

 i = 0
 for i in range(100):
 try:
 with open('Data%d.%d.%d.json'%(0,0,i,)): pass
 continue
 except IOError:
 edxCorrectDataFile = file('Data%d.%d.%d.json'%(0,0,i,), 'a+')
 break

 But here I have defined range 100, Is it possible without range it create
 many required files?

What about something as simple as this?

  i = 0
  while os.path.exists( 'Data{0}.{1}.{2}.json'.format( 0, 0, i ) ) :
  i += 1
  f = open( 'Data{0}.{1}.{2}.json'.format( 0, 0, i ), 'w' )

For your code you'ld make it e.g.

  i = 0
  try:
  while True :
  with open('Data%d.%d.%d.json'%(0,0,i)):
  i += 1
  except IOError:
  edxCorrectDataFile = file('Data%d.%d.%d.json'%(0,0,i), 'a+')

Note that I don't see how all this trying to open a file and
catching an exeception if it doesn't exist is any better
than simply using os.path.exists(). You may have read some-
where that it avoids a race condition and would thus be more
secure. That's not the case here, an attacker still could
create e.g. a symbolic link with the name of the file you're
going to open between the time the exception is thrown and your
program getting around to open the file you expect not to exist,
so nothing is gained by using this somewhat convoluted method.
What would be needed for avoiding a race condition is an addi-
tional flag to be passed to open() like the for example the
O_EXCL flag that can be passed to Unix' open() system function.
But that isn't supported by Pythons open() function which
rather likeky is based on C's fopen() function). The best
advice to avoid such problems is probably not to open files
with a predictable name (or better to use one of the methods
to create files with names guaranteed to be unique by the
system) in directories to which other users than you also
have write permission.
Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: in need of some help...

2013-05-12 Thread Jens Thoms Toerring
Chris “Kwpolska” Warrick kwpol...@gmail.com wrote:
 (slightly offtopic, sorry.)

 On Sun, May 12, 2013 at 12:20 AM, Jens Thoms Toerring j...@toerring.de 
 wrote:
  PS: If I may ask you a favor: consider refraining from using Google's
  completely broken interface to newsgroups - your post consists
  of nearly 200 lines of text containing all I wrote, with an empty
  line inserted between each of them, and a single line of text
  you wrote. It's rather annoying to have to sieve through that
  much of unrelated stuff just to find thar one line that's re-
  levant.

 Gmail automatically hides long quotes.  This is helpful in situations
 like this one.  More mail software should implement that
 functionality.  Seriously: once you go Gmail, you never go back.

i giess you mean Gougle groups and not Gmail, which I can't comment
on since I don't use it.

You still have to un-hide the message when you want to under-
stand what the other person is respoding to. And then you're back
to square one (and the double-spacing issue seems to remain). That
is why it has been a good tradition to remove everything when
you reply that isn't relevant to what you're replying to. Of
course, with the sorry excuse for an editor you habe in a web
based form that's more difficult to do than with a real editor.
That's one of the reasons I would advice to use a specialized
program that can be made to use the editor of your choice.

Another extremly annoying thing when reading via Google groups is
that there's no reasonable threading, i.e. it's not immediately
obvious what is meant as a reply to a specific post. That makes
Google groups basically useless for longer discussions.

And then Google groups overflow with spam messages that any
self-respecting news server would discard, so you never get
to see them.

  And this Google groups crap seems to make it nearly
  impossible to do it any other way. If you don't believe me see
  e.g.
 
http://wiki.python.org/moin/GoogleGroupsPython
 
  There are much better alternatives to Google groups,
  using a real usenet news server and a program that does
  not mess up content of news group postings. They've been
  developed with 30 years of experience with newsgroups.

 Or something even better: a mailing list.
 http://mail.python.org/mailman/listinfo/python-list is where you can
 find it.  Much friendlier than Usenet, and the software itself is
 developed by the FLUFL.

Mailing lists are quite fine and I'm on quite a number of them.
But I don't want many more to further fill up my inbox. And there
you again have the problem that there's no reasonable threading
of messages when these messages arrive in your mail folder.

  If I'd be conspiracy theorist I would conclude that Google
  is up to something bad in trying to make using newsgroups
  nearly impossible by their badly broken stuff (and, to add
  credibility to such a claim, their complete disregard for
  all the criticism they got over the years, actually making
  each version of Google groups even worse), but it's rather
  likely just another case of pure incompetence (or a why
  should we care attitude:-(

 They shouldn’t care because Usenet users often yell “Get off my
 lawn!”.  Young people don’t use newsgroups.  They don’t even know what
 Usenet is.

Well, it's a pity when younger people don't use newsgroups (no
idea if this is true, though) since they can be extremely useful
for all kinds of purposes - I e.g. learned a huge amount from
just lurking. And while some groups may be nicer than others,
that's no excuse for Google at all to actively trying to destroy
them. I'd rather prefer Google to simply get rid of Google groups
and put the long time archives they obtained from DajaNews into
better hands that do care.
   Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: in need of some help...

2013-05-12 Thread Jens Thoms Toerring
Chris Angelico ros...@gmail.com wrote:
 On Mon, May 13, 2013 at 7:56 AM, Jens Thoms Toerring j...@toerring.de wrote:
  Chris “Kwpolska” Warrick kwpol...@gmail.com wrote:
  Gmail automatically hides long quotes.  This is helpful in situations
  like this one.  More mail software should implement that
  functionality.  Seriously: once you go Gmail, you never go back.
 
  i giess you mean Gougle groups and not Gmail, which I can't comment
  on since I don't use it.

 No, Chris (not me, the other Chris... *an*other Chris okay, one of
 the chorus of Chrises of this list!) did mean Gmail, the Google
 webmail client. It does threading (and does it better than
 SquirrelMail does), and it does the hiding of long quotes, long
 signatures, etc.

Ok, sorry then about that - as I said I never have used Gmail
(and don't plan using it for other reasons than usability - and
then I would hardly consider anything with a web interface for a
text medium to be very usable;-). But, as far as I understand,
Gmail is about email, so I'm a bit at a loss to understand what
got this to do with news groups and Google groups (were the post
I originally was responing to according to the header seemed to
be coming from) that I intended this to be about?

  Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: in need of some help...

2013-05-11 Thread Jens Thoms Toerring
Alex Norton ayjayn1...@gmail.com wrote:
 On Wednesday, 1 May 2013 13:15:28 UTC+1, Jens Thoms Toerring  wrote:
  Of course, it might be nicer to have a result label some-
  where in the graphical interface which you set to the text
  instead of printing it out to the console. And you also will
  probably add some Quit button to end the game.

 how would i go about adding print outcomes of all options  to a label ?

If you have a QLabel you can set its text to anything you want
using its setText() method.
 Regaeds, Jens

PS: If I may ask you a favor: consider refraining from using Google's
completely broken interface to newsgroups - your post consists
of nearly 200 lines of text containing all I wrote, with an empty
line inserted between each of them, and a single line of text
you wrote. It's rather annoying to have to sieve through that
much of unrelated stuff just to find thar one line that's re-
levant. And this Google groups crap seems to make it nearly
impossible to do it any other way. If you don't believe me see
e.g.

  http://wiki.python.org/moin/GoogleGroupsPython

There are much better alternatives to Google groups,
using a real usenet news server and a program that does
not mess up content of news group postings. They've been
developed with 30 years of experience with newsgroups.

If I'd be conspiracy theorist I would conclude that Google
is up to something bad in trying to make using newsgroups
nearly impossible by their badly broken stuff (and, to add
credibility to such a claim, their complete disregard for
all the criticism they got over the years, actually making
each version of Google groups even worse), but it's rather
likely just another case of pure incompetence (or a why
should we care attitude:-(
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making safe file names

2013-05-07 Thread Jens Thoms Toerring
Andrew Berg bahamutzero8...@gmail.com wrote:
 Currently, I keep Last.fm artist data caches to avoid unnecessary API calls
 and have been naming the files using the artist name. However, artist names
 can have characters that are not allowed in file names for most file systems
 (e.g., C/A/T has forward slashes). Are there any recommended strategies for
 naming such files while avoiding conflicts (I wouldn't want to run into
 problems for an artist named C-A-T or CAT, for example)? I'd like to make
 the files easily identifiable, and there really are no limits on what
 characters can be in an artist name. --

It's not clear what the context that you need this for. You
could e.g. replace all characters not allowed by the file
system by their hexidecimal (ASCII) values, preceeded by a
'% (so '/' would be changed to '%2F', and also encode a '%'
itself in a name by '%25'). Then you have a well-defined
two-way mapping (isomorphic if I remember my math-lear-
nining days correctly) between the original name and the
way you store it. E.g.

  C/A/T  would become  C%2FA%2FT

and

  C%2FA/T  would become  C%252FA%2FT

You can translate back and forth between them with not too
much effort.

Of course, that assumes that '%' is a character allowed by
your file system - otherwise pick some other one, any one
will do in principle. It's a bit harder for a human to in-
terpret but rathe likely not that much of a problem. You
probably will have seen that kind of scheme used in URLs.
The concept is rather old and called 'escape character',
i.e. have one character that assumes some special meaning
and also escaped it.

If, on the hand, those names are never to be translated back
to the original name another strategy would be to use the SHA1
hash value of the artists name. Since clashes between SHA1 hash
values are rather hard to produce it's a rather safe method of
converting something (i.e. the artists name) to a number. The
drawback, of course, is that you can't translate back from the
hash value to the original name (if that would be simple the
whole thing wouldn't work;-)

   Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with loading file into an array

2013-05-05 Thread Jens Thoms Toerring
peter berrett pwberr...@gmail.com wrote:
 I am trying to build a program that can find comets in a series of
 astronomical images. I have already written functions to find the comet in a
 series of images, the data of which is stored in embedded lists.

 The area I am having difficulty with is take a standard gif file (1024 x
 1024) and reading it into an array or embedded lists.

 In a nutshell here is an example of what I want to do

 Let's say I have a gif file called 20130428__c2_1024.gif in a folder
 called c:\comets

 I want to read the data from that gif file taking the red data (excluding
 the green and blue data) and store that in an array called Image[][] which
 is a nested array length 1024 with a list in each item of 1024 length (ie
 1024 x 1024)

 Could someone please provide a piece of code to do the above so I can then
 go on to modify it to pick up different files from different folders? In
 particular I am keen to seen how you read in the data and also how you
 change the directory from which you are reading the image. 

the following should do the trick using, as Fábio already
suggested, the Python Image Library (PIL):

8-
#!/ur/bin/env python

import Image

im = Image.open( 'c:/comets/20130428__c2_1024.gif' )
w, h = im.size
red_arr = [ ]
for y in range( h ) :
red_line = [ ]
for x in range( w ) :
red_line.append( im.getpixel( ( x, y ) )[ 0 ] )
red_arr.append( red_line )
8-

For obvious reasons I couldn't use 'Image' as the name of the list
of lists, so it's 'red_arr' instead. This is probably not the fas-
test solution, but it's simple and hopefully will get you started.

Concerning reading other files: here I may not understand your
problem since it looks rather trivial to me by simply passing
the open() method of 'Image' the name of a file in a different
directory.
 Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do Perl programmers make more money than Python programmers

2013-05-05 Thread Jens Thoms Toerring
In comp.lang.python Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 
wrote:
 On Sun, 05 May 2013 12:11:11 -0500, Ignoramus16992 wrote:

  According to CIO.com, Python programmers make only $83,000 per year,
  while Perl programmers make $93,000 per year.
  
  http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10
  http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11
  
  I would like to know, what explains the discrepancy.

 Perl is much harder to use, so the average Perl programmer burns out 
 after a few years and takes up a less stressful career, like going 
 undercover in the Russian mob or the Taliban. So only the most dedicated, 
 brilliant and extreme programmers last long enough to become a Perl 
 expert, and consequently can demand higher pay, while any idiot can learn 
 to program Python, as I have.

 Also, Perl programmers are an unprincipled, devious bunch, always looking 
 for an opportunity to blackmail their employers into paying them extra. 
 Python programmers are a decent, law-abiding people with a strong moral 
 code who would never stoop to the sort of things that Perl coders are 
 proud of doing.

Now you got me badly worried, using both Perl and Python (and
other, unspeakable languages, but not VB I promise!) Will I
end up as a Python hacker for the mob or worse - or is there
a chance of redemption (perhaps after a few years in Guanta-
namo bay)? And should I, while it lasts, get the Perl or the
Python salary, or the mean or both combined? Got to consider
that when applying for my next job!

 Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do Perl programmers make more money than Python programmers

2013-05-05 Thread Jens Thoms Toerring
In comp.lang.python Rainer Weikusat rweiku...@mssgmbh.com wrote:
 j...@toerring.de (Jens Thoms Toerring) writes:
  In comp.lang.python Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 
  wrote:
  On Sun, 05 May 2013 12:11:11 -0500, Ignoramus16992 wrote:
 
   According to CIO.com, Python programmers make only $83,000 per year,
   while Perl programmers make $93,000 per year.
   
   http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide10
   http://www.cio.com/slideshow/detail/97819?source=ifwartcio#slide11
   
   I would like to know, what explains the discrepancy.
 
  Perl is much harder to use, so the average Perl programmer burns out 
  after a few years and takes up a less stressful career, like going 
  undercover in the Russian mob or the Taliban. So only the most dedicated, 
  brilliant and extreme programmers last long enough to become a Perl 
  expert, and consequently can demand higher pay, while any idiot can learn 
  to program Python, as I have.
 
  Also, Perl programmers are an unprincipled, devious bunch, always looking 
  for an opportunity to blackmail their employers into paying them extra. 
  Python programmers are a decent, law-abiding people with a strong moral 
  code who would never stoop to the sort of things that Perl coders are 
  proud of doing.
 
  Now you got me badly worried, using both Perl and Python (and
  other, unspeakable languages, but not VB I promise!) Will I
  end up as a Python hacker for the mob or worse

 https://en.wikipedia.org/wiki/Strange_Case_of_Dr_Jekyll_and_Mr_Hyde

 [SCNR]

Well, that didn't have a happy ending:-( Should have listened to
my parents when they told me again and again Never use Perl, just
say no!. Seems I'm doomed - what's the proper way to apply for a
job with the mob?

-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do Perl programmers make more money than Python programmers

2013-05-05 Thread Jens Thoms Toerring
In comp.lang.python Roy Smith r...@panix.com wrote:
 In article auo1hgfmri...@mid.uni-berlin.de,
  j...@toerring.de (Jens Thoms Toerring) wrote:

  Well, that didn't have a happy ending:-( Should have listened to
  my parents when they told me again and again Never use Perl, just
  say no!. Seems I'm doomed - what's the proper way to apply for a
  job with the mob?

 I don't think you apply.  If they want you, they'll find you.

I see, that's what's called headhuntering, isn't it?

-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sum operation in numpy arrays

2013-05-02 Thread Jens Thoms Toerring
Ana Dionísio anadionisio...@gmail.com wrote:
 Hello! I have several numpy arrays in my script and i want to add them. For 
 example:

 a=[1,2,3,4,5]
 b=[1,1,1,1,1]
 c=[1,0,1,0,1]

 for i in range(5):
 d[i]=a[i]+b[i]+c[i]

 print d

 [3,3,5,5,7]

 I did it like that but I get an error: TypeError: unsupported operand 
 type(s) for +: 'float' and 'numpy.string_'

 How can I sum my arrays?

There's no numpy array at all in use in your example program, you
only have normal lists. If you had numpy arrays you wouldn't need
to loop over the indivindual elements, you could do just

d = a + b + c

to add up the (numpy) arrays.

The error message you get may indicate that somewhere in your real
code (not the one you posted) one of the elements of one of the
(numpy) arrays is not a number but a string, e.g. by doing some-
thing like

a = numpy.array( [ 1, '2', 3, 4, 5 ] )

(note the single quotes around 2, that will result in the element
having a type of 'numpy.string'. But if it's like that is impossible
to tell without seeing the real code you're using;-) I guess you
will have to give us what you actually use if you want something
more helpful.
Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: in need of some help...

2013-05-01 Thread Jens Thoms Toerring
Alex Norton ayjayn1...@gmail.com wrote:
 thanks... ill take a look at the Qt event handling

It's rather simple: instead of the program running through a
sequence of steps, the program normally is basically doing
nothing. It just reacts to events that normally come from
the user, i.e. the user clicks on some icon or widget, or
(s)he enters something via the keyboard. You etermine which
of all the possible events to which widget are relevant to
you, write handler functions for them and tell the widget
to call some function when an event happens. The simplest
case is a button: you want to react to it, so you write a
function for what's to be done when it's clicked on and
then tell Qt to call that function once it gets clicked
(there are different events even for a simple push button,
it can be clicked, just pushed, released etc.). And if you
have set up everything for that you tell Qt to start waiting
for events.

So the steps are:

  1. Tell Qt that this is a program using it

 app = QtGui.QApplication( sys.argv )

  2. Create your graphical interface (what you seem to
 have done more or less)

  3. Connect desired events (what's called signals in
 Qt lingo) for a certain widget to the function to be
 called with something like

 your_widget.clicked.connect( your_function )

 (replace 'clicked' with e.g. 'pushed' or 'released'
 when interested in a push or release signal instead)

  4. Start the event loop (i.e. have Qt wait for the user
 to do something and call one of your functions if the
 user did something you're interested in) with

 app.exec_( )

 When this returns the game is over.

So you don't wait for keyboard input with input() like in
your original program but instead tell Qt to do the waiting
for you and call the appropriate function you defined when
something interesting happens.

What you probably will have to change about the graphical
interface is that instead of using QLabel widgets for 'Air',
'Earth', 'Fire', 'Water' to use e.g. QPushButtons since
QLabels are rather static objects - they don't receive any
click events and it's rather likely some kind of event
like this is what you're going to want to react to. And for
that QPushButtons seem to be the simplest choice to start
with.

So have an 'Air' button (let's call it 'bAir' and then do

   bAir.clicked.connect( air_clicked )

after defining a function air_clicked() in which you deal
with that case. that might be as simple as

def air_clicked( ) :
# Randomly pick one of 'air', 'fire', 'water' or 'earth'

z = [ 'air', 'fire', 'water', earth' ][ random.randrange( 4 ) ]

if z == 'air' :
print( 'Stalemate' )
elif z == 'water' :
print( 'Air removes Water, you win!' )
...

Now, when during the game the 'Air' button is clicked this
function will get called.

Of course, it might be nicer to have a result label some-
where in the graphical interface which you set to the text
instead of printing it out to the console. And you also will
probably add some Quit button to end the game.

  Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File Read issue by using module binascii

2013-04-28 Thread Jens Thoms Toerring
Tim Roberts t...@probo.com wrote:
 Jimmie He jimmie...@gmail.com wrote:

 When I run the readbmp on an example.bmp(about 100k),the Shell is become to 
 No respose,when I change f.read() to f.read(1000),it is ok,could someone 
 tell me the excat reason for this?
 Thank you in advance!
 
 Python Code as below!!
 
 import binascii
 
 def read_bmp():
 f = open('example.bmp','rb')
 rawdata = f.read()   #f.read(1000) is ok
 hexstr = binascii.b2a_hex(rawdata)   #Get an HEX number
 bsstr = bin (int(hexstr,16))[2:]

 I suspect the root of the problem here is that you don't understand what
 this is actually doing.  You should run this code in the command-line
 interpreter, one line at a time, and print the results.

 The read instruction produces a string with 100k bytes.  The b2a_hex then
 produces a string with 200k bytes.  Then, int(hexstr,16) takes that 200,000
 byte hex string and converts it to an integer, roughly equal to 10 to the
 240,000 power, a number with some 240,000 decimal digits.  You then convert
 that integer to a binary string.  That string will contain 800,000 bytes.
 You then drop the first two characters and print the other 799,998 bytes,
 each of which will be either '0' or '1'.

 I am absolutely, positively convinced that's not what you wanted to do.
 What point is there in printing out the binary equavalent of a bitmap?

 Even if you did, it would be much quicker for you to do the conversion one
 byte at a time, completely skipping the conversion to hex and then the
 creation of a massive multi-precision number.  Example:

 f = open('example.bmp','rb')
 rawdata = f.read()
 bsstr = []
 for b in rawdata:
 bsstr.append( bin(ord(b)) )
 bsstr = ''.join(bsstr)

 or even:
 f = open('example.bmp','rb')
 bsstr = ''.join( bin(ord(b))[2:] for b in f.read() )

Exactly my idea at first. But then I started to time it (using
the timeit module) by comparing the following functions:

  # Original version
  
  def c1( rawdata ) :
  h = binascii.b2a_hex( rawdata )
  z = bin( int( h, 16 ) )[ 2 : ]
  return '0' * ( 8 * len( r ) - len( z ) ) + z

  # Convert each byte directly

  def c2( rawdata ) :
  return ''.join( bin( ord( x ) )[ 2 : ].rjust( 8, '0' ) for x in r )

  # Convert each byte using a list for table look-up

  def c3( rawdata ) :
  h = [ bin( i )[ 2 : ].rjust( 8, '0' ) for i in range( 256 ) ]
  return ''.join( h[ ord( x ) ] for x in rawdata )

  # Convert each byte using a dictionary for table look-up (avoids
  # lots of ord() calls)

  def c4( rawdata ) :
  h = { chr( i ) : bin( i )[ 2 : ].rjust( 8, '0' ) for i in range( 256 ) }
  return ''.join( h[ x ] for x in rawdata )

As you can see I even in c3() and c4() tried to speed things up
further by using a table look-up instead if calling bin() etc.
on each byte. But the results was that c2() is nearly 15 times
slower than c1(), c3() about 3 times and c4() still more than 2
times slower! So the method the OP uses seems to be quite a bit
more efficient than one might be tempted to assume.

I would guess that the reason is that c1() does just a small
number of calls of functions that probably aren't implemented
in Python but in C and thus can be a lot faster then anything
you could achieve with Python, while the other functions use a
for loop in Python, which seems to account for a good part of
the CPU time used. To test for that I split the 'rawdata' string
into a list of character (i.e. single letter strings) and re-
assembled it using join() and a for loop:

r = list( rawdata( )
z = ''.join( x for x in r )

The second line alone took about 1.7 times longer than the
whole, seemingly convoluted c1() function!

What I take away from this is that a lot of the assumption one
is prone to make when coming from e.g. a C/C++ background can
be quite misleading when extrapolating to Python (or other in-
terpreted languages)...
  Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested For loop not running full

2013-04-26 Thread Jens Thoms Toerring
inshu chauhan insidesh...@gmail.com wrote:
 I have this part of my code where I am trying to traverse over an image by
 running a for loop for both x and y co-ordinate axis. But the loop is
 terminating by just reading first pixel. Can think of a reason why this is
 happening ?

 The code is:
 for sy in xrange(0, segimage.height):
 for sx in xrange(0, segimage.width):
 if segimage[sy,sx] == (0.0, 0.0, 0.0):
 continue
 else:
 seg_color = segimage[sy,sx]
 blue = int(seg_color[0])
 green = int(seg_color[1])
 red = int(seg_color[2])
 reg_num = blue + 256 * green + 65536 * red
 for l in f:
 sp = l.split(,)
 if len(sp) == 14:
 print sy, sx  # for checking which pixel its
 reading currently
 print reg_num, sp[0]  # for checking whats
 happening
 if reg_num == int(sp[0].strip()):
 print reg_num, sp[0].strip() # for checking
 whats happening
 classification = int(sp[13].strip())


 The inside for loop is for reading a csv format file from which I am
 extracting some information.

Are you sure that the loop is only run once? In that case the most
likely thing is that the image consists of only a single pixel
(or all except the first one are black, then it might also look
as if the loop would be run only once;-)

But what looks strange is the innermost loop. You never tell what
exactly 'f' is but I would tend to assume that it is a file object
for your CSV file, which you opened somewhere before. And now you
read it in completely when dealing with the very first pixel of
your image. Afterwards, when dealing with the other pixels of the
image, there's nothing left to be read in, so the inner loop won't
be run again, making it appear as if the outer loops would only be
run once.

If my assumptions are correct and you want to read in the file
again and again for each pixel then you should either open it
again and again for each pixel or, probably better, reset the
file object so that it points back to the start of the file
before the start of the innermost loop, using the seek() method
- a simple f.seek(0) should do the job (assuming that this is
a normal file, i.e. one that can be rewound and not e.g. a re-
directed pipe).

An even better solution (if you have enough memory) might be to
read in the whole file into a list and iterate over that instead
of the file itself. And better than that might be to build a
dictionary of values in the file that you can use later on, so
you don't have to run over the whole file again and again:

d = { }
for l in f :
sp = split( l, ',' )
if len( sp ) == 14 :
d[ int( sp[ 0 ].strip( ) ) ] = int( sp[ 13 ].strip( ) )

Then you can later check directly if some color value (what
you have named 'reg_num') is in the file by using 

if reg_num in d :

and the corresponding value from the file (what you assign
to 'classification') is simply the value of the dictionary
for the key given by 'reg_num'. i.e.

classification = d[ reg_num ]

Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.call

2013-04-19 Thread Jens Thoms Toerring
Ombongi Moraa Fe moraa.lovetak...@gmail.com wrote:
 [-- text/plain, encoding 7bit, charset: ISO-8859-1, 19 lines --]

 In my python script, I have this:

 command=lynx -dump
 'phpscript?param1=%sparam2=%sparam3=%sparam4=%sparam5=%s'%(value1,value2,value3,value4)

 result=subprocess.call(command,shell=True)
 print 'xml message'

 However, the response from running the php script is also printed on output
 screen. I don't want this output.

 How can i ensure that only the last print 'xml response' is returned?

You mean is printed out? Use subprocess.Popen() and redirect
stdout to a pipe, similar to this:

 p = subprocess.Popen(command, stdout=subprocess.PIPE)
 r = p.communicate()
 print r[0]   # This is the output (to stdout)

The return value of the Popen objects communicate() method is
a tuple with two elements, first contains what got written to
stdout (if redirected to a pipe, otherwise Nome), the second
what went to stderr (again if redirected to a pipe). If you
also redirect stdin then you can pass what you want to send
to the process spawned via Popen() as an argument to commu-
nicate().

BTW, according to the dicumentation you should split the
command line into its componenents and pass that as a list
to the call() or Popen() subprocess methods, so it would
seem to reasonable to use e.g.

 command = [ 'lynx', '-dump',
 ( 'phpscript?param1={0}param2={1}param3={2}
   param4={3}param5={4}' )
 .format( value1, value2, value3, value4, value5 ) ]


Note that there was one value for creating the string to be
passed to lynx was mising.
   Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing to same file from two threads

2013-02-27 Thread Jens Thoms Toerring
Antoine Pitrou solip...@pitrou.net wrote:
 Jens Thoms Toerring jt at toerring.de writes:
  
  Paul Rubin no.email at nospam.invalid wrote:
   jt at toerring.de (Jens Thoms Toerring) writes:
in garbled output (i.e. having some output from A inside a
line written by B or vice versae) because the main thread or
  
   Yes they do get garbled like that.  Preferred Python style is put a
   single thread in charge of all the i/o to that file, and communicate
   with it by message passing through Queue objects.  That is safer than
   directly using locks.
  
  Thank you for confirmig my suspicion But you have induced
  another question: why is using a Queue safer than locking (not
  that I doubt that it might be more elegant etc.). Is it safer
  because it's less likely that one gets it wrong (e.g. by for-
  grtting to acquire the lock) or is there something inherently
  unsafe about locks?

 For the record, binary files are thread-safe in Python 3, but text files
 are not.
 Locks are safe if you use them well. As you point out, if you forget
 to acquire your lock, or if you devise a situation where there is a
 deadlock between competing locks, you can have difficult to diagnose
 issues. Queues have their internal locking all done for you.

Thank you for your kind answers!
  Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Writing to same file from two threads

2013-02-26 Thread Jens Thoms Toerring
Hi,

   I noticed in someone elses program that it writes single
lines to the same file from (what I call for loss of a better
name) the main thread of the program and from a thread sub-
sequentally started. This got me worried if it might result
in garbled output (i.e. having some output from A inside a
line written by B or vice versae) because the main thread or
the other thread could be interrupted during a call of write().
Is this a valid concern (and thus locking the file object is
required before writing to it) or am I guaranteed that this
can't happen? In the latter case I would be grateful for an
explanation what mechanism is responsible for this never to
happen.
  Thanks and best regards, Jens

PS: I already have determined experimentally that a context
switch definitely can happen between two calls of write()
(and I expected nothing else), what I'm worried about are
context switches somewhere within the very innards of what
write() does.
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing to same file from two threads

2013-02-26 Thread Jens Thoms Toerring
Paul Rubin no.email@nospam.invalid wrote:
 j...@toerring.de (Jens Thoms Toerring) writes:
  in garbled output (i.e. having some output from A inside a
  line written by B or vice versae) because the main thread or

 Yes they do get garbled like that.  Preferred Python style is put a
 single thread in charge of all the i/o to that file, and communicate
 with it by message passing through Queue objects.  That is safer than
 directly using locks.

Thank you for confirmig my suspicion;-) But you have induced
another question: why is using a Queue safer than locking (not
that I doubt that it might be more elegant etc.). Is it safer
because it's less likely that one gets it wrong (e.g. by for-
grtting to acquire the lock) or is there something inherently
unsafe about locks?

  Thank you and best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Messing with the GC

2013-01-20 Thread Jens Thoms Toerring

Hi,

thank you for the explanations. I had overlooked the
cyclic nature of what I had produced here and, of course,
the GC can't be blamed for not collecting objects that are
part of a cycle. The other question about the last refe-
rence to an object vanishing within a method call (which,
as I now clearly understand, can't happen and wouldn't make
much sense) was triggered by a segmentation fault I get
when I do something similar in PySide, so I was getting
worried if it might be due to a GC issue. Now I know its
got to be something different;-)

 Thanks and best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Messing with the GC

2013-01-20 Thread Jens Thoms Toerring
Terry Reedy tjre...@udel.edu wrote:
 On 1/20/2013 3:09 PM, Jens Thoms Toerring wrote:

   thank you for the explanations. I had overlooked the
  cyclic nature of what I had produced here and, of course,
  the GC can't be blamed for not collecting objects that are
  part of a cycle. The other question about the last refe-
  rence to an object vanishing within a method call (which,
  as I now clearly understand, can't happen and wouldn't make
  much sense) was triggered by a segmentation fault I get
  when I do something similar in PySide, so I was getting
  worried if it might be due to a GC issue. Now I know its
  got to be something different;-)

 Perhaps the hardest part of writing C extensions to CPython directly in
 C (versus something like Cython) is properly balancing increfs and
 decrefs. An incref without a later decref can lead to a memory leak. A
 decref without a preceding incref (so CPython thinks the object can be
 deleted, when it should not be) can lead to segfaults.

Definitely - I got started with Python having to write glue
code to get Python to work with a C++ library. And keeping
track of which side thinks it owns an object can sometimes
be a bit of a challenge...

 So I would report PySide code leading to segfaults to the
 PySide people.

Now that I'm more sure that it's unlikely to be a Python GC
related issue (or my not understanding what I'm doing, to be
precise) this is on my to-do list. But first I have to distill
things down to a very short example program still exhibiting
the problem - and experience tells me that this will most li-
kely result in the realization that it's not a PySide issue
at all but some misunderstanding on my side;-)

 Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Messing with the GC

2013-01-19 Thread Jens Thoms Toerring
Hi,

   triggered by some problems I had with PySide I got a bit
confused about what the GC may do in certain situations.
Here's a small test program I cobbled together:

import sys

class X( object ) :
def __init__( self, parent, cnt ) :
print( In constructor for {0} {1}.format( self, cnt ),
   file = sys.stderr )
self.parent = parent
self.cnt = cnt

def __del__( self ) :
print( In destructor for {0} {1}.format( self, self.cnt ),
   file = sys.stderr )

def foo( self ) :
print( Before, file = sys.stderr )
self.parent.z = X( self.parent, 2 ) # Is this bad?
print( After, file = sys.stderr )

class Y( object ) :
def __init__( self ) :
print( In constructor for {0}.format( self ),
   file = sys.stderr )
self.z = X( self, 1 )

def __del__( self ) :
print( In destructor for {0}.format( self ),
   file = sys.stderr )

Y( ).z.foo( )

Have a look at the line with the comment. At this point the
only reference in existence to the X class instance, of which
a method is just being executed, goes out of scope. Thus I
would assume that the GC could now kick any time, possibly
even before the following call of print() or before the method
call returns. That, in turn might result in a crash of the
script.

Is my assumption about this flawed and there are no potential
dangers? Perhaps with

Y( ).z.foo( )

a temporary second reference is created that keeps the GC
for removing the X instance...

Another thing I'm puzzled about is the output of the
script:

In constructor for __main__.Y object at 0x2919210
In constructor for __main__.X object at 0x2919310 1
Before
In constructor for __main__.X object at 0x2919350 2
After
In destructor for __main__.X object at 0x2919310 1

Ok, the destrucor for the first instance of the X class is
called only after printing out After, so the GC didn't
delete the object before. But then there are obviously no
calls of the destructors of neither the second instance
of the X class nor of the Y class instance. Shouldn't
they be invoked before the program ends?

Thanks and best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to solve complex equation?

2013-01-01 Thread Jens Thoms Toerring
Usama Khan usamazo...@gmail.com wrote:
 how to solve complex equation in pyhton? and then use it to make a program.
 . i have created new post as my last post is i guessed ranked as a cheater.
 .:(

 i know very litle about python as well as programing. . 

 which equation am taliking u will be thinking. . i am giving u the link
 kindly c that equation. . n kindly let me know the way. .

 https://groups.google.com/forum/?fromgroups=#!topic/comp.lang.python/cxG7DLxXgmo

First of all, the equation given there is unusable (even the number
of parentheses doesn't match up). Propbably it's meant to be the
one someone else posted a link to:

http://classes.engr.oregonstate.edu/cce/winter2012/ce492/Modules/06_structural_design/06-3_body.htm

This thingy can't be solved on paper so you need some iterative
algorithm to find the solution. So waht you do is modify the equa-
tion so that you have 0 on one side and then consider the other
side to be a function of SN+1. Now the problem you're left with
is to find the value(s) of SN+1 (and thus of SN) where the func-
tion has a zero-crossing. A commonly use algorithms for finding
zero-crossings is Newton's method. You can find lots of sites on
the internet describing it in all neccessary detail. It boils
down to start with some guess for the result and then calculate
the next, better approximation via

  xn+1 = xn - f(xn) / f'(xn)

where f(xn)n) is the value of the function at point xn and
f'(xn) the value of the derivative of f (with respect to x)
also at xn. You repeat the process until the difference be-
tween xn an the next, better approximation, xn+1, has become
as small as you need it.

So it's very simple to implement and the ugliest bit is pro-
bably calculating the required derivative of the function with
respect to SN+1 (wbich you can take to be x).

 Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange effect with import

2012-12-21 Thread Jens Thoms Toerring
Hans Mulder han...@xs4all.nl wrote:
 Maybe something like this:

 class ReqHandler(SocketServer.BaseRequestHandler):
 def __init__(self, request, client_address, server, ham, spam)
 super(SocketServer, self).__init__(
 self, request, client_address, server)
 self.ham = ham
 self.spam = spam
 

The only thing I had to change about this was to assign the
additional class variables before calling super() because in
the __init__() method of the base class my overloaded handle()
method is already called which needs those extra variables.

 And later:

 import functools

 server = SocketServer.TCPServer((192.168.1.10, 12345),
functools.partial(ReqHandler, ham=hello, spam=42))

Thanks a lot, that's now all working perfectly well and I got
rid of those pesky global variables;-) Probably the guys that
wrote the SocketServer module indeed didn't expect people as
dense as me to use their module and thus didn't mention that
passing additional information to a handler object can be done
this way...
  Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Strange effect with import

2012-12-20 Thread Jens Thoms Toerring
Hi,

   I hope that this isn't a stupid question, asked already a
hundred times, but I haven't found anything definitive on
the problem I got bitten by. I have two Python files like
this:

 S1.py --
import random
import S2

class R( object ) :
r = random.random( )

if __name__ == __main__ :
print R.r
S2.p( )

 S2.py --
import S1

def p( ) :
print S1.R.r

and my expectation was that the static variable 'r' of class
R would be identical when accessed from S1.py and S2.py.
Unfortunately, that isn't the case, the output is different
(and R seems to get instantiated twice).

But when I define R in S2.py instead

 S1.py --
import S2

print S2.R.r
S2.p( )

 S2.py --
import random

class R( object ) :
r = random.random( )

def p( ) :
print R.r

or, alternatively, if I put the defintion of class R into
a third file which I then import from the other 2 files,
things suddenly start to work as expected/ Can someone
explain what's going one here? I found this a bit sur-
prising.

This is, of course, not my real code - it would be much
more sensible to pass the number to the function in the
second file as an argument - but is the smallest possinle
program I could come up with that demonstrate the prob-
lem. In my real code it's unfortunately not possible
to pass that number to whatever is going to use it in the
 other file, I have to simulate a kind of global variable
shared between different files.

Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange effect with import

2012-12-20 Thread Jens Thoms Toerring
Thanks a lot to all three of you: that helped me understand
the errors of my ways! You just saved me a few more hours
of head-scratching;-)

A few replies to the questions and comments by Steven:

Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote:
 On Thu, 20 Dec 2012 20:39:19 +, Jens Thoms Toerring wrote:
  and my expectation was that the static variable 'r' of class R 

 The terminology we prefer here is class attribute, not static 
 variable. Attributes are always assigned in dynamic storage, whether 
 they are per-instance or on the class.

I'm comimg from C/C++ and that's were my terminology is from,
I know I still have to learn a lot more about Python;-)

good advice snipped

  In my real code it's unfortunately not
  possible to pass that number to whatever is going to use it in the
   other file, I have to simulate a kind of global variable
  shared between different files.

 Well, I find that hard to believe. Not convenient? I could believe 
 that. Difficult? Maybe. Tricky? I could even believe that. But not 
 possible? No, I don't believe that it is impossible to pass variables 
 around as method arguments.

You are rather likely right and I probably should have written:
I don't see any way to pass that variable to the object that
is supposed to use it. Perhaps you have an idea how it could
be done correctly when I explain the complete picture: I'm
writing a TCP server, based on SocketServer:

 server = SocketServer.TCPServer((192.168.1.10, 12345), ReqHandler)

where ReqHandler is the name of a class derived from
SocketServer.BaseRequestHandler

 class ReqHandler(SocketServer.BaseRequestHandler):
 ...

A new instance of this class is gernerated for each connection
request to the server. In the call that creates the server I can
only specify the name of the class but no arguments to be passed
to it on instantiation - at least I found nothing in the docu-
mentation. On the other hand I need to get some information into
this class and thus the only idea I came up with was to use some
kind of global variable for the purpose. Perhaps there's a much
better way to do that but I haven't found one yet. Or perhaps it
is an omission in the design of SocketServer or (more likely) my
mis-understanding of the documentation (as I wrote I'm relatively
new to Python).
  Thnak you and best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange effect with import

2012-12-20 Thread Jens Thoms Toerring
Terry Reedy tjre...@udel.edu wrote:
server = SocketServer.TCPServer((192.168.1.10, 12345), ReqHandler)
 
  where ReqHandler is the name of a class derived from
  SocketServer.BaseRequestHandler

 You misunderstood the doc. You pass the class, not the name of the class.
  From 21.19.4.1. socketserver.TCPServer Example
  server = socketserver.TCPServer((HOST, PORT), MyTCPHandler)

Yes, I meant the class, but I'm a bit weak on nomenclature in
Python;-)

  A new instance of this class is gernerated for each connection
  request to the server. In the call that creates the server I can
  only specify the name of the class but no arguments to be passed

 Code those arguments directly into the handle method of your version of 
 MyTCPhandler. Or if you need to override multiple methods and use the 
 same values in multiple methods, override __init__ and add self.x = 
 x-value statements.

Sorry, you lost me there: what means code those arguments
directly into the handle method? According to the documen-
tation (or at least to my understanding of it;-) the handle()
method is suppose to accept just one argument, 'self'. And
even if I would change the method to accept more arguments
and that wouldnt blow up into my face, where would they be
coming from (and from where would I pass them)?

   Best regards, Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange effect with import

2012-12-20 Thread Jens Thoms Toerring
Hans Mulder han...@xs4all.nl wrote:
 What happens if instead of a class you pass a function that
 takes the same arguments as the SocketServer.BaseRequestHandler
 constructor and returns a new instance of your ReqHandler?

 That's not quite what the documentaion clls for, but I'd hope
 it's close enough.

Interesting idea - I'm not yet at a level of Python wizardry
that I would dare to do something that's not explicitely bles-
sed be the documentation;-) 

 Maybe something like this:

 class ReqHandler(SocketServer.BaseRequestHandler):
 def __init__(self, request, client_address, server, ham, spam)
 super(SocketServer, self).__init__(
 self, request, client_address, server)
 self.ham = ham
 self.spam = spam
 

 And later:

 import functools

 server = SocketServer.TCPServer((192.168.1.10, 12345),
functools.partial(ReqHandler, ham=hello, spam=42))

Ok, that's still way over may head at the moment;-) I will hhave
to read up on functools tomorrow, it's the first time I heard of
it but it looks quite interesting at a first glance.

Thank you for these ideas, I'll need a bit of time to figure out
these new concepts and I don't think I'm up to it tonight any-
more;-)
 Best regards. Jens
-- 
  \   Jens Thoms Toerring  ___  j...@toerring.de
   \__  http://toerring.de
-- 
http://mail.python.org/mailman/listinfo/python-list