calling loaded DLL function expecting POINT * argument

2012-08-24 Thread Tim Williams
Hello all,

I'm trying to use the ctypes module to call functions in a DLL. I've figured 
out how to modify my path so the library is found, and I can call LoadLibrary 
on it, but one of the functions expects an array of POINTS. Here is the 
prototype from the .h file:


TRACKER_API HRESULT InitializeMask(HANDLE pHandle, int nWidth, int nHeight, 
POINT* ptMasks, int nNumPoints);

I did a 

from ctypes.wintypes import *

Here's my test script:
##
'''
Created on Aug 23, 2012

@author: williams
'''
import os
import numpy as np
import scipy.io
from ctypes import *
from ctypes.wintypes import *


matdata = 
scipy.io.loadmat(os.path.join(os.environ['userprofile'],'Desktop','S31_f1.mat'))
data = matdata['data']
nHeight,nWidth = data.shape

pth = os.environ['path'].split(';')
pth.append(os.path.join(os.environ['userprofile'],'My Documents','DLLs'))
os.environ['path'] = ';'.join(pth)
tck=oledll.LoadLibrary('Tracker')
hTrkr = tck.CreateTracker()
maskImage = np.zeros((1024,1280),dtype='int32')
maskImage[300:305,100:105] = True
idx = np.array(maskImage.nonzero())
nPoint = idx.shape[1]

ptMaskType = nPoint * POINT
pts = zip(idx[1,:],idx[0,:])
ptMasks = ptMaskType(*pts)

tck.InitializeMask.argtypes=(HANDLE, c_int, c_int, c_void_p, c_int)
InitMaskResult = tck.InitializeMask(hTrkr, nWidth, nHeight, ptMasks, nPoint)

if __name__ == '__main__':
pass
##

so I have the POINT class, and I've created the array of POINTs. When I try to 
call this function, I get this message:

>>> InitMaskResult = tck.InitializeMask(hTrkr, nWidth, nHeight, ptMasks, nPoint)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Procedure probably called with too many arguments (20 bytes in 
excess)


This is the first time that I've tried to use the ctypes module, so any help is 
appreciated.

TIA,
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: calling loaded DLL function expecting POINT * argument

2012-09-04 Thread Tim Williams
On Sunday, August 26, 2012 9:23:49 PM UTC-4, Tim Roberts wrote:
> Tim Williams  wrote:
> 
> 
> 
> >Hello all,
> 
> >
> 
> >I'm trying to use the ctypes module to call functions in a DLL. I've
> 
> >figured out how to modify my path so the library is found, and I can 
> 
> >call LoadLibrary on it, but one of the functions expects an array of
> 
> > POINTS. Here is the prototype from the .h file:
> 
> >
> 
> >
> 
> >TRACKER_API HRESULT InitializeMask(HANDLE pHandle, int nWidth, int nHeight, 
> >POINT* ptMasks, int nNumPoints);
> 
> 
> 
> How is TRACKER_API defined?  You're using ctypes.oledll, which uses the
> 
> __stdcall calling convention.  It's possible your DLL is defined as
> 
> __cdecl.  Try cdll.LoadLibrary instead of oledll.LoadLibrary.
> 
> -- 
> 
> Tim Roberts, [email protected]
> 
> Providenza & Boekelheide, Inc.

Thanks for the reply. I've been out all last week. I'll give it a try.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: calling loaded DLL function expecting POINT * argument

2012-09-04 Thread Tim Williams
On Tuesday, September 4, 2012 8:16:33 AM UTC-4, Tim Williams wrote:
> On Sunday, August 26, 2012 9:23:49 PM UTC-4, Tim Roberts wrote:
> 
> > Tim Williams  wrote:
> 
> > 
> 
> > 
> 
> > 
> 
> > >Hello all,
> 
> > 
> 
> > >
> 
> > 
> 
> > >I'm trying to use the ctypes module to call functions in a DLL. I've
> 
> > 
> 
> > >figured out how to modify my path so the library is found, and I can 
> 
> > 
> 
> > >call LoadLibrary on it, but one of the functions expects an array of
> 
> > 
> 
> > > POINTS. Here is the prototype from the .h file:
> 
> > 
> 
> > >
> 
> > 
> 
> > >
> 
> > 
> 
> > >TRACKER_API HRESULT InitializeMask(HANDLE pHandle, int nWidth, int 
> > >nHeight, POINT* ptMasks, int nNumPoints);
> 
> > 
> 
> > 
> 
> > 
> 
> > How is TRACKER_API defined?  You're using ctypes.oledll, which uses the
> 
> > 
> 
> > __stdcall calling convention.  It's possible your DLL is defined as
> 
> > 
> 
> > __cdecl.  Try cdll.LoadLibrary instead of oledll.LoadLibrary.
> 
> > 
> 
> > -- 
> 
> > 
> 
> > Tim Roberts, [email protected]
> 
> > 
> 
> > Providenza & Boekelheide, Inc.
> 
> 
> 
> Thanks for the reply. I've been out all last week. I'll give it a try.

cdll.LoadLibrary did trick! Thanks again. Now on to the next step
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing dll

2012-09-06 Thread Tim Williams
On Thursday, September 6, 2012 11:07:07 AM UTC-4, Helpful person wrote:
> I am a complete novice to Python.  I wish to access a dll that has
> 
> been written to be compatible with C and VB6.  I have been told that
> 
> after running Python I should enter  "from ctypes import *" which
> 
> allows Python to recognize the dll structure.  I have placed the dll
> 
> into my active directory (if that's the correct word, one on my path)
> 
> for simplification.
> 
> 
> 
> I tried:   "import name.dll" but this just gave me an error telling me
> 
> that there was no such module.
> 
> 
> 
> Can someone please help?
> 
> 
> 
> Richard

I'm new to using the ctypes module also, but what I did to find the library was 
I appended the location of the dll to my PATH like so: (this is Windows)

pth = os.environ['path'].split(';')
pth.append(os.path.join(os.environ['userprofile'],'My Documents','DLLs'))
os.environ['path'] = ';'.join(pth)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing dll

2012-09-06 Thread Tim Williams
On Thursday, September 6, 2012 4:21:56 PM UTC-4, Tim Williams wrote:
> On Thursday, September 6, 2012 11:07:07 AM UTC-4, Helpful person wrote:
> 
> > I am a complete novice to Python.  I wish to access a dll that has
> 
> > 
> 
> > been written to be compatible with C and VB6.  I have been told that
> 
> > 
> 
> > after running Python I should enter  "from ctypes import *" which
> 
> > 
> 
> > allows Python to recognize the dll structure.  I have placed the dll
> 
> > 
> 
> > into my active directory (if that's the correct word, one on my path)
> 
> > 
> 
> > for simplification.
> 
> > 
> 
> > 
> 
> > 
> 
> > I tried:   "import name.dll" but this just gave me an error telling me
> 
> > 
> 
> > that there was no such module.
> 
> > 
> 
> > 
> 
> > 
> 
> > Can someone please help?
> 
> > 
> 
> > 
> 
> > 
> 
> > Richard
> 
> 
> 
> I'm new to using the ctypes module also, but what I did to find the library 
> was I appended the location of the dll to my PATH like so: (this is Windows)
> 
> 
> 
> pth = os.environ['path'].split(';')
> 
> pth.append(os.path.join(os.environ['userprofile'],'My Documents','DLLs'))
> 
> os.environ['path'] = ';'.join(pth)

I should have also mentioned to look at LoadLibrary in the ctypes module. e.g. 

mylib=cdll.LoadLibrary('mylib.dll')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: numpy array operation

2013-01-29 Thread Tim Williams
On Tuesday, January 29, 2013 3:41:54 AM UTC-5, C. Ng wrote:
> Is there a numpy operation that does the following to the array?
> 
> 
> 
> 1 2  ==>  4 3
> 
> 3 4   2 1
> 
> 
> 
> Thanks in advance.

>>> import numpy as np
>>> a=np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
   [3, 4]])
>>> np.fliplr(np.flipud(a))
array([[4, 3],
   [2, 1]])

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


Re: Accessing the files by last modified time

2012-03-22 Thread Tim Williams
On Mar 22, 7:33 am, Sangeet  wrote:
> Hi
>
> I am new to the python programming language.
>
> I've been trying to write a script that would access the last modified file 
> in one of my directories. I'm using Win XP.
>
> I saw a similar topic, on the forum before, however the reply using 
> (os.popen) didn't work out for me. I'm not sure whether it was due to syntax 
> errors either.
>
> Thanks,
> Sangeet

Check out os.stat()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem loading matlab data with ompc and python

2012-05-24 Thread Tim Williams
On May 23, 5:10 pm, no1  wrote:
> Hi, we're investigating transitioning our company from matlab to python. We 
> found OMPC as a MATLAB m-file-to Python translator, but we're encountering a 
> problem using the translated code to import MATLAB data structures into 
> Python. For example, when we save data within MATLAB this way:
>
> x.a = 5;
> x.b = 6;
> save -v6 test x
>
> this saves data in test.mat, with MATLAB version 6 compatibility (OMPC says 
> it's not compatible with the latest versions of MATLAB). The code to read in 
> data in MATLAB is just
>
> load test
>
> and when we run it through OMPC we get
>
> load(mstring('test.mat'))
>
> but when we run it we get the error message
>
> File "ompclib\ompclib_numpy.py", line 1496, in load
> KeyError: "[('a', '|O4'), ('b', '|O4')]"
>
> Reading in simpler data (up to arrays) does not have this problem.
>
> To get other people in the company to transition, we were hoping that the 
> translation process could be done in one step or on the fly. We could read in 
> MATLAB data using I/O functions imported from scipy, but then the transition 
> isn't seamless any more.
>
> Is there a simple fix to using OMPC? Or a similar alternative that would work 
> better?
>
> Thanks

Have you tried using loadmat from the scipy.io module?

http://docs.scipy.org/doc/scipy/reference/io.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: matplot plot hangs

2017-11-02 Thread Tim Williams
On Wednesday, November 1, 2017 at 6:30:27 PM UTC-4, Andrew Z wrote:
> nope. it doesnt:
> 
> I added print-s after each line and that produced:
> [az@hp src]$ cat ./main1.py
> import matplotlib.pyplot as plt
> print("imported")
> plt.plot([1,2,4,1])
> print("plot is done")
> plt.show()
> print("show is done")
> 
> [az@hp src]$ python3.5 ./main1.py
> imported
> ^C^Z
> [1]+  Stopped python3.5 ./main1.py
> 
> 
> On Wed, Nov 1, 2017 at 9:31 AM, Vlastimil Brom 
> wrote:
> 
> > 2017-11-01 13:49 GMT+01:00 Andrew Z :
> > > Wolfgang,
> > >  I tried to ran from ide with no rwsults, so now im trying from a
> > terminal
> > > in xwindow.
> > > The .plot is the last line in the script and it does hang trying to
> > execute
> > > it.
> > >
> > >
> > > On Nov 1, 2017 05:44, "Wolfgang Maier" <
> > > [email protected]> wrote:
> > >
> > > On 01.11.2017 00:40, Andrew Z wrote:
> > >
> > >> hello,
> > >>   learning python's plotting by using matplotlib with python35 on
> > fedora 24
> > >> x86.
> > >>
> > >> Installed matplotlib into user's directory.
> > >> tk, seemed to work -
> > >> http://www.tkdocs.com/tutorial/install.html#installlinux - the window
> > >> shows
> > >> up just fine.
> > >> but when trying to run the simple plot (
> > >> https://matplotlib.org/examples/pylab_examples/simple_plot.html) the
> > >> script
> > >> is hanging on;
> > >>
> > >> plt.plot(t, s)
> > >>
> > >> attempts to
> > >> matplotlib.interactive(True) didn't bring anything,
> > >>
> > >>
> > > Hi Andrew,
> > >
> > > From which environment are you trying to run the example? In the
> > terminal,
> > > from within some IDE, inside a jupyter notebook?
> > >
> > > Are you sure the script "is hanging on plt.plot(t, s)" and not after
> > that?
> > >
> > > Best,
> > > Wolfgang
> > >
> > > --
> > Hi,
> > sorry if it is too trivial, just to make sure, do you have a call to
> > "show()" the resulting plot in the code?
> >
> > An elementary plotting code might be e.g.:
> >
> > import matplotlib.pyplot as plt
> > plt.plot([1,2,4,1])
> > plt.show()
> >
> > Does this work in your environment?
> >
> > It was not quite clear, what do you plan with interactive drawing, or
> > whether you are using e.g. plt.interactive(True) already - this might
> > be a problem as there could be collisions or the plot window is closed
> > after the standalone script finishes.
> >
> > hth,
> >  vbr
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >

Have you tried 

plt.show(block=False)
?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem with assignment. Python error or mine?

2017-12-22 Thread Tim Williams
On Thursday, December 21, 2017 at 12:18:11 PM UTC-5, MarkA wrote:
> On Thu, 21 Dec 2017 07:05:33 -0800, rafaeltfreire wrote:
> From docs.python.org:
> 
> 8.10. copy — Shallow and deep copy operations
> 
> Source code: Lib/copy.py
> 
> Assignment statements in Python do not copy objects, they create bindings 
> between a target and an object. For collections that are mutable or 
> contain mutable items, a copy is sometimes needed so one can change one 
> copy without changing the other. This module provides generic shallow and 
> deep copy operations (explained below)...
> 
> 
> > Dear community, I am having the following problem when I am assigning
> > the elements of a vector below a certain number to zero or any other
> > value.
> > I am creating a new variable but Python edits the root variable. Why?
> > 
> > import numpy as np
> > 
> > X=np.arange(1, 1, 1) #root variable x1=X x1[x1<1]=0
> > 
> > print(X)
> > Out[1]: array([ 0.,  0.,  0., ...,  0.,  0.,  0.])
> > 
> > Why? It is supposed to be the original value Thank you for your
> > time Rafael
> 
> 
> 
> -- 
> MarkA
> 
> We hang petty theives, and appoint the great theives to public office
>   -- Aesop

Shouldn't the OP just create a list for what he want's to do?

X = list(np.arange(1, 1, 1)) #root variable x1=X x1[x1<1]=0

Then I think his other statements would do what he expects, no?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Problem with assignment. Python error or mine?

2017-12-22 Thread Tim Williams
On Friday, December 22, 2017 at 8:41:29 AM UTC-5, Tim Williams wrote:
> On Thursday, December 21, 2017 at 12:18:11 PM UTC-5, MarkA wrote:
> > On Thu, 21 Dec 2017 07:05:33 -0800, rafaeltfreire wrote:
> > From docs.python.org:
> > 
> > 8.10. copy — Shallow and deep copy operations
> > 
> > Source code: Lib/copy.py
> > 
> > Assignment statements in Python do not copy objects, they create bindings 
> > between a target and an object. For collections that are mutable or 
> > contain mutable items, a copy is sometimes needed so one can change one 
> > copy without changing the other. This module provides generic shallow and 
> > deep copy operations (explained below)...
> > 
> > 
> > > Dear community, I am having the following problem when I am assigning
> > > the elements of a vector below a certain number to zero or any other
> > > value.
> > > I am creating a new variable but Python edits the root variable. Why?
> > > 
> > > import numpy as np
> > > 
> > > X=np.arange(1, 1, 1) #root variable x1=X x1[x1<1]=0
> > > 
> > > print(X)
> > > Out[1]: array([ 0.,  0.,  0., ...,  0.,  0.,  0.])
> > > 
> > > Why? It is supposed to be the original value Thank you for your
> > > time Rafael
> > 
> > 
> > 
> > -- 
> > MarkA
> > 
> > We hang petty theives, and appoint the great theives to public office
> >   -- Aesop
> 
> Shouldn't the OP just create a list for what he want's to do?
> 
> X = list(np.arange(1, 1, 1)) #root variable x1=X x1[x1<1]=0
> 
> Then I think his other statements would do what he expects, no?

Disregard what I just posted. I didn't think this through enough. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a Python module to parse a date like the 'date' command in Linux?

2023-05-22 Thread Tim Williams
On Mon, May 22, 2023 at 12:41 PM Mats Wichmann  wrote:

> On 5/20/23 13:53, Chris Green wrote:
> > I'm converting a bash script to python as it has become rather clumsy
> > in bash.
> >
> > However I have hit a problem with converting dates, the bash script
> > has:-
> >
> >  dat=$(date --date "$1" +"%Y/%m/%d")
> >
> > and this will accept almost anything reasonably sensible that can be
> > interpreted as a date, in particular it accepts things like "tomorrow",
> > "yesterday" and "next thursday".
> >
> > Is there anything similar in Python or would I be better off simply
> > using os.system() to run date from the python program?
> >
>
> in the standard library, datetime
>
> as an addon module, dateutil  (install as python-dateutil)
>
> Don't know if either are exactly what you want, but do take a look.
>
> --
> https://mail.python.org/mailman/listinfo/python-list


In particular,check out dateutil.parser.
parser — dateutil 2.8.2 documentation


parser


This module offers a generic date/time string parser which is able to parse
most known formats to represent a date and/or time.

This module attempts to be forgiving with regards to unlikely input
formats, returning a datetime object even for dates which are ambiguous. If
an element of a date/time stamp is omitted, the following rules are applied:
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to store the result of df.count() as a new dataframe in Pandas?

2021-10-27 Thread Tim Williams
On Tue, Oct 26, 2021 at 6:36 PM Shaozhong SHI 
wrote:

> Hello,
>
> The result of df.count() appears to be a series object.  How to store the
> result of df.count() as a new dataframe in Pandas?
>
> That is data anyhow.
>
> Regards,
>
> David
> --
> https://mail.python.org/mailman/listinfo/python-list




Have you tried something like

df_count = pd.DataFrame(df.count())
?
(Untested, but I've converted Series objects to DataFrames doing something
similar before.)
This is more of a pandas question. Why don't you ask this on stackoverflow?

https://stackoverflow.com/questions/tagged/pandas
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a nice way to switch between 2 different packages providing the same APIs?

2018-07-05 Thread Tim Williams
On Thu, Jul 5, 2018 at 9:02 AM Mark Summerfield via Python-list <
[email protected]> wrote:

> For GUI programming I often use Python bindings for Qt.
>
> There are two competing bindings, PySide and PyQt.
>
> Ideally I like to have applications that can use either. This way, if I
> get a problem I can try with the other bindings: if I still get the
> problem, then it is probably me; but if I don't it may be an issue with the
> bindings.
>
> But working with both means that my imports are very messy. Here's a tiny
> example:
>
> if PYSIDE: # bool True -> Use PySide; False -> Use PyQt5
> from PySide2.QtCore import Qt
> from PySide2.QtGui import QIcon
> from PySide2.QtWidgets import (
> QDialog, QFrame, QGridLayout, QLabel, QLineEdit, QPushButton,
> QVBoxLayout)
> else:
> from PyQt5.QtCore import Qt
> from PyQt5.QtGui import QIcon
> from PyQt5.QtWidgets import (
> QDialog, QFrame, QGridLayout, QLabel, QLineEdit, QPushButton,
> QVBoxLayout)
>
> The PYSIDE constant is imported from another module and is used for all
> .py files in a given project so that just by changing PYSIDE's value I can
> run an entire application with PySide2 or with PyQt5.
>
> But I'd really rather just have one lot of imports per file.
>
> One obvious solution is to create a 'Qt.py' module that imports everything
> depending on the PYSIDE switch and that I then use in all the other .py
> files, something like this:
>
> from Qt.QtCore import Qt
> from Qt.QtGui import QIcon
> ... etc.
>
> But I'm just wondering if there's a nicer way to do all this?
> --
> https://mail.python.org/mailman/listinfo/python-list


Check out pyqtgraph 

It tries to use  PyQt5/PyQt4/PySide depending on which if these packages
were imported before importing pyqtgraph.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: conda/anaconda and pip3 (pip)

2018-12-09 Thread Tim Williams
On Saturday, December 8, 2018 at 10:13:14 PM UTC-5, Monte Milanuk wrote:
> Did you find any solution(s)?

I usually just lurk and read on this list. I don't reply since there's usually 
more competent people that regularly post helpful answers. (I lurk to learn 
from them!)

If no one's replied yet, I'll give it my 2 cents ...

Without being a pip expert, I see from 'pip install -h' that you can specify 
where you want the package to be installed.

Install Options:
  -r, --requirement Install from the given requirements file. This 
option can be used multiple times.
  -c, --constraint  Constrain versions using the given constraints 
file. This option can be used multiple
  times.
  --no-deps   Don't install package dependencies.
  --pre   Include pre-release and development versions. By 
default, pip only finds stable
  versions.
  -e, --editableInstall a project in editable mode (i.e. 
setuptools "develop mode") from a local project
  path or a VCS url.
  -t, --target   Install packages into . By default this will 
not replace existing files/folders in
  . Use --upgrade to replace existing packages 
in  with new versions.
  --user  Install to the Python user install directory for 
your platform. Typically ~/.local/, or
  %APPDATA%\Python on Windows. (See the Python 
documentation for site.USER_BASE for full
  details.)
  --root Install everything relative to this alternate 
root directory.
  --prefix   Installation prefix where lib, bin and other 
top-level folders are placed
  -b, --buildDirectory to unpack packages into and build in. 
Note that an initial build still takes
  place in a temporary directory. The location of 
temporary directories can be controlled
  by setting the TMPDIR environment variable (TEMP 
on Windows) appropriately. When passed,
  build directories are not cleaned in case of 
failures.
  --src  Directory to check out editable projects into. 
The default in a virtualenv is "/src". The default for global installs is 
"/src".
  -U, --upgrade   Upgrade all specified packages to the newest 
available version. The handling of
  dependencies depends on the upgrade-strategy used.
  --upgrade-strategy 
  Determines how dependency upgrading should be 
handled [default: only-if-needed]. "eager"
  - dependencies are upgraded regardless of whether 
the currently installed version
  satisfies the requirements of the upgraded 
package(s). "only-if-needed" -  are upgraded
  only when they do not satisfy the requirements of 
the upgraded package(s).
  --force-reinstall   Reinstall all packages even if they are already 
up-to-date.
  -I, --ignore-installed  Ignore the installed packages (reinstalling 
instead).
  --ignore-requires-pythonIgnore the Requires-Python information.
  --no-build-isolationDisable isolation when building a modern source 
distribution. Build dependencies
  specified by PEP 518 must be already installed if 
this option is used.
  --install-option   Extra arguments to be supplied to the setup.py 
install command (use like --install-
  option="--install-scripts=/usr/local/bin"). Use 
multiple --install-option options to
  pass multiple options to setup.py install. If you 
are using an option with a directory
  path, be sure to use absolute path.
  --global-optionExtra global options to be supplied to the 
setup.py call before the install command.
  --compile   Compile Python source files to bytecode
  --no-compileDo not compile Python source files to bytecode
  --no-warn-script-location   Do not warn when installing scripts outside PATH
  --no-warn-conflicts Do not warn about broken dependencies
  --no-binary 
  Do not use binary packages. Can be supplied 
multiple times, and each time adds to the
  existing value. Accepts either :all: to disable 
all binary packages, :none: to empty the
  set, or one or more package names with commas 
between them. Note that some packages are
  tricky to compile and may fail to install when 
this option is used on them.
  --only-binary 
  Do not use source packages. Can be supplied 
multiple times, and each time adds to the
  existing value. Accepts either :all: to disable 
a

Re: Python Pandas split Date in day month year and hour

2020-08-19 Thread Tim Williams
On Wed, Aug 19, 2020 at 1:43 PM J Conrado  wrote:

>
>
> Hi,
>
>
> I'm satarting using Pandas to read excel. I have a meteorological
> synoptic data and I have for date:
>
>
> 0   2017-11-01 00:00:00
> 1   2017-11-01 03:00:00
> 2   2017-11-01 06:00:00
> 3   2017-11-01 09:00:00
> 4   2017-11-01 12:00:00
> ..  ...
> 229 2017-11-30 09:00:00
> 230 2017-11-30 12:00:00
> 231 2017-11-30 15:00:00
> 232 2017-11-30 18:00:00
> 233 2017-11-30 21:00:00
>
>
> I would like know how can I get for this array the values for day, month
> and hour:
>
> 2017-11-01 03:00:00   year = 2017  month = 11day = 1and
> hour = 3
>
>
>
> Thanks,
>
>
> Conrado
>
> --
> https://mail.python.org/mailman/listinfo/python-list


I'm also starting to learn pandas. A better place to ask a pandas question
is StackOverflow. Here's a link that may answer your question.

Convert timestamp to day, month, year and hour
<https://stackoverflow.com/questions/57515291/convert-timestamp-to-day-month-year-and-hour>


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


Re: ValueError: arrays must all be same length

2020-10-04 Thread Tim Williams
On Fri, Oct 2, 2020 at 11:00 AM Shaozhong SHI 
wrote:

> Hello,
>
> I got a json response from an API and tried to use pandas to put data into
> a dataframe.
>
> However, I kept getting this ValueError: arrays must all be same length.
>
> Can anyone help?
>
> The following is the json text.  Regards, Shao
>
> (snip json_text)


> import pandas as pd
>
> import json
>
> j = json.JSONDecoder().decode(req.text)  ###req.json
>
> df = pd.DataFrame.from_dict(j)
>

I copied json_text into a Jupyter notebook and got the same error trying to
convert this into a pandas DataFrame:When I tried to copy this into a
string, I got an error,, but without enclosing the paste in quotes, I got
the dictionary.

dir(json_text)
['__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__setattr__',
 '__setitem__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'clear',
 'copy',
 'fromkeys',
 'get',
 'items',
 'keys',
 'pop',
 'popitem',
 'setdefault',
 'update',
 'values']

pd.DataFrame(json_text)

---

ValueErrorTraceback (most recent call last)
 in 
> 1 pd.DataFrame(json_text)

D:\anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data,
index, columns, dtype, copy)
433 )
434 elif isinstance(data, dict):
--> 435 mgr = init_dict(data, index, columns, dtype=dtype)
436 elif isinstance(data, ma.MaskedArray):
437 import numpy.ma.mrecords as mrecords

D:\anaconda3\lib\site-packages\pandas\core\internals\construction.py in
init_dict(data, index, columns, dtype)
252 arr if not is_datetime64tz_dtype(arr) else arr.copy()
for arr in arrays
253 ]
--> 254 return arrays_to_mgr(arrays, data_names, index, columns,
dtype=dtype)
255
256

D:\anaconda3\lib\site-packages\pandas\core\internals\construction.py in
arrays_to_mgr(arrays, arr_names, index, columns, dtype)
 62 # figure out the index, if necessary
 63 if index is None:
---> 64 index = extract_index(arrays)
 65 else:
 66 index = ensure_index(index)

D:\anaconda3\lib\site-packages\pandas\core\internals\construction.py in
extract_index(data)
363 lengths = list(set(raw_lengths))
364 if len(lengths) > 1:
--> 365 raise ValueError("arrays must all be same length")
366
367 if have_dicts:

ValueError: arrays must all be same length


I got a different error trying json.loads(str(json_text)),
---
JSONDecodeError   Traceback (most recent call last)
 in 
> 1 json.loads(str(json_text))

D:\anaconda3\lib\json\__init__.py in loads(s, cls, object_hook,
parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
355 parse_int is None and parse_float is None and
356 parse_constant is None and object_pairs_hook is None
and not kw):
--> 357 return _default_decoder.decode(s)
358 if cls is None:
359 cls = JSONDecoder

D:\anaconda3\lib\json\decoder.py in decode(self, s, _w)
335
336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
338 end = _w(s, end).end()
339 if end != len(s):

D:\anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
351 """
352 try:
--> 353 obj, end = self.scan_once(s, idx)
354 except StopIteration as err:
355 raise JSONDecodeError("Expecting value", s, err.value)
from None

JSONDecodeError: Expecting property name enclosed in double quotes: line 1
column 2 (char 1)

I think the solution is to fix the arrays so that the lengths match.

for k in json_text.keys():
if isinstance(json_text[k], list):
print(k, len(json_text[k]))

relationships 0
locationTypes 0
regulatedActivities 2
gacServiceTypes 1
inspectionCategories 1
specialisms 4
inspectionAreas 0
historicRatings 4
reports 5

HTH,.

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


Re: ValueError: arrays must all be same length

2020-10-04 Thread Tim Williams
On Sun, Oct 4, 2020 at 8:39 AM Tim Williams  wrote:

>
>
> On Fri, Oct 2, 2020 at 11:00 AM Shaozhong SHI 
> wrote:
>
>> Hello,
>>
>> I got a json response from an API and tried to use pandas to put data into
>> a dataframe.
>>
>> However, I kept getting this ValueError: arrays must all be same length.
>>
>> Can anyone help?
>>
>> The following is the json text.  Regards, Shao
>>
>> (snip json_text)
>
>
>> import pandas as pd
>>
>> import json
>>
>> j = json.JSONDecoder().decode(req.text)  ###req.json
>>
>> df = pd.DataFrame.from_dict(j)
>>
>
> I copied json_text into a Jupyter notebook and got the same error trying
> to convert this into a pandas DataFrame:When I tried to copy this into a
> string, I got an error,, but without enclosing the paste in quotes, I got
> the dictionary.
>
>
(delete long response output)


> for k in json_text.keys():
> if isinstance(json_text[k], list):
> print(k, len(json_text[k]))
>
> relationships 0
> locationTypes 0
> regulatedActivities 2
> gacServiceTypes 1
> inspectionCategories 1
> specialisms 4
> inspectionAreas 0
> historicRatings 4
> reports 5
>
> HTH,.
>
>
This may also be more of a pandas issue.

json.loads(json.dumps(json_text))

has a successful round-trip


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


Re: ValueError: arrays must all be same length

2020-10-05 Thread Tim Williams
On Mon, Oct 5, 2020 at 6:47 AM Shaozhong SHI  wrote:

>
> Hi, I managed to flatten it with json_normalize first.
>
> from pandas.io.json import json_normalize
> atable = json_normalize(d)
> atable
>
> Then, I got this table.
>
> brandId brandName careHome constituency
> currentRatings.overall.keyQuestionRatings currentRatings.overall.rating
> currentRatings.overall.reportDate currentRatings.overall.reportLinkId
> currentRatings.reportDate dormancy ... providerId region registrationDate
> registrationStatus regulatedActivities relationships reports specialisms
> type uprn
> 0 BD510 BRAND MACC Care Y Birmingham, Northfield [{u'reportDate':
> u'2020-10-01', u'rating': u'R... Requires improvement 2020-10-01
> 1157c975-c2f1-423e-a2b4-66901779e014 2020-10-01 N ... 1-101641521 West
> Midlands 2013-12-16 Registered [{u'code': u'RA2', u'name':
> u'Accommodation
>
> Then, I tried to expand the column
> of currentRatings.overall.keyQuestionRatings, with
>
> mydf =
> pd.DataFrame.from_dict(atable['currentRatings.overall.keyQuestionRatings'][0])
> mydf
>
> Then, I got another table.
>
> name rating reportDate reportLinkId
> 0 Safe Requires improvement 2020-10-01
> 1157c975-c2f1-423e-a2b4-66901779e014
> 1 Well-led Requires improvement 2020-10-01
> 1157c975-c2f1-423e-a2b4-66901779e014
> 2 Caring Good 2019-10-04 63ff05ec-4d31-406e-83de-49a271cfdc43
> 3 Responsive Good 2019-10-04 63ff05ec-4d31-406e-83de-49a271cfdc43
> 4 Effective Requires improvement 2019-10-04
> 63ff05ec-4d31-406e-83de-49a271cfdc43
>
>
> How can I re-arrange to get a flatten table?
>
> Apparently, the nested data is another table.
>
> Regards,
>
> Shao
>
>
> I'm fairly new to pandas myself. Can't help there. You may want to post
this on Stackoverflow, or look for a similar issue on github.

https://stackoverflow.com/questions/tagged/pandas+json
https://github.com/pandas-dev/pandas/issues




>
> On Sun, 4 Oct 2020 at 13:55, Tim Williams  wrote:
>
>> On Sun, Oct 4, 2020 at 8:39 AM Tim Williams  wrote:
>>
>> >
>> >
>> > On Fri, Oct 2, 2020 at 11:00 AM Shaozhong SHI 
>> > wrote:
>> >
>> >> Hello,
>> >>
>> >> I got a json response from an API and tried to use pandas to put data
>> into
>> >> a dataframe.
>> >>
>> >> However, I kept getting this ValueError: arrays must all be same
>> length.
>> >>
>> >> Can anyone help?
>> >>
>> >> The following is the json text.  Regards, Shao
>> >>
>> >> (snip json_text)
>> >
>> >
>> >> import pandas as pd
>> >>
>> >> import json
>> >>
>> >> j = json.JSONDecoder().decode(req.text)  ###req.json
>> >>
>> >> df = pd.DataFrame.from_dict(j)
>> >>
>> >
>> > I copied json_text into a Jupyter notebook and got the same error trying
>> > to convert this into a pandas DataFrame:When I tried to copy this into a
>> > string, I got an error,, but without enclosing the paste in quotes, I
>> got
>> > the dictionary.
>> >
>> >
>> (delete long response output)
>>
>>
>> > for k in json_text.keys():
>> > if isinstance(json_text[k], list):
>> > print(k, len(json_text[k]))
>> >
>> > relationships 0
>> > locationTypes 0
>> > regulatedActivities 2
>> > gacServiceTypes 1
>> > inspectionCategories 1
>> > specialisms 4
>> > inspectionAreas 0
>> > historicRatings 4
>> > reports 5
>> >
>> > HTH,.
>> >
>> >
>> This may also be more of a pandas issue.
>>
>> json.loads(json.dumps(json_text))
>>
>> has a successful round-trip
>>
>>
>> > --
>> >> https://mail.python.org/mailman/listinfo/python-list
>> >>
>> >
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Sendmail with many attach and recipients

2005-02-03 Thread Tim Williams
<[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
Here, my code of sendmail
If Somebody had a doubt please contact me?
[EMAIL PROTECTED]

[snip]

If you have multiple recipients,  you need to modify

server.sendmail(msg['From'],toaddrs,msg.as_string())

to

failed = server.sendmail(msg['From'],toaddrs,msg.as_string())
# exception is only raised if ALL recips fail
# failed is a list of failed recips, or empty

You could also expand your smtplib exception handling
[not tested]

try:
server = smtplib.SMTP(servidor_smtp)
## Faz o envio do email.
try:
failed = server.sendmail(msg['From'],toaddrs,msg.as_string())
if failed:
do something
## Fecha a conexão com o servidor
except smtplib.SMTPRecipientsRefused, x :  #all recips failed before
data sent = MSG failed
do something
except smtplip.SMTPDataError, x: # an error at the end of the
message body receipt =  MSG Failed
do something
except smtplib.SMTPSenderRefused, x : # the sender was refused = MSG
failed
do something
except: #can't connect
do something
finally
server.quit()
except  Exception, e:
arq=open(dir_log,'a')
print >> arq, "Falha no envio da mensagem de email, não foi

-- 

=
Tim Williams

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


Re: login website that using PHP

2005-06-20 Thread Tim Williams

- Original Message - 
From: "frost" <[EMAIL PROTECTED]>


> Hi,
>
> I am trying to login a website that using PHP and javascript. This is
> what happend if you browse that website using IE, after you login, you
> can go anywhere without enter your name and password again, as long as
> you keep that IE open, but after you close that IE, then later on open
> that website in a new window, you need to login again. I guess some
> session is created as long as your original login window dose not
> close.
>
> How I can handle this in python? I want get some information from that
> website. But the login page is not what I want, how can I simulate
> regular IE browser? I mean after I login, I can get to other pages from
> it, but I find my programe can't go other place except the login page.


Depending on your requirements,  "ishy_browser"  might help,  I found it the
very quick and easy to get running.   It controls an IE window on your
desktop and gives you access to each page's "objects".   You need
winGuiAuto to get it to work, but that's trivial.

You can either import the script as a module,  or wirte your own _main_
based on the one in the script.

I heard about it on this list.

HTH :)


-- Forwarded message --
From: [EMAIL PROTECTED] <[EMAIL PROTECTED]>
Date: 26 May 2005 07:18:09 -0700
Subject: Re: Automatically populate and submit HTML Forms
To: [email protected]


These two blog entries might be of help to you.

http://www.ishpeck.net/index.php?P=b1115239318ishpeck
second half is at
http://www.ishpeck.net/index.php?P=b1115225809ishpeck

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


Re: reading a list from a file

2005-06-20 Thread Tim Williams

- Original Message - 
From: "David Bear" <[EMAIL PROTECTED]>


> I have a file that contains lists -- python lists. sadly, these are not
> pickled. These are lists that were made using a simple print list
> statement.
> 
> Is there an easy way to read this file into a list again?  I'm thinking I
> would have to 
> 
> 
> I was hoping there was a pythonic way of doing this..
> 

a = '[1,2,3,4,5]'
>>> b = eval(a)
>>> b
[1, 2, 3, 4, 5]
>>> b[2]
3

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: smtplib and TLS

2005-06-21 Thread Tim Williams
- Original Message - 
From: "Matthias Kluwe" <[EMAIL PROTECTED]>

> > Have you verified that its your end that is broken,  not gmail's,  do
other
> > servers give the same response ?
>
> No, I have not -- I should have, as I know now: Connecting, starttls,
> login and sending mail works fine without the above mentioned error
> using my previous mail provider.
>
> Does that mean Gmail is in error here? I don't know...


Looks like it is GMAIL ,  (though TLS is not required to be able to send via
smtp.gmail.com:587 )

TLS using TLSlite also fails when connecting to GMAIL, but not to other
servers.

('5 send:', '(16:39:23) ehlo x\r\n')
('6 reply:', '(16:39:23) 250-mx.gmail.com at your service\r\n')
('6 reply:', '(16:39:23) 250-SIZE 20971520\r\n')
('6 reply:', '(16:39:23) 250-8BITMIME\r\n')
('6 reply:', '(16:39:23) 250-STARTTLS\r\n')
('6 reply:', '(16:39:23) 250 ENHANCEDSTATUSCODES\r\n')
('5 send:', '(16:39:23) STARTTLS\r\n')
('6 reply:', '(16:39:23) 220 2.0.0 Ready to start TLS\r\n')
('Status:', '(16:39:24) 2202.0.0 Ready to start TLS')
('5 send:', '(16:39:24) ehlo x\r\n')
('6 reply:', '(16:39:24) 250-mx.gmail.com at your service\r\n')
('6 reply:', '(16:39:24) 250-SIZE 20971520\r\n')
('6 reply:', '(16:39:24) 250-8BITMIME\r\n')
('6 reply:', '(16:39:24) 250-AUTH LOGIN PLAIN\r\n')
('6 reply:', '(16:39:24) 250 ENHANCEDSTATUSCODES\r\n')
('5 send:', '(16:39:24) noop\r\n')
('6 reply:', '(16:39:24) 250 2.0.0 OK\r\n')
('5 send:', '(16:39:24) rset\r\n')
('6 reply:', '(16:39:24) 250 2.1.0 Flushed d61sm2700367wra\r\n')
('5 send:', '(16:39:24) noop\r\n')
('6 reply:', '(16:39:24) 250 2.0.0 OK\r\n')
('5 send:', '(16:39:24) quit\r\n')
Traceback (most recent call last):
  File "C:\test\tls.py", line 103, in ?
s.quit()
  File "C:\test\smtplib.py", line 737, in quit
self.docmd("quit")
  File "C:\test\smtplib.py", line 395, in docmd
return self.getreply()
  File "C:\test\smtplib.py", line 367, in getreply
line = self.file.readline()
  File "C:\Python23\Lib\site-packages\tlslite\FileObject.py", line 152, in
readline
data = self._sock.recv(self._rbufsize)
  File "C:\Python23\Lib\site-packages\tlslite\TLSRecordLayer.py", line 393,
in recv
return self.read(bufsize)
  File "C:\Python23\Lib\site-packages\tlslite\TLSRecordLayer.py", line 182,
in read
for result in self.readAsync(max, min):
  File "C:\Python23\Lib\site-packages\tlslite\TLSRecordLayer.py", line 201,
in readAsync
for result in self._getMsg(ContentType.application_data):
  File "C:\Python23\Lib\site-packages\tlslite\TLSRecordLayer.py", line 564,
in _getMsg
for result in self._getNextRecord():
  File "C:\Python23\Lib\site-packages\tlslite\TLSRecordLayer.py", line 737,
in _getNextRecord
raise TLSAbruptCloseError()
tlslite.errors.TLSAbruptCloseError


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


Re: need to strip stuff off email

2005-06-22 Thread Tim Williams


> "nephish" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > hey there,
> > i have a script that retrieves my email, but i need it to
> > be able to strip all the stuff off except the body (the message itself)
> > so i can later write it to a text file.
> >
> > anyone know how to accomplish this?
> > thanks

The body is:   The rest of the email after "the first blank line after the
subject header".   In practice it is the first blank line.

If you get the message into a string it can sometimes be easier to just
RSPLIT the string at '\r\n\r\n',  if the message is in a list then the body
= '\r\n'.join(msg[x:]) where x = that blank line +1  ,  that way if you
don't need any of the header info,  you don't have to decode the message and
rebuild it in a file.

if you *are* using the email module, eg

msg = email.message_from_file(a_file)

then rsplit the msg to get the same result.

As someone will no doubt point out,   some emails are broken and parts of
the headers will end up in the body (even when you view the email it in a
client),  this is very rare though.




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


Re: need to strip stuff off email

2005-06-22 Thread Tim Williams
- Original Message - 
From: "Tim Williams" <[EMAIL PROTECTED]>
To: 
Sent: Wednesday, June 22, 2005 10:48 AM
Subject: Re: need to strip stuff off email


>
>
> > "nephish" <[EMAIL PROTECTED]> wrote in message
> > news:[EMAIL PROTECTED]
> > > hey there,
> > > i have a script that retrieves my email, but i need it to
> > > be able to strip all the stuff off except the body (the message
itself)
> > > so i can later write it to a text file.
> > >
> > > anyone know how to accomplish this?
> > > thanks
>
> The body is:   The rest of the email after "the first blank line after the
> subject header".   In practice it is the first blank line.
>
> If you get the message into a string it can sometimes be easier to just
> RSPLIT the string at '\r\n\r\n',  if the message is in a list then the
body
> = '\r\n'.join(msg[x:]) where x = that blank line +1  ,  that way if you
> don't need any of the header info,  you don't have to decode the message
and
> rebuild it in a file.
>
> if you *are* using the email module, eg
>
> msg = email.message_from_file(a_file)
>
> then rsplit the msg to get the same result.
>
> As someone will no doubt point out,   some emails are broken and parts of
> the headers will end up in the body (even when you view the email it in a
> client),  this is very rare though.

Ah,  trying to do anything before my first cup of coffee in the morning is
always a mistake - substitute RSPLIT with LSPLIT  for both occurrences above
!!!  :(

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


Re: Getting/Saving email attachments w/ poplib and email modules

2005-06-22 Thread Tim Williams

- Original Message - 
From: <[EMAIL PROTECTED]>
> 
> Here's what I'm trying to do:
> 
> I need to connect to a pop3 server, download all messages, and copy all
> of the attachments into a specific directory.  The actual email message

##
import email
import poplib

mimes = ["image/tif","image/tiff","images/x-tif","image/x-tiff",
   "application/tif","application/tiff","application/x-tif",
   "application/x-tiff"]

def WriteAttachment(msg):
for part in msg.walk():
if part.get_type() in mimes:
name = part.get_filename()
data = part.get_payload(decode=True)
f = file(name,'wb')
f.write(data)
f.close()

ms = poplib.POP3(server)
ms.user(user)
ms.pass_(passw)

msgcount = len(ms.list()[1])
for i in range(msgcount):
response, msg_as_list, size = ms.retr(i+1)
msg = email.message_from_string('\r\n'.join(msg_as_list))
WriteAttachment(msg)

##
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Loop until condition is true

2005-06-22 Thread Tim Williams
- Original Message - 
From: "Greg Lindstrom" <[EMAIL PROTECTED]>


> A bit off topic, but what does the expression "Don't try to teach your
> grandfather how to suck eggs." mean?  I've never heard it before and am
> curious to the story behind it.

A relatively well know phrase, however  as quoted it is incorrect,  it
should have referred to "grandmother"

http://www.phrases.org.uk/bulletin_board/18/messages/50.html
http://www.worldwidewords.org/qa/qa-tea1.htm

:)

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


Re: Optimize a cache

2005-06-23 Thread Tim Williams
- Original Message - 
From: "Florian Lindner" <[EMAIL PROTECTED]>


> Hello,
> I am building a object cache in python, The cache has a maximum size and
the
> items have expiration dates.
> At the moment I'm doing like that:

> What possible you see to optimize this lookup? Or anything else you see to
> make it better?

Have a look at http://py.vaults.ca/apyllo.py/514463245.769244789.92554878
it already has some of your requirements,  and can be used as a building
block.


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


Re: Newbie MySQLdb / MySQL version problem, I think

2004-11-29 Thread Tim Williams
"Dave Merrill" <[EMAIL PROTECTED]> wrote in message news:<[EMAIL PROTECTED]>...
> Sorry for the newbness... Win2K, Python 2.3.3, MySQL 4.1.7. Downloaded and
> extracted MySQL-python-1.0.0.win32-py2.3.zip. Put the whole extracted
> directory into C:\Program Files\Python23\Lib\site-packages\ and renamed it
> to "MySQLdb.
> 
> MySQLdb.__version__ returns '1.0.0', so I think it's installed ok.
> 
> However, I still can't connect, and I just want to confirm my guess on what
> the problem is, which is that MySQLdb doesn't yet support MySQL 4.1. I'd
> hoped that all that meant was that it wouldn't support any of 4.1's new
> features, but it appears this combination may be completely non-functional.
> Test and results below.
> 
> Has anyone else tried this?
> 
> Thanks,
> 
> Dave Merrill
> 
> 
> TEST CODE (from http://www.kitebird.com/articles/pydbapi.html):
> --
> import MySQLdb
> conn = MySQLdb.connect (host = "localhost",
>user = "test_user",
>passwd = "secret",
>db = "test_db")
> cursor = conn.cursor ()
> cursor.execute ("SELECT VERSION()")
> row = cursor.fetchone ()
> print "server version:", row[0]
> cursor.close ()
> conn.close ()
> --
> 
> RESULT:
> --
> Traceback (most recent call last):
>   File "C:\PROGRA~1\PYTHON23\Lib\site-packages\sm\scriptutils.py", line 49,
> in run
> exec codeObject in mainDict
>   File "", line 9, in ?
>   File "C:\PROGRA~1\PYTHON23\lib\site-packages\MySQLdb\__init__.py", line
> 64, in Connect
> return apply(Connection, args, kwargs)
>   File "C:\PROGRA~1\PYTHON23\lib\site-packages\MySQLdb\connections.py", line
> 116, in __init__
> self._make_connection(args, kwargs2)
>   File "C:\PROGRA~1\PYTHON23\lib\site-packages\MySQLdb\connections.py", line
> 41, in _make_connection
> apply(super(ConnectionBase, self).__init__, args, kwargs)
> OperationalError: (1251, 'Client does not support authentication protocol
> requested by server; consider upgrading MySQL client')
> --


I had a similar problem and found this thread:

http://groups.google.com/groups?hl=en&lr=&selm=mailman.5325.1098460353.5135.python-list%40python.org

I changed the user's password using OLD_PASSWORD() and now it works
for me.

HTH.
-- 
http://mail.python.org/mailman/listinfo/python-list


smtplib STARTTLS problems

2005-04-12 Thread Tim Williams
I have a working SMTP client that I need to add TLS capability to,I
absolutely need the client to timeout within a specified time,  but when I
use the sock.timeout() line it freezes the reading of chars from SSLFakeFile
used during TLS  instead of timing out the client connection.I am
working from my own hacked version of smtplib,  but can demonstrate the
problem using the standard module (Python 2.3.4).   Can anyone suggest a
workaround hack that I can impliment in my own version of smtplib ??

Thanks in advance

Working version =

import smtplib as mx
s=mx.SMTP('e32.co.us.ibm.com')
s.set_debuglevel(1)
s.ehlo('x')
resp, null =  s.starttls()
if resp == 220:
s.ehlo('x')
s.mail('[EMAIL PROTECTED]')
s.quit()



Non-Working version =

import smtplib as mx
s=mx.SMTP('e32.co.us.ibm.com')
s.sock.settimeout(20)
s.set_debuglevel(1)
s.ehlo('x')
resp, null =  s.starttls()
if resp == 220:
s.ehlo('x')
s.mail('[EMAIL PROTECTED]')
s.quit()

Traceback (most recent call last):
  File "", line 1, in ?
  File "tls2.py", line 8, in ?
print s.ehlo('x')
  File "C:\Python23\lib\smtplib.py", line 404, in ehlo
(code,msg)=self.getreply()
  File "C:\Python23\lib\smtplib.py", line 356, in getreply
line = self.file.readline()
  File "C:\Python23\lib\smtplib.py", line 160, in readline
chr = self.sslobj.read(1)
sslerror: The read operation timed out



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


Re: text representation of HTML

2006-07-20 Thread Tim Williams
On 20 Jul 2006 15:12:27 GMT, Duncan Booth <[EMAIL PROTECTED]> wrote:
> Ksenia Marasanova wrote:
> > i want to send plain text alternative of html email, and would prefer
> > to do it automatically from HTML source.
> > Any hints?
>
> Use htmllib:
>
> >>> import htmllib, formatter, StringIO
> >>> def cleanup(s):
> out = StringIO.StringIO()
> p = htmllib.HTMLParser(
> formatter.AbstractFormatter(formatter.DumbWriter(out)))
> p.feed(s)
> p.close()
> if p.anchorlist:
> print >>out
> for idx,anchor in enumerate(p.anchorlist):
> print >>out, "\n[%d]: %s" % (idx+1,anchor)
> return out.getvalue()
>
> >>> print cleanup('''TitleThis is a  />test''')
>
> Title
>
> This is a
> test
> >>> print cleanup('''TitleThis is a test with  href="http://python.org";>a link to the Python homepage''')
>
> Title
>
> This is a
> test with a link[1] to the Python homepage
>
> [1]: http://python.org
>

cleanup()  doesn't handle script and styles too well.  html2text will
do a much better job of these and give a more structured output
(compatible with Markdown)

http://www.aaronsw.com/2002/html2text/

>>> import html2text
>>> print html2text.html2text('''TitleThis is a test with http://python.org";>a link to the Python
homepage''')

# Title

This is a
test with [a link][1] to the Python homepage

[1]: http://python.org


HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: format a number for output

2006-08-07 Thread Tim Williams
On 7 Aug 2006 07:55:11 -0700, abcd <[EMAIL PROTECTED]> wrote:
> if i have a number, say the size of a file, is there an easy way to
> output it so that it includes commas?
>
> for example:
>
> 1890284
>
> would be:
>
> 1,890,284
>

I was bored !!

>>> a = 1890284
>>> ','.join([str(a)[::-1][x:x+3] for x in range(len(str(a)))[::3]])[::-1]
'1,890,284'

Ugly !

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting previous file name

2006-08-07 Thread Tim Williams
On 7 Aug 2006 13:52:16 -0700, Hitesh <[EMAIL PROTECTED]> wrote:
>
> I have a small script here that goes to inside dir and sorts the file
> by create date. I can return the create date but I don't know how to
> find the name of that file...
> I need file that is not latest but was created before the last file.

I notice that your path is UNC & windows,   if you are running this on
a windows platform only you could let the windows DIR do the work for
you.

>>> import os
>>> i,o,e = os.popen3('dir /O-D /A-D /B')
>>> o.readlines()[1].strip()
'dnscheck.html'

I would put a try:except around the o.readlines()   bit in case
there is only 0 or 1 file in the directory.

Hints:

c:\> dir /?  (enter)

>>> i,o,e = os.popen3('dir /O-D /A-D /B)
>>>o.readlines()
['recover.tbl\n', 'dnscheck.html\n', 'v3changes.txt\n',
'V3todo.txt\n', 'fupdates.pyc\n', 'Auth_db.pyc\n']

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: timeout calling local sendmail

2006-08-09 Thread Tim Williams
On 9 Aug 2006 08:22:03 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> that's
>  "timeout calling local sendmail"
> not
> "timeout calling local se"
>
>
> [EMAIL PROTECTED] wrote:
> > (Environment: RedHat Linux recent, Python 2.3.5)
> >
> > We have a batch processing script that on occasion needs to send out an
> > email. We have a sendmail running locally.
> >
> > Sometimes we get a socket timeout on sending that email. Increasing the
> > timeout to 30sec reduced but did not eliminate it.
> >
> > It seems to happen more often when sending to some addresses in the UK,
> > but it's definitely not limited to that.
> >
> > And, while we sometimes send a number of messages in a short period of
> > time, (a) it's all single-threaded, and (b) the failure is often but
> > not always the 1st message in the batch.
> >
> > Any ideas on what's going on? I don't know much about the
> > thread/process model of sendmail to have any clue why this is
> > happening.
>


RFC 1123

http://www.freesoft.org/CIE/RFC/1123/109.htm

I find that a timeout of 120 seconds is the bare minimum.   If the
timeout  is too short you get a condition where the email is received
succesfully but appears to the sending server to have failed and a
retry may occur - so the recipient gets multiple copies.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Read a file with open command

2006-08-11 Thread Tim Williams
On 11 Aug 2006 09:39:23 -0700, jean-jeanot <[EMAIL PROTECTED]> wrote:

> Anyway many thanks.Here is the program:
>
> >>> file_obj= open ("D:/Mes documents/ADB Anna.ods",'r')
> >>> s = file_obj
> >>> s.readlines()

Please remember not to top-post :)

Try this

>>> s = open ("D:/Mes documents/ADB Anna.ods",'r')
>>> s.readlines()
>>> s.close()

or

>>> s = open ("D:/Mes documents/ADB Anna.ods",'r').readlines()

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested if and expected an indent block

2006-08-13 Thread Tim Williams
On 13 Aug 2006 16:28:45 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Greetings:
>
> I'm brand new to Python and decided to write a syllogism solver for a
> class I'm taking. At the start of the program, I define a function that
> classifies the type of each statement in the syllogism. Python tells me
> that it is expecting an indented block at the s in "some". I can see
> what I'm doing wrong. Here's the code:
>
> def class_stmt(q,c):
> """
> This function classifies a statement according to the rules of
> categorical syllogisms and returns A, E, I, O to identify the
> statement type.
> """
> if q.lower() == "all":
> if "not" in c:
> stmt_type = "E"
> else:
> stmt_type = "A"
> elif q.lower() == "some"# s in some is highlighted
> if "not" in c:
> stmt_type = "O"
> else:
> stmt_type = "I"

It might be the missing colon after "some"

If that's a typo on your email rather than in your script you should
post the full traceback of the error.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Beginner Textbook

2006-08-15 Thread Tim Williams
On 15/08/06, M_M <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am looking for a simple text book to introduce 13 to 18 year olds to
> python programming. Suggestion?
>

You might consider "Learn to programme using Python"  by Alan Gauld
as a means to introduce both programming and python at the same time.

http://www.freenetpages.co.uk/hp/alan.gauld/

The cover describes itself as  "A Tutorial for hobbyists,
self-starters and all who want to learn the art of computer
programming"

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding a char inside path string

2006-08-16 Thread Tim Williams
On 16/08/06, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On 16 Aug 2006 09:00:57 -0700, "Hitesh" <[EMAIL PROTECTED]> declaimed
> the following in comp.lang.python:
>
> >
> > Thank you Fredrik. That works for a string.
> > But I am getting list of tuples from DB.
> >
> > rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',),
> > ('\\serverName\C:\FolderName1\FolderName2\example2.exe',),
> > ('\\serverName\C:\FolderName1\FolderName2\example3.exe',),
> > ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)]
> >
> > I tried this:
> > for i in rows:
> > row = str(i)
> > path = row.replace("C:" , "c$")
> > print path
> >
> > I am getting path something like
> >
> > ('\\serverName\c$:\FolderName1\FolderName2\example.exe',)
> >
> > How on the earth I can remove those paranthesis?
> >
> By accessing the contents of the tuple, not the tuple itself
>
> >>> rows = [('\\serverName\C:\FolderName1\FolderName2\example.exe',),
>  ('\\serverName\C:\FolderName1\FolderName2\example2.exe',),
>  ('\\serverName\C:\FolderName1\FolderName2\example3.exe',),
>  ('\\serverName\C:\FolderName1\FolderName2\example4.exe',)]
> >>> rows
> [('\\serverName\\C:\\FolderName1\\FolderName2\\example.exe',),
> ('\\serverName\\C:\\FolderName1\\FolderName2\\example2.exe',),
> ('\\serverName\\C:\\FolderName1\\FolderName2\\example3.exe',),
> ('\\serverName\\C:\\FolderName1\\FolderName2\\example4.exe',)]
> >>> modRows = [itm[0].replace("C:", "C$") for itm in rows]
> >>> modRows
> ['\\serverName\\C$\\FolderName1\\FolderName2\\example.exe',
> '\\serverName\\C$\\FolderName1\\FolderName2\\example2.exe',
> '\\serverName\\C$\\FolderName1\\FolderName2\\example3.exe',
> '\\serverName\\C$\\FolderName1\\FolderName2\\example4.exe']
> >>>

Try

modRows = ['\\'+itm[0].replace(":", "$") for itm in rows]

It will work with any drive letter and makes an allowance for the
first escape character.

>>> modRows
['serverName\\C$\\FolderName1\\FolderName2\\example.exe',
'serverName\\C$\\FolderName1\\FolderName2\\example2.exe',
etc

for r in modRows:
print r

\\serverName\C$\FolderName1\FolderName2\example.exe
\\serverName\C$\FolderName1\FolderName2\example2.exe
..etc


:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding a char inside path string

2006-08-16 Thread Tim Williams
On 16 Aug 2006 10:30:26 -0700, Hitesh <[EMAIL PROTECTED]> wrote:
>
> Thank you all it worked!.
>
> Tim,
>
> > modRows = ['\\'+itm[0].replace(":", "$") for itm in rows]
>
> What are those two forward slashes for?

Hi Hitesh,

 \  is an escape character,  it can give unexpected results depending
on the character following it.

Try this

>>> print "c:\server\test"

then try

>>> print "c:\\server\\test"

If you need \ in a string, you should use a pair of them for safety, see

http://docs.python.org/ref/strings.html

and http://pyfaq.infogami.com/windows-index  (the comments section)

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Anyone have a link handy to an RFC 821 compliant email address regex for Python?

2006-08-16 Thread Tim Williams
On 16 Aug 2006 15:23:06 -0700, fuzzylollipop <[EMAIL PROTECTED]> wrote:
> I want to do email address format validations, without turning to ANTLR
> or pyparsing, anyone know of a regex that is COMPLIANT with RFC 821.
> Most of the ones I have found from google searches are not really as
> robust as I need them to be.
>

RFC 2821 and 2822 specify the format for email addresses.

Contrary to popular belief,  just about all characters are valid and
this makes validating an address format very difficult.  Also,  just
because an email address is in the correct format doesn't mean its a
real or valid address.

http://en.wikipedia.org/wiki/E-mail_address has a short overview

>From experience,  you might be better off not putting too much energy
into this,  and maybe instead just do a basic template validation eg:
some text followed by @ followed by some text which when split by '.'
gives at least 2 parts.  In addition a DNS MX record lookup will tell
you if the domain part is capable of receiving email,  the address
can't be valid (for internet transmission) without MX records.

If you want to take it a step further, you can also query the
server(s) pointed to in the MX records,  but this takes time, and is
not accurate.  Some domains have catchalls,  and others like yahoo
don't refuse an address until after a whole email has been received.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Anyone have a link handy to an RFC 821 compliant email address regex for Python?

2006-08-16 Thread Tim Williams
On 17/08/06, Tim Williams <[EMAIL PROTECTED]> wrote:
> On 16 Aug 2006 15:23:06 -0700, fuzzylollipop <[EMAIL PROTECTED]> wrote:
> > I want to do email address format validations, without turning to ANTLR
> > or pyparsing, anyone know of a regex that is COMPLIANT with RFC 821.
> > Most of the ones I have found from google searches are not really as
> > robust as I need them to be.
> >
>
> RFC 2821 and 2822 specify the format for email addresses.
>

This also makes interesting reading

http://www.regular-expressions.info/email.html

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding a char inside path string

2006-08-17 Thread Tim Williams
On 17/08/06, Sybren Stuvel <[EMAIL PROTECTED]> wrote:
> Dennis Lee Bieber enlightened us with:
> >   What happens when you get a pathname that looks like:
> >
> > \\who\cares\common.exe\program.exe
>
> Is that possible on Windows? At one point, I named a directory
> "www.something.com" and then it wouldn't open, because it was an
> invalid .COM file...

It is possible on XP.   I just created this structure

C:\Documents and Settings\TW\Desktop\test\test.com\test.exe\test
space\data\data.txt

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unclear on argument passing to "sendmail'

2006-08-21 Thread Tim Williams
On 21/08/06, John Draper <[EMAIL PROTECTED]> wrote:
> In "smtplib" module, the "sendmail" method of function is to be  passed
 > host, but it is the Domain name
> for the SMTP Server as gotten from the "dig" command?   IE:   dig -tMX
> would give me the SMTP
> server.   In my code I have:
 >
> try:
> print "Sending message to host: %s" % mailHost
> server=smtplib.SMTP(mailHost)
> server.sendmail(reply_email,email,body)
> server.quit()
> except:
> print "Uunable to send"
>
> Is mailHost like "mail.t-mobile.com" which I get from the MX record for
> a given domain?
> But also,  is the "email" just the mail account,  ie:  the username?
> without the @?
>
> I need to be able to query the mail server?   Also,  I want to be able
> to handle
> the "SMTPRecipientsRefused" exception.   What is the proper syntax for
> handling
> this?
>
> Do I do it like this?
>
> try:
> print "Sending message to host: %s" % mailHost
> server=smtplib.SMTP(mailHost)
> server.sendmail(reply_email,email,body)
> server.quit()
> except SMTPRecipientsRefused:
> print "Recipient refused"
>
> Is that the right syntax?   I have severe problems with not enough
> example code.
>

mailHost is the name of the mail server server you are sending the
email to/via,  for internet mail this will be a server from the
recipient's domain's mx records (or your ISP server).

reply_email is the full email address of the sender, or the email
address you wish to appear as the sender. It does not have to be the
same address as used in the body headers

email is a bad choice for a variable name,  try something like
to_email,  in your case it should contain the full email address of
the recipeint or a list of recipients.  The address(es)  do not have
to be the same address as used in the body headers

server.sendmail returns a list of failed recipient email addresses if
only some failed,   if all the recipients failed you get an exception.

Apologies for the bad formatting below,  its untested but should show
you the structure for managing an SMTP msg send.  You could tidy it up
without too much effort

import sys

for mailHost in MX_records:
try:
print "Sending message to host: %s" % mailHost
server=smtplib.SMTP(mailHost)
failed = server.sendmail(reply_email,email,body)
server.quit()
break
except smtplib.SMTPRecipientsRefused, x :  #all recips failed
for recip in x.recipients:
print recip
server.quit()
break
except smtplib.SMTPDataError, x: # an error at the end of the
 # message body =  MSG Failed
# all recips failed
print x[0], x[1]
server.quit()
break
except smtplib.SMTPSenderRefused, x : # the sender was refused = #MSG failed
# all recips failed
print x[0], x[1]
server.quit()
    break
except: #can't connect so continue to next MX server - don't fail !!!
e_error = str(sys.exc_info()[0])
print e_error
server.quit()
continue

for recip in failed:  # some failed, some didn't
print recip
-- 

Tim Williams
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unclear on argument passing to "sendmail'

2006-08-21 Thread Tim Williams
On 21/08/06, Tim Williams <[EMAIL PROTECTED]> wrote:
> On 21/08/06, John Draper <[EMAIL PROTECTED]> wrote:

Sorry, there's an indentation error here

except smtplib.SMTPRecipientsRefused, x :  #all recips failed
   for recip in x.recipients:
   print recip
   server.quit()
   break

it should be

except smtplib.SMTPRecipientsRefused, x :  #all recips failed
   for recip in x.recipients:
   print recip  # will give the recip and the SMTP error too
   server.quit()
   break
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unclear on argument passing to "sendmail'

2006-08-22 Thread Tim Williams
On 22/08/06, Tim Roberts <[EMAIL PROTECTED]> wrote:
> John Draper <[EMAIL PROTECTED]> wrote:

Hi Tim :)

> Tim William's answer is not exactly correct.  The host you specify in the
> smtplib.SMTP constructor should NOT be the MX record for any of the
> recipients.  You should never have to look up MX records yourself.

actually, I said:
this will be a server from the recipient's domain's mx records (or
your ISP server).  :)

However it really depends on the use-case,   relaying through another
server will give you no control over bad addresses,  you have to wait
for bounces from the recipient's server,  or conversely the ISP server
can give fails 4xx & 5xx for valid addresses.   The ISP may only
accept email addressed from their local domains, and you may be
breaking their TOCs or AUP and get blocked.

On the flip side, some ISPs block outbound port 25 except through
their servers, or transparent proxy port 25,  so Direct MX is
unusable.

Blacklists are hit and miss, even large ISPs can get listed.  If you
are on a "clean" range then it shouldn't matter, you can look up your
IP on various blacklists to see if it is affected.  Also Many fixed IP
addresses don't have valid PTRs so they can fail as often as dynamic
IP addresses which usually do have valid PTRs

 John Draper <[EMAIL PROTECTED]> wrote:

> Hmmm - the problem I have is if I knowingly
> put in a bad recipient,  and try to send to a
> unknown user,  I get all appearances that
> the mail went through.

Yes this will happen if you use a relay server.

> Ok,   so If I already have a MX hostname
> of "mail.myhost.com",  then I would put
> into my "to_email"[email protected] for

Yes,  if you just used username the server wouldn't know which domain
the email was being sent to and therefore how to route it.

> By the way,  I'm sending this mail to a "sms"
> gateway to a cellular provider, so the
> username is their phone number.

If you only ever send to this gateway then you might as well try MX
records, you will have more control,  but you will need to manage
queueing yourself for temporary failures,  or you may decide that if
you get a temporary failure (4xx) to just fire the email off to your
ISP server and let them deal with it.


> But
> (sigh),  these providers don't appear
> to tell me if I put in a bogus phone number.

Unfortunately not all mail servers will fail an invalid address even
if they aren't relaying the email.   But in this case the SMS
"gateway" is probably relaying to a backend system for the SMSc and
will accept any address with a valid domain part.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cannot import a module from a variable

2006-10-17 Thread Tim Williams
On 16/10/06, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote:
> Jia Lu wrote:
> > Hi all:
> >
> > I try to do things below:
>  import sys
>  for i in sys.modules.keys():
> >   import i
> > Traceback (most recent call last):
> >   File "", line 2, in 
> > import i
> > ImportError: No module named i
> >
> > But it seems that import donot know what is i ?
>
> The import statement expects a name (a symbol), not a string.
>

eval( 'import %s' % modname)

and

eval( 'reload(%s)' % modname)

Usual warnings about eval apply,  but in this case it is usable.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help using smtplib to work ..

2006-11-08 Thread Tim Williams
On 8 Nov 2006 15:31:27 -0800, NicolasG <[EMAIL PROTECTED]> wrote:
>
> > 
> >
> > Under outgoing mail smtp server, use smtp.gmail.com. Since it requires SMTP
> > authorization, use your Gmail account as username (e.g. [EMAIL PROTECTED]) 
> > and
> > your Gmail password as password. You can turn TSL on or off.
> >
> > 
> >
> > The email server requires authentication before it can send.
> > Normally this can be accomplished either by calling
> >
> Actually I used alt1.gmail-smtp-in.l.google.com for a server I don't
> need authentication. The problem is that for some mail addresses I can
> send mail's while for other's I can't . I don't know how to explained
> that.

Without authentication, you will only be able to send to the severs
local addresses eg [EMAIL PROTECTED]  [EMAIL PROTECTED]

> > The only alternative is to use an open relay email server that allows
> > you to send without authenticating.  Hope info at least points you in
> > the right direction.

or you can probably use your ISP's outgoing mail server, which usually
won't require authentication.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python to tell what is the IP of my PC .

2006-11-08 Thread Tim Williams
On 8 Nov 2006 15:35:31 -0800, NicolasG <[EMAIL PROTECTED]> wrote:
> How can I use python to get the real IP address of my DSL router (when
> my PC is part of the local home LAN) ?

The router will the PC's default gateway IP address,  if you are on a
windows platform,  you can view it by typing IPCONFIG (enter) from a
command prompt.

A quick google, gave me this,  look at the comments at the end for an
example of finding the Default Gateway IP address using Python

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/162994

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python to tell what is the IP of my PC .

2006-11-08 Thread Tim Williams
On 09/11/06, Nicolas G <[EMAIL PROTECTED]> wrote:
>
>
> On 11/9/06, Tim Williams <[EMAIL PROTECTED]> wrote:
> > On 8 Nov 2006 15:35:31 -0800, NicolasG <[EMAIL PROTECTED]> wrote:
> > > How can I use python to get the real IP address of my DSL router (when
> > > my PC is part of the local home LAN) ?
> >
> > The router will the PC's default gateway IP address,  if you are on a
> > windows platform,  you can view it by typing IPCONFIG (enter) from a
> > command prompt.
>
> This is the  router local IP, I need to get the router "outside" IP witch is
> the real one.

Oh sorry I missed that.

In that case,  get a free dynamic hostname from someone like noip.com.
Install their client on your PC.   The client will keep the hostname
updated with your "real" external IP address,  and you can use
socket.gethostbyname(hostname) to query it.

It will only take a few minutes to set up the NO-IP account and client

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help using smtplib to work ..

2006-11-08 Thread Tim Williams
On 09/11/06, Nicolas G <[EMAIL PROTECTED]> wrote:
>
>
> >
> > Usually when sending/relaying without authentication,  the From is
> > irrelevant, only the To is taken into account.  Maybe, GMAIL do
> > something different because they have to put the mail in the sender's
> > mailbox as well as the recipient's.   Some ISPs will only allow a
> > local FROM address though.
> >
> > As we're only talking about the envelope TO & FROM, you could try no
> > FROM address (None or <> I forget which Smtplib needs) .
>
> If I left From empty I don't get any error but I still don't receive any
> message.
>
> >  The header FROM can still have the real FROM address in it, as its not
> (normally)
> > used during SMTP relaying.
>
> How you can explain that only for some address in the From field I can
> receive mails' and from others I can't ?
> It looks really weird to me...

It must be down to how gmail's server(s) work.

> Other question : Suppose I use the login procedure, the password will be
> vulnerable to attacks ? If I'm right I think when no ssl is used for
> authentication some one can easy see the password just by sniffing the
> packets.

I think the risk is low to be honest.   However, smtp.gmail.com uses
TLS (SSL) on ports 465 or 587.   You can call STARTTLS from within
smtplib.  but TrevorP's TLS-lite extensions to smtplib handle it
better

http://trevp.net/tlslite/

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help using smtplib to work ..

2006-11-09 Thread Tim Williams
On 8 Nov 2006 19:45:00 -0800, Jordan <[EMAIL PROTECTED]> wrote:

>  For some reason,
> smtp for gmail seems to require that you call server.ehlo() a second
> time, after having called server.starttls(), otherwise, the server
> won't accept authorization.  Thanks.

Not just GMAIL,  all (RFC Compliant) SMTP servers require a 2nd EHLO
after a successful STARTTLS command.

The list of returned options / available commands will usually be
different for the 2nd EHLO.  Often this will just mean the removal of
the STARTTLS option, but sometimes there can be additional
functionality offered.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extract text from a string

2006-11-09 Thread Tim Williams
On 9 Nov 2006 07:45:25 -0800, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:

>
> I have a string containing: "+abc_cde.fgh_jkl\n" and what I need to
> become is "abc_cde.fgh_jkl".  Could anybody be so kind and write me a
> code of how to extract this text from that string?
>

for that particular string

>>> "+abc_cde.fgh_jkl\n"[1:-1]
'abc_cde.fgh_jkl'
>>> "+abc_cde.fgh_jkl\n".strip()[1:]
'abc_cde.fgh_jkl'
>>> "+abc_cde.fgh_jkl\n"[1:].strip()
'abc_cde.fgh_jkl'
>>> "+abc_cde.fgh_jkl\n"[1:].strip().replace('+','')
'abc_cde.fgh_jkl'
>>> "+abc_cde.fgh_jkl\n"[1:].replace('\n','').replace('+','')
'abc_cde.fgh_jkl'

I'm sure there are other ways too !!

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting externally-facing IP address?

2006-11-10 Thread Tim Williams
On 10/11/06, Michael B. Trausch <[EMAIL PROTECTED]> wrote:



  
  



Every programming example that I have seen thus far shows simple server code and how to bind to a socket--however, every example binds to the localhost address.  What I am wondering is this:  Is there a clean way to get the networked IP address of the machine the code is running on?  For example, my laptop's IP address is 
192.168.0.101, and I want to bind a server to that address.  Is there a clean way of doing so that will work, for example, when I move the code to my server (which obviously doesn't have the same IP address)?


Try  using 0.0.0.0  as the IP address,  or possibly giving IP address at all.   HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Getting externally-facing IP address?

2006-11-10 Thread Tim Williams
On 10/11/06, Tim Williams <[EMAIL PROTECTED]> wrote:
>
>
> On 10/11/06, Michael B. Trausch <[EMAIL PROTECTED]> wrote:
> >
> >
> >
> > Every programming example that I have seen thus far shows simple server
> code and how to bind to a socket--however, every example binds to the
> localhost address.  What I am wondering is this:  Is there a clean way to
> get the networked IP address of the machine the code is running on?  For
> example, my laptop's IP address is 192.168.0.101, and I want to bind a
> server to that address.  Is there a clean way of doing so that will work,
> for example, when I move the code to my server (which obviously doesn't have
> the same IP address)?
> >
> >
>
> Try  using 0.0.0.0  as the IP address,  or possibly giving IP address at
> all.
>
> HTH :)
>
>

Correction:

-> or possibly giving *no* IP address at
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string to list of numbers conversion

2006-11-10 Thread Tim Williams
On 5 Nov 2006 04:34:32 -0800, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> Hi,
>   I have a string '((1,2), (3,4))' and I want to convert this into a
> python tuple of numbers. But I do not want to use eval() because I do
> not want to execute any code in that string and limit it to list of
> numbers.
>   Is there any alternative way?
>

?? I want to convert this into a python tuple of numbers ??

Do you want a python tuple with those numbers ie (1,2, 3,4),  or a
direct evaluation giving a tuple of tuples with those numbers, ie
((1,2), (3,4))

If the former then:

>>> a, l = '((1,2), (3,4), (-5,-6),(12,-13), (a,b), (0.1,0.2))', []
>>> for c in a.split(','):
... try:
... c = c.replace('(','').replace(')','')
... if '.' in c: l.append(float(c))
... else:   l.append(int(c))
... except: pass
...
>>> tuple(l)
(1, 2, 3, 4, -5, -6, 12, -13, 0.10001, 0.20001)
>>>

Its not so good with floats, but if you are only expecting integers you can use.

>>> a, l = '((1,2), (3,4), (-5,-6),(12,-13), (a,b), (0.1,0.2))', []
>>> for c in a.split(','):
... try: l.append(int(c.replace('(','').replace(')','')))
... except: pass
...
>>> tuple(l)
(1, 2, 3, 4, -5, -6, 12, -13)
>>>

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [*SPAM*] Python open proxy honeypot

2006-06-13 Thread Tim Williams
On 13/06/06, Alex Reinhart <[EMAIL PROTECTED]> wrote:
>
> Is running Python's built-in smtpd, pretending to accept and forward all
> messages, enough to get me noticed by a spammer, or do I have to do
> something else to "advertise" my script as an open proxy?

This will get you noticed by crawlers that scan the Internet looking
for SMTP open-relays on port 25,  its not an open-proxy :):)

This will work as planned,  but you should also have some email
addresses using this server for a full range of spam hits.   A single
domain is cheap and you can use it just for incoming spam - seed a few
addresses around the internet and wait

Things you should be aware of:

a) You may be breaking your ISP's T&Cs and AUPs

b) your ISP connection must have port 25 open

c) Be prepared for potentially huge numbers of connections in
intermittent but sustained batches which may make your connection
unusable.

d) point c might get you noticed in relation to point a.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Search String for Word

2006-06-26 Thread Tim Williams
On 26 Jun 2006 08:24:54 -0700, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> And what if I want to search for an item in a tuple, is there a
> similarly easy method?
>
> Tim Chase wrote:
> > > What's the best way to search a string for a particular word and get a
> > > booleen value indicating whether it exists in the string or not?
> >
> >  >>> substring = 'foo'
> >  >>> targetstring = 'blah foo bar'
> >  >>> substring in targetstring
> > True
> >  >>> if substring in targetstring: print 'yup'
> > yup

>>> t = ('a', 'b', 'c', 'd', 'e')
>>> a = 'a'
>>> a in t
True
>>> y = 'y'
>>> y in t
False

>>> t = ('test', 'black', 'white')
>>> a = 'a'
>>> [i for i in t if a in i]
['black']
>>>

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a high performance web server written in Python, and supports CGI/FastCGI

2006-07-07 Thread Tim Williams
On 07/07/06, Jack <[EMAIL PROTECTED]> wrote:
> I just did some testing between CherryPy's web server and lighttpd.
> My test was very simple and I used ab.exe for this purpose.
> CherryPy web server can serve about 140 simple request / second, while
> lighttpd can handle around 400 concurrent requests.
>
> > You haven't really said much about your requirements though.  Perhaps
> > if you describe them in more detail (for example, what does "high
> > performance" mean to you?) someone can make a more useful recommendation.
> >

Karrigell has an async server, in standalone mode it won't be as fast
as lighttpd but its simpler to use and code in than cherrypy.
However, it can also work behind lighttpd,  so you might get a good
mix of server speed and fast development time.

http://www.karrigell.com

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for a high performance web server written in Python, and supports CGI/FastCGI

2006-07-07 Thread Tim Williams
On 7 Jul 2006 06:27:43 -0700, Gerard Flanagan <[EMAIL PROTECTED]> wrote:
>
> Tim Williams wrote:
> > On 07/07/06, Jack <[EMAIL PROTECTED]> wrote:
> > > I just did some testing between CherryPy's web server and lighttpd.
> > > My test was very simple and I used ab.exe for this purpose.
> > > CherryPy web server can serve about 140 simple request / second, while
> > > lighttpd can handle around 400 concurrent requests.
> > >
> > > > You haven't really said much about your requirements though.  Perhaps
> > > > if you describe them in more detail (for example, what does "high
> > > > performance" mean to you?) someone can make a more useful 
> > > > recommendation.
> > > >
> >
> > Karrigell has an async server, in standalone mode it won't be as fast
> > as lighttpd but its simpler to use and code in than cherrypy.
> > However, it can also work behind lighttpd,  so you might get a good
> > mix of server speed and fast development time.
> >
> > http://www.karrigell.com
> >
>
> Yes, agree about Karrigell's quick development time - an almost flat
> learning curve if you've programmed for the web before, and none of the
> arcana which you can run into  elsewhere.
>
> "high performance"? - I'm using it to provide an intranet for a small
> library with *three* networked computers - performance is impeccable!

If you disable the Reverse-DNS lookups in one of the python (not
Karrigell) web modules (I'll dig out a previous post somewhere about
this),  it speeds up karrigell tremendously,  my apps aren't huge,
but I have no problems with karrigell across   the internet :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python web service ...

2006-08-26 Thread Tim Williams
> At this time right now I prefer to do something that works the quickest
> possible...
> I never had any experience with CGI, do I need to set up a web server
> for that ?
> can you point me some usefull reading material so I can get a start ?
> I will post for a comment at Zope , I had installed once and it was
> very easy. Don't know if it will be easy too to get my job done...

If you need a quick-start and short learning curve,  Karrigell is the
one to go for.   You can have the beginnings of your own site/web-app
running within minutes of downloading it.

It now has better CGI handling too - if you must go that route :)

www.karrigell.com

I recommend the Karrigell tour also,  click on the icon next to each
example to see how each one is coded, and it has a file upload example
that should get you started.

http://karrigell.no-ip.info/demo/frame_tour_en.htm

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: block a network port

2006-08-30 Thread Tim Williams
On 29 Aug 2006 20:43:49 -0700, alex23 <[EMAIL PROTECTED]> wrote:
> abcd wrote:
> > ok, no of any python solutions?  or command-line firewalls?
>
> You did try searching Google for "python firewall", right?
>
> http://www.google.com.au/search?q=python+firewall
>
> The very first entry is a pointer to a solution for Windows.

That first entry was my thread :)

IPFW is stable and runs as a Windows service,  the rules can be
added/deleted/changed/viewed in real-time making it a good candidate
for pairing with Python.

I have used it to write servers that temporarily firewall themselves
against dubious connections.

My preferred method is something like

def ip_fw(fwcmd): #
try:
i,o,e = win32pipe.popen3('C:\\ipfw\\bin\\ipfw '+fwcmd)
return i,o,e
except:
print sys.exc_info()[0]

Nowadays I might use something other than popen3 depending on the
level of return status I needed.

(from memory, not tested, and not the best Python code )

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding sender name to email

2006-09-01 Thread Tim Williams
On 1 Sep 2006 03:26:12 -0700, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> How do I add a Sender name to the emails sent by the following script:
>

>
> writer = MimeWriter.MimeWriter(out)
> # set up some basic headers... we put subject here
> # because smtplib.sendmail expects it to be in the
> # message body
> #
> writer.addheader("Subject", subject)
> writer.addheader("MIME-Version", "1.0")
> #

add the line

writer.addheader("From", a_sender_address)

and you should also have

writer.addheader("To", some_recipient_address(es)_as_a_string)

The smtp sender & recipients do not relate to the sender & recipients
in the email itself,  Often they are the same, but they don't have to
be.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding sender name to email

2006-09-01 Thread Tim Williams
On 01/09/06, Tim Williams <[EMAIL PROTECTED]> wrote:
> On 1 Sep 2006 03:26:12 -0700, [EMAIL PROTECTED]
> <[EMAIL PROTECTED]> wrote:
> > How do I add a Sender name to the emails sent by the following script:
> >
> add the line
>
> writer.addheader("From", a_sender_address)
>
> and you should also have
>
> writer.addheader("To", some_recipient_address(es)_as_a_string)
>
> The smtp sender & recipients do not relate to the sender & recipients
> in the email itself,  Often they are the same, but they don't have to
> be.
>

as an afterthought,  you should also add date & msg-id headers, or
your emails may fall foul of spam-filters.


-- 

Tim Williams
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Test for number?

2006-09-04 Thread Tim Williams
On 04/09/06, Dr. Pastor <[EMAIL PROTECTED]> wrote:
> In the following code I would like to ascertain
> that x has/is a number. What the simplest TEST should be?
> (Could not find good example yet.)
> ---
> x=raw_input('\nType a number from 1 to 20')
> if TEST :
> Do_A
> else:
> Do_B
> ---

Something simple like the following might help,

>>> def numtest():
... x=raw_input('\nType a number from 1 to 20')
... try:
... x=int(x)
... print x, "is a number between 1 & 20 "
... except:
... print x, "is not a number"
...
>>> numtest()   # enter 1
1 is a number between 1 & 20
>>> numtest()  #  enter "f"
f is not a number

its not a final solution though,  think input = -2, 5.5  or 21

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Removing from a List in Place

2006-09-05 Thread Tim Williams
On 05/09/06, Gregory Piñero <[EMAIL PROTECTED]> wrote:> I'm going
to assume that it's supposed to work like this, but could
> someone tell me the reasoning behind it?  I.E. why is 3 skipped?
>
> >>> alist=[1,2,3]
> >>> for item in alist:
> print item
> if item==2:
> alist.remove(item)
> 
> 1
> 2
> >>>


>
> Bonus Question:
> Can we make this behave more intuitiviely in Python 3000?

It does already,  you just haven't grasped list fully yet :):)

when you remove 2 from alist,  the list becomes length 2, there is no
longer a 3rd item in the list to iterate over.

Try this

> >>> alist=[1 ,2 ,3, 4]
> >>> for item in alist:
> print item
> if item==2:
> alist.remove(item)
> print alist
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Removing from a List in Place

2006-09-05 Thread Tim Williams
On 05/09/06, Tim Williams <[EMAIL PROTECTED]> wrote:
> On 05/09/06, Gregory Piñero <[EMAIL PROTECTED]> wrote:> I'm going
> to assume that it's supposed to work like this, but could
> > someone tell me the reasoning behind it?  I.E. why is 3 skipped?
> >
> > >>> alist=[1,2,3]
> > >>> for item in alist:
> > print item
> > if item==2:
> > alist.remove(item)
> > 
> > 1
> > 2
> > >>>
>
>
> >
> > Bonus Question:
> > Can we make this behave more intuitiviely in Python 3000?
>
> It does already,  you just haven't grasped list fully yet :):)
>
> when you remove 2 from alist,  the list becomes length 2, there is no
> longer a 3rd item in the list to iterate over.
>
> Try this
>
> > >>> alist=[1 ,2 ,3, 4]
> > >>> for item in alist:
> > print item
> > if item==2:
> > alist.remove(item)
> > print alist
>

sorry,  I meant to offer an alternative also

in yourgiven case you can iterate over a copy of the list like this:

>>> for item in alist[:] :
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Removing from a List in Place

2006-09-05 Thread Tim Williams
On 05/09/06, Gregory Piñero <[EMAIL PROTECTED]> wrote:
> On 9/5/06, Tim Williams <[EMAIL PROTECTED]> wrote:
> > > It does already,  you just haven't grasped list fully yet :):)
> > >
> > > when you remove 2 from alist,  the list becomes length 2, there is no
> > > longer a 3rd item in the list to iterate over.
> > >
> > > Try this
> > >
> > > > >>> alist=[1 ,2 ,3, 4]
> > > > >>> for item in alist:
> > > > print item
> > > > if item==2:
> > > > alist.remove(item)
> > > > print alist
> > >
> >
> > sorry,  I meant to offer an alternative also
> >
> > in yourgiven case you can iterate over a copy of the list like this:
> >
> > >>> for item in alist[:] :
> >
>
> Thanks Tim.  I suppose that does make sense.  Iterating over a copy is
> a good alternative.
>

You could also use a list comprehension for your case

>>> alist = [1 ,2 ,3]
>>> alist = [x for x in alist if x != 2]
>>> alist
[1, 3]
>>>

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Removing from a List in Place

2006-09-05 Thread Tim Williams
On 5 Sep 2006 16:05:36 -0700, bayerj <[EMAIL PROTECTED]> wrote:
> > I'm going to assume that it's supposed to work like this, but could
> > someone tell me the reasoning behind it?  I.E. why is 3 skipped?
>
> Because:
>
> >>> alist[2]
> 3
>
> You are removing the third item, not the second.
>

Actually,  he's removing 2 from the list,   but then the length of the
list shrinks by 1 and iteration stops.

The example would have been better if alist = ['a','b','c'] and 'b' was removed.

 L.remove(value) -- remove first occurrence of value

you were possibly  thinking of alist.pop(2),  which removes the item
alist[2] from alist

HTH :)


-- 

Tim Williams
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sending emails using python

2006-09-07 Thread Tim Williams
On 07/09/06, Sybren Stuvel <[EMAIL PROTECTED]> wrote:
> sridhar enlightened us with:
> > iam having user account on an exchangeserver.
> > with that can i send an email using python?
> >
> > if iam using the following code iam getting error
> >
> > Traceback (most recent call last):
> >   File
> > "C:\sridhar\Beginning_Python\Beginning_Python\Chapter16\tryitout\InitialMailExample.py",
> > line 5, in ?
> > server = smtplib.SMTP("hstmsg002",25)
> >   File "C:\Python24\lib\smtplib.py", line 244, in __init__
> > (code, msg) = self.connect(host, port)
> >   File "C:\Python24\lib\smtplib.py", line 307, in connect
> > (code, msg) = self.getreply()
> >   File "C:\Python24\lib\smtplib.py", line 351, in getreply
> > raise SMTPServerDisconnected("Connection unexpectedly closed")
> > SMTPServerDisconnected: Connection unexpectedly closed
>
> Well that's useless. You could install a network sniffer
> (http://www.ethereal.com/) and find out what's actually sent over the
> network connection, and where things go wrong.

Have you verified that you are allowed to use SMTP on this server ?
Can you send email via it using outlook express or a similar POP3/IMAP
mail client?

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sending emails using python

2006-09-07 Thread Tim Williams
On 07/09/06, Sybren Stuvel <[EMAIL PROTECTED]> wrote:
> Tim Williams enlightened us with:
> > Can you send email via it using outlook express or a similar
> > POP3/IMAP mail client?
>
> Wouldn't you use a SMTP client to send email?

Outlook Express *is* a mail client that uses SMTP as the outbound protocol.

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sending emails using python

2006-09-07 Thread Tim Williams
On 07/09/06, Hari Sekhon <[EMAIL PROTECTED]> wrote:
>
>  Grant Edwards wrote:
>  On 2006-09-07, Sybren Stuvel
> <[EMAIL PROTECTED]> wrote:
>
>
>  Tim Williams enlightened us with:
>
>
>  Can you send email via it using outlook express or a similar
> POP3/IMAP mail client?
>
>  Wouldn't you use a SMTP client to send email?
>
>  I would, but I don't use exchange server. :)
>
> The one exchange server I used in the past didn't accept SMTP
> mail.
>
>
>  errr, I used to admin Exchange, if it does accept SMTP then how could it
> function as a live mail server?

Did you mean *doesn't accept*  ??

There are a couple of possibilities at least,   of the top of my head:

* The IMC / IMS  (smtp connector) is located on a different server
within its Exchange organisation,  or a DMZ'ed exchange server that is
only used for SMTP.

* The server doesn't accept SMTP from local IP ranges,  only from
external (ie inbound email)

Outlook / Exchange clients use MAPI,  internal SMTP is only a
requirement if you have non-MAPI clients sending email through the
server.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Beautiful Soup to entangle bookmarks.html

2006-09-07 Thread Tim Williams
On 7 Sep 2006 14:30:25 -0700, Adam Jones <[EMAIL PROTECTED]> wrote:
>
> Francach wrote:
> > Hi,
> >
> > I'm trying to use the Beautiful Soup package to parse through the
> > "bookmarks.html" file which Firefox exports all your bookmarks into.
> > I've been struggling with the documentation trying to figure out how to
> > extract all the urls. Has anybody got a couple of longer examples using
> > Beautiful Soup I could play around with?
> >
> > Thanks,
> > Martin.
>
> If the only thing you want out of the document is the URL's why not
> search for: href="..." ? You could get a regular expression that
> matches that pretty easily. I think this should just about get you
> there, but my regular expressions have gotten very rusty.
>
> /href=\".+\"/
>

I doubt the bookmarks file is huge so something simple like

f = open('bookmarks.html').readlines()
data = [x for x in f if x.strip().startswith('http://mail.python.org/mailman/listinfo/python-list


Re: Extracting text from a string

2006-09-07 Thread Tim Williams
On 07/09/06, Anthra Norell <[EMAIL PROTECTED]> wrote:
> s = '''
>  $14.99
> , 
>  $27.99
> , 
>  $66.99
> , 
>  $129.99
> , 
>  $254.99
>'''
>
> >>> for line in [l.strip () for l in s.splitlines ()]:
>   if line [0] == '$': print line
>
> $14.99
> $27.99
> $66.99
> $129.99
> $254.99
>
> Why parse? Why regular expressions?
>


>>> print '\n'.join([i for i in s.splitlines() if i[0] == '$'])
$14.99
$27.99
$66.99
$129.99
$254.99

and
>>> print '\n'.join([i.strip() for i in s.splitlines () if i [0] == '$'])
parses with no trailing whitespace

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extracting text from a string

2006-09-07 Thread Tim Williams
On 08/09/06, Tim Williams <[EMAIL PROTECTED]> wrote:

> >>> print '\n'.join([i for i in s.splitlines() if i[0] == '$'])
> $14.99
> $27.99
> $66.99
> $129.99
> $254.99
>

or even more terse,

>>> print '\n'.join([i for i in s.splitlines ()[1::2]])
$14.99
$27.99
$66.99
$129.99
$254.99
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extracting substrings from a file

2006-09-11 Thread Tim Williams
On 11 Sep 2006 05:29:17 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I have a file with several entries in the form:
>
> AFFX-BioB-5_at   E. coli  /GEN=bioB  /gb:J04423.1  NOTE=SIF
> corresponding to nucleotides 2032-2305 of /gb:J04423.1  DEF=E.coli
> 7,8-diamino-pelargonic acid (bioA), biotin synthetase (bioB),
> 7-keto-8-amino-pelargonic acid synthetase (bioF), bioC protein, and
> dethiobiotin synthetase (bioD), complete cds.
>
> 1415785_a_at /gb:NM_009840.1 /DB_XREF=gi:6753327 /GEN=Cct8 /FEA=FLmRNA
> /CNT=482 /TID=Mm.17989.1 /TIER=FL+Stack /STK=281 /UG=Mm.17989 /LL=12469
> /DEF=Mus musculus chaperonin subunit 8 (theta) (Cct8), mRNA.
> /PROD=chaperonin subunit 8 (theta) /FL=/gb:NM_009840.1 /gb:BC009007.1
>
> and I would like to create a file that has only the following:
>
> AFFX-BioB-5_at  /GEN=bioB  /gb:J04423.1
>
> 1415785_a_at /gb:NM_009840.1 /GEN=Cct8
>
> Could anyone please tell me how can I do it?

If each entry is a single line, then the following is just to give you
some ideas.  It is not robust enough for "production" though.

The 2nd input line has 2 /gb fields, and your script would need to
have some way of knowing which one to pick.

>>> for x in s.splitlines():
... data = x.split()
... output = [ data[0] ]
... for z in data[1:]:
... if (z.startswith('/GEN') or z.startswith('/gb'))and z not in 
output:
... output.append(z)
... print ' '.join(output)
... 
AFFX-BioB-5_at /GEN=bioB /gb:J04423.1
1415785_a_at /gb:NM_009840.1 /GEN=Cct8 /gb:BC009007.1

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: best way of testing a program exists before using it?

2006-09-11 Thread Tim Williams
On 11/09/06, Hari Sekhon <[EMAIL PROTECTED]> wrote:
>
>  Steve Holden wrote:
>  Hari Sekhon wrote:
>
>
>  The easiest way to test whether the command will run is to try and run
> it. If the program doesn't exist then you'll get an exception, which you
> can catch. Otherwise you'll be stuck with non-portable mechanisms for
> each platform anyway ...
>
> regards
>  Steve
>
>
>  Yeah, this occurred to me just after I sent the mail, but I don't really
> want to run the program because it will go off and do some work and take
> time to come back. If there is a better way then that would be great. I
> can't think of anything other than what you have suggested with a message
> saying that the program wasn't found in the path which would be the most
> appropriate error since the path could also be wrong.


If you run your wrapper and the program exists then you don't have to
test for it,  so the overall process is quicker and cleaner than
testing-for *then* running the program

If you run your wrapper and the program doesn't exist, then you have
performed your "if exists"  test without extra code and with very
little processing, and the raised exception will lead you nicely into
your "not exists" scenario.

try:
run_somecommand
except:
print "you don't have %s installed" % somecommand


HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: best way of testing a program exists before using it?

2006-09-11 Thread Tim Williams
On 11/09/06, Hari Sekhon <[EMAIL PROTECTED]> wrote:
> Tim Williams wrote:
> > On 11/09/06, Hari Sekhon <[EMAIL PROTECTED]> wrote:
> >>
> >>  Steve Holden wrote:
> >>  Hari Sekhon wrote:
> >>
> >>
> >>  The easiest way to test whether the command will run is to try and run
> >> it. If the program doesn't exist then you'll get an exception, which you
> >> can catch. Otherwise you'll be stuck with non-portable mechanisms for
> >> each platform anyway ...
> >>
> >> regards
> >>  Steve
> >>
> >>
> >>  Yeah, this occurred to me just after I sent the mail, but I don't
> >> really
> >> want to run the program because it will go off and do some work and take
> >> time to come back. If there is a better way then that would be great. I
> >> can't think of anything other than what you have suggested with a
> >> message
> >> saying that the program wasn't found in the path which would be the most
> >> appropriate error since the path could also be wrong.
> >
> >
> > If you run your wrapper and the program exists then you don't have to
> > test for it,  so the overall process is quicker and cleaner than
> > testing-for *then* running the program
> >
> > If you run your wrapper and the program doesn't exist, then you have
> > performed your "if exists"  test without extra code and with very
> > little processing, and the raised exception will lead you nicely into
> > your "not exists" scenario.
> >
> > try:
> >run_somecommand
> > except:
> >print "you don't have %s installed" % somecommand
> >
> >
> > HTH :)
> >
> The down side to that is the program has to be run which consumes time
> and slows the script down a fair bit (as well as outputting garbage to
> the screen)
>

Sorry,  I don't follow!!

If the program doesn't exist how can you run it ???

If the program does exist then running is what you wanted it to do
anyway and you've saved doing a test for it first.

:)

-- 

Tim Williams
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sending emails using python

2006-09-14 Thread Tim Williams
On 14 Sep 2006 00:11:05 -0700, sridhar <[EMAIL PROTECTED]> wrote:
>
> Tim Williams wrote:
> > Have you verified that you are allowed to use SMTP on this server ?
> > Can you send email via it using outlook express or a similar POP3/IMAP
> > mail client?
> >
> > :)
>
>
> yes , because iam having account on that server

Having an account on an Exchange server doesn't automatically mean you
have SMTP access to that server.Some exchange servers don't have
SMTP running at all, and others will not allow SMTP access from local
IP addresses - even with authentication.

Can you telnet to your Exchange server on port 25 and start an SMTP
conversation, using the EHLO or HELO, MAIL and RCPT commands, but
quitting before the DATA command?  If you can successfully specify a
recipient, then your script should work,  if not you will need your
script to work around the problem based on what you do or don't get
back from the server.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Limitate speed of a socket-based data transferring

2006-09-14 Thread Tim Williams
On 14/09/06, Diez B. Roggisch <[EMAIL PROTECTED]> wrote:
> billie schrieb:
> > Hi all. I'm writing a TCP-based application that I will use to trasfer
> > binary files through the network. This piece of code represents how do
> > I get a file from a remote peer and save it on my local hard drive:
> >
> > file_obj = open('downloaded.ext', 'wb')
> > while 1:
> > buf = sock.recv(2048)
> > if len(buf) == 0:
> >  break
> > file_obj.write(buf)
> > file_obj.close()
> > sock.close()
> >
> > I would like to know how could be possible to limit the file transfer
> > speed (for example: don't write more than 50 Kb/sec).
> > Some ideas?
>
> If you are on unix, use trickle. If you must limit it from within your
> own code, I can only assume that computing the transfer rate so far and
> introducing timeouts might help - but I never did such a thing, nor do I
> know what that means for example for the  network stack.
>
> But maybe even then trickle may help you to get an idea, as it is a
> user-space program AFAIK. So they might have some information (at least
> the code... ) out there that could be of use.

You could wrap  buf = sock.recv(xxx) in a data counter and sleep loop
so that you burst to no more than 50KB/s average.

Completely untestest and for illustration only :)

file_obj = open('downloaded.ext', 'wb')
interval = 1.0 #  seconds  eg. 0.5 or 2.0
# smaller the interval, the less bursty and smoother the throughput
max_speed = 51200   # 50k * 1024 =  bytes
data_count = 0   # keep track of the amount of data transferred
time_next = time.time() + interval
while 1:
   buf = sock.recv(512) # smaller chunks = smoother, more accurate
   if len(buf) == 0:
break
   data_count += len(buf)
   if data_count >= max_speed * interval:
   data_count = 0
   sleep_for =  time_next - time.time()
   if sleep_for > 0:
time.sleep(sleep_for)
time_next = time.time() + interval
   file_obj.write(buf)
file_obj.close()
sock.close()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: something for itertools

2006-09-15 Thread Tim Williams
On 15/09/06, Daniel Nogradi <[EMAIL PROTECTED]> wrote:
> In a recent thread,
> http://mail.python.org/pipermail/python-list/2006-September/361512.html,
> a couple of very useful and enlightening itertools examples were given
> and was wondering if my problem also can be solved in an elegant way
> by itertools.
>
> I have a bunch of tuples with varying lengths and would like to have
> all of them the length of the maximal and pad with None's. So
> something like
>
> a = ( 1, 2, 3 )
> b = ( 10, 20 )
> c = ( 'x', 'y', 'z', 'e', 'f' )
>
> should turn into
>
> a = ( 1, 2, 3, None, None )
> b = ( 10, 20, None, None, None )
> c = ( 'x', 'y', 'z', 'e', 'f' )
>

or maybe a one liner  :)

>>> (a + 5*(None,))[:5]
(1, 2, 3, None, None)

>>> pad_to = 5
>>> (a + pad_to*(None,))[:pad_to]
(1, 2, 3, None, None)
>>> (b + pad_to*(None,))[:pad_to]
(10, 20, None, None, None)
>>> (c + pad_to*(None,))[:pad_to]
('x', 'y', 'z', 'e', 'f')
>>>


-- 

Tim Williams
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: something for itertools

2006-09-15 Thread Tim Williams
On 15/09/06, Daniel Nogradi <[EMAIL PROTECTED]> wrote:

> > or maybe a one liner  :)
> >
> > >>> (a + 5*(None,))[:5]
> > (1, 2, 3, None, None)
> >
>
> Well, something like this is what I actually do. But for this first I
> have to loop over all tuples and pick out the maximal length, so over
> all it won't be a one liner but two loops.

I meant it as a one line answer to your padding query,  you would
still need to iterate over you collection of tuples as it only acts on
1 tuple at a time.

That said, its not too difficult to get the whole process, including
finding the maximal length to a 2 liner once you have collected your
tuples :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mp3 libs and programs

2006-09-15 Thread Tim Williams
On 15 Sep 2006 18:16:41 -0700, Jay <[EMAIL PROTECTED]> wrote:
> I'm writing a python script that involves playing mp3 files.  The first
> approach I had was sending commands to unix command-line programs in
> order to play them.  I tired mpg123 and moosic, but there was a key
> feature to my program's success that's missing.  SEEK!  I need to be
> able to start playing a song at an arbitrary point in the song.  I
> can't find anything to let me do this.  Does anyone have any advice or
> has anyone needed something similar?

http://pymedia.org/   maybe

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Limitate speed of a socket-based data transferring

2006-09-17 Thread Tim Williams
On 15/09/06, Grant Edwards <[EMAIL PROTECTED]> wrote:
> On 2006-09-15, Steve Holden <[EMAIL PROTECTED]> wrote:

> >
> > Of course this depends crucially on the window size. Since the
> > addition of the window scaling TCP option it's been possible
> > to specify very large windows, which are useful over
> > high-bandwidth high-delay links.
>
> True.  If the window size is large compared to the amount of
> data being transferred, then the throttling won't happen.
>
> > The remote (send) throttling will only start to cut in when
> > the window is full (since the whole point of the sliding
> > window mechanism is to allow continuous transmission in the
> > face of acknowledgment delay).
>
> Yup.

If the OP is also writing the client,  the throttling can be at the
client side to control the speed of writing to the socket.  But if
multiple clients connect to the server concurrently then this may not
help either!

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Don't use regular expressions to "validate" email addresses

2006-09-22 Thread Tim Williams
> >
> Just as a matter of interest, are you expecting that you'll find out
> about the undeliverable ones? Because in many cases nowadays you wont,
> since so many domains are filtering out "undeliverable mail" messages as
> an anti-spam defence.
>

...and then there is the problem of validating that the valid email
address belongs to the person entering it !!  If it doesn't,  any
correspondence you send to that email address will itself be spam (in
the greater modern definition of spam).

You could allow your form to accept any email address,  then send a
verification in an email  to the address given,  asking the recipient
to click a link if they did in fact fill in the form.  When they click
the link the details from the original form are then verified and can
be activated and processed.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Verify an e-mail-adress - syntax and dns

2006-09-24 Thread Tim Williams
On 24/09/06, Ingo Linkweiler <[EMAIL PROTECTED]> wrote:
> Has anyone a function/script to verify an e-mail-address?
>
> It should:
> a) check the syntax
> b) verify an existing mailserver or DNS/MX records
>

b) is problematical.

A domain with MX records may not really have a mail server at all.

Under RFC's,  if  no MX records exist the A record returned for the
mail-domain (the bit after the @ ) should be used as the MX record.

So to check b)  you must not rely on DNS records,  you need to
physically connect to the domain's mailserver(s).

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: concat next line with previous

2006-09-26 Thread Tim Williams
On 26 Sep 2006 03:16:25 -0700, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> hi
> what is the python way to concat 2 lines eg
>
> line  1 with some text
> line 2 with some text
>
> i want to bring line 2 up , such that i get one whole string.
>
> line  1 with some text line 2 with some text
>

also

line3 = "%s %s" % (line1, line2)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using the email module

2006-09-28 Thread Tim Williams
On 28/09/06, Erik Johnson wellkeeper dot
<"com>"@bag.python.org> wrote:

> #!/usr/bin/env python
>
> import smtplib
> from email.MIMEText import MIMEText
> from email.MIMEMultipart import MIMEMultipart
>
>
> # GLOBAL DATA
> #=
> MAIL_SERVER  = 'your_server.com'
> MAIL_SUBJECT = "Python.SMTP email test"
> MAIL_TO  = '[EMAIL PROTECTED]'
> MAIL_FROM= "Python.SMTP email test"
>
>
> # Create a text/plain message
>
> Body = """\
> This is intended to be the body of my email. The HTML below should not
> be seen directly in the body but should be a separate attachment.
> """
>
> msg = MIMEMultipart()
> msg['Subject'] = MAIL_SUBJECT
> msg['From']= MAIL_FROM
> msg['To']  = MAIL_TO
> msg.preamble   = Body
>
>
> html = """\
> 
> 
>   Sample HTML File
> 
>
> 
>   Can you see this?
>   This is a short paragraph.
> 
>
> 
> """

html_part = MIMEText(html, _subtype='html')
html_part.add_header('Content-Disposition', 'attachment',
filename='my_text.html')
msg.attach(html_part)

># msg.attach(MIMEText(html, 'html'))
> # print msg.as_string()
>
>
> # Send the message via our own SMTP server, but don't include the
> # envelope header.
> smtp = smtplib.SMTP(MAIL_SERVER)
> smtp.sendmail(MAIL_FROM, [MAIL_TO], msg.as_string())
> smtp.close()
> # end of python script
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29/09/06, Matthew Warren <[EMAIL PROTECTED]> wrote:
>  I have
> found that in real usage of other programs within the company that use
> lockfiles, it sometimes causes a bit of troubleshooting time when it stops
> working due to a stale lockfile.. This especially happens when the program
> is killed, the lockfile remains and causes minor annoyance (to somebody who
> knows that is, more annoyance to somebody who doesn't).

Then they were checking for the wrong thing :)  Chcking for a lock
file's existance is prone to the above problem.  Checking to see if
anyone "owns" the lock file is better,  and if a running program
doesn't close gracefully it still releases the lock file.

def check_lock():
import os, sys
try:
os.remove({filename})
except:
if "Permission denied" in sys.exc_info()[1]:
print 'This program is already running'
sys.exit()
f_lock = open({filename},'w')

You may want to close the lock file properly during a gracefull shutdown.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29/09/06, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Tim Williams wrote:
>
> > def check_lock():
> > import os, sys
> > try:
> > os.remove({filename})
> > except:
> > if "Permission denied" in sys.exc_info()[1]:
> > print 'This program is already running'
> > sys.exit()
> > f_lock = open({filename},'w')
>
> have you actually tried this on a Unix system ?

Nope ,  I don't have one anywhere close.   I'm M$ only.

I did forget disclaimers like "try something like" and "not tested"
etc etc  - my bad  !!!

So that I know my mistake, which bit fails (the text from
sys.exc_info()[1]?? ) , or is it all complete rubbish - and not
do-able -  on a *nix system ?

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29/09/06, Sybren Stuvel <[EMAIL PROTECTED]> wrote:
> Checking for a lock, and later acquiring the lock, seems non-atomic to
> me. It's easier to simply create a lock directory. If it fails, the
> dir already exists and someone else holds the lock. If it succeeds,
> the lock is immediately yours, since you just created the directory.
> This is atomic on most OSses AFAIK.

My reply was in response to a post that mentioned a known problem with
this,  what happens when the previously running program doesn't exit
gracefully, and leaves the directory in place ??

If the directory creation fails (because it already exists), it
doesn't mean there is another instance of the program running, it
means that the directory hasn't yet been deleted by its creator.

   :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29 Sep 2006 09:47:12 -0700, Paul Rubin
<"http://phr.cx"@nospam.invalid> wrote:
> "Tim Williams" <[EMAIL PROTECTED]> writes:
> > My reply was in response to a post that mentioned a known problem with
> > this,  what happens when the previously running program doesn't exit
> > gracefully, and leaves the directory in place ??
>
> Don't use the presence or absence of a file as a lock.  Have the file
> there all the time, and have the app open it and use fcntl to get an
> exclusive kernel lock on the file descriptor.  That lock should go
> away when the process exits, so you don't have to worry about cleaning
> it up.  See "man fcntl" for details.

That's the same kind of principle as my posted snippet,  it doesn't
rely on the file's presence or absence as such.  It only cares when
the file exists *and* is held open by another process.When the
process exits,  the lock ends without needing to clean up.

It works on Windows, but that doesn't help the OP :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29 Sep 2006 10:04:15 -0700, Paul Rubin
<"http://phr.cx"@nospam.invalid> wrote:
> "Tim Williams" <[EMAIL PROTECTED]> writes:
> > That's the same kind of principle as my posted snippet,  it doesn't
> > rely on the file's presence or absence as such.  It only cares when
> > the file exists *and* is held open by another process.When the
> > process exits,  the lock ends without needing to clean up.
> >
> > It works on Windows, but that doesn't help the OP :)
>
> Huh?  Fcntl is on linux.

No, my snippet does  LOL :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making sure script only runs once instance at a time.

2006-09-29 Thread Tim Williams
On 29/09/06, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Tim Williams wrote:
>
> > So that I know my mistake, which bit fails (the text from
> > sys.exc_info()[1]?? ) , or is it all complete rubbish - and not
> > do-able -  on a *nix system ?
>
> opening a file for writing doesn't lock it on Unix.  if you want
> locking, you need other mechanisms, and I'm not sure any of them
> work properly on all configurations, under all circumstances.
>
> (there's a reason why people are still using "1970s technology"
> to do locking on Unix...)

Thanks for that Fredrik

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


Re: changing numbers to spellings

2006-10-01 Thread Tim Williams
On 1 Oct 2006 14:08:24 -0700, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> I guess I'm just looking for a small code sample hooked up to the code
> I gave, that would collect the input, compare it to code such as:
>
> if x==5
>  print "Five"
> elif x==6
>  print "Six"
> elif x==7
>  print "Seven"
>
> Something along those lines. That was actually like the code I used for
> something else a while ago.
>
> My only problem is, I want it to be printed to a textvariable for use
> in the label.

You can replace the above snippet with:

my_nums = { 1 : 'One' , 2 : 'Two' ,  3 : 'Three' , 4 : 'Four' }  # etc etc
print my_nums[x]

so...

>>> x = 2
>>> print my_nums[x]
'Two'
>>> x = 4
>>> print my_nums[x]
'Four'

and my_nums[x] is a "variable"  for you to use.

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping over a list question

2006-10-03 Thread Tim Williams
On 3 Oct 2006 10:50:04 -0700, [EMAIL PROTECTED]
<[EMAIL PROTECTED]> wrote:
> I found myself writing:
>
> for f in [i for i in datafiles if '.txt' in i]:
> print 'Processing datafile %s' % f
>
> but I was wishing that I could have instead written:
>
> for f in in datafiles if '.txt' in f:
> print 'Processing datafile %s' % f
>
> Has there ever been a proposal for this? Just wondering ...
>

Maybe

>>> def myfunc(txt):
... print txt
... 
>>> datafiles = ['1.txt','2.txt','3.txt','4.tst']
>>> null = [myfunc(i) for i in datafiles if '.txt' in i]
1.txt
2.txt
3.txt
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "10, 20, 30" to [10, 20, 30]

2006-11-23 Thread Tim Williams
On 23 Nov 2006 03:13:10 -0800, Daniel Austria <[EMAIL PROTECTED]> wrote:
> Sorry,
>
> how can i convert a string like "10, 20, 30" to a list [10, 20, 30]
>
> what i can do is:
>
> s = "10, 20, 30"
> tmp = '[' + s + ']'
> l = eval(tmp)
>
> but in my opinion this is not a nice solution
>

Not nice, especially if you can't control what is in s :)

A simple solution if you know s will always contain string
representations of integers is:

>>> s = "10, 20, 30"
>>> [int(x) for x in s.split(',')]
[10, 20, 30]
>>>

Otherwise a good starting point might be:

>>> for i in s.split(','):

HTH :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "10, 20, 30" to [10, 20, 30]

2006-11-23 Thread Tim Williams
On 23/11/06, Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> On Thu, 23 Nov 2006 03:13:10 -0800, Daniel Austria wrote:
>
> > Sorry,
> >
> > how can i convert a string like "10, 20, 30" to a list [10, 20, 30]
> >
> > what i can do is:
> >
> > s = "10, 20, 30"
> > tmp = '[' + s + ']'
> > l = eval(tmp)
> >
> > but in my opinion this is not a nice solution
>
>
> It is a dangerous solution if your data is coming from an untrusted source.
>
> >>> s = "10, 20, 30"
> >>> L = [x.strip() for x in s.split(',')]
> >>> L
> ['10', '20', '30']
> >>> L = [int(x) for x in L]
> >>> L
> [10, 20, 30]
>
> Or, as a one liner:  [int(x.strip()) for x in s.split(',')]

You don't need the strip()

>>> int('10 ')
10
>>>

:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket.error connection refused

2006-11-23 Thread Tim Williams
On 23 Nov 2006 04:09:18 -0800, Vania <[EMAIL PROTECTED]> wrote:
> Hi, I'm not sure this is the proper forum but I try nevertheless.
> The problem I'am facing is that the socket library always fail to
> connect to an URL. The net effect is that I can not use setuptools.
> I'm using Python2.4 on a windows XPPRO Sp2 machine.
> The firewall is disabled.
> There is no NLTM proxy.
> I connect to the internet through a NAT server (and it works).
> Other than with easy_install I tried to connect to a number of external
> urls
> (all of them running) and even to localhost,
> directly in script using urllib
> and the error is always the same errno:  10061 connection refused.
> Any ideas?

A socket can't connect to a URL, a URL is an absolute location of an
internet resource, eg hostname + (virtual) location on a server + page
name.  A socket can connect to an IP address or hostname - which is
the first part of the URL after the "http://";  (or ftp:// etc)

You need to post a code snippet and the errors you are getting.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "10, 20, 30" to [10, 20, 30]

2006-11-23 Thread Tim Williams
On 23/11/06, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Tim Williams wrote:
> >>>>
>
> and the use of a list comprehension is pretty silly to, given that you want
> to apply the same *function* to all items, and don't really need to look
> it up for every item:
>
> map(int,  s.split(','))

Haha, thanks Frederic,  I wondered how long it would take for a reply
from you :)

"Silly" though ??

Tim :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing a dictionary from a string

2006-12-15 Thread Tim Williams
On 15/12/06, Benjamin Georgi <[EMAIL PROTECTED]> wrote:
> Hello list,
>
> I could use some help extracting the keys/values of a list of
> dictionaries from a string that is just the str() representation of the
> list (the problem is related to some flat file format I'm using for file
> IO).
>
> Example:
>  >>> s = str(dict_list)
>  >>> s
> '[{0: [2], 1: []}, {0: [], 1: [], 2: []}, {0: [1, 2]}]'
>
> Then, what I want to do is to reconstruct dict_list given s.
> Now, one possible solution would be
>
>  >>> dict_list = eval(s)
>
> but since the content of s cannot be blindly trusted I`d rather not do
> it that way. Basically my question is whether there is another solution
> which is simpler than using regular expressions.


I'm sure I'll get flamed for the following !!  LOL

You could check that the string contains at least part of what you are
expecting.

if  s[0] == '[' :dict_list = eval(s)

or
if  s[:2] == '[{' :   dict_list = eval(s)
if  s[:2] == '[{' and  s[-2:] == '}]' :  dict_list = eval(s)
etc

I doubt it is perfect,  but it is quick and simple and much safer than
a straight eval()

*or*

try:
dict_list = [eval(x+'}') for x in
s.replace('[{','{').replace('}]','').split('},')]
except:
print "file may be compromised"
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   >