[Tutor] Generic dictionary

2016-11-20 Thread Thorsten Kampe
[Crossposted to tutor and general mailing list]

Hi,

I'd like to extend the dictionary class by creating a class that acts 
like a dictionary if the class is instantiated with a dictionary and 
acts like a "dictitem" ([(key1, value1), (key2, value2), ...]) if 
instantiated with a list (that is dictitem).

The code (see extract at bottom) works well but it contains a lot of 
"if this is a dictionary then do as a dictionary already does" 
boilerplate code". How can I "inherit"(?)/"subclass"(?)/derive from 
dict so I don't have to write the code for the dictionary case?

Thorsten

```
class GenericDict:
"""
a GenericDict is a dictionary or a list of tuples (when the keys
are not hashable)
"""
def __init__(inst, generic_dict):
inst._generic = generic_dict

def __getitem__(inst, key):
if isinstance(inst._generic, dict):
return inst._generic[key]
else:
return inst.values()[inst.keys().index(key)]

def values(inst):
if isinstance(inst._generic, dict):
return inst._generic.values()
else:
try:
return list(zip(*inst._generic))[1]
except IndexError:  # empty GenericDict
return ()

def keys(inst):
if isinstance(inst._generic, dict):
return inst._generic.keys()
else:
try:
return list(zip(*inst._generic))[0]
except IndexError:  # empty GenericDict
return ()
```

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Standardizing on Unicode and utf8

2009-02-25 Thread Thorsten Kampe
* Dinesh B Vadhia (Fri, 20 Feb 2009 02:52:27 -0800)
 We want to standardize on unicode and utf8

Very good idea.

 and would like to clarify and verify their use to minimize encode
 ()/decode()'ing:
 
 1.  Python source files 
 Use the header: # -*- coding: utf8 -*-

Good idea (although only valid for comments and inline strings


 2.  Reading files
 In most cases, we don't know the source encoding of the files being
 read. Do we have to decode('utf8') after reading from file?

No. If you don't know the encoding of the file you can't decode it, of 
course. You can read() it of course, but you can't process it (as text).
 
 3. Writing files
 We will always write to files in utf8. Do we have to encode('utf8')
 before writing to file?

Yes, sure.

 Is there anything else that we have to consider?

Hm, in general nothing I'm aware of.

Thorsten

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Standardizing on Unicode and utf8

2009-02-25 Thread Thorsten Kampe
* spir (Fri, 20 Feb 2009 13:02:59 +0100)
 Le Fri, 20 Feb 2009 02:52:27 -0800,
 Dinesh B Vadhia dineshbvad...@hotmail.com s'exprima ainsi:
 
  We want to standardize on unicode and utf8 and would like to clarify and
  verify their use to minimize encode()/decode()'ing:
  
  1.  Python source files 
  Use the header: # -*- coding: utf8 -*-
 
 You don't even need fancy decoration:
 
 # coding: utf-8
 
 is enough.

Sure? Never heard of that. Interesting...

Thorsten

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Add readline capabilities to a Python build 2.6 on Ubuntu

2009-02-25 Thread Thorsten Kampe
* Eric Dorsey (Thu, 19 Feb 2009 12:24:07 -0700)
 Still doesnt work.. I just get this when I hit the up arrow: ^[[A
 
 Bah. It works in the 2.5 version that came packaged with it. Thanks for
 trying :)

There's a log for the ./configure. See if the configure script can find 
readline.

Thorsten

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to make python program as executable

2008-01-30 Thread Thorsten Kampe
* Luke Paireepinart (Wed, 30 Jan 2008 03:59:59 -0600)
 I think he meant  i want to make the application [into an]
 executable I.E. he wants an .exe file on Windows.
 In this case, you can use py2exe, and there's another alternative I
 can't remember.

The better alternative is Pyinstaller...

Thorsten

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Versions

2007-12-15 Thread Thorsten Kampe
* Tiger12506 (Fri, 14 Dec 2007 16:23:00 -0500)
  Despite what your english teacher might have tried to make you
  believe, they were wrong about the lack of a neutral in english.
  Just like ending sentences with prepositions has always been done
  and always will be done, the use of they to refer to someone of
  indeterminate gender has been well and alive for hundreds of
  years.
 
  The fact you think it isn't okay is because some english teacher
  sold you a line of crap about prescriptive grammar rules that
  don't actually hold true in actual writing. Many grammar books try
  to make the language into what it is not, rather than describing
  it as it is.
 
 No. I form my own opinions and do not believe individuals such as my
 english teachers unless I truly believe that each one is correct.
 Each of my english teachers will not tell you to use they or even
 the masculine, instead prefering the proper way to be his/her,
 and equivalent forms. I personally believe this to be a waste of
 time, and efficiency is one of my primary motivators. Therefore, for
 space and time I use he for an unknown.

That's common English usage:
'In languages with a masculine and feminine gender (and possibly a 
neuter), the masculine is usually employed by default to refer to 
persons of unknown gender. This is still done sometimes in English, 
although an alternative is to use the singular they.'[1]

 Proper english (as it is from my viewpoint) would be to restructure
 the sentence so that it uses one in that instance. (My english
 teachers would gasp at this) This makes the most logical sense. For
 more than one person of unknown gender, English uses everyone. So
 for one person of unknown gender, English uses one.

No (see above).

 They does not match plurality. Using they as you describe it
 would mean that we should be forming sentences such as They works
 They bakes They codes in python. Clearly this does not sound
 correct because the word they is meant to be used as a plural
 pronoun only.

No (see above).

Thorsten
[1] 
http://en.wikipedia.org/wiki/Grammatical_gender#Indeterminate_gender

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] ipython / readline problem

2007-12-14 Thread Thorsten Kampe
* Tiago Saboga (Thu, 13 Dec 2007 17:05:55 -0200)
 On Thu, Dec 13, 2007 at 07:41:08AM -0500, Kent Johnson wrote:
  Tiago Saboga wrote:
  type 'exceptions.UnicodeEncodeError': 'ascii' codec can't encode
  character u'\xe7' in position 2: ordinal not in range(128)
 
  ===
 
  What's happening? Why do the readline methods accept a multibyte
  string ('ação') but not a unicode (u'ação')?
 
  I don't know what is happening with readline but this error is usually the
  result of converting a Unicode string to a plain string without specifying
  encoding, either explicitly by calling str() or implicitly such as in a
  print statement:
 
 I already knew that, but it helped somehow ;)
 
 Apparently the problem is that ipython converts the input to unicode,
 while the readline module wants a string object. With single line
 input, ipython doesn't interfere with readline, but for multiline
 input, it updates readline's history, but it tries to to that with the
 unicode object. I've sent a patch to the ipython bug to reencode the
 string to sys.stdin.encoding before submitting it to readline.

For the new Ipython there's a new setting:

pyreadline.unicode_helper.pyreadline_codepage='utf8'

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Installing modules via setuptools in a script

2007-11-18 Thread Thorsten Kampe
Hi,

can anyone give me a short code snippet how to install a missing 
module via setuptools (assuming setuptools is already installed)?!

Something like this:

try:
import missing_module
except import_error
import setuptools
setuptools.whatever.install(missing_module)

Thorsten

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] repeated times

2007-11-04 Thread Thorsten Kampe
* linda.s (Sun, 4 Nov 2007 01:39:46 -0800)
 On Nov 2, 2007 1:03 AM, ALAN GAULD [EMAIL PROTECTED] wrote:
I want to run an .exe file and get the output many times.
   Given that I know that you know about loops I have to
   ask what you see as the problem?
  
  I want to run it many times and export all the output to a text file.
 
  OK, Do you mean you want to run the program multiple times
  but put the output in the same file?
 
 Yes. For example, I want to run the exe file one hundred times and
 save all the results into one file.
 Is there any example code to do that?

There's no reason to do that in Python. You should use a batch or 
shell script for that. If you really insist on using Python then look 
at the subprocess module...

Thorsten

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to Python and Linux

2007-09-28 Thread Thorsten Kampe
* Armand Nell (Wed, 26 Sep 2007 08:07:12 +0200)
 I am new to python programming and also the linux enviroment most of my
 skills are windows based and programming skills is visual basics. I decided
 that it would be a great start and new direction for me to learn python and
 at the same time linux. However I have already run into a wall, and any help
 would be appreciated even if you can direct me where to find the info or
 'turor'.
 [...]
 In windows, if i write a program in Python and save it I then can simply
 double click the icon and the program will execute in a console window. Now
 under Fedoracore I write my program in gedit save it in my
 \home\(username)\python directory, when I double click it, it opens up agian
 in gedit. Now true it is maybe a simple error from me but mostly it is me
 that don't know how to work with python on linux.
 
 I would like to know how do I test(run) the programs I write under
 fedoracore?

It's exactly the same as with with Visual Basic (visual basics? Are 
you sure you have experience in that language?) and Windows: run it 
in a command window (like python myscript.py) or associate the file 
type (.py) with the program.

How you do that depends on your desktop environment (KDE or Gnome 
probably) but it shouldn't take you more than ten seconds to find out 
how to do it.


Thorsten

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to Python and Linux

2007-09-28 Thread Thorsten Kampe
* Thorsten Kampe (Fri, 28 Sep 2007 14:09:24 +0100)
 It's exactly the same as with with Visual Basic [...]

Guess I mixed that up with VBScript...

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] executing file properly

2007-08-07 Thread Thorsten Kampe
* Vivian Tini (Tue,  7 Aug 2007 12:20:29 +0200)
 The TestCases executable works properly when I run it from the shell prompt.
 
 Then I try to run it from the Python command prompt by the following script:
  import os
  os.system(home/.../.../.../TestCases) 
 
 I used ... to type it short.
 
 Then it gives the following error output:
 cannot open Des.in 
 
 Well in this case tbe os.system command is typed correctly already since this 
 the error message given out is written in the code itself. So the executable 
 run already only when running from Python prompt it cannot open this file.
 
 Could anyone suggest me what would be the cause of this problem?

You said it yourself: I used ... to type it short.

 And how  should I handle it ?

Even simpler: don't use ...

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] os.path.exists(path) returns false when the pathactually exists!

2007-07-28 Thread Thorsten Kampe
* Tiger12506 (Sat, 28 Jul 2007 10:33:36 -0500)
  So the better question is, does is this file a broken symbolic link or
  can os.stat() be executed on it?
 
  How do I find if it is a broken symbolic link in Windows 2000 ?
 
  os.stat(path) returns an OSError saying that there is no such file or 
  directory
 
 Wow. I've never heard of this. What are the file's attributes? What does it 
 say about the file when you right-click Properties? Hmmm... what's going on 
 here? Permission not granted to execute os.stat()? Why wouldn't anyone have 
 permission to do that?
 
 A broken symbolic link... That means a hard link that has been cut-off 
 right?

No, symbolic links and hard links are totally different.

 (Hard-links are like pointers to files in NTFS)  ~ so if the file's 
 been moved, that hard link will point to nothing, being broken, right?

Thre are no broken hard links...

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Shelve del not reducing file size

2007-07-28 Thread Thorsten Kampe
* Kent Johnson (Fri, 27 Jul 2007 08:06:33 -0400)
 Barton David wrote:
  *sigh* I'm really going off Python.
 
 In what way is it Python's fault that the dbm database doesn't reclaim 
 disk space?

It's actually how most databases work. Even a simple Outlook pst file 
(which is a database, too) works this way. I thought everyone knows or 
heard about this.

Thorsten

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how long?

2007-07-03 Thread Thorsten Kampe
* Ben Waldin (Tue, 3 Jul 2007 19:46:42 +1200)
 How long will it take until I successfully create my own working program that 
 is useful? I have crated the address book ones in the tutors and just want to 
 know how long it takes before I start to create my own thought up programs 
 that will be useful. Thanks Ben   

Approximately ten days, four hours and six minutes

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Power Shells [WAS:] optimization: faster than for

2007-07-02 Thread Thorsten Kampe
* elis aeris (Sun, 1 Jul 2007 22:27:11 -0700)
 uh, can i ask about something very quickly?

Don't hijack a completely unreleated thread. 
 
 how do i write a function to do
 
 a pop up window with designated window name, type (ok, cancel, those) and
 message?

Okay, a very quick answer: EasyGUI

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pop up window

2007-07-02 Thread Thorsten Kampe
* elis aeris (Mon, 2 Jul 2007 02:32:07 -0700)
 i won't do that again, i am a 2 day newbie (hello)
 
 uh,
 
 how about a less quick one that's built-in in python ?

Are you replying to me? Please quote the parts you are referring to. 
Anyway, Python does not come with built-in pop-up support.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] #!/usr/bin/env python vs #!/usr/local/bin/python

2007-06-14 Thread Thorsten Kampe
*  (Thu, 14 Jun 2007 13:14:13 +0300)
 how to choose between #!/usr/bin/env python and
 #!/usr/local/bin/python in the beginning of the script ?

Just choose. Say I want to the script. Say I want '#!/usr/bin/env 
python'

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] easygui question, again

2007-06-11 Thread Thorsten Kampe
* Rafael Bejarano (Sun, 10 Jun 2007 20:49:40 -0500)
 As I understand it from the description of this list, that is exactly  
 its purpose--to help people who no relatively little python.

That's correct. But your problem (or the solution to your problem) is 
more related to _Operating System_ basics as to Python basics (see 
also Alan Gaulds response).

And please don't send me private email.

Thanks, Thorsten

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] easygui question, again

2007-06-10 Thread Thorsten Kampe
* Rafael Bejarano (Sun, 10 Jun 2007 02:24:56 -0500)
 On Jun 9, 2007, at 5:26 AM, Kent Johnson wrote:
  You could try running the easygui demo - just type
  python easygui.py
  on the command line from the directory containing easygui.
 
 At your convenience, please explain the above statement. I don't know  
 what from the command line means.

It would really make sense if you learn a bit of the basics before you 
start doing the advanced stuff like (Python) programming...

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] easygui question, again

2007-06-10 Thread Thorsten Kampe
* Rafael Bejarano (Sun, 10 Jun 2007 19:35:32 -0500)
 On Jun 10, 2007, at 5:00 PM, Thorsten Kampe wrote:
  It would really make sense if you learn a bit of the basics before you
  start doing the advanced stuff like (Python) programming...
 
 What do you mean by a bit of the basics?

Well, like - what is a command line, how do I change from one 
directory to another and stuff. The basics...

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Multi-line comments?

2007-06-07 Thread Thorsten Kampe
* Brad Tompkins (Wed, 6 Jun 2007 16:41:31 -0700)
 Is there a way to make use of multi-line comments when programming using
 python?  Having to stick a # in front of every line gets pretty tedious when
 I want to make a comment more detailed than I normally would.
 
 If there isn't a way, can someone recommend a text editor (I know emacs and
 probably vi can do this, but they seem difficult to use) that will comment
 out blocks of text automatically for me?

EditPad Pro under Windows and Komodo Edit from ActiveState...

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lists - append - unique and sorted

2007-06-06 Thread Thorsten Kampe
* roberto (Wed, 6 Jun 2007 17:17:05 +0200)
 can i append a item to a list using criterias:
 
 - UNIQUE - if there already exist don't append

test whether it's already in the with in or use sets
 
 and/or
 
 - SORTED - INSERT in the correct place using some criteria?

insert and then sort again by this criterion

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] installing maya/python on linux

2007-06-04 Thread Thorsten Kampe
* Preecha Bundrikwong (Mon, 4 Jun 2007 16:07:49 +0700)
 Can anybody please give me an easy instruction on installing maya/python
 (pysource/sourcepy etc.) on a Linux machine.

http://cgkit.sourceforge.net/mayadoc/install.html

 I've already downloaded it but never succeeded installing.

Aha. Why?
 
 BTW, there's no way to make installing process easier to life than this?
 (all those compile, source, .so, etc.. h...)

http://cgkit.sourceforge.net/support.html

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] SSH using python

2007-06-04 Thread Thorsten Kampe
* Chandrashekar (Mon, 4 Jun 2007 01:45:57 -0700 (PDT))
 Can anyone tell me how to do ssh to a machine using python and execute 
 programs on the remote machine? Thanks in advance. 

Paramiko

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] installing maya/python on linux

2007-06-04 Thread Thorsten Kampe
* Preecha Bundrikwong (Mon, 4 Jun 2007 17:30:03 +0700)
 Thanks, but sorry again; how do you 'compile' the downloaded package? I'm
 new to Linux :-(

./configure
make
make install

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating a closed source application in Python?

2007-05-28 Thread Thorsten Kampe
* Sophie Marston (Mon, 28 May 2007 09:56:45 +0100)
 Is it possible to create a closed source project in Python? Like in C++ 
 or something, where they can't view your code?

Google for Python code obfuscation (web and comp.lang.python)

Thorsten

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] trouble with if

2007-05-28 Thread Thorsten Kampe
* Rikard Bosnjakovic (Mon, 28 May 2007 17:55:42 +0200)
 On 5/28/07, Thorsten Kampe [EMAIL PROTECTED] wrote:
  Do you really think someone can or will read what you wrote? I've
  never seen something so horribly formatted like you emails - and I've
  seen lots of awful formatted emails...
 
 Looks fine at my end.

As Brian van den Broek said[1] to the Hotmail guy: snip of all 
previous exchanges which are too badly formatted to be readable.

Maybe because he's posting HTML and the text part is complete crap. 
*Hotmail* *gnarrf*.


Thorsten
[1] http://permalink.gmane.org/gmane.comp.python.tutor/40742

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] html links

2007-05-25 Thread Thorsten Kampe
* max . (Mon, 14 May 2007 20:27:15 -0600)
 does anyone know of a tutorial for finding links in a web site with python.

import formatter, \
   htmllib,   \
   urllib

url = 'http://python.org'

htmlp = htmllib.HTMLParser(formatter.NullFormatter())
htmlp.feed(urllib.urlopen(url).read())
htmlp.close()

print htmlp.anchorlist

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor